Solved

how to use "createprocess" winapi from vb? (system programming)

Posted on 2000-04-22
11
324 Views
Last Modified: 2008-03-17
I have to run an application which takes command line arguments. I am unable to succeed in it, I guess it is not taking the command line arguments.
Funny thing is that I had run some other application which does not take command line arguments  successfully through createprocess.
So my problem is how to invoke an application which takes command line arguments through createprocess. Please help me to solve this problem.
0
Comment
Question by:rajiv_konx
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 1

Accepted Solution

by:
plasmatek earned 200 total points
Comment Utility
dunno if this helps but if it does accept comment as answer.

'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()
    '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

0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
why are you opposed to using the shell command?
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
Rather than APIs.. you can Shell very efficiently (with parameters) from the Windows Scripting Host.

1. Start a new Standard.Exe Project.
2. Add a reference (VB Menu -> Project -> References) to "Windows Scripting Host Object" (WSHOM.OCX).
3. Copy / Paste the following into your Form1 code window.
4. Press F5 to run.

Option Explicit

Private Sub Form_Load()

   Const SW_HIDE = 0
   Const SW_SHOWNORMAL = 1
   Const SW_SHOWMINIMIZED = 2
   Const SW_SHOWMAXIMIZED = 3
   Const SW_SHOWNOACTIVE = 4
   Const SW_SHOW = 5
   Const SW_MINIMIZE = 6
   Const SW_SHOWMINNOACTIVE = 7
   Const SW_SHOWNA = 8
   Const SW_RESTORE = 9

   Dim strCommand() As String
   ReDim strCommand(2)
   strCommand(0) = Chr(34) & "c:\windows\explorer.exe" & Chr(34)
   strCommand(1) = Chr(34) & "c:\windows" & Chr(34)   strCommand(0) = Join(strCommand, " ")
   
   Dim booWait2Finish As Boolean
   booWait2Finish = False
   
   Dim wshShell As New IWshShell_Class
   Call wshShell.Run(strCommand(0), SW_SHOWNORMAL, booWait2Finish)
   
End Sub

--------------------------------------
This obviously is more code than you need. Each value in strCommand is enclosed in double quotes to account for long file names. The booWait2Finish switch, determines whether the calling program should continue to run or wait for the external program to finish before continuing.
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
try this for createprocess:

Dim retval As Long
Dim sCmdline As String
Dim start As STARTUPINFO
Dim sa As SECURITY_ATTRIBUTES
Dim proc As PROCESS_INFORMATION

sa.nLength = Len(sa)
sa.bInheritHandle = True
sCmdline = "your command line string"

CreateProcessA(0&, scmdline, sa, sa, True, NORMAL_PRIORITY_CLASS, 0&, 0, start, proc)
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
oops, that last 0 should be 0& as well
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:rajiv_konx
Comment Utility
Hello AzraSound,
          I am sorry yaar. I have proceeded in the same way u have specified, but could not succeed. createprocess is returning 0 i.e failure. before, atleast it was returning 1 but not taking command line argument properly.
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
btw, what OS are you running?
0
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
you need to read these articles from MSDN.  As stated before, you may consider taking a different approach:

http://support.microsoft.com/support/kb/articles/Q173/0/85.ASP
http://support.microsoft.com/support/kb/articles/Q150/9/56.ASP
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
A small correction to my code above (cosmetic).. give it a try.. <smile>.

Rather than APIs.. you can Shell very efficiently (with parameters) from the Windows Scripting Host.

1. Start a new Standard.Exe Project.
2. Add a reference (VB Menu -> Project -> References) to "Windows Scripting Host Object" (WSHOM.OCX).
3. Copy / Paste the following into your Form1 code window.
4. Press F5 to run.

Option Explicit

