I created a service which uses shellexecuteex and WaitForSingleObject to open
an application. my requirement is to keep the app running until the service
is stopped, even if the user terminates the app from task manager. here is my
code but it doesn't restart the app when it is closed. any help is
appreciated, thanks

restart:
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = szPath;
ShExecInfo.lpParameters = ""; //"service";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
CloseHandle(ShExecInfo.hProcess);
goto restart;

RE: shellexecuteex in windows service, to run the app persistently by ganesh

ganesh
Fri Jul 21 13:44:02 CDT 2006

ResumeThread(ShExecInfo.hProcess); seems to work; however, the service stops
once the app has been terminated

restart:
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = szPath;
ShExecInfo.lpParameters = ""; //"service";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
ResumeThread(ShExecInfo.hProcess);
//CloseHandle(ShExecInfo.hProcess);
goto restart;


"ganesh" wrote:

> I created a service which uses shellexecuteex and WaitForSingleObject to open
> an application. my requirement is to keep the app running until the service
> is stopped, even if the user terminates the app from task manager. here is my
> code but it doesn't restart the app when it is closed. any help is
> appreciated, thanks
>
> restart:
> ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
> ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
> ShExecInfo.hwnd = NULL;
> ShExecInfo.lpVerb = NULL;
> ShExecInfo.lpFile = szPath;
> ShExecInfo.lpParameters = ""; //"service";
> ShExecInfo.lpDirectory = NULL;
> ShExecInfo.nShow = SW_SHOW;
> ShExecInfo.hInstApp = NULL;
> ShellExecuteEx(&ShExecInfo);
> WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
> CloseHandle(ShExecInfo.hProcess);
> goto restart;
>

Re: shellexecuteex in windows service, to run the app persistently by JJ

JJ
Fri Jul 21 14:34:20 CDT 2006

ResumeThread won't work - that only resumes a thread that was previously
suspended with SuspendThread. A process that has ended is not suspended -
it has ended.

Are you doing any error checking?

The second time through, what does ShellExecuteEx return?


"ganesh" <ganesh@discussions.microsoft.com> wrote in message
news:D4E809A2-C072-4BE0-B688-2AF8D5E00933@microsoft.com...
> ResumeThread(ShExecInfo.hProcess); seems to work; however, the service
> stops
> once the app has been terminated
>
> restart:
> ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
> ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
> ShExecInfo.hwnd = NULL;
> ShExecInfo.lpVerb = NULL;
> ShExecInfo.lpFile = szPath;
> ShExecInfo.lpParameters = ""; //"service";
> ShExecInfo.lpDirectory = NULL;
> ShExecInfo.nShow = SW_SHOW;
> ShExecInfo.hInstApp = NULL;
> ShellExecuteEx(&ShExecInfo);
> WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
> ResumeThread(ShExecInfo.hProcess);
> //CloseHandle(ShExecInfo.hProcess);
> goto restart;
>
>
> "ganesh" wrote:
>
>> I created a service which uses shellexecuteex and WaitForSingleObject to
>> open
>> an application. my requirement is to keep the app running until the
>> service
>> is stopped, even if the user terminates the app from task manager. here
>> is my
>> code but it doesn't restart the app when it is closed. any help is
>> appreciated, thanks
>>
>> restart:
>> ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
>> ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
>> ShExecInfo.hwnd = NULL;
>> ShExecInfo.lpVerb = NULL;
>> ShExecInfo.lpFile = szPath;
>> ShExecInfo.lpParameters = ""; //"service";
>> ShExecInfo.lpDirectory = NULL;
>> ShExecInfo.nShow = SW_SHOW;
>> ShExecInfo.hInstApp = NULL;
>> ShellExecuteEx(&ShExecInfo);
>> WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
>> CloseHandle(ShExecInfo.hProcess);
>> goto restart;
>>



Re: shellexecuteex in windows service, to run the app persistently by ganesh

ganesh
Fri Jul 21 15:02:02 CDT 2006

I believe it returns hProcess though i'm not able to capture it.
I removed the ResumeThread now.
One thing I couldn't figure out how does the service know the application is
closed; and how do i prevent the service from stopping when the application
is closed


"JJ" wrote:

