DllMain

The DllMain function is an optional entry point into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated, or upon calls to the LoadLibrary and FreeLibrary functions.

DllMain is a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools.

MSDN  是這樣解釋的

DllMain 是DLL的入口函數,在進程或線程被初始化(或結束)的時候調用和該進程或線程相關的DLLMain函數,在使用loadlibrary和freeLibrary的時候也調用該函數

還有一點要注意的就是:就是DLLMain的第二個函數

BOOL WINAPI DllMain(
  HINSTANCE ,
  DWORD ,
  LPVOID 
);

DWORD fdwReason,
是指該函數被調用的原因

有如下幾點原因導致該函數被調用

DLL_PROCESS_ATTACH

DLL_THREAD_ATTACH

DLL_THREAD_DETACH

DLL_PROCESS_DETACH

可以使用switch()  case 的語句來判斷並做出相應的動作

比如輸入法的的窗口類註冊程序

BOOL WINAPI DLLEntry ( HINSTANCE hInstDLL, DWORD dwFunction, LPVOID lpNot)
{
    switch (dwFunction)
      {
        case DLL_PROCESS_ATTACH:
        hInst= hInstDLL;
        wc.style = CS_MYCLASSFLAG | CS_IME;
        wc.lpfnWndProc = MyUIServerWndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 2 * sizeof(LONG);
        wc.hInstance = hInst;
        wc.hCursor = LoadCursor( NULL, IDC_ARROW);
        wc.hIcon = NULL;
        wc.lpszMenuName = (LPSTR) NULL;
        wc.lpszClassName = (LPSTR) szUIClassName;
        wc.hbrBackground = NULL;
        if(!RegisterClass((LPWNDCLASS)&wc))
        return FALSE;
        wc.style = CS_MYCLASSFLAG | CS_IME;
        wc.lpfnWndProc = MyCompStringWndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = cbMyWndExtra;
        wc.hInstance = hInst;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hIcon = NULL;
        wc.lpszMenuName = (LPSTR) NULL;
        wc.lpszClassName = (LPSTR) szUICompStringClassName;
        wc.hbrBackground = NULL;
        if(!RegisterClass((LPWNDCLASS)&wc))
        return FALSE;
        break;
        case DLL_PROCESS_DETACH:
        UnregisterClass(szUIClassName,hInst);
        UnregisterClass(szUICompStringClassName,hInst);
        break;
     }
   return TRUE;
}


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