开发者

One time Hook DLL initialization

My program us开发者_JS百科es SetWindowsHookEx to set a global hook function in my DLL. However I want the DLL to work with a file so I need it a file to be opened once. I can't use DllMain's DLL_PROCESS_ATTACH because it's called multiple times. What is the best solution to my problem?


Use a static flag to tell whether you've initialized already or not.

void DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
    static BOOL initialized = FALSE;

    switch(dwReason) {
        case DLL_PROCESS_ATTACH:
            if(!initialized) {
                // Perform initialization here...ex: open your file.
                initialized = TRUE;
            }
            break;
        case DLL_PROCESS_DETACH:
            if(initialized) {
                // Perform cleanup here...ex: close your file.
                initialized = FALSE;
            }
            break;
    };

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