CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: CreateProcess

  1. #1
    Join Date
    May 2001
    Location
    Kansas City, Missouri
    Posts
    45

    CreateProcess

    I'm trying to use the CreateProcess API to launch an application (because I need to do more with it than just launch the application). The program I'm launching has command line parameters which I am also trying to pass. My problem is that I can't seem to get the API to work when I pass the command line switches. It works fine when I pass just the app name. I've tried a number of different things(comma separated, space separated, with the app name, without etc.) but have not been able to stumble upon the one that works.

    I would really appreciate seeing a code sample of the CreateProcess API that uses command line parameters.

    Thank you


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: CreateProcess

    Here is an exampple how to use CreateProcess API

    'This program needs a common dialog box, named CDBox
    ' (to add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
    ' and select Microsoft Common Dialog control)
    Const INFINITE = &HFFFF
    Const STARTF_USESHOWWINDOW = &H1
    private Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
    End Enum
    private Type PROCESS_INFORMATION
    hProcess as Long
    hThread as Long
    dwProcessId as Long
    dwThreadId as Long
    End Type
    private 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 Byte
    hStdInput as Long
    hStdOutput as Long
    hStdError as Long
    End Type
    private Type SECURITY_ATTRIBUTES
    nLength as Long
    lpSecurityDescriptor as Long
    bInheritHandle as Long
    End Type
    private Enum enPriority_Class
    NORMAL_PRIORITY_CLASS = &H20
    IDLE_PRIORITY_CLASS = &H40
    HIGH_PRIORITY_CLASS = &H80
    End Enum
    private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (byval lpApplicationName as string, byval lpCommandLine as string, lpProcessAttributes as SECURITY_ATTRIBUTES, lpThreadAttributes as SECURITY_ATTRIBUTES, byval bInheritHandles as Long, byval dwCreationFlags as Long, lpEnvironment as Any, byval lpCurrentDriectory as string, lpStartupInfo as STARTUPINFO, lpProcessInformation as PROCESS_INFORMATION) as Long
    private Declare Function WaitForSingleObject Lib "kernel32" (byval hHandle as Long, byval dwMilliseconds as Long) as Long
    private Function SuperShell(byval App as string, byval WorkDir as string, dwMilliseconds as Long, byval start_size as enSW, byval Priority_Class as enPriority_Class) as Boolean
    Dim pclass as Long
    Dim sinfo as STARTUPINFO
    Dim pinfo as PROCESS_INFORMATION
    'Not used, but needed
    Dim sec1 as SECURITY_ATTRIBUTES
    Dim sec2 as SECURITY_ATTRIBUTES
    'set the structure size
    sec1.nLength = len(sec1)
    sec2.nLength = len(sec2)
    sinfo.cb = len(sinfo)
    'set the flags
    sinfo.dwFlags = STARTF_USESHOWWINDOW
    'set the window's startup position
    sinfo.wShowWindow = start_size
    'set the priority class
    pclass = Priority_Class
    'Start the program
    If CreateProcess(vbNullString, App, sec1, sec2, false, pclass, _
    0&, WorkDir, sinfo, pinfo) then
    'Wait
    WaitForSingleObject pinfo.hProcess, dwMilliseconds
    SuperShell = true
    else
    SuperShell = false
    End If
    End Function
    private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    'set the dialog's title
    CDBox.DialogTitle = "Choose an EXEC-File ..."
    'error when canceled
    CDBox.CancelError = true
    'set the dialog's filter
    CDBox.Filter = "EXEC-Files (*.exe)|*.exe|All files (*.*)|*.*"
    'Show the 'Open File'-dialog
    CDBox.ShowOpen
    'Execute the program
    SuperShell CDBox.filename, Left$(CDBox.filename, len(CDBox.filename) - len(CDBox.FileTitle)), 0, SW_NORMAL, HIGH_PRIORITY_CLASS
    End
    End Sub




    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    May 2001
    Location
    Kansas City, Missouri
    Posts
    45

    Re: CreateProcess

    Thank you for the example, but that's not quite what I'm looking for.

    As I said before, when I just pass the app name, it works fine. It's when I try to pass an app name and command line parameters that I get stuck. For example if I pass something like "c:\program.exe" in the lpApplicatioName parameter and something like "param1 param2" in the lpCommandLine parameter, nothing happens. I've tried just about every variation that seams resonable given the API documentation. So far, nothing works.


  4. #4
    Join Date
    Jul 2001
    Location
    USA
    Posts
    8

    Re: CreateProcess

    VERY HELPFUL piece of code. This takes care one of the posts I had concerning the SetCurrentDirectory problem in Win NT. By using this function, you can set the working directory of the COM object.
    Thanks for this submission. It works beautifully!


    Ron D.
    This request or reply is sponsered by:
    Coffee -- the breakfast of champions!

  5. #5
    Join Date
    Sep 2001
    Posts
    49

    Re: CreateProcess

    This should help what you are looking for. I use it and works great.
    http://support.microsoft.com/support...n_SRCH&SPR=VBB


  6. #6
    Join Date
    Sep 2001
    Posts
    49

    Re: CreateProcess

    Forgot to mention. Just add parameters where the line has C:\notepad.exe" Of course I pass a variable instead of hard coded string which is build up with parameters.

    Otherwise is there something wrong with VB Shell function?


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

Featured