Skip to content

Instantly share code, notes, and snippets.

@mmmunk
Created February 19, 2019 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmmunk/69d3c196d65fb1ba466603437d337e27 to your computer and use it in GitHub Desktop.
Save mmmunk/69d3c196d65fb1ba466603437d337e27 to your computer and use it in GitHub Desktop.
Example of creating and using a Windows DLL with MinGW
// x86_64-w64-mingw32-gcc -Wall -shared -O2 -s -o helper.dll helper.c
#include <stdio.h>
#include <string.h>
__declspec(dllexport) __stdcall int proc_sql_modify(char *sql, int size) {
strcpy(sql, "NEW SQL HERE");
return 0;
}
// x86_64-w64-mingw32-gcc -Wall -O2 -s -o usage.exe usage.c
#include <stdio.h>
#include <windows.h>
typedef int (__stdcall *PROC_SQL_MODIFY)(char *sql, int size);
int main() {
char helper_dll_name[128] = "helper.dll"; // From settings, default NULL
HMODULE dll_instance = NULL;
PROC_SQL_MODIFY proc_sql_modify = NULL;
char sqlstr[1024] = "INSERT INTO table1 (a,b,c) VALUES ('Text', 55, 3.1415)";
if (*helper_dll_name) {
dll_instance = LoadLibrary(helper_dll_name);
if (dll_instance)
proc_sql_modify = (PROC_SQL_MODIFY)GetProcAddress(dll_instance, "proc_sql_modify");
}
printf("SQL before DLL-modification: %s\n\n", sqlstr);
if (proc_sql_modify)
proc_sql_modify(sqlstr, sizeof sqlstr);
printf("SQL after DLL-modification: %s\n", sqlstr);
if (dll_instance)
FreeLibrary(dll_instance);
}
type
TProcSqlModify = function(sql: PAnsiChar; size: Integer): Integer; stdcall;
var
dll_instance: HMODULE;
proc_sql_modify: TProcSqlModify;
sqlstr: array[0..127] of AnsiChar;
begin
dll_instance := LoadLibrary('helper.dll');
proc_sql_modify := GetProcAddress(dll_instance, 'proc_sql_modify');
proc_sql_modify(sqlstr, SizeOf(sqlstr));
Caption:=sqlstr;
FreeLibrary(dll_instance);
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment