發個Vfb呼叫Stdcall、cdcel、COM組件的例子
Stdcall、cdcel的dll呼叫so easy就不說了,來個COM組件的例子
1、vb6創建一個dll工程,修改類名為EventClass
類里寫入以下代碼:
Public Event OnEvent()
Public Event OnOtherEvent()
Public Sub DoRaiseEvent()
RaiseEvent OnEvent
MsgBox "測驗事件1"
End Sub
Public Sub DoOtherRaiseEvent()
RaiseEvent OnOtherEvent
MsgBox "測驗事件2"
End Sub
2、創建一個Vfb工程添加以下代碼:
公共區:
Type ExcelSink Extends Object
Declare Virtual Function QueryInterface (ByVal riid As REFIID, ByVal ppvObject As LPVOID Ptr) As HRESULT
Declare Virtual Function AddRef() As ULong
Declare Virtual Function Release() As ULong
Declare Virtual Function GetTypeInfoCount(ByVal pctinfo As UINT Ptr) As HRESULT
Declare Virtual Function GetTypeInfo(ByVal iTInfo As UINT, ByVal lcid As LCID, ByVal ppTInfo As ITypeInfo Ptr Ptr) As HRESULT
Declare Virtual Function GetIDsOfNames(ByVal riid As Const IID Const Ptr, ByVal rgszNames As LPOLESTR Ptr, ByVal cNames As UINT, ByVal lcid As LCID, ByVal rgDispId As DISPID Ptr) As HRESULT
Declare Virtual Function Invoke(ByVal dispIdMember As DISPID, ByVal riid As Const IID Const Ptr, ByVal lcid As LCID, ByVal wFlags As WORD, ByVal pDispParams As DISPPARAMS Ptr, ByVal pVarResult As VARIANT Ptr, ByVal pExcepInfo As EXCEPINFO Ptr, ByVal puArgErr As UINT Ptr) As HRESULT
cRef As ULong
End Type
Function ExcelSink.QueryInterface(ByVal riid As REFIID, ByVal ppvObject As LPVOID Ptr) As HRESULT
*ppvObject = @this
Function =S_OK
End Function
Function ExcelSink.AddRef() As ULong
This.cRef += 1
Function = This.cRef
End Function
Function ExcelSink.Release() As ULong
This.cRef -= 1
Function = This.cRef
End Function
Function ExcelSink.GetTypeInfoCount(ByVal pctinfo As UINT Ptr) As HRESULT
*pctInfo = 0
Return E_NOTIMPL
End Function
Function ExcelSink.GetTypeInfo(ByVal iTInfo As UINT, ByVal lcid As LCID, ByVal ppTInfo As ITypeInfo Ptr Ptr) As HRESULT
Return E_NOTIMPL
End Function
Function ExcelSink.GetIDsOfNames(ByVal riid As Const IID Const Ptr, ByVal rgszNames As LPOLESTR Ptr, ByVal cNames As UINT, ByVal lcid As LCID, ByVal rgDispId As DISPID Ptr) As HRESULT
Return E_NOTIMPL
End Function
Function ExcelSink.Invoke(ByVal dispIdMember As DISPID, ByVal riid As Const IID Const Ptr, ByVal lcid As LCID, ByVal wFlags As WORD, ByVal pDispParams As DISPPARAMS Ptr, ByVal pVarResult As VARIANT Ptr, ByVal pExcepInfo As EXCEPINFO Ptr, ByVal puArgErr As UINT Ptr) As HRESULT
Select Case dispIdMember
Case 1
MessageBox(0, "我的事件1", "", 0)
Case 2
MessageBox(0, "我的事件2", "", 0)
End Select
Function = 0
End Function
Function Advise(ByVal MeObj As IDispatch Ptr, ByVal pEvtObj As IDispatch Ptr, ByVal m_riidEvt As IID Ptr) As HRESULT
If pEvtObj = Null Then Return E_POINTER
Dim pCPC As IConnectionPointContainer Ptr
Dim hr As HRESULT = IUnknown_QueryInterface(MeObj, @IID_IConnectionPointContainer, @pCPC)
If hr <> S_OK Or pCPC = Null Then
Return hr
End If
Dim pCP As IConnectionPoint Ptr
hr = pCPC->lpvtbl->FindConnectionPoint(pCPC, m_riidEvt, @pCP)
If hr <> S_OK Or pCP = Null Then
AfxSafeRelease(pCPC)
Return hr
End If
Dim m_dwCookie As DWord
If m_dwCookie Then hr = pCP->lpvtbl->Unadvise(pCP, m_dwCookie)
m_dwCookie = 0
hr = pCP->lpvtbl->Advise(pCP, Cast(IUnknown Ptr, pEvtObj), @m_dwCookie)
If hr <> S_OK Then MessageBox(0, "8", "", 0)
AfxSafeRelease(pCPC)
AfxSafeRelease(pCP)
Return hr
End Function
呼叫區:(按鈕事件)
Dim pAxHost As CAxHost Ptr
Dim AppPtr As IDispatch Ptr = pAxHost->CreateObject("test.EventClass")
Dim pDisp As CDispInvoke = AppPtr
If pDisp.DispPtr = Null Then MessageBox(0, "AppPtr出問題了", "測驗", 0)
Dim MySink As ExcelSink
Dim pSink As IDispatch Ptr
MySink.QueryInterface(@IID_IDispatch, @pSink)
Const AFX_IID___EventClass = "{B73195A6-8732-44A2-BC0F-1E6E1D6AE3E8}"
Dim EventIID As clsid
CLSIDFromString(AFX_IID___EventClass, @EventIID)
Advise(pDisp.DispPtr, pSink, @EventIID)
pDisp.Invoke("DoRaiseEvent")
pDisp.Invoke("DoOtherRaiseEvent")
3、其中AFX_IID___EventClass需要查注冊表事件介面的IID
'[
' uuid(B73195A6-8732-44A2-BC0F-1E6E1D6AE3E8),
' version(1.0),
' hidden,
' nonextensible
']
'dispinterface __EventClass {
' properties:
' methods:
' [id(0x00000001)]
' void OnEvent();
' [id(0x00000002)]
' void OnOtherEvent();
'};
中的UUID或使用vfb自帶的工具“COM型別庫查看器”
生成bi檔案復制該常量或放在生成目錄下編譯完活。
uj5u.com熱心網友回復:
事件UUID用vfb自帶的工具“COM型別庫查看器”生成bi檔案后,直接放在exe生成目錄下,編譯即可!轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/40388.html
