tags:

views:

148

answers:

3

Hello,

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.

+3  A: 

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.

1800 INFORMATION
Thanks for the information , But still it hangs ...
TuxGeek
I've fixed the problem and found that it was not because of the reason that I've mentioned.
TuxGeek
A: 

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.

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

According to the documentation (http://msdn.microsoft.com/en-us/library/aa383841%28VS.85%29.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.

Luke