VB6进程间互操作

废话少说,直接上代码

1、VB ActiveEXE 工程。
2、Form1 + Module1 + Class1
3、Form1 Code

Private Sub Form_Unload(Cancel As Integer)
Set oClass1 = nothing
End Sub

4、Module1 Code

Option Explicit

Public oClass1 As Class1

Sub Main()

Set oClass1 = New Class1

Form1.Show

End Sub

5、Class1 Code

Option Explicit

Private mPatientID As String

Private Type GUIDs

Data1 As Long

Data2 As Integer

Data3 As Integer

Data4(0 To 7) As Byte

End Type

'Declares needed to register object in the ROT (Run Object Table)

Private Const ACTIVEOBJECT_STRONG = 0

Private Const ACTIVEOBJECT_WEAK = 1

Private Declare Function CLSIDFromProgID Lib "ole32.dll" (ByVal ProgID As Long,rclsid As GUIDs) As Long

Private Declare Function CodisconnectObject Lib "ole32.dll" (ByVal pUnk As IUnkNown,pvReserved As Long) As Long

Private Declare Function RegisteractiveObject Lib "oleaut32.dll" (ByVal pUnk As IUnkNown,rclsid As GUIDs,ByVal dwFlags As Long,pdwRegister As Long) As Long

Private Declare Function RevokeActiveObject Lib "oleaut32.dll" (ByVal dwRegister As Long,ByVal pvReserved As Long) As Long

Private OLEInstance As Long

Public Property Let PatientID(ByVal Value As String)

mPatientID = Value

End Property

Public Property Get PatientID() As String

PatientID = mPatientID

End Property

Public Sub AddToROT()

Dim mGuid As GUIDs

Dim lp As Long

'The magic happens here 'This code is responsible for creating the entry in the ROT

'Make sure to insert the correct qualified object (class) that

'you want in the ROT.

OLEInstance = 0

lp = CLSIDFromProgID(StrPtr("Project1.Class1"),mGuid)

If lp = 0 Then

lp = RegisteractiveObject(Me,mGuid,ACTIVEOBJECT_WEAK,OLEInstance)

End If

End Sub

Public Sub RemoveFromrOT()

'Once we are done with the main program,lets clean up the rot

'by removing the entry for our ActiveX Server/DLL

If OLEInstance <> 0 Then

RevokeActiveObject OLEInstance,0

End If

CodisconnectObject Me,0

End Sub

Private Sub Class_Initialize()

AddToROT

mPatientID = "123456"

End Sub

Private Sub Class_Terminate()

RemoveFromrOT

End Sub

6、编译,生成EXE,运行。
7、Test Code

Private Sub Command1_Click()

Dim oTestObject As Object

Set oTestObject = Getobject(,"Project1.Class1")

If oTestObject Is nothing Then

MsgBox "Error"

Else

MsgBox oTestObject.patientid

End If

End Sub

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...