Solved

Running another VB program from VB

Posted on 1998-12-15
5
165 Views
Last Modified: 2010-05-03
I am building a system monitoring application. It looks at certain files to see if their date/time stamps are changing.  ( To see if they are locked up).  It also counts files in directories and checks the oldest time stamp and reports problems through alphanumeric paging. It also needs to run some maintenance on its data each day (Clearing some counters and logs).  Eventually it will be running some scripts to dial into individual modems to see if they are operational.

I want to run this maintenance against the database and the dial out scripts which will be written in VB code from within my application.  I want to pass paramaters to them and return results.  I think that I would rather not have them be functions or sub's within my application because I would like to have the ability to kick off other programs from my application at defined intervals, while passing paramaters.  I am pretty new to VB.  Should I create these as DLL's, ActiveX components or something else.

Please be specific and provide some example code.
0
Comment
Question by:louy
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:mark2150
ID: 1450239
Just build your maintenance programs as separate .EXE files and run them as you would any other task. This allows you to build components that can run stand-alone (and can be *tested* in that mode) and then just spawn them off like any other windows app.

M

0
 

Author Comment

by:louy
ID: 1450240
How do I pass paramaters and return results and how exactly do I run them from within my VB application.  Please provide some code.
0
 
LVL 12

Accepted Solution

by:
mark2150 earned 200 total points
ID: 1450241
You can pass params on the command line or you can use an external file. The file is the simplest and allows you to run each part separately.

If your program has menus and such, then you can use the SENDKEYS command to tell it what to do.

SHELL( yourprog & " " & paramlist, 1)

If you need to wait for the child to complete, then use ExecCmd instead of SHELL.

'
' These data type declarations are required to support the obscure windows
' function to spawn a child task and wait for it to finish before continuing
'
Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type
'
Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type
'
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
'
Private Declare Function CreateProcessA Lib "kernel32" ( _
    ByVal lpApplicationName As Long, _
    ByVal lpCommandLine As String, _
    ByVal lpProcessAttributes As Long, _
    ByVal lpThreadAttributes As Long, _
    ByVal bInheritHandles As Long, _
    ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, _
    ByVal lpCurrentDirectory As Long, _
    lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long
'
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'
Const NORMAL_PRIORITY_CLASS = &H20&
Const INFINITE = -1&
'
' I don't really understand the above declares, but they do work so DON'T MESS WITH THEM!

Public Sub ExecCmd(cmdline$)
'
' This executes an external windows program and waits for it to complete
'
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
'
' Initialize the STARTUPINFO structure:
'
start.cb = Len(start)
'
' Start the shelled application
'
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
'
' Wait for the shelled application to finish:
'
holdhere:
    DoEvents
    ret& = WaitForSingleObject(proc.hProcess, 5000)
    If ret& <> 0 Then GoTo holdhere
'
ret& = CloseHandle(proc.hProcess)
End Sub

0
 

Author Comment

by:louy
ID: 1450242
I want to use the ExecCmd, I tried it and it works well.  To pass the data back and forth should I just write to a text file from each .exe or should I use a table in my Access DB or is there a cleaner way?
0
 
LVL 12

Expert Comment

by:mark2150
ID: 1450243
Straight ASCII text file is simplest and cleanest. This also allows you to component test by creating fake files with EDIT. You can also review outputs the same way.

M

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

24 Experts available now in Live!

Get 1:1 Help Now