getwindowthreadprocessid different user

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??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
}
Last edited on
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.

Ideas?

NOTE: I am using windows 7.
Last edited on
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?
s there something I can do so that GetWindow etc can be grabbing windows from a different session?


No. Microsoft explicitly disabled service access cross-sessions for security reasons. Find another way.
Registered users can post here. Sign in or register to post.