🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
5 messages
5 total messages Started by Paul Fri, 20 Jul 2007 12:05
Service spawns Process - EnumWindows problem
#1593
Author: Paul
Date: Fri, 20 Jul 2007 12:05
46 lines
2288 bytes

-- 

Hi,
We have a service-started automatically on system startup to
-Start Process A
-Start process B
-Start process Guardian.
the service then terminates.

Each of the above is started with 
if (CreateProcess(NULL, process_name.c_str(), NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS, NULL, NULL, &startup_info, &process_info[0]))
  {...}

Using Windows Messaging 
- RegisterWindowMessage
- EnumWindows to get the required handle
- then PostMessage

At this point EnumWindows can find the required handle from the process
id. There may or may not be a user logged on at this point - it makes
no difference to the following outcomes.

The messaging problem starts when a process stops and is subsequently
restarted by the guardian, using CreateProcess(..as above..)

Using the SysInternals Process Explorer and debug output from the
Guardian I can see that the Guardian has the correct process id for the
restarted process.

Problems
1. EnumWindows does not have this process id in the list returned when
trying to get the new window handle to use, so no messaging is possible
withou a valid handle.

2.Manually entering the windows handle of the restarted process into
the guardian and using PostMessage - The restarted process does not see
the message.


Anyone have some clues on what I am doing wrong ?

cheers
paul
Re: Service spawns Process - EnumWindows problem
#1594
Author: Bob Gonder
Date: Fri, 20 Jul 2007 15:49
25 lines
1826 bytes
Paul wrote:

Please don't start messages with "-" as it strips the rest of the message from replys.


>1. EnumWindows does not have this process id in the list returned when
>trying to get the new window handle to use, so no messaging is possible
>withou a valid handle.

The window hasn't been created yet so Enum won't find it.

>2.Manually entering the windows handle of the restarted process into
>the guardian and using PostMessage - The restarted process does not see
>the message.

Probably the wrong handle.
Unless RegisterWindowMessage is messing up.

>Anyone have some clues on what I am doing wrong ?

Pass the starter's (service or guardian) handle (converted to text) to the Application on
the commandline and wait for it to contact you with the  registered message.  It can place
it's own handle in the message. Eliminates enum.

(BTW, who guards the guardian?)
Re: Service spawns Process - EnumWindows problem
#1595
Author: Remy Lebeau (TeamB)
Date: Fri, 20 Jul 2007 17:33
50 lines
3021 bytes
"Paul" <camrad@internode.on.net> wrote in message
news:46a0a522$1@newsgroups.borland.com...

> Each of the above is started with
> if (CreateProcess(...
<snip>

Which means that each process is going to run in the context of the user
account that the service is running it.  By default, that is the SYSTEM
account.  Also, unless you are filling in the lpDesktop member of the
STARTUPINFO structure, then each process is going to run in the context of
the service's workstation as well, which is hidden and does not have access
to the workstation that the user logs into.

> Using Windows Messaging
> - RegisterWindowMessage
> - EnumWindows to get the required handle
> - then PostMessage

That won't work in a service unless you first connect it to the user's
desktop via SetThreadDesktop().  Same thing with spawned processes - they
must be running in the same desktop that the target window is running in, or
else EnumWindows() and PostMessage() will fail to locate it.

> At this point EnumWindows can find the required handle from
> the process id. There may or may not be a user logged on at
> this point - it makes no difference to the following outcomes.

Yes, it does make a difference.

> EnumWindows does not have this process id in the list returned
> when trying to get the new window handle to use, so no messaging
> is possible withou a valid handle.

Every time a process is restarted, the other apps will have to rescan the
active windows in order to get the new ID.  Alternative, assuming the
guardian is running in the same desktop as the other processes, it could
simply broadcast a message containing the old and new IDs, and the processes
can then update themselves accordingly.

> Manually entering the windows handle of the restarted process
> into the guardian and using PostMessage - The restarted process
> does not see the message.

That suggests to me that the guardian is not actually running in the same
desktop afterall, as window messages cannot pass across desktop boundaries.


Gambit
Re: Service spawns Process - EnumWindows problem
#1596
Author: Paul
Date: Sat, 21 Jul 2007 10:30
29 lines
1937 bytes
Remy Lebeau (TeamB) wrote:

> 
> "Paul" <camrad@internode.on.net> wrote in message
> news:46a0a522$1@newsgroups.borland.com...
> 
> > Each of the above is started with
> > if (CreateProcess(...
> <snip>
> 
> Which means that each process is going to run in the context of the
> user account that the service is running it.  By default, that is the
> SYSTEM account.  Also, unless you are filling in the lpDesktop member
> of the STARTUPINFO structure, then each process is going to run in
> the context of the service's workstation as well, which is hidden and
> does not have access to the workstation that the user logs into.

I was not setting any value for lpDesktop. Setting this to 
"WinSta0\\Default" in the Service and the Guardian fixed my problem in
this instance. Reading the M$ web on this subject presents other
possibilities I have not yet explored.
> 
...snip

Thanks for you help on this. Got me going in the right direction. Much
appreciated.

cheers
Paul
Re: Service spawns Process - EnumWindows problem
#1497
Author: Paul
Date: Sat, 21 Jul 2007 10:38
22 lines
1409 bytes
Bob Gonder wrote:

> Paul wrote:
> 
> Please don't start messages with "-" as it strips the rest of the
> message from replys.

Point taken.

In this case, the problem was solved by setting the lpDesktop parameter
to "WinSta0\\Default" in both the Service and Guardian when calling
CreateProcess()
...snip

> (BTW, who guards the guardian?)

No one, If there was a guardians guardian, who would guard the
guardians guardian ?

cheers
Paul
-- 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads