Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

Executing a program without using Shell or ExecCmd

I need to execute an executable for Connect:DIRECT called DIrect.exe from within my VB project.  I need a solution that doesn't use Shell or ExecCmd since I haven't been able to get either command to work on Windows NT.
0
TWilbert
Asked:
TWilbert
  • 2
  • 2
  • 2
  • +1
1 Solution
 
FoyalCommented:
recommended for NT:

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

Const ERROR_FILE_NOT_FOUND = 2&
Const ERROR_PATH_NOT_FOUND = 3&
Const ERROR_BAD_FORMAT = 11&

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

hwn: Long - A handle to a window. Sometimes it is necessary for a Windows application to show a message box before it has created its own main window. If this occurs, the window specified by this parameter will be used as the parent window of the message box. Under Visual Basic you would usually use the window handle of the active form for thie parameter...
(me.hwnd)

lpOperation: String - The string "Open" to open the lpFile document or "Print" to print it. This may be vbNullString to default to "Open" (or run).

lpFile: String - A program name or the name of a file to print or open using the associated program... (this should be the path and name, i.e., C:\Windows\Notepad.exe, http://www.microsoft.com, or mailto:soandso@hotmail.com,  etc.)

lpParameters: String - A string containing parameters to pass to the executable file if lpFile is an executable file. vbNullString if lpFile refers to a document file or if no parameters are used.

lpDirectory: String - The full path of the default directory to use. (This can be C:\ or the App.Path or anything else as far as I can tell)

nShowCmd Long - One of the constants declared above specifying how to show the launched program.

This function returns > 32 if successful,31 if no association exists, 0 if the system is out of memory or low on resources, ERROR_FILE_NOT_FOUND if the file does not exist, ERROR_PATH_NOT_FOUND if the path does not exist. ERROR_BAD_FORMAT if the executable format is invalid.

This function is what you are looking for.
See 'ya,
Foyal

0
 
Brendt HessSenior DBACommented:
See:

http://support.microsoft.com/support/kb/articles/Q129/7/97.asp?LN=EN-US&SD=gn&FR=0

It discusses using the CreateProcess API call to execute programs in Win32.
0
 
TWilbertAuthor Commented:
Foyal...This is the way I am trying to execute the command you sent me:

RetVal = ShellExecute(Me.hwnd, vbNullString, Mid$(DriveLtr, 1, 2) & "\programs DIRECT.exe", "-n172.18.24.57 -uCDunidial -pCDunidial <" & Mid$(DriveLtr, 1, 2) & "\working\" & Mid$(filename, 1, iFindExt - 1) & "C.txt", Mid$(DriveLtr, 1, 2) & "\", 0)
The RetVal is equal to 2 afterwards.  Even if I change \programs to \prog which doesn't exist I get the same return value.  Am I doing something wrong?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
KDivadCommented:
"\programs DIRECT.exe"

If "programs" is a folder, then I would guess that there needs to be a \ between "programs" and "DIRECT.exe". Otherwise, you are looking for an app called "programs DIRECT.exe".
0
 
TWilbertAuthor Commented:
Sorry, you are correct.  I replaced the \programs Direct.exe with \programs\direct.exe and it is executing the direct.exe program.  The only problem now is with the parameter I am passing.  It's having a problem with it but the DOS window flashes so fast I can't determine what it is?  The file does exist.  Any ideas?  Foyal, I will give you the points regardless I'm just looking for some closure.  Thanks
0
 
KDivadCommented:
<<Sorry, you are correct. ... Foyal, I will give you the points regardless I'm just looking for some closure.  Thanks>>
Hmm...


Change the properties of direct.exe so that it doesn't have "Close On Exit" checked and try it again. This time you'll see the error.
Also, you can try this right before you use the ShellExecute command to see exactly what parameter you are passing:
MsgBox "-n172.18.24.57 -uCDunidial -pCDunidial <" & Mid$(DriveLtr, 1, 2) & "\working\" & Mid$(filename, 1, iFindExt - 1) & "C.txt"
It's impossible to figure out without knowing the values of DriveLtr, filename and iFindExt, but it seems to me that you are constructing the file path (in part) by removing the extension (iFileExt) from another file path and adding the filename to it instead of removing the whole filename i.e. "C:\io.sys" -> " C:\io.C.txt".
0
 
FoyalCommented:
This might help:
Build your command before calling shellexecute.

Dim S As String
Dim T As Long

S = "This" & "\" & "That" & "\" & "TheOtherThing"

T = ShellExecute(me.hwnd,vbNullString,S,vbNullString,"C:\",SW_SHOWNORMAL)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now