gpt4 book ai didi

c++ - 显式链接 DLL 和程序挂起

转载 作者:行者123 更新时间:2023-11-30 02:12:40 25 4
gpt4 key购买 nike

我的程序中有以下代码,它动态链接 wtsapi32.dll 文件以获取 WTS_SESSION_LOCKWTS_SESSION_UNLOCK 等 session 通知并在后台运行。第一次锁定/解锁后,程序挂起并且没有响应。

这是进行显式链接的正确方法吗?

    void RegisterSession(HWND hwnd) 
{
typedef DWORD (WINAPI *tWTSRegisterSessionNotification)( HWND,DWORD );

tWTSRegisterSessionNotification pWTSRegisterSessionNotification=0;
HINSTANCE handle = ::LoadLibrary("wtsapi32.dll");
pWTSRegisterSessionNotification = (tWTSRegisterSessionNotification) :: GetProcAddress(handle,"WTSRegisterSessionNotification");
if (pWTSRegisterSessionNotification)
{
pWTSRegisterSessionNotification(hwnd,NOTIFY_FOR_THIS_SESSION);
}
::FreeLibrary(handle);
handle = NULL;
}

已编辑:

我有另一个调用 WTSUnRegisterSessionNotification 的方法 UnRegisterSession() 函数,我在 WinMain 方法中调用 RegisterSession()(按照 1800 的建议删除了 FreeLibrary)并在 CALLBACK WindowProcedure 函数的 WM_DESTROY 中调用 UnRegisterSession()。但应用程序仍然挂起。

最佳答案

我想说你可能不能像那样安全地调用 FreeLibrary - 你将卸载你想要调用你的代码。您可能应该确保在收到通知之前不要释放 dll。

关于c++ - 显式链接 DLL 和程序挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1529942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com