Why can only call SwitchDesktop once (calling again throw error)?

  • Thread starter Willy Denoyette [MVP]
  • Start date
W

Willy Denoyette [MVP]

|I have a DLL that checks to see if a user has locked their desktop. The
code
| is below. When i use this code a command-line, everything works perfectly
| every time. I can lock the desktop and it returns "true" every time.
| However, when I install it as an NT service, it works the first time and
| then throws an exception the second time. I have no idea why. So I have
two
| questions:
|
| 1. Any idea why it throws an error the second time it's called?
| 2. Where do i find the meaning of the error code returned by
SwitchDesktop?
|
|
| bool checkLocked()
| {
| HDESK currentDesktop;
| BOOL gotDesktop;
| DWORD errorNumber;
| bool isLocked = false;
|
| currentDesktop = OpenDesktop( "Default", 0, FALSE,
| DESKTOP_SWITCHDESKTOP );
|
| file://This is NOT where it throws a problem
| if ( currentDesktop == 0 ) ThrowException( "Unable to detect,
| problem..." );
| else
| {
| gotDesktop = SwitchDesktop( currentDesktop );
|
| errorNumber = GetLastError();
|
| if ( gotDesktop == 0 )
| {
| if ( errorNumber == 0 ) isLocked = true;
| else
| {
| /*** THIS IS WHERE IT ENDS UP ON THE SECOND CALL AND
| ALL LATER CALLS. IT PASSES THE FIRST TIME, HOWEVER. */
| wprintf( L"Error: %d", errorNumber);
| }
| }
| else isLocked = false;
| }
|
| CloseDesktop( currentDesktop );
| currentDesktop = NULL;
|
| return isLocked;
| }
|
|

Please post such questions to a win32 NG
(microsoft.public.Win32.programmer.ui), this NG is for CSHARP related
questions.

Willy.
 
J

John Smith

I have a DLL that checks to see if a user has locked their desktop. The code
is below. When i use this code a command-line, everything works perfectly
every time. I can lock the desktop and it returns "true" every time.
However, when I install it as an NT service, it works the first time and
then throws an exception the second time. I have no idea why. So I have two
questions:

1. Any idea why it throws an error the second time it's called?
2. Where do i find the meaning of the error code returned by SwitchDesktop?


bool checkLocked()
{
HDESK currentDesktop;
BOOL gotDesktop;
DWORD errorNumber;
bool isLocked = false;

currentDesktop = OpenDesktop( "Default", 0, FALSE,
DESKTOP_SWITCHDESKTOP );

file://This is NOT where it throws a problem
if ( currentDesktop == 0 ) ThrowException( "Unable to detect,
problem..." );
else
{
gotDesktop = SwitchDesktop( currentDesktop );

errorNumber = GetLastError();

if ( gotDesktop == 0 )
{
if ( errorNumber == 0 ) isLocked = true;
else
{
/*** THIS IS WHERE IT ENDS UP ON THE SECOND CALL AND
ALL LATER CALLS. IT PASSES THE FIRST TIME, HOWEVER. */
wprintf( L"Error: %d", errorNumber);
}
}
else isLocked = false;
}

CloseDesktop( currentDesktop );
currentDesktop = NULL;

return isLocked;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top