I am using the following code to grab a handle to a window which works fine if my application is running normally.
If however I run my application as a service the code does not iterate over any of the users windows (guessing its because my app is now ran user SYSTEM user / session??).
Any help please on getting the below to work as a service targeting user windows??
Pointer pnt = GetTopWindow(0);
//Find the handle to the window
while (pnt) {
GetWindowThreadProcessId(pnt, iptr);
if (iptr = *pid of my target window*) {
break;
}
w = GetWindow(w, 2);
}
//assume it found it for now
if (w) {
DWORD hMyThread = GetWindowThreadProcessId(GetForegroundWindow(), null);
DWORD hOtherThread = GetWindowThreadProcessId(pnt, null);
AttachThreadInput(hMyThread, hOtherThread, true);
SetForegroundWindow(pnt);
AttachThreadInput(hMyThread, hOtherThread, false);
if (IsIconic(pnt))
ShowWindow(pnt, SW_RESTORE);
else
ShowWindow(pnt, SW_SHOW);
}
Using an interactive service does not work in windows 7/8, better use CreateProcessAsUser to spawn a new process which runs in interactive user context (using explorer.exe token).
Or just make the interactive process runs automatically at startup and communicate with the service.
I am using createprocessasuser to create a process but now I want to get a handle to the window and bring it to the front.
I had planned on using the above code to find the window handle by process ID (return by the createprocessasuser) but thegetwindow(w, NEXT) doesn't iterate over the launched window.
Use your ' thegetwindow(w, NEXT)' function inside your user process, not inside the service process (just comunicate with the service as needed). Why don't you spawn a user process at startup that does nothing and comunicate with the service as needed (all the job is done by this process, not the service itself) ?
If I could ask, why do you need a service at all ?
I have to have this run pre-logon for other reason so a service is definitely required and I cannot have another process run, I would preferably like to have this code functioning in a service.
Is there something I can do so that GetWindow etc can be grabbing windows from a different session?