在我上一個問題以來的一些進度測量中,我得到了以下項目進行編譯。它基於https://stackoverflow.com/a/10949784/846550使用CoCreateInstance從C++調用COMVisible VB.Net dll
但是在運行時對CoCreateInstance的調用失敗。 HRESULT是0x80131522 - 谷歌這個和大多數命中似乎涉及SQL服務器,我不使用。
C++代碼,非託管
#include <iostream>
#include <atlbase.h>
#import "..\ClassLibrary1\bin\debug\ClassLibrary1.tlb" no_namespace
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ATLENSURE_SUCCEEDED(CoInitialize(NULL));
CLSID clsid;
_ComClass1 *t;
ATLENSURE_SUCCEEDED(::CoInitialize(NULL));
ATLENSURE_SUCCEEDED(CLSIDFromProgID(OLESTR("ClassLibrary1.ComClass1"), &clsid));
HRESULT hr = CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER,
__uuidof(_ComClass1),(LPVOID*) &t);
ATLENSURE_SUCCEEDED(hr);
ATLENSURE_SUCCEEDED(t->showform());
ATLENSURE_SUCCEEDED(t->increment());
CoUninitialize();
return 0;
}
這裏是很好的措施的DLL代碼 - 幾乎完全自動生成作爲在VB一個COM DLL類。 (DLL顯示爲使用oleview註冊)。
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "46783a4a-5ca9-4930-addb-cde91a377c02"
Public Const InterfaceId As String = "e2ad7298-7339-45fb-aad1-cb82188bf067"
Public Const EventsId As String = "b7e10cdf-dc2a-4052-9b0f-f6f9d5a1f0ac"
#End Region
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
Private f1 As Form1
Public Sub showform()
f1 = New Form1()
f1.Show()
End Sub
Public Sub increment()
f1.ProgressBar1.Increment(5)
End Sub
End Class
即時贏。謝謝! –