Click to See Complete Forum and Search --> : Finding window handle from a service


thylboy
March 16th, 2003, 08:09 PM
Hi All!

Iīm sorry if this ha been posted already but I havenīt been able to find it....

I have an NT service that is trying to send a message to a user application window. I know the window title so Iīm trying to use FindWindow and FindWindowEx to get a handle that I can use to send messages using PostMessage and SendMessage... As long as I do it from a "normal" application (Window or console based) it works neat and tidy, but when run from my service, the same piece of code i sunable to retrieve a handle to the window....

So, my question is: How do I correctly get the handle of a user application from an NT service?

Thanks in advance!

dudiav
April 10th, 2003, 01:55 AM
You should set your service to interact with the desktop.

thylboy
April 10th, 2003, 05:55 PM
Doesnīt work if the service runs as a user account(which mine has to do)....

However, I now have found a way to find the handle to an interactive window anyway.

Thanks anyway!

muthuis
April 11th, 2003, 10:43 PM
How did you do it? If you tell us we may also be benefitted.

thylboy
April 12th, 2003, 10:50 AM
Well, it was actually simpler than I thought....

In short (I donīt have the details here, but if you like I can elaborate more later on):

Start by getting the Process object of the target application (the one running in user context), using GetProcessByName(string processname).

Now that you have the process you can actually find an IntPtr handle to the main window of the target process by using Process.GetMainWindow() (donīt remember the exact name, but it was similar to that).

Now you have IntPtr that you can pass to the API function PostMessage or SendMessage as the recieving window!

Let me know if you have any problems implementing this...

Greetings!

muthuis
April 12th, 2003, 08:09 PM
Thanks for the tip!