Results 1 to 8 of 8

Thread: CreateThread / CreateProcess examples?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    4

    Question CreateThread / CreateProcess examples?

    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

  2. #2
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534

    Smile

    hi,

    Please check it in your API text Viewer, which comes along with VB. Or else check it in www.google.com or msdn.com

    Pradeep
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

  3. #3
    Fanatic Member pradeepkrao's Avatar
    Join Date
    Sep 2001
    Location
    New Jersey
    Posts
    534
    put the following code in your module.bas file

    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)

    Sleep (10000)
    hThread = 0
    End Sub


    Put the following code in your form

    Private Sub Command1_Click()
    hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID)
    MsgBox "thread created"
    CloseHandle hThread

    End Sub
    Private Sub Form_Unload(Cancel As Integer)
    'If the thread is still running, close it
    If hThread <> 0 Then TerminateThread hThread, 0
    End Sub



    Check this out....
    Pradeep
    Learn by others experience as you cannot live long to experience them all.
    www.freewebs.com/pradeepkrao

    LOOK AT MY GAMES AT MY WEB SITE.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    I've had a question for a while and it looks like a good time to ask.

    So I've created a separate thread in which I can run another app.

    How do I ensure the app starts in the thread I've created?

  5. #5
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    pradeepkrao:

    Have you tried your example out of the IDE.
    This subject been discussed at length previously
    and out:

    1. Only works in IDE or in an ActiveX.

  6. #6
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    pradeepkrao:

    Take a look at this example.

    Since your using MsgBox you may not be creating a 2nd thread.
    Attached Files Attached Files

  7. #7
    Addicted Member
    Join Date
    Mar 2001
    Location
    Greece
    Posts
    164

    same problem here...

    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?

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    4

    Smile

    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

    lpProcAttr.bInheritHandle = 0
    lpProcAttr.lpSecurityDescriptor = 0
    lpProcAttr.nLength = Len(lpProcAttr)

    lpThrdAttr.bInheritHandle = 0
    lpThrdAttr.lpSecurityDescriptor = 0
    lpThrdAttr.nLength = Len(lpThrdAttr)

    startInfo.cb = Len(startInfo)
    startInfo.dwFlags = STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW
    startInfo.wShowWindow = SW_SHOWNORMAL
    startInfo.lpDesktop = ""
    startInfo.cbReserved2 = 0
    startInfo.lpReserved = 0
    startInfo.lpTitle = ""
    startInfo.lpReserved2 = 0
    lSuccess = CreateProcess(sNull, "Q:\LOTSUITE\123R5W\PROGRAMS\123W.EXE", lpProcAttr, lpThrdAttr, _
    vbNull, CREATE_SEPARATE_WOW_VDM, ByVal 0&, sNull, startInfo, processInfo)
    If lSuccess = 0 Then
    MsgBox "failed"
    Else
    MsgBox "Launched"
    lRetVal = TerminateProcess(processInfo.hProcess, 0&)
    lRetVal = CloseHandle(processInfo.hThread)
    lRetVal = CloseHandle(processInfo.hProcess)
    MsgBox "teminated"
    End If

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width