Technical Articles
Process Synchronization with VBScript
In a few posts we discuss the possibility to use multiple VBScript processes. With the Run command you can start different processes asynchronously. But after the start, you have not really a chance to synchronize the processes. Maybe via a file interface. For the process synchronization and communication offers Windows the IPC interface (Interprocess Communication).
There are different technics to synchronize and to communicate between processes, like
- Events,
- Semaphores,
- FileMaps and
- the ClipBoard
- etc.
To use this possibilities I programmed a COM library which offers this technics. You find it here.
With COMIPC you can easily e.g. synchronize processes, look at the following example:
'-Begin-----------------------------------------------------------------
'-Directives----------------------------------------------------------
Option Explicit
'-Constants-----------------------------------------------------------
Const INFINITE = -1
'-Variables-----------------------------------------------------------
Dim oIPC, hEvent, wsh, extVBS, rc
'-Main----------------------------------------------------------------
Set oIPC = CreateObject("COMIPC")
If IsObject(oIPC) Then
hEvent = oIPC.EventCreate("TestEvent", 0, 0)
If hEvent Then
extVBS = "Script 2.vbs"
Set wsh = CreateObject("WScript.Shell")
If IsObject(wsh) Then
wsh.Run("wscript.exe " & extVBS)
Set wsh = Nothing
End If
rc = oIPC.EventWait("TestEvent", INFINITE)
rc = oIPC.EventClose(hEvent)
End If
Set oIPC = Nothing
End If
MsgBox "End"
'-End-------------------------------------------------------------------
We create an event, start a second script and wait until the event is set in the second script.
'-Begin-----------------------------------------------------------------
'-Directives----------------------------------------------------------
Option Explicit
'-Variables-----------------------------------------------------------
Dim oIPC, rc
'-Main----------------------------------------------------------------
Set oIPC = CreateObject("COMIPC")
If IsObject(oIPC) Then
MsgBox "Event"
rc = oIPC.EventSet("TestEvent")
Set oIPC = Nothing
End If
'-End-------------------------------------------------------------------
If the event is set, in the second script, the first script will finish.
So it is e.g. possible to start different SAP GUI Scripts and to set different synchronization points. Also you can communicate between different processes via FileMap – File mapping is the association of a file’s contents with a portion of the virtual address space of a process.
Enjoy it.
Hello Stefan,
I try to use this COM library (COMIPC) .
I copy this dll to c:\windows\system32 but when I run the script ther message:
Activex can´t create object 'COMIPC'
This DLL run on Windows 64 ?
Hello Evando,
thanks for your reply.
Your OS is Windows x64, so the 32-bit (x86) libraries are in the directory SysWOW64. But it is not necessary to copy the library in this system directory. Register the library with a x86 console like this:
Now you can use the library in x86 and x64 programs, but if you use VBA in a x64 Office application the code completion etc. don't work, use the workaround from here - post from Aug. 20, 2015.
Enjoy it and let us know your results.
Cheers
Stefan
Hello Stefan,
Thanks a lot for the help. I followed your instructions and it worked perfectly.
Evando