Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Shell Calling BTEQ....

Status
Not open for further replies.
Oct 22, 2001
215
US
I am trying to run a windows program using Shell command.

The commands that work from command prompt is as follows:

Bteq <Script.scr> Log.txt

I tried the following code:
Shell &quot;Bteq <Script.scr> Log.txt&quot;

But only the Bteq program is open but I guess the parameter is not passed. The general command for this should be:
Bteq <ScriptName
I tried by reference or By Val using function but did not get much...
Any Suggestion?
TIA
 
I would suggest that you write a wrapper in c for your bteq jobs that takes two command line args, one for the script file to use, and another for the bteq log output filename.

This is a pretty simple program.

I have attempted to do this sort of thing exclusively within VB (using stdin/stdout read/writes direct from VB via system library calls), but this was a real pain to use/debug and not stable enough for my purposes.

I resorted to creating a wrapper in c and everything went fine.

Another thing you might consider (if you are perl friendly) is writing your wrapper in perl - this would be extremely simple code. Call the wrapper with &quot;perl btqwrap.pl infile.btq outfile.log&quot; from VB.

Regards,

TeraOz
 
here's a real good start towards all you need to know
about shell func in vb

Option Explicit

Public Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias _
&quot;ShellExecuteA&quot; (ByVal hwnd As Long, ByVal lpszOp As _
String, ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long

Public Declare Function GetDesktopWindow Lib &quot;user32&quot; () As Long

' hWnd = Window handle to a parent window.
' This window receives any message boxes that an application produces.
'
' lpszOp = Address of a null-terminated string that specifies the operation to perform.
' The following operation strings are valid:
' open, print, explore
' This parameter can be NULL. In that case, the function opens the file specified
' by lpFile.
'
' lpFile = Address of a null-terminated string that specifies the file to open
' or print or the folder to open or explore. The function can open an executable
' file or a document file. The function can print a document file.
'
' lpParameters = If the lpFile parameter specifies an executable file,
' lpParameters is an address to a null-terminated string that specifies
' the parameters to be passed to the application.
' If lpFile specifies a document file, lpParameters should be NULL.
'
' lpDirectory = Address of a null-terminated string that specifies
' the default directory.
'
' nShowCmd = If lpFile specifies an executable file, nShowCmd specifies
' how the application is to be shown when it is opened.
' This parameter can be one of the following values:

Const SW_HIDE = 0 ' Hides the window and activates another window.
Const SW_MAXIMIZE = 3 ' Maximizes the specified window.
Const SW_MINIMIZE = 6 ' Minimizes the specified window and activates the next top-level window in the z-order.
Const SW_RESTORE = 9 ' Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
Const SW_SHOW = 5 ' Activates the window and displays it in its current size and position.
Const SW_SHOWDEFAULT = 10 ' Sets the show state based on the SW_ flag specified in theSTARTUPINFO structure passed to theCreateProcess function by the program that started the application. An application should callShowWindow with this flag to set the initial show state of its main window.
Const SW_SHOWMAXIMIZED = 3 ' Activates the window and displays it as a maximized window.
Const SW_SHOWMINIMIZED = 2 ' Activates the window and displays it as a minimized window.
Const SW_SHOWMINNOACTIVE = 7 ' Displays the window as a minimized window. The active window remains active.
Const SW_SHOWNA = 8 ' Displays the window in its current state. The active window remains active.
Const SW_SHOWNOACTIVATE = 4 ' Displays a window in its most recent size and position. The active window remains active.
Const SW_SHOWNORMAL = 1 ' Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.

' If lpFile specifies a document file, nShowCmd should be zero.
' You can use this function to open or explore a shell folder.
' To open a folder, use either
'
' ShellExecute(handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);
' or
' ShellExecute(handle, &quot;open&quot;, path_to_folder, NULL, NULL, SW_SHOWNORMAL);
'
' To explore a folder, use the following call:
'
' ShellExecute(handle, &quot;explore&quot;, path_to_folder, NULL, NULL, SW_SHOWNORMAL);
'
' If lpOperation is NULL, the function opens the file specified by lpFile. If lpOperation is &quot;open&quot; or &quot;explore&quot;, the function will attempt to open or explore the folder.
'
' To obtain information about the application that is launched as a result of calling
'
' Returns a value greater than 32 if successful, or an error value that is less
' than or equal to 32 otherwise. The following table lists the error values.
' The return value is cast as an HINSTANCE for backward compatibility with 16-bit
' Microsoft® Windows® applications. It is not a true HINSTANCE, however.
' The only thing that can be done with the returned HINSTANCE is to cast it to an
' integer and compare it with the value 32 or one of the error codes below.

Const SE_ERR_FNF = 2 ' File not found
Const SE_ERR_PNF = 3 ' Path not found
Const SE_ERR_ACCESSDENIED = 5 ' Access denied
Const SE_ERR_OOM = 8 ' Out of memory
Const SE_ERR_DLLNOTFOUND = 32 ' DLL not found
Const SE_ERR_SHARE = 26 ' A sharing violation occurred
Const SE_ERR_ASSOCINCOMPLETE = 27 ' Incomplete or invalid file association
Const SE_ERR_DDETIMEOUT = 28 ' DDE Time out
Const SE_ERR_DDEFAIL = 29 ' DDE transaction failed
Const SE_ERR_DDEBUSY = 30 ' DDE busy
Const SE_ERR_NOASSOC = 31 ' No association for file extension
Const ERROR_BAD_FORMAT = 11& ' Invalid EXE file or error in EXE image
Const ERROR_FILE_NOT_FOUND = 2& ' The specified file was not found.
Const ERROR_PATH_NOT_FOUND = 3& ' The specified path was not found.
Const ERROR_BAD_EXE_FORMAT = 193& ' The .exe file is invalid (non-Win32® .exe or error in .exe image).


Public Function ShellExecLaunchFile(ByVal strPathFile As String, ByVal strOpenInPath As String, ByVal strArguments As String) As Long

Dim Scr_hDC As Long

'Get the Desktop handle
Scr_hDC = GetDesktopWindow()

'Launch File
ShellExecLaunchFile = ShellExecute(Scr_hDC, &quot;Open&quot;, strPathFile, &quot;&quot;, strOpenInPath, SW_SHOWNORMAL)

End Function


Public Function ShellExecLaunchErr(ByVal lngErrorNumber As Long, ByVal blnRaiseMsg As Boolean) As String

Dim msg As VbMsgBoxResult
Dim strErrorMessage As String

If lngErrorNumber < 33 Then
'There was an error
Select Case lngErrorNumber
Case SE_ERR_FNF
strErrorMessage = &quot;File not found&quot;
Case SE_ERR_PNF
strErrorMessage = &quot;Path not found&quot;
Case SE_ERR_ACCESSDENIED
strErrorMessage = &quot;Access denied&quot;
Case SE_ERR_OOM
strErrorMessage = &quot;Out of memory&quot;
Case SE_ERR_DLLNOTFOUND
strErrorMessage = &quot;DLL not found&quot;
Case SE_ERR_SHARE
strErrorMessage = &quot;A sharing violation occurred&quot;
Case SE_ERR_ASSOCINCOMPLETE
strErrorMessage = &quot;Incomplete or invalid file association&quot;
Case SE_ERR_DDETIMEOUT
strErrorMessage = &quot;DDE Time out&quot;
Case SE_ERR_DDEFAIL
strErrorMessage = &quot;DDE transaction failed&quot;
Case SE_ERR_DDEBUSY
strErrorMessage = &quot;DDE busy&quot;
Case SE_ERR_NOASSOC
strErrorMessage = &quot;No association for file extension&quot;
Case ERROR_BAD_FORMAT
strErrorMessage = &quot;Invalid EXE file or error in EXE image&quot;
Case ERROR_FILE_NOT_FOUND
strErrorMessage = &quot;The specified file was not found.&quot;
Case ERROR_PATH_NOT_FOUND
strErrorMessage = &quot;The specified path was not found.&quot;
Case ERROR_BAD_EXE_FORMAT
strErrorMessage = &quot;The .exe file is invalid (non-Win32® .exe or error in .exe image).&quot;
Case Else
strErrorMessage = &quot;Unknown error&quot;
End Select

'If the blnRaiseMsg = True then raise a MsgBox with error
If blnRaiseMsg = True Then msg = MsgBox(strErrorMessage, vbCritical, &quot;Error:&quot;)

'Return Error string
ShellExecLaunchErr = blnRaiseMsg

End If

End Function


' So the way to use all this is:
'
' Dim lngReturnNumber As Long
'
' lngReturnNumber = ShellExecLaunchFile(txtPathFile.Text, txtArguments.Text, txtStartPath.Text)
' If lngReturnNumber < 33 Then
' Call ShellExecLaunchErr(lngReturnNumber, True)
' Exit Sub
' End If
'
 
Kool....IS this the solution or a suggestion?? Did u just cut/paste it from the MSDN Library?
 

i wouldnt know if it is the solution without knowing
more about the task at hand

what it is is what i said it is

it came from my toolbox, vbguru.

 
This will do it. The API call is for a NT machine. If you don't want to wait for the shell command to finish, then you can delete the associated code. One word of caution, your call &quot;Bteq <Script.scr> Log.txt&quot; should have a path to ensure 'file not found' errors. I suggest using a &quot;Paths.txt&quot; file so you can read in the needed paths or use a relative path to 'app.path'. Both options will avoid having to 'hardcode' any paths in your code.

Hope it helps...


Option Explicit

Private Declare Function EnumProcesses Lib &quot;psapi.dll&quot; _
(ByRef lpidProcess As Long, ByVal cb As Long, _
ByRef cbNeeded As Long) As Long


Private Sub Runshell()

Dim lTaskId As Long
Dim sArg As String

'Sample App - sArg = &quot;D:\#Test#\Return.exe Arg1 Arg2&quot;
sArg = &quot;Bteq <Script.scr> Log.txt&quot;

'** Run App
lTaskId = Shell(sArg, vbNormalFocus)

'** Wait until App is done
Do While FindIdNT(lTaskId) = True
DoEvents
Loop

End Sub

Public Function FindIdNT(TaskID) As Boolean

Dim cb As Long
Dim cbNeeded As Long
Dim lRet As Long
Dim NumElements As Long
Dim i As Long
Dim ProcessIDs() As Long

'Get the array containing the process id's for each process object
cb = 8
cbNeeded = 96

'if cbNeeded == cb upon return, allocate a larger array
'and try again until cbNeeded is smaller than cb.
Do While cb <= cbNeeded
cb = cb * 2
ReDim ProcessIDs(cb / 4) As Long
lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
Loop

'calculate how many process IDs were returned
NumElements = cbNeeded / 4

For i = 1 To NumElements
If ProcessIDs(i) = TaskID Then
FindIdNT = True
Exit Function
End If
Next

FindIdNT = False

End Function


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top