> ResumeThread won't work - that only resumes a thread that was previously
> suspended with SuspendThread. A process that has ended is not suspended -
> it has ended.
>
> Are you doing any error checking?
>
> The second time through, what does ShellExecuteEx return?
>
>
> "ganesh" <ganesh@discussions.microsoft.com> wrote in message
> news:D4E809A2-C072-4BE0-B688-2AF8D5E00933@microsoft.com...
> > ResumeThread(ShExecInfo.hProcess); seems to work; however, the service
> > stops
> > once the app has been terminated
> >
> > restart:
> > ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
> > ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
> > ShExecInfo.hwnd = NULL;
> > ShExecInfo.lpVerb = NULL;
> > ShExecInfo.lpFile = szPath;
> > ShExecInfo.lpParameters = ""; //"service";
> > ShExecInfo.lpDirectory = NULL;
> > ShExecInfo.nShow = SW_SHOW;
> > ShExecInfo.hInstApp = NULL;
> > ShellExecuteEx(&ShExecInfo);
> > WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
> > ResumeThread(ShExecInfo.hProcess);
> > //CloseHandle(ShExecInfo.hProcess);
> > goto restart;
> >
> >
> > "ganesh" wrote:
> >
> >> I created a service which uses shellexecuteex and WaitForSingleObject to
> >> open
> >> an application. my requirement is to keep the app running until the
> >> service
> >> is stopped, even if the user terminates the app from task manager. here
> >> is my
> >> code but it doesn't restart the app when it is closed. any help is
> >> appreciated, thanks
> >>
> >> restart:
> >> ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
> >> ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
> >> ShExecInfo.hwnd = NULL;
> >> ShExecInfo.lpVerb = NULL;
> >> ShExecInfo.lpFile = szPath;
> >> ShExecInfo.lpParameters = ""; //"service";
> >> ShExecInfo.lpDirectory = NULL;
> >> ShExecInfo.nShow = SW_SHOW;
> >> ShExecInfo.hInstApp = NULL;
> >> ShellExecuteEx(&ShExecInfo);
> >> WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
> >> CloseHandle(ShExecInfo.hProcess);
> >> goto restart;
> >>
>
>
>

Re: shellexecuteex in windows service, to run the app persistently by JJ

JJ
Fri Jul 21 16:00:57 CDT 2006

No, ShellExecuteEx returns a BOOL that is TRUE if successful or FALSE if
not.

If it is TRUE (1), then it started the process.

If it is FALSE (0), an error occurred and you have to call GetLastError() to
find out what the error code is.

The service knows when the application started with ShellExecuteEx closes
because you have the WaitForSingleObject(x,INFINITE) call. That call will
"block" (wait) forever until the process handle you passed to it has ended.
Then it returns. When a process ends, it's handle becomes "signaled" and
that trips WaitForSingleObject.

I'm not sure what you mean by preventing the service from stopping when the
application closes - as long as you stay in some sort of loop, it will not
end.

"ganesh" <ganesh@discussions.microsoft.com> wrote in message
news:7D124071-54E7-4C71-8FE8-D7B2803ED9D8@microsoft.com...
>I believe it returns hProcess though i'm not able to capture it.
> I removed the ResumeThread now.
> One thing I couldn't figure out how does the service know the application
> is
> closed; and how do i prevent the service from stopping when the
> application
> is closed
>
>
> "JJ" wrote:
>
>> ResumeThread won't work - that only resumes a thread that was previously
>> suspended with SuspendThread. A process that has ended is not
>> suspended -
>> it has ended.
>>
>> Are you doing any error checking?
>>
>> The second time through, what does ShellExecuteEx return?
>>
>>
>> "ganesh" <ganesh@discussions.microsoft.com> wrote in message
>> news:D4E809A2-C072-4BE0-B688-2AF8D5E00933@microsoft.com...
>> > ResumeThread(ShExecInfo.hProcess); seems to work; however, the service
>> > stops
>> > once the app has been terminated
>> >
>> > restart:
>> > ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
>> > ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
>> > ShExecInfo.hwnd = NULL;
>> > ShExecInfo.lpVerb = NULL;
>> > ShExecInfo.lpFile = szPath;
>> > ShExecInfo.lpParameters = ""; //"service";
>> > ShExecInfo.lpDirectory = NULL;
>> > ShExecInfo.nShow = SW_SHOW;
>> > ShExecInfo.hInstApp = NULL;
>> > ShellExecuteEx(&ShExecInfo);
>> > WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
>> > ResumeThread(ShExecInfo.hProcess);
>> > //CloseHandle(ShExecInfo.hProcess);
>> > goto restart;
>> >
>> >
>> > "ganesh" wrote:
>> >
>> >> I created a service which uses shellexecuteex and WaitForSingleObject
>> >> to
>> >> open
>> >> an application. my requirement is to keep the app running until the
>> >> service
>> >> is stopped, even if the user terminates the app from task manager.
>> >> here
>> >> is my
>> >> code but it doesn't restart the app when it is closed. any help is
>> >> appreciated, thanks
>> >>
>> >> restart:
>> >> ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
>> >> ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
>> >> ShExecInfo.hwnd = NULL;
>> >> ShExecInfo.lpVerb = NULL;
>> >> ShExecInfo.lpFile = szPath;
>> >> ShExecInfo.lpParameters = ""; //"service";
>> >> ShExecInfo.lpDirectory = NULL;
>> >> ShExecInfo.nShow = SW_SHOW;
>> >> ShExecInfo.hInstApp = NULL;
>> >> ShellExecuteEx(&ShExecInfo);
>> >> WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
>> >> CloseHandle(ShExecInfo.hProcess);
>> >> goto restart;
>> >>
>>
>>
>>



