c++ - 在 visual basic 2008 中创建 COM dll 并在 C++ 项目中使用它

标签 c++ vb.net visual-studio-2008

我按照本教程在 Visual Basic 中创建了一个 COM dll。 http://www.codeproject.com/KB/COM/Basics_of_Idl_file.aspx

我现在想在 C++ 项目中使用这个 dll。我使用 OLE/COM 查看器创建了一个 .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++项目时收到以下错误

error LNK2001: unresolved external symbol _CLSID_MyComClass error LNK2001: unresolved external symbol IID_MyComClass

谁能帮我弄清楚我做错了什么?

最佳答案

如果您没有完成创建类型库的最后一部分,那很重要。

然后你需要一个 #import在 C++ 代码中声明使用 .tlb 文件(如果类型库嵌入到 dll 中,则为 .dll,这很常见)。

#import相当于用COM包含一个头文件,但是会自动生成一个.tlh文件(头)和一个.tli(实现)。

关于c++ - 在 visual basic 2008 中创建 COM dll 并在 C++ 项目中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6861961/

相关文章:

.net - VB.NET 严格关闭在线选项

C#:如果在源代码中使用了某些方法,则在 Visual Studio 中创建自定义警告

c++ - 防止使用 glibc 头文件

c++ - 将 Boost MultiArray (2d) 转换为普通二维数组的最简单方法是什么?

c++ - 由于在 aix6.1 上使用 g++4.6.1 出现 "The displacement must be greater than or equal to -32768 and less than or equal to 32767",编译失败

visual-studio-2008 - Visual Studio 2008 - 状态栏图标词汇表?

visual-studio-2010 - 在 VS2010 中禁用代码折叠中的双行距

c++ - 使用对象作为键时保持 std::map 平衡

mysql - 如何将vb.net组合框内容保存到mysql表中?

.net - 如何在派生类上动态调用静态方法