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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值