Does anyone have any examples of CreateThread or CreateProcess being called from VB (version 5 or 6). In particular, where can I get the values for all the enumerated dwCreationFlags.
I am hoping I can use one of these commands to launch a 16-bit app in its own memory space (dwCreationFlags to include CREATE_SEPATARE_WOW_VDM).
Thanks,
Bannerman
Public Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Public Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public hThread As Long, hThreadID As Long
Public Sub AsyncThread()
'Let this thread sleep for 10 seconds
i = WinExec("notepad", 1)
I have succesfully managed to create a multithreaded app, which worked really great on IDE.... When tried to compile it to EXE (standard or active-x the app crashed)
If anyone has an idea, or work-around, send a reply.
Hyperspaced
PS: I heard that VB.NET will enable smooth multi-threading. Is that true?
Folks - thanks for your help.
In my case I'm calling something synchronously (i.e. control passes to that process) so I'm not multi-threading or anything complicated - in fact the user is going to do a spreadsheet operation as part of a "workflow" script. But don't let me cramp your style as the issues of multi-threading are also interesting of course.
I'm going to use something based on the following code.
Incidentally, the WIN API viewer didn't give the values for the enumerated dwCreationFlags and so I found out some sample code by seaching on www.google.com (looking for CREATESEPARATE_WOW_VDM =).
In this noddy project there is one command button called "Run".
Obviously the naming standards etc leave something to be desired.
In the bas module:
Option Explicit
Public Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Const NORMAL_PRIORITY_CLASS = &H20
Public Const INFINITE = &HFFFF
Public Const STARTF_FORCEOFFFEEDBACK = &H80
Public Const STARTF_FORCEONFEEDBACK = &H40
Public Const STARTF_RUNFULLSCREEN = &H20
Public Const STARTF_USECOUNTCHARS = &H8
Public Const STARTF_USEFILLATTRIBUTE = &H10
Public Const STARTF_USEPOSITION = &H4
Public Const STARTF_USESHOWWINDOW = &H1
Public Const STARTF_USESIZE = &H2
Public Const STARTF_USESTDHANDLES = &H100
Public Const SW_SHOWNORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10
Public Const SW_PARENTCLOSING = 1
Public Const SW_OTHERZOOM = 2
Public Const SW_PARENTOPENING = 3
Public Const SW_OTHERUNZOOM = 4
Public Const SW_HIDE = 0
Public Const SW_NORMAL = 1
Public Const SW_MAXIMIZE = 3
Public Const SW_MAX = 10
Public Const SW_SCROLLCHILDREN = &H1
Public Const SW_INVALIDATE = &H2
Public Const SW_ERASE = &H4
Public Const CREATE_SEPARATE_WOW_VDM = &H800
Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" _
(ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
lpProcessAttributes As Any, _
lpThreadAttributes As Any, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
lpEnvironment As Any, _
ByVal lpCurrentDriectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
Public Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
In the main code:
Option Explicit
Private Sub cmdRun_Click()
Dim processInfo As PROCESS_INFORMATION
Dim startInfo As STARTUPINFO
Dim lpProcAttr As SECURITY_ATTRIBUTES
Dim lpThrdAttr As SECURITY_ATTRIBUTES
Dim lSuccess As Long
Dim sNull As String
Dim lRetVal As Long