开发者

One time Hook DLL initialization

开发者 https://www.devze.com 2023-01-07 04:06 出处:网络
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_PRO

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

精彩评论

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