Solved

Having Problems Executing Command Using Shell Function

Posted on 2000-03-21
22
335 Views
Last Modified: 2012-05-04
Hi,
I am trying to Execute a Command from VB using Shell Function.

Its like
'Sorts the data in the file.
FName = "D:\Rpsort102\Rpsort.com /+1:11 /D H:\X.txt H:\X_sort.txt"

Shell(FName,3)

This Code works if I create Project with only this piece of code .


But When i use the same code in my project is doesn't work.

I have tried using ShellExecute, ShellExecuteEx,ShellExecuteInfo API but none of them run a Command from VB(In the Sense they don't Execute commands)

Please suggest me a way to solve my problem.


Thanks,
Sriram
0
Comment
Question by:kishoresr
  • 6
  • 4
  • 4
  • +4
22 Comments
 
LVL 3

Expert Comment

by:jjmartin
ID: 2640484
Can you show us the section of code in your project where this fails?  If this works in a stand alone project, and not in another, it stands to reason that it's not this piece of code that isn't working, but something else within your project.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2640602
FYI, if you will not realize what the problem is, then you may try to use the WshIShell.Run or .Execute(References:Windows Scripting Host)
command
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2640971
If you are accessing the sorted file after the call to shell, you need to wait for the shelled app to finish before continuing your code.

"Q129796 - HOWTO: 32-Bit App Can Determine When a Shelled Process Ends"
http://support.microsoft.com/support/kb/articles/Q129/7/96.asp

And:
"Q161303 - HOWTO: Close a Shelled Process When Finished Under Windows"
http://support.microsoft.com/support/kb/articles/Q161/3/03.ASP
0
 

Author Comment

by:kishoresr
ID: 2641113
Let me explain what is happening.
When I use a Stand Alone Project with only this piece of code.
~~~~~~~~~~~~~
Private sub cmdSort_Click()
Shell(FName,3)
End sub
~~~~~~~~~~~~
Opens the MS-Dos window and stays there till the command is executed.
Since I can't control the Shell command once it is invoked ..the command Executes even after the project is closed.

But in my second case when I use the same code..it opens the MS-DOS window and Quickly closes the MS-Dos window(Starts and Ends Immediately)..I am not able to figure it out what is causing it...

My main problem is I need to run this Command from VB.
The sort file is a ".com" file.
Please let me know any other way to do this thing.

Thanks
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2641624
Try calling:

h = Shell(Environ("COMSPEC") & " " & fName, 3)

Or try redirecting the output to read the error messages:

h = Shell(fName & " >c:\redir.log", 3)  
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2641743
What you need is a Shell and Wait. The VB Shell command when you execute it, runs the program and then immediately continues along its way.. A Shell and Wait will execute the other program.. and then stop and wait until it is done.. <smile>.

As AngelIII comments, there is a Shell and Wait Function in the Windows Scripting Host.. to use it.. add a reference to your project (VB Menu.. Project.. References) and click on the Windows Scripting Runtime (SCRRUN.DLL). Having done that.. add the following code..

<----- Code Begin ----->

Option Explicit

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

Private Sub Form_Load()

    Dim strCommand as String
    strCommand = "c:\Windows\Calc.Exe"

    MsgBox ("About to run " & strCommand)

    Call xShellAndWait (strCommand, True)  
    MsgBox (strCommand & " COMPLETED!!!")

End Sub


Private Sub xShellAndWait _
(Byval strCommand as String, _
ByVal booWaitToComplete as Boolean)

    Dim oShl As New IWshShell_Class
    Call oShl.Run(strCommand, SW_SHOWNORMAL, booWaitToComplete)
   
End Sub

0
 
LVL 1

Accepted Solution

by:
Argonaut earned 50 total points
ID: 2642092
Shell is not a very reliable method of launching another program.  Use this Win32api call:

Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long


Your code will look something like:

Dim RetVal as Long
Dim sinfo As STARTUPINFO
Dim pinfo As PROCESS_INFORMATION
   
    sinfo.cb = Len(sinfo)
    sinfo.lpReserved = vbNullString
    sinfo.lpDesktop = vbNullString
    sinfo.lpTitle = vbNullString
    sinfo.dwFlags = 0

RetVal = CreateProcessBynum(App.Path & "OtherProgram.exe", , 0, 0, True, NORMAL_PRIORITY_CLASS, ByVal 0&, App.Path, sinfo, pinfo)
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2642170
Argonaut Writes: "Shell is not a very reliable method of launching another program."

HUH?.. Argonaut, which version of VB are you using.. the dart board set?.. PLEASE DOCUMENT YOUR STATEMENT.. <sheesh>.

Frankly.. the ANSWER(?) you provide only does exactly what the questioner is doing.. it launchs the external program.. and then goes merrily upon its way. As I interpret the question, the questioner needs to Shell to the program and then WAIT for it to complete.. before processing continues. With that in mind, you should have added a WaitForObject API to your example.

Argonaut.. As you are obviously new here, I won't blast you for locking down this question. Unless your answewr is absolutely the best and only approach.. please hit the comment button rather than the answer.. give the questioner the benefit of other experts opinions too.. and let the questioner choose which answer best satisfies his/her means.. <smile>.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2642546
Ditto.
The link I provided earlier
"Q129796 - HOWTO: 32-Bit App Can Determine When a Shelled Process Ends"
http://support.microsoft.com/support/kb/articles/Q129/7/96.asp
uses CreateProcess.
0
 
LVL 20

Expert Comment

by:hes
ID: 2642653
Agree with wsh2, only put it as an answer if you are 99+ % sure it is the complete answer. (LOL agreeing with wsh2 ;) ) Eric did provide a correct answer with his link.
0
 
