Solved

Executing a program without using Shell or ExecCmd

Posted on 2000-04-18
7
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Brendt Hess
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

717 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