473,563 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_SWITCHD ESKTOP );

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;
}
Nov 2 '06 #1
2 2339


"John Smith" <as****@asdasd. comwrote in message
news:eS******** ******@TK2MSFTN GP02.phx.gbl...
|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_SWITCHD ESKTOP );
|
| 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.publ ic.Win32.progra mmer.ui), this NG is for CSHARP related
questions.

Willy.

Nov 2 '06 #2
Hi,

You might want to fix your clock.

--
Dave Sexton

"John Smith" <as****@asdasd. comwrote in message
news:eS******** ******@TK2MSFTN GP02.phx.gbl...
>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_SWITCHD ESKTOP );

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


Nov 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1861
by: Eric Lilja | last post by:
Hello, I'm working with a hash table that is encapsulated in a class. One of its member functions insert() throws an exception if the insertion fails (for example, if the value was already present in the hash table). Now I have client code that looks like this: bool exception_thrown = false; try { hash_table.insert(some_value);
8
5459
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I...
4
2745
by: Alex Hunsley | last post by:
I've seen a few discussion about the use of 'super' in Python, including the opinion that 'super' should only be used to solve inheritance diamond problem. (And that a constructor that wants to call the superclass methods should just call them by name and forget about super.) What is people's opinion on this? Does it make any sense?
1
1597
by: Vagabond Software | last post by:
An ASP app is calling a T-SQL stored procedure, which creates and instance of a DLL and calls one of its methods. The DLL method creates an instance of an API to process credit card transactions. The client is seeing an OLE DB object error message, presumably raised by the ASP app after the stored procedure fails to call the DLL method. ...
3
5926
by: bb | last post by:
I have a windows network device driver written in c++ and a user interface im porting to c#, my problem is i dont seem to be getting notified of the event calls from the driver to the c# app im using the following code in c# in the UI to create an event public static IntPtr OpenGrantedPacketEvent() { IntPtr objDriver =...
5
6521
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly new to VB.NET, so I need some help. Here is a code snippit :
10
2956
by: Ami | last post by:
Hello everyone, I have developed a small access application and now I need to lock my forms against unwanted edits. I have used the code by Allen Browne I found here http://allenbrowne.com/ser-56.html and it works great, but I need to unlock and lock my main form and subform separately, using two different buttons.
18
9800
by: barry | last post by:
I'm having a problem creating a delay before each list item gets moved ( http://www.polisource.com/PublicMisc/list-splitter-timeout-bug.html ). Choose any number of columns, vertical layout, and view as a web page. They'll be a small delay before the entire bunch of list items converts from left-to-right to top-to-bottom order. I expected a...
8
6240
by: Nick | last post by:
Hi there, I have a GridView in an UpdatePanel, each time the UpdatePanels Load event fires I set the DataSource and call DataBind of the grid view. This works great once, I add an item to the list and the control updates, my breakpoints get in in the UpdatePanel Load event. Only problem is on the second time, even though the code is...
0
7658
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6238
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2077
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
912
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.