Search code examples
powershellshellexecute

How to keep the window open: ShellExecuteW(0,0,"powershell.exe", "..","..",SW_SHOW)?



from another program (metatrader) I start a ps-script to download some emails:

 shl = ShellExecuteW(0,0,"powershell.exe", "-file x.ps1","..\\path\\to\\scripts",SW_SHOW);

After a couple of weeks without any problem(!) I saw all of a sudden some red error code in the console - but it was closed too fast the return code (shl) signals no error: shl <=32.

Now I tried to start ShellExecuteW(..) with

  shl = ShellExecuteW(0,0,"powershell.exe","..","..",SW_SHOWNOACTIVATE);
  # SW_SHOWNOACTIVATE = 4 
  # 4: Displays a window in its most recent size and position. The active window remains active.

But again the console disappears :(

1) What do I have to enter so that the console stays open - for me to close 1t manually?
2) How do I force ShellExecuteW(..) to add the error-messages to an error file?

Thanks and a nice weekend,
Gooly

PS: After I re-started the program with ShellExecuteW(..) it runs again without any error?


Solution

  • I think the problem is not ShellExecute, but how Powershell is invoked.

    Try this:

    shl = ShellExecuteW(0,0,"powershell.exe", "-noexit -file x.ps1","..\\path\\to\\scripts",SW_SHOW);