Re: shellexecuteex in windows service, to run the app persistently by ganesh

ganesh
Fri Jul 21 18:05:01 CDT 2006

> I'm not sure what you mean by preventing the service from stopping when the
> application closes - as long as you stay in some sort of loop, it will not
> end.
Basically i'm using this shellexecuteex within servicestart(). my idea is
when the application is closed by the user it would read 'goto restart' and
restart the application again but instead it just stops the service itself
VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
{
restart:
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
goto restart
}


"JJ" wrote:

> No, ShellExecuteEx returns a BOOL that is TRUE if successful or FALSE if
> not.
>
> If it is TRUE (1), then it started the process.
>
> If it is FALSE (0), an error occurred and you have to call GetLastError() to
> find out what the error code is.
>
> The service knows when the application started with ShellExecuteEx closes
> because you have the WaitForSingleObject(x,INFINITE) call. That call will
> "block" (wait) forever until the process handle you passed to it has ended.
> Then it returns. When a process ends, it's handle becomes "signaled" and
> that trips WaitForSingleObject.
>
> I'm not sure what you mean by preventing the service from stopping when the
> application closes - as long as you stay in some sort of loop, it will not
> end.
>
> "ganesh" <ganesh@discussions.microsoft.com> wrote in message
> news:7D124071-54E7-4C71-8FE8-D7B2803ED9D8@microsoft.com...
> >I believe it returns hProcess though i'm not able to capture it.
> > I removed the ResumeThread now.
> > One thing I couldn't figure out how does the service know the application
> > is
> > closed; and how do i prevent the service from stopping when the
> > application
> > is closed
> >
> >
> > "JJ" wrote:
> >
> >> ResumeThread won't work - that only resumes a thread that was previously
> >> suspended with SuspendThread. A process that has ended is not
> >> suspended -
> >> it has ended.
> >>
> >> Are you doing any error checking?
> >>
> >> The second time through, what does ShellExecuteEx return?
> >>
> >>
> >> "ganesh" <ganesh@discussions.microsoft.com> wrote in message
> >> news:D4E809A2-C072-4BE0-B688-2AF8D5E00933@microsoft.com...
> >> > ResumeThread(ShExecInfo.hProcess); seems to work; however, the service
> >> > stops
> >> > once the app has been terminated
> >> >
> >> > restart:
> >> > ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
> >> > ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
> >> > ShExecInfo.hwnd = NULL;
> >> > ShExecInfo.lpVerb = NULL;
> >> > ShExecInfo.lpFile = szPath;
> >> > ShExecInfo.lpParameters = ""; //"service";
> >> > ShExecInfo.lpDirectory = NULL;
> >> > ShExecInfo.nShow = SW_SHOW;
> >> > ShExecInfo.hInstApp = NULL;
> >> > ShellExecuteEx(&ShExecInfo);
> >> > WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
> >> > ResumeThread(ShExecInfo.hProcess);
> >> > //CloseHandle(ShExecInfo.hProcess);
> >> > goto restart;
> >> >
> >> >
> >> > "ganesh" wrote:
> >> >
> >> >> I created a service which uses shellexecuteex and WaitForSingleObject
> >> >> to
> >> >> open
> >> >> an application. my requirement is to keep the app running until the
> >> >> service
> >> >> is stopped, even if the user terminates the app from task manager.
> >> >> here
> >> >> is my
> >> >> code but it doesn't restart the app when it is closed. any help is
> >> >> appreciated, thanks
> >> >>
> >> >> restart:
> >> >> ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
> >> >> ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
> >> >> ShExecInfo.hwnd = NULL;
> >> >> ShExecInfo.lpVerb = NULL;
> >> >> ShExecInfo.lpFile = szPath;
> >> >> ShExecInfo.lpParameters = ""; //"service";
> >> >> ShExecInfo.lpDirectory = NULL;
> >> >> ShExecInfo.nShow = SW_SHOW;
> >> >> ShExecInfo.hInstApp = NULL;
> >> >> ShellExecuteEx(&ShExecInfo);
> >> >> WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
> >> >> CloseHandle(ShExecInfo.hProcess);
> >> >> goto restart;
> >> >>
> >>
> >>
> >>
>
>
>

Re: shellexecuteex in windows service, to run the app persistently by Bruno

Bruno
Sun Jul 23 03:50:14 CDT 2006

> Basically i'm using this shellexecuteex within servicestart(). my idea is
> when the application is closed by the user it would read 'goto restart'
> and
> restart the application again but instead it just stops the service itself
> VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
> {
> restart:
> ShellExecuteEx(&ShExecInfo);
> WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
> goto restart
> }

You don't perform any error checking after ShellExecuteEx. How do you know
that it succeeded?
Please check the return value, and use GetLastError appropriately.

Another thing: while I am absolutely not opposed to using goto in code for
clarity, using goto like this is ugly IMO.
It would be better to use a loop, and use the return value of ShellExecute
as the loop variable that determines if the loop has to repeat or not.

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"