Solved

Executing a program without using Shell or ExecCmd

Posted on 2000-04-18
7
392 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

910 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

20 Experts available now in Live!

Get 1:1 Help Now