Skip to content

Instantly share code, notes, and snippets.

@relyky
Last active July 27, 2018 03:01
Show Gist options
  • Save relyky/a335a6b73d18216cd999 to your computer and use it in GitHub Desktop.
Save relyky/a335a6b73d18216cd999 to your computer and use it in GitHub Desktop.
VB6不註冊調用ActiveX Dll
'類別名稱:ComWithoutRegister
'程式語言:Visual Basic
'開發平台:Microsft Viaual Basic 6(VB6)
'說明:VB6不註冊調用ActiveX Dll。以下為本人案例, 請依你的需求變更“your”的部份。
'注意:此範例無法處理動態調用。
'注意2:需設定引用項目olelib.tlb (Edanmo's OLE interfaces & functions v1.81),http://www.mvps.org/emorcillo/download/vb6/tl_ole.zip
'注意3:在引用不註冊的COM Class時,其中的 ReDim 指令只能用 private 型態,不然會失效。這不科學但存在。若非用不可需自己多加一道額外的轉換 public 操作。
'參考資料:http://blog.yam.com/wyattkid/article/16416433
Option Explicit
'your ActiveX DLL
Private Declare Function VB6SayHello_DllGetClassObject Lib "D:\MyLab\VB6SayHello\VB6SayHello.dll" Alias "DllGetClassObject" (rclsid As UUID, riid As UUID, ByRef ppv As Any) As Long
'your Class-ID
Private Const VB6SayHello_CSayHello_ClsId As String = "{71E0B70A-7170-4BC0-B59B-81EE1570A127}" 'VB6SayHello.SayHello
'IID_IClassFactory
Private Const strIID_IClassFactory As String = "{00000001-0000-0000-C000-000000000046}"
'your class constructor
Public Function CreateSayHelloInstance() As Object
Dim tFac As olelib.IClassFactory
Dim tobj As olelib.IUnknown
Dim errDesc As String 'error description
'get the related UUIC
Dim ClsId_Obj As UUID
Dim iid_iunknow As UUID
Dim iid_iclassfactory As UUID
CLSIDFromString strIID_IClassFactory, iid_iclassfactory
CLSIDFromString IIDSTR_IUnknown, iid_iunknow
CLSIDFromString VB6SayHello_CSayHello_ClsId, ClsId_Obj 'your Class-ID
'get IClassFactory with Class-ID
Call VB6SayHello_DllGetClassObject(ClsId_Obj, iid_iclassfactory, tFac)
'error handling
If tFac Is Nothing Then
errDesc = "無法取得IClassFactory物件!請重新確認ClassID是否正確。"
GoTo errhan
End If
'construct the class instance
tFac.CreateInstance Nothing, iid_iunknow, tobj
'release resource
Set tFac = Nothing
'return
Set CreateSayHelloInstance = tobj
Exit Function
errhan:
'release resource
Set tFac = Nothing
Err.Raise vbObjectError + 1, TypeName(Me), errDesc
End Function
'
'僅截取Demo部份相關的程式碼
'
Private Sub cmdComWithoutRegister_Click()
Dim hello As Object
Dim comWR As New ComWithoutRegister
Set hello = comWR.CreateSayHelloInstance() '--- instead of using: CreateObject("VB6SayHello.CSayHello")
hello.SayHello 'demo 1 無參數
MsgBox hello.Concat("I am ", "Foo") 'demo 2 有參數
End Sub
'ActiveX DLL:VB6SayHello
'類別名稱:CSayHello
'程式語言:Visual Basic
'開發平台:Microsft Viaual Basic 6(VB6)
'說明:用於測試案例,測試 ComWithoutRegister 有無作用。
Option Explicit
'demo 1, 無參數
Public Sub SayHello()
MsgBox ("Hello, I am CSayHello and come from VB6SayHello.dll")
End Sub
'demo 2, 有參數
Public Function Concat(str1 As String, str2 As String) As String
' return
Concat = str1 + str2
End Function
@xxdoc
Copy link

xxdoc commented May 27, 2018

有用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment