用 Mingw gcc 編譯 dll

#include <windows.h>


__declspec(dllexport) int __cdecl Add(int a, int b)
{
  return (a + b);
}


BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
  switch (fdwReason)
  {
    case DLL_PROCESS_ATTACH:
      MessageBoxA(NULL, "hello", "world", 0);
      /* Code path executed when DLL is loaded into a process's address space. */
      break;


    case DLL_THREAD_ATTACH:
      /* Code path executed when a new thread is created within the process. */
      break;


    case DLL_THREAD_DETACH:
      /* Code path executed when a thread within the process has exited *cleanly*. */
      break;


    case DLL_PROCESS_DETACH:
      /* Code path executed when DLL is unloaded from a process's address space. */
      break;
  }


  return TRUE;

}


編譯命令:

gcc -c -o add_basic.o add_basic.c
gcc -o add_basic.dll -s -shared add_basic.o -Wl,--subsystem,windows


轉載http://www.transmissionzero.co.uk/computing/building-dlls-with-mingw/


還有不少編程編譯的細節可以在上面的網址上找到:比如給DLL加入版本信息,如何使用導入導出庫。

發佈了22 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章