WindowsでgccでDLL作成

メモ。LoadLibraryの説明するのにちょっと調べて書いてた。
ツッコミはいらぬ。

foo.c

#include<windows.h>
#include<stdio.h>

__declspec(dllexport) __stdcall int MessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType) {
  printf(lpText);
  return 0;
}

test.c

#include<windows.h>

#ifdef foo
  #define DLL "foo.dll"
#else
  #define DLL "user32.dll"
#endif

int main(){
  int (__stdcall *func) (HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
  HMODULE hModule;
  hModule = LoadLibrary(DLL);
  if(hModule!=NULL){
    func = (int (__stdcall *) (HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType))GetProcAddress(hModule, "MessageBoxA");
    func(0, "こんにちは", "ほげ", 1);
  }
  return 0;
}

ビルド

gcc -c foo.c
gcc -shared -o foo.dll foo.o -Wl,--kill-at
gcc -o test.exe test.c -Dfoo

こんにちはって表示されるだけ。user32.dllのほうだとメッセージボックスで表示。