Microsoft KB Archive/265793

= How To Use the CreateProcess API from eMbedded Visual Basic =

Article ID: 265793

Article Last Modified on 8/19/2005

-

APPLIES TO


 * Microsoft eMbedded Visual Basic 3.0
 * Microsoft Windows CE Toolkit for Visual Basic 6.0

-



This article was previously published under Q265793



SUMMARY
This article illustrates how the CreateProcess API function call can be made from an eMbedded Visual Basic (eVB) application that runs on a Windows CE-based device that runs Windows CE 2.11 or later.



Step-by-Step Example
The following sample code starts ActiveSync on the device.  Start a new Windows CE project in eMbedded Visual Basic. Form1 is created by default. On the Project menu, choose Add Module to add a new module to the project.  Paste the following code into Module1: Option Explicit Public Declare Function TerminateProcess Lib &quot;Coredll&quot; (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Public Declare Function CreateProcess Lib &quot;coredll.DLL&quot; _ Alias &quot;CreateProcessW&quot; _ (ByVal lpApplicationName As String, _   ByVal lpCommandLine As String, _    ByVal lpProcessAttributes As Long, _    ByVal lpThreadAttributes As Long, _    ByVal bInheritHandles As Long, _    ByVal dwCreationFlags As Long, _    ByVal lpEnvironment As Long, _    ByVal lpCurrentDirectory As Long, _    ByVal lpStartupInfo As Long, _    ByVal lpProcessInformation As String) As Long Public Function MemStringToLong(StringIn As String) As Long On Error Resume Next Dim hWorkVal As String '      ' Convert the String back to Long Integer. ' Converting back to Big Endian format. Dim i As Long For i = 4 To 1 Step -1 hWorkVal = hWorkVal & Hex(AscB(MidB(StringIn, i, 1))) Next i       ' ' Return Long Integer value. MemStringToLong = CLng(&quot;&H&quot; & hWorkVal) End Function

Public Sub getPROCESS_INFORMATION(ByVal sPROCESS_INFORMATION As String, _                  ByRef hProcess As Long, ByRef hThread As Long, _                   ByRef dwProcessId As Long, ByRef dwThreadId As Long) '   ' Convert memory-formatted String back to Long Integer. hProcess = MemStringToLong(MidB(sPROCESS_INFORMATION, 1, 4)) hThread = MemStringToLong(MidB(sPROCESS_INFORMATION, 5, 4)) dwProcessId = MemStringToLong(MidB(sPROCESS_INFORMATION, 9, 4)) dwThreadId = MemStringToLong(MidB(sPROCESS_INFORMATION, 13, 4)) End Sub

Public Function LongToMemoryString(ByVal lInputValue As Long) As String

Dim hWorkVal As String Dim n As Long Dim i As Long '   ' Convert to HEX value. hWorkVal = Hex(lInputValue) '   ' Check to see if it is not zero. If hWorkVal <> &quot;0&quot; Then '       ' Convert to memory storage format (Little Endian). ' For example, 0000A411 would convert to 11A40000. '       ' Place leading zeros in 8 character sequence to         ' maintain consistent character count n = Len(hWorkVal) If n < 8 Then hWorkVal = String(8 - n, &quot;0&quot;) & hWorkVal End If        ' ' Use ChrB to rebuild Bytes. For i = 7 To 1 Step -2 LongToMemoryString = LongToMemoryString & _ ChrB(CInt(&quot;&H&quot; & Mid(hWorkVal, i, 2))) Next i    Else ' Just return zeros. ' Use ChrB to build Bytes. LongToMemoryString = ChrB(CInt(&quot;&H00&quot;)) LongToMemoryString = LongToMemoryString & ChrB(CInt(&quot;&H00&quot;)) LongToMemoryString = LongToMemoryString & ChrB(CInt(&quot;&H00&quot;)) LongToMemoryString = LongToMemoryString & ChrB(CInt(&quot;&H00&quot;)) End If End Function Public Function PROCESS_INFORMATION(hProcess As Long, hThread As Long, _                    dwProcessId As Long, dwThreadId As Long) As String '   ' Convert inbound Long Integers to a memory storage String format. PROCESS_INFORMATION = LongToMemoryString(hProcess) & _ LongToMemoryString(hThread) & _ LongToMemoryString(dwProcessId) & _ LongToMemoryString(dwThreadId) End Function  Add a CommandButton, two TextBox, and two Label controls to Form1. Do not be concerned with the placement of the controls.  Paste the following code into Form1: Option Explicit Private Sub Command1_Click Dim lRet As Long Dim sPROCESS_INFORMATION As String Dim hProcess As Long Dim hThread As Long Dim dwProcessId As Long Dim dwThreadId As Long '   ' Initialize PROCESS_INFORMATION memory string. ' Convert initial Rect values to String to pass into CreateProcess API. sPROCESS_INFORMATION = PROCESS_INFORMATION(0, 0, _       0, 0) '   ' Call CreateProcess. lRet = CreateProcess(CStr(Text1.Text), CStr(Text2.Text), _       0, 0, 0, 0, 0, 0, 0, sPROCESS_INFORMATION) '   'convert string back to long integer getPROCESS_INFORMATION sPROCESS_INFORMATION, hProcess, hThread, _ dwProcessId, dwThreadId '   'The handle to the process is returned in the sPROCESS_INFORMATION 'string when CreateProcess is called. This hProcess value can 'be passed to TerminateProcess. 'Uncomment the 3 lines below to terminate the process. 'MsgBox &quot;Click to terminate process&quot; 'Dim x As Long 'x = TerminateProcess(hProcess, 0) End Sub

Private Sub Form_Load Command1.Move 240, 240, 2895, 615 Label1.Move 240, 960, 1935, 255 Label2.Move 240, 1680, 1935, 255 Text1.Move 240, 1320, 1935, 255 Text2.Move 240, 2040, 1935, 255 Command1.Caption = &quot;Create Process&quot; Label1.Caption = &quot;Path to executable:&quot; Label2.Caption = &quot;Command line:&quot; Text1.Text = &quot;\windows\repllog.exe&quot; Text2.Text = &quot;/remote&quot; End Sub  Run the application on the remote device. Disconnect the device from the desktop.</li> Click Command1, and note that the ActiveSync program starts.</li></ol>

<div class="references_section">