pinvokes from windows service
-
I have a .NET 1.1 (for compatibility reasons) application that runs as a windows service. I have successfully ran it, except the only problem is im not able to invoke methods in the user32 dll i need to call. I have ran the exact same code as a normal process, and this doesn't seem to be happening. Ive also isolated and ran portions of the code just to tell if the invocations were even successful, and they weren't, so i know it's not a problem with my applications code. As far as i can guess running as a service changes some sort of permissions or something that doesn't allow me to invoke these methods. Anyway im not an expert on the windows os, so before i make myself sound even dumber im gonna shut up.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
-
I have a .NET 1.1 (for compatibility reasons) application that runs as a windows service. I have successfully ran it, except the only problem is im not able to invoke methods in the user32 dll i need to call. I have ran the exact same code as a normal process, and this doesn't seem to be happening. Ive also isolated and ran portions of the code just to tell if the invocations were even successful, and they weren't, so i know it's not a problem with my applications code. As far as i can guess running as a service changes some sort of permissions or something that doesn't allow me to invoke these methods. Anyway im not an expert on the windows os, so before i make myself sound even dumber im gonna shut up.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
Hi,
jacobjordan wrote:
user32 dll
jacobjordan wrote:
doesn't allow me to invoke these methods
which functions exactly? anything related to user interaction? (a service has no desktop, no user, by default) :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
I have a .NET 1.1 (for compatibility reasons) application that runs as a windows service. I have successfully ran it, except the only problem is im not able to invoke methods in the user32 dll i need to call. I have ran the exact same code as a normal process, and this doesn't seem to be happening. Ive also isolated and ran portions of the code just to tell if the invocations were even successful, and they weren't, so i know it's not a problem with my applications code. As far as i can guess running as a service changes some sort of permissions or something that doesn't allow me to invoke these methods. Anyway im not an expert on the windows os, so before i make myself sound even dumber im gonna shut up.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
Add some exception handling to your code around the PInvoke calls and write to the error log in the catch blocks and you will be able to tell exactly what the problem is. If it's a permission issue then you may need to specify a user to run the service as by setting the System.ServiceProcess.ServiceAccount[^]
Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Add some exception handling to your code around the PInvoke calls and write to the error log in the catch blocks and you will be able to tell exactly what the problem is. If it's a permission issue then you may need to specify a user to run the service as by setting the System.ServiceProcess.ServiceAccount[^]
Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)The methods im invoking are GetAsyncKeyState, GetWindowText, and GetForegroundWindow, all of which return values. No exceptions are being thrown when i invoke, only when i run my app as a service they return a value of 0 (since all return either ints, shorts, or intptrs). Obviously the methods aren't being called for some reason.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
-
The methods im invoking are GetAsyncKeyState, GetWindowText, and GetForegroundWindow, all of which return values. No exceptions are being thrown when i invoke, only when i run my app as a service they return a value of 0 (since all return either ints, shorts, or intptrs). Obviously the methods aren't being called for some reason.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
jacobjordan wrote:
GetWindowText, and GetForegroundWindow
those are GUI functions, your service by default has no user and no desktop, hence no GUI. You must tell it somehow to allow user interaction and specify the account type. I don't know the details. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
-
The methods im invoking are GetAsyncKeyState, GetWindowText, and GetForegroundWindow, all of which return values. No exceptions are being thrown when i invoke, only when i run my app as a service they return a value of 0 (since all return either ints, shorts, or intptrs). Obviously the methods aren't being called for some reason.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
As Luc said, these are GUI functions. As the service isn't using the logged in user(s) desktop(s) these functions will not suceed or will fail gracefuly. Why does this need to be a service? An app minimized to the tray would be able to do the job perfectly.
Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
The methods im invoking are GetAsyncKeyState, GetWindowText, and GetForegroundWindow, all of which return values. No exceptions are being thrown when i invoke, only when i run my app as a service they return a value of 0 (since all return either ints, shorts, or intptrs). Obviously the methods aren't being called for some reason.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
Found a good blog here[^] that may help you rethink and workaround your problem
Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
As Luc said, these are GUI functions. As the service isn't using the logged in user(s) desktop(s) these functions will not suceed or will fail gracefuly. Why does this need to be a service? An app minimized to the tray would be able to do the job perfectly.
Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)I need to have it running no matter who is logged on, or if nobody is logged on. I know there are probably other ways of doing that, but creating a service was the first thing that came to mind. I would love it if you could suggest and easier way.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
-
I need to have it running no matter who is logged on, or if nobody is logged on. I know there are probably other ways of doing that, but creating a service was the first thing that came to mind. I would love it if you could suggest and easier way.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
I see your logic for a service but services are non GUI in the sense that they have no UI of their own and can't (since Vista) interact directly with a user's UI.
jacobjordan wrote:
or if nobody is logged on
That makes no sense as those calls will be useless.
jacobjordan wrote:
no matter who is logged on
You can place the path to your exe in
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
and your app will run at start up for all users - you can run it minimized to tray, or invisible by setting the Visible of the main form and ShowInTaskbar to false.Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I need to have it running no matter who is logged on, or if nobody is logged on. I know there are probably other ways of doing that, but creating a service was the first thing that came to mind. I would love it if you could suggest and easier way.
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -Robert Firth
Yes, sounds like a service. However, what is a service going to do with some WindowText if it doesn't even know if anyone is logged in? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.