ukanth
ukanth

Reputation: 2978

Explicit Linking DLL and Program Hangs

I've the following piece of code in my program which dynamically links wtsapi32.dll file for session notifications like WTS_SESSION_LOCK and WTS_SESSION_UNLOCK and runs in background. After the first lock/unlock the program hangs and not responding.

Is this a right way of doing explicit linking ?

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

Edited:

I have another method UnRegisterSession() function which calls WTSUnRegisterSessionNotification, I am calling the RegisterSession() in WinMain method ( removed FreeLibrary as suggested by 1800) and calling UnRegisterSession() in WM_DESTROY of CALLBACK WindowProcedure function. But still the application hangs.

Upvotes: 1

Views: 783

Answers (3)

Luke
Luke

Reputation: 11441

According to the documentation (http://msdn.microsoft.com/en-us/library/aa383841(VS.85).aspx):

"When a window no longer requires these notifications, it must call WTSUnRegisterSessionNotification before being destroyed."

I would try unregistering the notification during WM___CLOSE instead of WM_DESTROY.

Upvotes: 0

Elemental
Elemental

Reputation: 7521

MS documentation suggests that you must call WTSUnRegisterSessionNotification before re-registering the session - as it only happens on your second attempt to lock it perhaps this is your issue?

With 1800 wrt the free library - you must keep this library loaded while you use it.

Upvotes: 0

1800 INFORMATION
1800 INFORMATION

Reputation: 135463

I'd say you probably cannot safely call FreeLibrary like that - you will be unloading the code you want to have call you. You should probably ensure not to free the dll until after you are finished getting notifications.

Upvotes: 4

Related Questions