March 05, 2021, 07:40:46 AM

Author Topic: [bmx] Run program (Windows-only) by BlitzSupport [ 1+ years ago ]  (Read 1034 times)

Offline BlitzBot

Title : Run program (Windows-only)
Author : BlitzSupport
Posted : 1+ years ago

Description : This gives a little more control over launching graphical applications on Windows than other methods available, eg. OpenURL, system_, CreateProcess, etc.

(Whether or not the minimise/maximise/hide flags have any effect depends on the program, though most should work correctly; the one I actually intended this for, Google Chrome, basically ignores all of the SW_SHOW parameters!)


Code :
Code: BlitzMax
  1. ?Win32
  2.  
  3. ' See http://msdn.microsoft.com/en-gb/library/windows/desktop/bb762153(v=vs.85).aspx
  4. ' for explanation of each constant...
  5.  
  6. Const SW_HIDE                   = 0
  7. Const SW_SHOWNORMAL             = 1
  8. Const SW_SHOWMINIMIZED          = 2
  9. Const SW_SHOWMAXIMIZED          = 3
  10. Const SW_MAXIMIZE               = 3 ' No idea why this duplicate exists...
  11. Const SW_SHOWNOACTIVATE         = 4
  12. Const SW_SHOW                   = 5
  13. Const SW_MINIMIZE               = 6
  14. Const SW_SHOWMINNOACTIVE        = 7
  15. Const SW_SHOWNA                 = 8
  16. Const SW_RESTORE                = 9
  17. Const SW_SHOWDEFAULT            = 10
  18.  
  19. Extern "win32"
  20.         Function ShellExecuteA (window:Int, op:Byte Ptr, file:Byte Ptr, params:Byte Ptr, dir:Byte Ptr, show:Int)
  21. End Extern
  22.  
  23. ' RunProgram parameters: exe = path to executable file, params = valid parameters for executable, showflag = one of the above constants, dir = working directory for program
  24.  
  25. Function RunProgram:Int (exe:String, params:String = "", showflag:Int = SW_SHOWNORMAL, dir:String = "")
  26.         If dir = "" Then dir = ExtractDir (exe)
  27.         Print dir
  28.         If ShellExecuteA (0, "open".ToCString (), exe.ToCString (), params.ToCString (), dir, showflag) > 32
  29.                 Return True
  30.         Else
  31.                 Return False
  32.         EndIf
  33. End Function
  34. ?
  35.  
  36. ' Run one instance maximised, one minimised and one normal...
  37.  
  38. RunProgram "notepad.exe", "", SW_SHOWMAXIMIZED
  39. Delay 500
  40.  
  41. RunProgram "notepad.exe", "", SW_SHOWMINIMIZED
  42. Delay 500
  43.  
  44. RunProgram "notepad.exe"
  45.  
  46. ' You can also run programs "hidden"; if you try this, you'll have to use Task Manager to kill notepad.exe afterwards!
  47.  
  48. ' RunProgram "notepad.exe", "", SW_HIDE
  49.  
  50. ' NB. Notepad is normally in the default Windows path, hence no need to provide the full path in this case; however, most programs will need the full path to the executable.


Comments :


Henri(Posted 1+ years ago)

 Hi,I'm wondering how would this work when used in "batch mode". For example you have a commandline program that modifies files or creates something according to program parameters and you would use ShellExecute to run this program 1000 times in a row. Would each start as it's own process and then you might run out of resources ? Would there be a way to wait (not freeze program flow) until process ends ?-Henri


BlitzSupport(Posted 1+ years ago)

 I only just saw this, sorry! You can force each call to wait until the program exits by amending the code to use <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx" target="_blank">ShellExecuteEx[/url]. There's a 'no async' flag that can be set to do this.


Henri(Posted 1+ years ago)

 Better late then never ;-)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal