Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Showing a process launched from a service

Status
Not open for further replies.

Griffyn

Programmer
Jul 11, 2002
1,077
AU
Hi all,

I've spent quite a few hours on this and haven't got anywhere. I'm writing a service application that executes Excel at some point (Excel will quit by itself after some processing due to it's macros). I want the Excel window to be hidden unless it takes too long to do it's processing, at which point it's window should be shown, and presumably the reason for it's hanging will be obvious. I can't manage to get the Excel window showing under any circumstances. I'm using this standard procedure:

Code:
[b]function[/b] WinExecAndWait32(FileName: [b]string[/b]; Visibility: Integer;
  TimeOut: Integer; [b]var[/b] Signal: Integer): dWord;
[b]var[/b]
  zAppName: [b]array[/b][[purple]0..512[/purple]] [b]of[/b] Char;
  zCurDir: [b]array[/b][[purple]0..255[/purple]] [b]of[/b] Char;
  WorkDir: [b]string[/b];
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;

  [b]procedure[/b] ExecuteProcess;
  [b]begin[/b]
    Signal := WaitforSingleObject(ProcessInfo.hProcess, TimeOut);
    GetExitCodeProcess(ProcessInfo.hProcess, Result);
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
  [b]end[/b];

[b]begin[/b]
  StrPCopy(zAppName, FileName);
  GetDir([purple]0[/purple], WorkDir);
  StrPCopy(zCurDir, WorkDir);
  FillChar(StartupInfo, Sizeof(StartupInfo), [teal]#0[/teal]);
  StartupInfo.cb := Sizeof(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := Visibility;
  [b]if[/b] [b]not[/b] CreateProcess([b]nil[/b],
           zAppName, [navy][i]{ pointer to command line string }[/i][/navy]
           [b]nil[/b], [navy][i]{ pointer to process security attributes }[/i][/navy]
           [b]nil[/b], [navy][i]{ pointer to thread security attributes }[/i][/navy]
           false, [navy][i]{ handle inheritance flag }[/i][/navy]
           CREATE_NEW_CONSOLE [b]or[/b] [navy][i]{ creation flags }[/i][/navy]
           NORMAL_PRIORITY_CLASS,
           [b]nil[/b], [navy][i]{ pointer to new environment block }[/i][/navy]
           [b]nil[/b], [navy][i]{ pointer to current directory name }[/i][/navy]
           StartupInfo, [navy][i]{ pointer to STARTUPINFO }[/i][/navy]
           ProcessInfo) [b]then[/b]
    Result := [purple]0[/purple] [navy][i]{ pointer to PROCESS_INF }[/i][/navy]
  [b]else[/b]
    ExecuteProcess;
[b]end[/b];

and am calling it with
Code:
WinExecAndWait32([teal]'"'[/teal] + FOfficeDir + [teal]'excel.exe" /r "blah.xls"'[/teal], SW_SHOWNORMAL, [purple]60000[/purple], m)

The 60000 is a timeout of 60 seconds, I handle this elsewhere.

That should work I thought. The service is logged in as a domain user, not the LocalSystem (has to be this way), so I don't have the opportunity to click the Interact with Desktop option in the service settings. The Interactive property in the TService object is set to True.

Maybe I have to attach the excel process to a visible window or something? like explorer.exe? I really don't know which direction I should be looking in next.

Thanks for your help!
 
I've had problems with Excel & macros in the past.

This is a stab in the dark, but have you tried putting a timer in the excel macro of say 5 seconds before it starts executing?
 
Griffyn, did you try your code from a normal app? (not a service)

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Hi guys,

The service used to be a normal app years ago and has been a stable service since then. I ported it to a service app specifically to hide all Excel windows, particularly the 'Printing x of x' dialog that pops up when printing.

Executed from a normal app, the Excel window does show. In Process Explorer (highly recommended freeware the Excel process shows up as a child process of my service app, so I'm presuming that because my service app has no visible window, it inherits that behaviour regardless of what visibility flags I throw at it. I've been looking at at how to change the parent window of a child process, but it appears it can only be done when the new parent and and the old parent are part of the same parent process.

I could redesign the Excel part so that instead of launching and quitting each time it has to do it's process, it instead runs all the time and just checks if it has a new process to perform, but then I'm back where I was with the Printing dialog showing up.

My main frustration is that this hang doesn't happen most of the time. If I launch Excel manually and get it to do it's thing, it's done it perfectly every time I've tried.
 
Well, I haven't managed to get the window visible, but through more logging in the Excel book I've identified where the hang is occuring. So this issue is temporarily closed for the moment.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top