Solved

Executing a program without using Shell or ExecCmd

Posted on 2000-04-18
7
404 Views
Last Modified: 2008-02-01
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
Comment
Question by:TWilbert
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Accepted Solution

by:
Foyal earned 100 total points
ID: 2728351
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
 
LVL 32

Expert Comment

by:bhess1
ID: 2728414
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
 

Author Comment

by:TWilbert
ID: 2728494
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 5

Expert Comment

by:KDivad
ID: 2728538
"\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
 

Author Comment

by:TWilbert
ID: 2728663
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
 
LVL 5

Expert Comment

by:KDivad
ID: 2729475
<<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
 
LVL 3

Expert Comment

by:Foyal
ID: 2732275
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

777 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