2011-07-28 17 views
0

Visual BasicでCOM dllを作成するために、このtuorialに従いました。 http://www.codeproject.com/KB/COM/Basics_of_Idl_file.aspxVisual Basic 2008でCOM dllを作成し、C++プロジェクトで使用する

私はこのDLLをC++プロジェクトで使いたいと思っています。このチュートリアルの後半で説明するように、OLE/COM Viewerを使用して.idlファイルを作成しました。 http://www.codeproject.com/KB/COM/vb_from_vc.aspx

私はmidlコンパイラで.idlをコンパイルし、私のC++プロジェクトで作成された.hファイルを含めました。ここで

は私のVisual Basicコードここ

<ComClass(MyComClass.ClassId, MyComClass.InterfaceId, MyComClass.EventsId)> _ 
Public Class MyComClass 

#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 = "46604f8a-85a2-4027-9728-0390534c9209" 
    Public Const InterfaceId As String = "30274029-711d-459a-9270-f9d73ad8737f" 
    Public Const EventsId As String = "5e234d69-5263-4001-86ff-c475b113a77d" 
#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 
    Protected Overrides Sub Finalize() 
     MyBase.Finalize() 
    End Sub 
    Public Sub DisplayMessage() 
     MsgBox("Hello from MyComClass!") 
    End Sub 
End Class 

である私のC++コード

// Declare an HRESULT and a pointer to the clsVBTestClass interface 

HRESULT  hr; 
_MyComClass *IVBTestClass = NULL; 

// Now we will intilize COM 

hr = CoInitialize(0); 

// Use the SUCCEEDED macro and see if we can get a pointer 

// to the interface 

if(SUCCEEDED(hr)) 
{ 
    hr = CoCreateInstance(CLSID_MyComClass, 
       NULL, 
       CLSCTX_INPROC_SERVER, 
       IID__MyComClass, 
       (void**) &IVBTestClass); 

    // If we succeeded then call the CountStringLength method, 

    // if it failed then display an appropriate message to the user. 

    if(SUCCEEDED(hr)) 
    { 
     long  ReturnValue; 
     _bstr_t  bstrValue("Hello World"); 

     // We can test this HR as well if we wanted to 

     hr = IVBTestClass->DisplayMessage(); 

     hr = IVBTestClass->Release(); 
    } 
    else 
    { 

    } 
} 
// Uninitialize COM 

CoUninitialize(); 

私は私のC++プロジェクト

エラーLNK2001をコンパイルするとき、私は次のエラーを受け取る:未解決外部シンボル_CLSID_MyComClass エラーLNK2001:未解決の外部シンボルIID _MyComClass

誰かが私が間違っていることを理解するのに役立つでしょうか?

答えて

1

タイプライブラリを作成する最後の段階に達していない場合は、それが重要です。

したがって、.tlbファイル(またはタイプライブラリがDLLに埋め込まれている場合は.dll(これは一般的です))を使用するには、C++コードに#import文が必要です。

#importは、COMでヘッダーファイルを含めるのと同じですが、.tlhファイル(ヘッダー)と.tli(実装)を自動的に生成します。

+0

.hファイルのインクルードを削除し、COMクラスをビルドしたときにVisual Basicによって.dllとともに生成された.tlbファイルの#importを追加しました。コンパイル時に同じエラーが表示されます。 – James

+0

.tliと.tlhファイルを見てください。それは何が欠けているか(または何かが別の名前を持っているかどうか)を理解するのに役立ちます。 – crashmstr

関連する問題