Microsoft KB Archive/265793

From BetaArchive Wiki
Knowledge Base


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.

MORE INFORMATION

Step-by-Step Example

The following sample code starts ActiveSync on the device.

  1. Start a new Windows CE project in eMbedded Visual Basic. Form1 is created by default.
  2. On the Project menu, choose Add Module to add a new module to the project.
  3. Paste the following code into Module1:

    Option Explicit
    Public Declare Function TerminateProcess Lib "Coredll" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Public Declare Function CreateProcess Lib "coredll.DLL" _
        Alias "CreateProcessW" _
        (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("&H" & 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 <> "0" 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, "0") & hWorkVal 
            End If 
            '
            ' Use ChrB to rebuild Bytes.
            For i = 7 To 1 Step -2 
                LongToMemoryString = LongToMemoryString & _
                                     ChrB(CInt("&H" & Mid(hWorkVal, i, 2))) 
            Next i 
               
        Else
            ' Just return zeros.
            ' Use ChrB to build Bytes.
            LongToMemoryString = ChrB(CInt("&H00"))
            LongToMemoryString = LongToMemoryString & ChrB(CInt("&H00"))
            LongToMemoryString = LongToMemoryString & ChrB(CInt("&H00"))
            LongToMemoryString = LongToMemoryString & ChrB(CInt("&H00"))
        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
                        
  4. Add a CommandButton, two TextBox, and two Label controls to Form1. Do not be concerned with the placement of the controls.
  5. 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 "Click to terminate process"
        '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 = "Create Process"
        Label1.Caption = "Path to executable:"
        Label2.Caption = "Command line:"
        Text1.Text = "\windows\repllog.exe"
        Text2.Text = "/remote"
    End Sub
                        
  6. Run the application on the remote device.
  7. Disconnect the device from the desktop.
  8. Click Command1, and note that the ActiveSync program starts.


REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

241530 How To Call an API That Uses Structures from VBCE6


222527 How To Launch ActiveSync from a Visual Basic CE 6.0 Application



Additional query words: evb shell

Keywords: kbhowto kbapi KB265793