aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Why does this throw exception when called twice from java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Why does this throw exception when called twice from java?" Watch "Why does this throw exception when called twice from java?" New topic
Author

Why does this throw exception when called twice from java?

Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
I have a JNI DLL that checks to see if a user has locked their desktop. The code is below. When i use this code from Java at 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 (with Tanuki), 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?

Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
What does GetLastError return?

If it's a standard error code, you can use the ErrorLookup tool that comes with VC++. Or you can look up the error codes here. If you want to get an error string from Windows, you can use the FormatMessage api. It might help you but sometimes WIndows error messages are cryptic. Usually, if I get a cryptic error message, I google the message that leads me to message boards where people have asked the same question.

My hunch is that once you call SwitchDesktop, the desktop stays switched, and next time when you call it, Windows gives error. That's just a hunch. You will have to get the error message to say for sure.
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Assuming I'm printing the error properly, it's 18.

I do:

char msg[256];
wsprintf( msg, "Error: %d", errorNumber);

then I print it to system.out and it says 18.

however, when I look that up in the MSDN link you provided, it says that means "ERROR_NO_MORE_FILES" which obviously isn't right. So the error must be defined elsewhere. any ideas?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

My question is, why doesn't it throw an exception the first time you use it?

To me it doesn't make sense for a Windows service to be asking about the desktop, because it doesn't really have a desktop. It does make sense for a logged-in user to do what you're asking, but given that there's Remote Desktop Connection available, it's possible for there to be several logged-in users on a Windows machine at one time. Which of their desktops would the service be asking about?
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Originally posted by Paul Clapham:
My question is, why doesn't it throw an exception the first time you use it?

To me it doesn't make sense for a Windows service to be asking about the desktop, because it doesn't really have a desktop. It does make sense for a logged-in user to do what you're asking, but given that there's Remote Desktop Connection available, it's possible for there to be several logged-in users on a Windows machine at one time. Which of their desktops would the service be asking about?


Good point, so yes, why does it work the first time? Any ideas what might be going on there?
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Dan, can you check if the screensaver is running when your get the error?
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Originally posted by Jayesh Lalwani:
Dan, can you check if the screensaver is running when your get the error?


No screensaver. I have screensaver turned off.
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Originally posted by Paul Clapham:
My question is, why doesn't it throw an exception the first time you use it?

To me it doesn't make sense for a Windows service to be asking about the desktop, because it doesn't really have a desktop. It does make sense for a logged-in user to do what you're asking, but given that there's Remote Desktop Connection available, it's possible for there to be several logged-in users on a Windows machine at one time. Which of their desktops would the service be asking about?


I'm still confused about this, then, from two aspects:

1. Is it an undocumented "feature" that the first time your nt service (running as system) goes into a locked desktop, it acts as the user it started under? (makes no sense)

2. How then would I determine a locked desktop from something running as System? Is it possible? Would the following work?

* NT Service tries to open and switch to desktop "Default" of user "A"

Would this have the same result? And would if that were done, and worked, what if at the same time another service is checking to see if User "A" is logged on, would it return true?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does this throw exception when called twice from java?