现在的位置: 首页 > 综合 > 正文

DllMain

2013年02月01日 ⁄ 综合 ⁄ 共 1908字 ⁄ 字号 评论关闭

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 hinstDLL,
  DWORD fdwReason,
  LPVOID lpvReserved
);

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;
}


抱歉!评论已关闭.