Link to home
Start Free TrialLog in
Avatar of weiroblpay
weiroblpayFlag for United States of America

asked on

URGENT! How to verify a process finished successfully using Shell or CreateProcess and close the resulting command window?

Hi all!

Here's my problem: I need to run a command line program from my VB6 application. In this case, it's sqlcmd.exe that I use to do some backup of a SQL Express 2005 database. I don't have any problem with the command line program working, I just have a problem with knowing if it succeeded or not and closing the resulting window.

The command line is:  
sqlcmd -SmyServer\SQLEXPRESS -UmyUser -PmyPWD -q"BACKUP DATABASE ARMS TO DISK = 'C:\Data\DB-04152006.dat'"

Here's my code that calls the process:

strcmd = "sqlcmd -SmyServer\SQLEXPRESS -UmyUser -PmyPWD -q"BACKUP DATABASE ARMS TO DISK = 'C:\Data\DB-04152006.dat'""

Shell strCmd, vbHide

This works great and the database gets backed up, but I can't find out how to verify that without physically checking it (after waiting long enough for the process to finish). I need to be able to give the user a prompt letting them know that it was backed up and where.

I've also tried this which also works just fine, but this leaves the DOS box open and I still don't know if it succeeded or not from within my VB6 application (just visually).

Call ExecCmd (strcmd)

Public Sub ExecCmd(stCmdline As String)

   Dim proc As PROCESS_INFORMATION
   Dim start As STARTUPINFO
   Dim ret As Long

   ' Initialize the STARTUPINFO structure:
   start.cb = Len(start)

   ' Start the shelled application:
   ret = CreateProcessA(0&, stCmdline, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

   ' Wait for the shelled application to finish:
   ret = WaitForSingleObject(proc.hProcess, INFINITE)
   ret = CloseHandle(proc.hProcess)
   
End Sub


What I need is a solution that runs the process and gives me some value that lets me know if it succeeded or not AND also closes the command window (DOS Box).

I've also tried ShellExecute, but can't get it to run the command at all.

Any help would be greatly appreciated!

Thanks,

Ron
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Try using the /C switch in your command line this should execute your string and then terminate the dos window automatically
Avatar of weiroblpay

ASKER

Thanks eql1044, but I found I can terminate the dos window OK, I just can't tell if it ran successfully or if it failed. I need my application to be able to tell if the command ran properly or if there were errors.
I am not familiar with the command line but you might be able to use createpipe to throw back the data to your application but this would only work if that command line actually tells you if it succeeded or failed. Does this happen?
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. The link and your comments provided me with the help I needed to get confirmation of what was going on. Even better, it works by showing the dos box, then going away and giving my app what I need to verify what happened.

Thanks for your help.