You know how a lot of software these days use plugins, whether it's a graphics program, or a web browser, that allows additional functionality that was not present in the base program? Well I figured out how to do this in VB6.
Here's 2 templates, one for a plugin host, and one for a plugin. These are commented enough that you will be able to see how to use them. They are intended to be placed in modules (BAS files).
First template is modPluginHost, and should be used when compiling your main program's EXE file.
Second template is modPlugin, and should be used when compiling your plugin's EXE file.
Other than defining the elements of the InitStructType user defined type (which should match exactly between the host and the plugin), there's really nothing that needs to be edited in these templates.
Here's 2 templates, one for a plugin host, and one for a plugin. These are commented enough that you will be able to see how to use them. They are intended to be placed in modules (BAS files).
First template is modPluginHost, and should be used when compiling your main program's EXE file.
Code:
Private Const SYNCHRONIZE As Long = &H100000
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32.dll" () As Long
Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Type InitStructType
StructSize As Long
'Define the structure's elements here.
End Type
Public InitStruct As InitStructType
' This plugin initialization structure will contain
' all data that must be passed from the host to the
' plugin. For example, in an image processing
' plugin, this would contain image dimensions and a
' pointer to pixel data.
Public Function CallPlugin(ByVal PluginFileName As String) As Boolean
' PluginFileName is the EXE file of the plugin.
' It can be a relative or absolute path.
Dim ProcID As Long
Dim hProc As Long
On Error Resume Next
ProcID = Shell("""" & PluginFileName & """ " & CStr(GetCurrentProcessId) & " " & CStr(VarPtr(InitStruct)), vbNormalFocus)
If Err.Number Then Err.Clear
If ProcID = 0 Then Exit Function
hProc = OpenProcess(SYNCHRONIZE, 0, ProcID)
WaitForSingleObject hProc, -1
If hProc = 0 Then Exit Function
CloseHandle hProc
CallPlugin = True
End Function
Code:
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Long) As Long
Public Type InitStructType
StructSize As Long
'Define the structure's elements here.
End Type
Public InitStruct As InitStructType
' This plugin initialization structure will contain
' all data that must be passed from the host to the
' plugin. For example, in an image processing
' plugin, this would contain image dimensions and a
' pointer to pixel data.
Dim HostProcID As Long
Public Sub InitPlugin()
'This should be the very first thing called in your plugin.
'Preferably, call this in the Form_Load event of your plugin's main form.
Dim CmdLineArgs() As String
Dim HostInitStructAddr As Long
Dim hProc As Long
CmdLineArgs() = Split(Command, " ")
HostProcID = CmdLineArgs(0)
HostInitStructAddr = CmdLineArgs(1)
hProc = OpenProcess(GENERIC_READ, 0, HostProcID)
If hProc = 0 Then End
If ReadProcessMemory(hProc, HostInitStructAddr, InitStruct, 4) <> 0 Then
CloseHandle hProc
End
End If
If ReadProcessMemory(hProc, HostInitStructAddr, InitStruct, InitStruct.StructSize) <> 0 Then
CloseHandle hProc
End
End If
CloseHandle hProc
End Sub
Public Function ReadDataFromHost(ByVal LocalDataAddr As Long, ByVal HostDataAddr As Long, ByVal DataSize As Long) As Boolean
Dim hProc As Long
hProc = OpenProcess(GENERIC_READ, 0, HostProcID)
If hProc = 0 Then Exit Function
If ReadProcessMemory(hProc, HostDataAddr, ByVal LocalDataAddr, DataSize) <> 0 Then
CloseHandle hProc
Exit Function
End If
CloseHandle hProc
ReadDataFromHost = True
End Function
Public Function WriteDataToHost(ByVal LocalDataAddr As Long, ByVal HostDataAddr As Long, ByVal DataSize As Long) As Boolean
Dim hProc As Long
hProc = OpenProcess(GENERIC_WRITE, 0, HostProcID)
If hProc = 0 Then Exit Function
If WriteProcessMemory(hProc, HostDataAddr, ByVal LocalDataAddr, DataSize) <> 0 Then
CloseHandle hProc
Exit Function
End If
CloseHandle hProc
WriteDataToHost = True
End Function