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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing simultanaeous execution 1

Status
Not open for further replies.

robinsql

Programmer
Aug 2, 2002
236
IE
Hi,

I need to run two executables (which are virus checkers) on login to a machine.
My problem is that if one is running, the other one fails, so I need to tell one to wait until the other has finished and then execute the second one.
Is there some kind of script I can write in VB to do this? Maybe it would check the processes and ensure that if one is running the other will wait, until that process does not exist.
I have never done anything like this and would appreciate any ideas.
Thanks in advance,
Robin
 
Hi,

Yeh you'll need to use the API CreateProcess and WaitForSingleObject

Here is the code for the module


'Win32 (Kernel32.lib) constants, datatypes and funtions
'(used to create and execute processes).
Public Const K32_INFINITE = &HFFFF 'Infinite Wait period.
Public Const K32_NORMAL_PRIORITY_CLASS = &HB0 'Process priority.
Public Const K32_TRUE = 1 'Kernal value for TRUE flag.
Public Const K32_LOGON_NETCREDENTIALS_ONLY = &H2&
Public Const K32_LOGON_WITH_PROFILE = &H1&


Type STARTUPINFO 'Process start data.
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 Long
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Type PROCESS_INFORMATION 'Process information data.
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type

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

Public Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long _
) As Long

Public Declare Function WaitForSingleObject Lib "kernel32" ( _
ByVal hHandle As Long, _
ByVal dwMilliseconds As Long _
) As Long

********************

And now the code for the execution of the command

Private Function RunShellAsProcess(strShellCmd As String) As Integer

Dim doProc As PROCESS_INFORMATION
Dim doStart As STARTUPINFO
Dim lResult As Long

On Error GoTo ErrH

'Execute the file and wait for execution to complete.
doStart.cb = Len(doStart)
lResult = CreateProcess(vbNullString, strShellCmd, ByVal 0&, ByVal 0&, K32_TRUE, K32_NORMAL_PRIORITY_CLASS, 0&, vbNullString, doStart, doProc)

'Shell (sCmdsFilePath)
'moTextStream.Write (CStr(Now) & ": Run batch file result = " & lResult & vbCrLf)
If lResult = K32_TRUE Then
lResult = CloseHandle(doProc.hThread)
lResult = WaitForSingleObject(doProc.hProcess, K32_INFINITE)
lResult = CloseHandle(doProc.hProcess)
End If

RunShellAsProcess = 0

Exit Function

ErrH:

RunShellAsProcess = -1

End Function

******************

Just call the function RunShellAsProcess passing a shell command to run you virus scan/checker.


Hope this helps,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top