Ooops
November 28, 2020, 01:02:05 PM

Author Topic: [bmx] Create Process (windows) by Lomat [ 1+ years ago ]  (Read 516 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Create Process (windows) by Lomat [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Create Process (windows)
Author : Lomat
Posted : 1+ years ago

Description : My first posting to the blitzmax code archive so i hope it works :) Comments and suggestions welcome.

This class will allow you to spawn processes from within you application (under windows) and allow you to keep you blitz max application running in the background.

Example usage:

Local p:Process = New Process
p.setExe("c:windows
otepad.exe")
p.setArgs("C:Program FilesBlitzMaxdochome.html")
p.create()
While p.isRunning()
   Print "Process is running..."
   Delay(1000)
Wend
Print "Process finished with return value " + p.getReturnValue()


Code :
Code: BlitzMax
  1. Extern "Win32"
  2.         Function WaitForSingleObject:Int(hHandle:Int, dwMilliseconds:Int)
  3.         Function GetExitCodeProcess:Int(hProcess:Int, lpExitCode:Int)
  4.         Function CreateProcessA:Int(lpApplicationName:Byte Ptr, lpCommandLine:Byte Ptr, lpProcessAttributes:Int, lpThreadAttributes:Int, bInheritHandles:Int, dwCreationFlags:Int, lpEnvironment:Int, lpCurrentDirectory:Byte Ptr, lpStartupInfo:ProcessStartUpInfo, lpProcessInformation:ProcessInformation)
  5.         Function CloseHandle:Int(hProcess:Int)
  6.         Function GetActiveWindow:Int()
  7.         Function SetForegroundWindow(hWnd:Int)
  8.         Function SetActiveWindow(hWnd:Int)
  9.         Function OpenProcess:Int(dwDesiredAccess:Int, bInheritHandle:Int, dwProcessId:Int)
  10.         Function keybd_event(bVk:Byte, bScan:Byte, dwFlags:Int, dwExtraInfo:Int)
  11. End Extern
  12.  
  13. Type ProcessStartUpInfo
  14.    Field cb:Int
  15.    Field lpReserved:String
  16.    Field lpDesktop:String
  17.    Field lpTitle:String
  18.    Field dwX:Int
  19.    Field dwY:Int
  20.    Field dwXSize:Int
  21.    Field dwYSize:Int
  22.    Field dwXCountChars:Int
  23.    Field dwYCountChars:Int
  24.    Field dwFillAttribute:Int
  25.    Field dwFlags:Int
  26.    Field wShowWindow:Int
  27.    Field cbReserved2:Int
  28.    Field lpReserved2:Int
  29.    Field hStdInput:Int
  30.    Field hStdOutput:Int
  31.    Field hStdError:Int
  32. End Type
  33.  
  34. Type ProcessInformation
  35.    Field hProcess:Int
  36.    Field hThread:Int
  37.    Field dwProcessID:Int
  38.    Field dwThreadID:Int
  39. End Type
  40.  
  41. Type Process
  42.  
  43.         Field exe:String
  44.         Field args:String
  45.         Field proc:ProcessInformation
  46.         Field returnValue:Int
  47.         Field pHandle:Int
  48.         Field myhWnd:Int
  49.        
  50.         Method New()
  51.                 myHwnd = GetActiveWindow()
  52.         End Method
  53.        
  54.         Method setExe (appPath:String)
  55.                 exe = appPath
  56.         End Method
  57.  
  58.         Method setArgs (argString:String)
  59.                 args = argString
  60.         End Method
  61.        
  62.         Method getReturnValue:Int ()
  63.                 Return returnValue
  64.         End Method
  65.        
  66.         Method create()
  67.                 ' change to exe directory
  68.                 ChangeDir(ExtractDir(exe))
  69.                 ' spawn process...
  70.                 Local cmd:String  = "~q" + exe + "~q" + " " + "~q" + args + "~q"
  71.                 Print "Run process: " + cmd
  72.                 Local start:ProcessStartUpInfo = New ProcessStartUpInfo
  73.                 proc = New ProcessInformation
  74.                 CreateProcessA(Null, cmd.ToCString(), 0, 0, 1, 0, 0, Null, start, proc)
  75.                 pHandle     = OpenProcess($100000, -1, proc.hProcess)
  76.                 returnValue = Null
  77.         End Method
  78.        
  79.         Method isRunning:Int()
  80.                 Local v:Int = WaitForSingleObject(pHandle, 0)
  81.                 Select v
  82.                         Case 0
  83.                                 returnValue = GetExitCodeProcess(pHandle, v)
  84.                                 CloseHandle(pHandle)
  85.                                 Print "Process finished with return value: " + returnValue
  86.                                 ' change back to this app dir.
  87.                                 ChangeDir(LaunchDir$)
  88.                                 ' Set focus back to this application.
  89.                                 SetForegroundWindow(myHWnd)
  90.                                 SetActiveWindow(myHWnd)
  91.                                
  92.                                 ' Since were a full screen app Windows does not like
  93.                                 ' makign you go back into full screen mode so we have
  94.                                 ' to emulate the user pressing Alt + Enter :)
  95.                                
  96.                                 ' Add a little delay
  97.                                 Delay(100)
  98.                                 ' send alt + enter to get back full screen window :)
  99.                                 keybd_event(KEY_ALT, 0, 0, 0)
  100.                                 keybd_event(KEY_RETURN, 0, 0, 0)
  101.                                 ' we have to let go of the keys :)
  102.                                 keybd_event(KEY_RETURN, 0, 2, 0)
  103.                                 keybd_event(KEY_ALT, 0, 2, 0)
  104.                                 ' app should now be back in focus and on the screen.
  105.                                 Return 0
  106.                         Case -1
  107.                                 ' We normally get this if the process failed to launch.
  108.                                 Print "WaitForSingleObject failed."
  109.                                 CloseHandle(pHandle)
  110.                                 Return 0
  111.                         Default
  112.                                 ' Process is running.
  113.                                 Return 1                               
  114.                 End Select
  115.         End Method
  116.        
  117. End Type


Comments :


Lomat(Posted 1+ years ago)

 For getting back to full screen sending ALT+TAB instead of ALT+ENTER might work better. YMMV so see which one works for you. :)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal