FreeLibraryAndExitThread 最佳实践

由于 FreeLibraryAndExitThread 内部会使用 ExitThread 终止调用线程,如果调用线程是用_beginthreadex创建的,可能会造成 _beginthreadex 分配的资源得不到释放。解决方法是使用 CreateThread 创建新线程,在新线程中首先等待 _beginthreadex 创建的线程正常终止,然后在调用 FreeLibraryAndExitThread , 示例代码如下:

#include <process.h>
#include <windows.h>

static HMODULE __hModule = NULL;
static HANDLE __hDllMainThread = NULL;

// 在这个函数中做实际工作,这个函数返回后自动卸载当前DLL
extern unsigned Main(HMODULE hModule);

static DWORD WINAPI __FreeLibraryThread(LPVOID lpParam) {
    ::WaitForSingleObject(__hDllMainThread, INFINITE);
    ::CloseHandle(__hDllMainThread);
    ::FreeLibraryAndExitThread(__hModule, 0);
    return 0;
}

/* when this thread exit, the DLL will be free */
static unsigned __stdcall __DllMainThread(void *lpParam) {
    unsigned ret = EXIT_FAILURE;
    __try {
        __try {
            unsigned ret = Main(__hModule);
        }
        __except (EXCEPTION_EXECUTE_HANDLER) {
            ;
        }
    }
    __finally {
        HANDLE h = ::CreateThread(NULL, 0, __FreeLibraryThread, 0, 0, NULL);
        if (h != NULL) {
            ::CloseHandle(h);
        }
        else {
            ;
        }
    }
    return ret;
}

// DLL加载时创建线程,并在线程中调用Main函数。
// Main函数返回后,自动卸载DLL。

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        __hModule = hModule;
        __hDllMainThread = (HANDLE)::_beginthreadex(NULL, 0, __DllMainThread, NULL, 0, NULL);
        if (__hDllMainThread == NULL) {
            return FALSE;
        }
        break;
    case DLL_THREAD_ATTACH:
        break;
    case DLL_THREAD_DETACH:
        break;
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Creating Windows CreateMDIWindow CreateWindow CreateWindowEx RegisterClass RegisterClassEx UnregisterClass Message Processing BroadcastSystemMessage CallNextHookEx CallWindowProc DefFrameProc DefMDIChildProc DefWindowProc DispatchMessage GetMessage GetMessageExtraInfo GetMessagePos GetMessageTime GetQueueStatus InSendMessage PeekMessage PostMessage PostQuitMessage PostThreadMessage RegisterWindowMessage ReplyMessage SendMessage SendMessageCallback SendMessageTimeout SendNotifyMessage SetMessageExtraInfo SetWindowsHookEx TranslateMessage UnhookWindowsHookEx WaitMessage Window Information AnyPopup ChildWindowFromPoint ChildWindowFromPointEx EnableWindow EnumChildWindows EnumPropsEx EnumThreadWindows EnumWindows FindWindow FindWindowEx GetClassInfoEx GetClassLong GetClassName GetClientRect GetDesktopWindow GetFocus GetForegroundWindow GetNextWindow GetParent GetProp GetTopWindow GetWindow GetWindowLong GetWindowRect GetWindowText GetWindowTextLength IsChild IsIconic IsWindow IsWindowEnabled IsWindowUnicode IsWindowVisible IsZoomed RemoveProp SetActiveWindow SetClassLong SetFocus SetForegroundWindow SetParent SetProp SetWindowLong SetWindowText WindowFromPoint Processes and Threads CreateEvent CreateMutex CreateProcess CreateSemaphore CreateThread DeleteCriticalSection DuplicateHandle EnterCriticalSection ExitProcess ExitThread GetCurrentProcess GetCurrentProcessId GetCurrentThread GetCurrentThreadId GetExitCodeProcess GetExitCodeThread GetPriorityClass GetThreadPriority GetWindowThreadProcessId InitializeCriticalSection InterlockedDecrement InterlockedExchange InterlockedIncrement LeaveCriticalSection OpenEvent OpenMutex OpenProcess OpenSemaphore PulseEvent ReleaseMutex ReleaseSemaphore ResetEvent ResumeThread SetEvent SetPr

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值