Solved

Executing a program without using Shell or ExecCmd

Posted on 2000-04-18
7
383 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

744 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

13 Experts available now in Live!

Get 1:1 Help Now