LVL 20

Expert Comment

by:hes
ID: 2642673
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:kishoresr
ID: 2644857
Hi All,
Thanks a lot for your suggestions one more quick question.
I was reading the Suggestion by Erick37
~~~~~~
"Q129796 - HOWTO: 32-Bit App Can Determine When a Shelled Process Ends"
http://support.microsoft.com/support/kb/articles/Q129/7/96.asp

~~~~~~
This works ...and has all the things (APIs) but this Invokes the application and the MS-DOS window comes up.
Is there any way to hide the DOS-window.
I saw the Parameters  and couldn't find any thing which hides the application.

Please let me know you valuable suggestions.

Thanks,
0
 
LVL 20

Expert Comment

by:hes
ID: 2645353
In the declare add in :

Private Const SW_SHOWMINIMIZED = 2
Private Const STARTF_USESHOWWINDOW = &H1

Then right after start.cb = Len(start)
add:

start.dwFlags = STARTF_USESHOWWINDOW
start.wShowWindow = SW_SHOWMINIMIZED
0
 

Author Comment

by:kishoresr
ID: 2645553
Hi All,
Hes suggestion was Great ..I had to make only one change(i.e)
Private Const SW_SHOWMINIMIZED = 2 (0)which hides the DOS-window and it works..
I will try to implement it in the Orginal Project and see how it works..
Let you know..

Thanks
0
 

Author Comment

by:kishoresr
ID: 2646210
Hi Eric37 & Hes
I am still getting the same problem..it working on the test project and not working on the working project.
I am getting a return code of 51 which is a system error and the Desc for this
is
51
The remote computer is not available.  ERROR_REM_NOT_LIST
I am not connecting to any remote computer..I have no idea why it is giving me that error.

Please give me some Input.

Thanks
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2646368
Try running another program other than rpsort on d:\ and see if the same problem exists.

This will narrow down the problem.

What version of Windows?
0
 

Author Comment

by:kishoresr
ID: 2646458
Hi,
I am running both my projects on Windows NT Service Pack 5.0
I tested my Application in another PC's it works fine .
I don't know its making me Frustrate.
I works on other PC's not on mine.
it works on Test Project of mine but Not the Working project.

Please suggest something.
This is horrible.

Thanks,
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2646508
If you don't want to use APIs to Shell and Wait.. see my comment above.. (paid for by the Campaign to reerect the looney-bin resident)

As to the Dos Window problem.. place your program in a BAT file.. and pass the parameters you need for the program to it. Using Windows Explorer.. change the BAT file properties to display however you like.. <smile>
0
 
LVL 1

Expert Comment

by:Argonaut
ID: 2646691
Use the example I suggested above then in a while loop call the GetObject api to see when your App is finished.

While GetObject(YourApp.hWnd) <> 0
   DoEvents
Loop
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2646809
Argonaut:
Your solution will still leave the application open for processing. IF the program requires the completion of this Shell to continue, the last comment you proferred may not be the best solution. Keeping in line with your viable solution, you may want to add a comment about using a WaitForSingleObject API to make the VB program pause until his DOS program is finished.. <smile>.
0
 
LVL 1

Expert Comment

by:Argonaut
ID: 2650088


Public Sub ExecuteAndWait(cmdline As String)
    Dim NameOfProc As PROCESS_INFORMATION
    Dim NameStart As STARTUPINFO
    Dim X As Long
    NameStart.cb = Len(NameStart)
    X = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
    X = WaitForSingleObject(NameOfProc.hProcess, INFINITE)
    X = CloseHandle(NameOfProc.hProcess)
End Sub

0
 

Author Comment

by:kishoresr
ID: 2665650
Hi All,
I am back..my Shell thing..I finally figured out what my problem is..as I mentioned to you I am getting a Exit Code 51(Which means that Remote Computer Not Found)..I know what's causing that but don't know Why and How to fix it..
here are the Steps I am doing in this method..
I wrote a method which reads a text file from a CD .
Then splits the Text file into two ".txt" files.
Once it splits into two files I take one of the files and with the Help of Shell Command I Execute a command to sort my file.
I already have ".txt file " to be sorted
But if I directly execute my command without reading from the CD-Rom using (Common dialog Control) it works fine but if I use the Common Dialog Control..Intialise the Dir,Filter and Open it ..it fails...

Please give me your input on this.
Is there any limitations to Common dialog..what is the connection between Common Dialog and Shell.
~~~~~~~~
Code
~~~~~~~~
'Initiates the Directory and the Drive
    cDialog.InitDir = CDFileDir
   'Allows to View only the text files
    cDialog.Filter = "Text Files(*.txt)"
   'Opens the Dialog Box
    cDialog.ShowOpen
    ~~~~Failing right here..I mean if I comment it out it works fine
    '
    'Calls the Procedure to wait for 5 Min
      LngPTime = 30
    '
      Call Wait(LngPTime)
'
   '
   'strGFileName = cDialog.FileName
   'Checks for the FileName selected
   'BolFileName = CheckFile(strGFileName)
   '
   'If BolFileName Then
           '
           '
           BolSplitFile = True
           'Calls the Process to do the split the file into Patient and NDC
           'BolSplitFile = DataLoad.Load(strGFileName)
           '
           If BolSplitFile Then
               'Set the Value for the BolSplit file
                BolSplitFile = False
               'Calls the Procedure to Sort the NDC File
               'Dblret = ExecCmd(RPSortDir)
               Dblret = ExecCmd(RPSortDir)
               '
              MsgBox "Process Finished, Exit Code " & Dblret
               
0

Featured Post

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

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

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