Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Running other Programs using VB

Options
  • 06-02-2002 2:44pm
    #1
    Closed Accounts Posts: 10


    I am creating a GUI and I want to run a dos prompt C executable program. I want to enter the information that the program requires into text boxes on a Visual Basic 6 form and then this information will be sent to the C exe program. Can this be done? and if so how is it done and what is the code. If you can't understand this reply back and we will put up screen shots


Comments

  • Registered Users Posts: 2,781 ✭✭✭amen


    use the Shell function in VB ie
    Dim retVal
    retVal = Shell("c:\windows\notepad.exe c:\boot.int")

    will start notepad and open the boot.ini file if it exists.

    retVal returns the unique process id for the application you have called.

    if you have probs post your code


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    It should be noted that shell will spawn an asynchronous process. In other words, it launches it, and after launching, the VB program will continue running.

    Therefore, this approach is not suitable for launching a seperate application (command-line or otherwise), waiting till it completes, and then continuing.

    If this is what you want to do, you need to resort to some API calls (IIRC) which I can probably dig up somewhere....

    jc


  • Registered Users Posts: 2,494 ✭✭✭kayos


    Yep I have to agree with bonkey on that one and I will go one further heres some code for you to mess around with.
    ' Declarations and such needed for the example:
    ' (Copy them to the (declarations) section of a module.)
    Public Type SHELLEXECUTEINFO
    	cbSize As Long
    	fMask As Long
    	hwnd As Long
    	lpVerb As String
    	lpFile As String
    	lpParameters As String
    	lpDirectory As String
    	nShow As Long
    	hInstApp As Long
    	lpIDList As Long
    	lpClass As String
    	hkeyClass As Long
    	dwHotKey As Long
    	hIcon As Long
    	hProcess As Long
    End Type
    Public Const SEE_MASK_NOCLOSEPROCESS = &H40
    Public Const SW_SHOWNORMAL = 1
    Public Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpExecInfo As _
    	SHELLEXECUTEINFO) As Long
    Public Const SE_ERR_FNF = 2
    Public Const SE_ERR_NOASSOC = 31
    Public Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal _
    	dwMilliseconds As Long) As Long
    Public Const INFINITE = &HFFFF
    Public Const WAIT_TIMEOUT = &H102
    
    ' *** Place the following code inside window Form1. ***
    Private Sub Command1_Click()
    	Dim sei As SHELLEXECUTEINFO  ' structure used by the function
    	Dim retval As Long  ' return value
    	
    	' Load the information needed to open C:\Docs\readme.txt
    	' into the structure.
    	With sei
    		' Size of the structure
    		.cbSize = Len(sei)
    		' Use the optional hProcess element of the structure.
    		.fMask = SEE_MASK_NOCLOSEPROCESS
    		' Handle to the window calling this function.
    		.hwnd = Form1.hWnd
    		' The action to perform: open the file.
    		.lpVerb = "open"
    		' The file to open.
    		.lpFile = "C:\Docs\readme.txt"
    		' No additional parameters are needed here.
    		.lpParameters = ""
    		' The default directory -- not really necessary in this case.
    		.lpDirectory = "C:\Docs\"
    		' Simply display the window.
    		.nShow = SW_SHOWNORMAL
    		' The other elements of the structure are either not used
    		' or will be set when the function returns.
    	End With
    	
    	' Open the file using its associated program.
    	retval = ShellExecuteEx(sei)
    	If retval = 0 Then
    		' The function failed, so report the error.  Err.LastDllError
    		' could also be used instead, if you wish.
    		Select Case sei.hInstApp
    		Case SE_ERR_FNF
    			Debug.Print "The file C:\Docs\readme.txt was not found."
    		Case SE_NOASSOC
    			Debug.Print "No program is associated with *.txt files."
    		Case Else
    			Debug.Print "An unexpected error occured."
    		End Select
    	Else
    		' Wait for the opened process to close before continuing.  Instead
    		' of waiting once for a time of INFINITE, this example repeatedly checks to see if the
    		' is still open.  This allows the DoEvents VB function to be called, preventing
    		' our program from appearing to lock up while it waits.
    		Do
    			DoEvents
    			retval = WaitForSingleObject(sei.hProcess, 0)
    		Loop While retval = WAIT_TIMEOUT
    		Debug.Print "Notepad (or whatever program was opened) has just closed."
    	End If		
    End Sub
    

    Use lpFile to specify your app with the full path and pass your params with lpParameters look up the api's on MSDN for all the options but this should set you up nicely.

    kayos


  • Registered Users Posts: 2,781 ✭✭✭amen


    I was aware of that but from the mail I did not think it mattered


Advertisement