Private Sub Form_Load()

   Const SW_SHOWNORMAL = 1

   Dim strCommand() As String
   ReDim strCommand(2)
   strCommand(0) = Chr(34) & "c:\windows\explorer.exe" & Chr(34)
   strCommand(1) = Chr(34) & "c:\windows" & Chr(34)
   strCommand(0) = Join(strCommand, " ")
   
   Dim booWait2Finish As Boolean
   booWait2Finish = False
   
   Dim wshShell As New IWshShell_Class
   Call wshShell.Run(strCommand(0), SW_SHOWNORMAL, booWait2Finish)
   
End Sub

0
 

Expert Comment

by:Jalen
Comment Utility
What you need is to use another API call in conjunction with CREATEPROCESS which allows parameters. Here is the complete Details for new API call:

STARTUPINFO

VB Declaration

Type STARTUPINFO    ‘  68 Bytes
        cb As Long
        lpReserved As Long
        lpDesktop As Long
        lpTitle As Long
        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

Description

This structure is used by the CreateProcess function to specify additional start-up parameters for a process. The GetStartupInfo function may be used to retrieve this information for a process.

Field      Type/Description
cb      Long—The size of this structure. Currently 68.
lpReserved      Long—Reserved. Set to zero. May also be defined as a string - set to vbNullString.
lpDesktop      Long—The address of a string buffer containing the name of a desktop, or the window station and desktop for the process. A leading backslash indicates that both are included. Zero to inherit the desktop and window station information for the current process. Zero on Windows 95. May also be defined as a string (see chapter 15).
lpTitle      Long—The address of a string buffer containing the title bar of a console window. Zero to use the name of the executable file. Zero for Windows applications or nonwindow console processes. May also be defined as a string (see chapter 15).
dwX, dwY      Long—If STARTF_USEPOSITION is set in the dwFlags field, this specifies the x and y offsets of the upper-left corner of the new window for the process in screen coordinates.
dwXSize, dwYSize      Long—If STARTF_USESIZE is set in the dwFlags field, this specifies the width and height of the window if a new window is created.
dwXCountChars, dwYCountChars      Long—If STARTF_USECOUNTCHARS is set in the dwFlags field, this specifies the screen buffer width and height measured in characters for a new console window.
dwFillAttribute      Long—If STARTF_USEFILLATTRIBUTE is set in the dwFlags field, this specifies the initial text and background colors for a new console window.
dwFlags      Long—Specifies which fields in this structure to use. May be any combination of the following:STARTF_USESHOWWINDOW: Use the wShowWindow field.STARTF_USEPOSITION: Use the dwX and dwY fields.STARTF_USESIZE: Use the dwXSize and dwYSize fields.STARTF_USECOUNTCHARS: Use the dwXCountChars and dwYCountChars fields.STARTF_USEFILLATTRIBUTE: Use the dwFillAttribute field.STARTF_FORCEONFEEDBACK: A process launch cursor is displayed during initialization of the process.STARTF_FORCEOFFFEEDBACK: The normal cursor is displayed during initialization of the process.STARTF_SCREENSAVER: Specifies that this is a screen saver application. This modifies the priority of the process so that it has a lower priority than other background processes when inactive.STARTF_USESTDHANDLES: Use the hStdInput, hStdOutput, and hStdError fields to specify the standard input, output, and error handles for this process. The CreateProcess function’s bInheritHandles field must also be set to TRUE.
wShowWindow      Integer—If STARTF_USESHOWWINDOW is set in the dwFlags field, this specifies the default value the first time the ShowWindow function is called with the SW_SHOWDEFAULT flag. May be any other SW_* constant.
cbReserved2      Integer—Reserved, set to zero.
lpReserved2      Long—Reserved, set to zero.
hStdInput, hStdOutput, hStdError      Long—If STARTF_USESTDHANDLES is set in the dwFlags field, these specify the standard input, output, and error handles for the process.

0
 

Author Comment

by:rajiv_konx
Comment Utility
sorry ,
this may not be useful for me
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now