-2

I use the folowing function to start MS Word and open a file. This is done OK but the Word app is not maximized on top of my application. Is that not possible to add to my function?

function TFiles.ExecuteAndWait(const aFile: string; aParam: string = ''; const aHidden: boolean = False): integer;
var
  SEInfo: TShellExecuteInfo;
  ExitCode: DWORD;
begin
  FillChar(SEInfo, SizeOf(SEInfo), 0) ;
  SEInfo.cbSize := SizeOf(TShellExecuteInfo) ;
  with SEInfo do
    begin
      fMask := SEE_MASK_NOCLOSEPROCESS;
      Wnd := Application.Handle;
      lpFile := PChar(aFile) ;
      lpParameters := PChar(aParam) ;
      if aHidden = True then
        nShow := SW_HIDE
      else
        nShow := SW_SHOWNORMAL;
    end;
  if ShellExecuteEx(@SEInfo) then
    begin
      repeat
        Application.ProcessMessages;
        GetExitCodeProcess(SEInfo.hProcess, ExitCode) ;
      until (ExitCode <> STILL_ACTIVE) Or Application.Terminated;
      Result := ExitCode;
    end
  else
    Result := -1;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
OZ8HP
  • 1,443
  • 4
  • 31
  • 61

1 Answers1

0

There are a few problems here:

  1. Pass SW_MAXIMIZE if you want the window to be maximized.
  2. You leak the process handle. Call CloseHandle when you have finished with the process handle.
  3. The busy loop works, but is clumsy. You should use a wait function. Either in a separate thread use WaitForSingleObject. Or if you must wait in the main thread, and must use ProcessMessages, use MsgWaitForMultipleObjects.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Eurekalog doesn't report any memoryleaks so it must be another kind of leak you are talking about. – OZ8HP May 03 '15 at 11:47
  • Have you got an example of option 3? I am having a bit trouble following you. – OZ8HP May 03 '15 at 11:48
  • Yes, you are leaking a handle to the process. EurekaLog won't tell you about that. The superior madExcept will! – David Heffernan May 03 '15 at 11:50
  • As for option 3, which part. There are two distinct approaches suggested to avoid the busy loop. – David Heffernan May 03 '15 at 11:51
  • MsgWaitForMultipleObjects: http://stackoverflow.com/questions/10334553/processmessages-and-use-of-application/10341681#10341681 – David Heffernan May 03 '15 at 11:59
  • OK - so I should CloseHandle(Wnd)? – OZ8HP May 03 '15 at 15:02
  • No. Close the process handle. I'm not sure that you really understand the code that you wrote. Have you read the documentation for the api function that you called? – David Heffernan May 03 '15 at 15:04
  • It isn't my code - I inherited it from a colleague at my last work and haven't really looked at it since it worked - except for the focus. – OZ8HP May 03 '15 at 15:12
  • It has problems. I'd understand them and fix them if I were you. – David Heffernan May 03 '15 at 15:23
  • Are you still stuck on this? – David Heffernan May 04 '15 at 10:53
  • I wouldn't say stuck - I found the process to release. But the waiting is as it has always been. It works but may not be the finest code – OZ8HP May 04 '15 at 12:15
  • I'm very sorry that you did not find this helpful. I felt that I went beyond the original question but you obviously don't agree. – David Heffernan May 04 '15 at 12:17
  • Don't misunderstand me - it was helpfull. I found the leak but the waiting I am still working on solving. I just don't have that much time and as I say it works but it could be better. – OZ8HP May 04 '15 at 12:21
  • You didn't ask about any of that though. That was a bonus. It looks like I should have just answered your original question! – David Heffernan May 04 '15 at 12:29
  • That you did i item 1 - but LU RD did first – OZ8HP May 04 '15 at 12:31
  • There's only one answer so far as I can see. Also, do you care who comes first. It's easy to write a single sentence comment. Takes more effort to write an answer like mine. Looks like you care more about a quick answer than the content. That's not right. – David Heffernan May 04 '15 at 12:42