' A Poor Man's Inter-Process Communication.
'
' Jon Vote, Idioma Software Inc.
'
' 02/2002
'
' www.idioma-software.com
'
' Here is a quick and easy way for two processes to communicate. It is not the
' best way, but it works and I've used this method to good use when there was no
' other way to do it. This technique involves using an .ini file as the basis
' of common communication between the two jobs.
'
' (See also - Interprocess Communication using Sockets)
'
' To run this demo, create the project as listed below. Then compile and run two
' instances of the program. You will be prompted to enter a ProcessID for each of the
' two jobs. For example, use 001 for the first job, and 002 for the other job.
'
' 1) Create a new project. Form1 will be created by default.
' 2) Add a ListBox and Command Button to the Form.
' 3) Paste the following code into the declarations section of Form1:
' --- Begin code for Form1
' A Poor Man's Inter-Process Communication.
'
' Jon Vote, Idioma Software Inc.
'
' 02/2002
'
' www.idioma-software.com
Option Explicit
'This file will be created in the Windows directory
'You can create an .ini file in another place if you
'fully qualify the path name.
Private Const INI_FILE = "Skycoder.ini"
Private Const COMMON_SECTION = "Common"
Private Const PROCESS_KEY = "ProcessID"
Private Const MESSAGE_KEY = "Message"
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Long
Private m_strThisProcessID As String
Private m_strOtherProcessID As String
Private Sub Command1_Click()
Dim strSendThis As String
Dim lngRC As Long
strSendThis = InputBox$("Send?", "Send", "Hello world")
Call PutIni(INI_FILE, m_strThisProcessID, MESSAGE_KEY, strSendThis)
End Sub
Private Sub Form_Load()
Dim strProcessID As String
'Get a default process id... if one in the .ini file we will
'assume this is the other process, else we will assume we are the
'first process.
'Check .ini file for a process id entry
strProcessID = GetIni(INI_FILE, COMMON_SECTION, PROCESS_KEY)
'If none found, default to 001
If strProcessID = "" Then
m_strThisProcessID = "001"
m_strOtherProcessID = "002"
Else 'here we assume we are the second process
m_strOtherProcessID = strProcessID
m_strThisProcessID = Format$(Val(strProcessID) + 1, "000")
End If
m_strThisProcessID = Trim$(InputBox$("ProcessID for this job", "Get ProcessID", m_strThisProcessID))
m_strOtherProcessID = Trim$(InputBox$("ProcessID for the other job", "Get ProcessID", m_strOtherProcessID))
If m_strThisProcessID = m_strOtherProcessID Then
MsgBox "You must use unique names for each process id"
Unload Me
ElseIf m_strThisProcessID = "" _
Or m_strOtherProcessID = "" Then
MsgBox "You have to enter something for each process id"
Unload Me
End If
Call PutIni(INI_FILE, COMMON_SECTION, PROCESS_KEY, m_strThisProcessID)
Me.Caption = "Process " & m_strThisProcessID & " taking with " & m_strOtherProcessID
Command1.Caption = "&Send"
Me.Show
ProcessLoop
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call PutIni(INI_FILE, COMMON_SECTION, PROCESS_KEY, "")
End
End Sub
Private Sub ProcessLoop()
Dim strMessage As String
Do
strMessage = GetIni(INI_FILE, m_strOtherProcessID, MESSAGE_KEY)
If strMessage <> "" Then
Call PutIni(INI_FILE, m_strOtherProcessID, MESSAGE_KEY, "")
Me.SetFocus
List1.AddItem "Message from " & m_strOtherProcessID & ": " & strMessage
End If
DoEvents
Loop
End Sub
'Returns entry from .ini file:''(strIniFile)''[Section]'Key=Entry'
Function GetIni(ByVal strIniFile As String, ByVal strSection As String, ByVal strKey As String) As String
Dim lngRC As Long
Dim strEntry As String
strEntry = Space$(256)
lngRC = GetPrivateProfileString(strSection, strKey, "", strEntry, Len(strEntry), strIniFile)
strEntry = Left$(strEntry, lngRC)
GetIni = strEntry
End Function
'Puts an entry to an .ini file:''(strIniFile)''[Section]'Key=Entry'
Sub PutIni(ByVal strIniFile As String, ByVal strSection As String, ByVal strKey As String, ByVal strEntry As String)
' puts an entry to the .ini file
Dim lngRC As Long
lngRC = WritePrivateProfileString(strSection, strKey, strEntry, strIniFile)
End Sub
' --- End code for Form1 ---
' 4) You will need to compile and run two instances of the program.