COM与.Net的互操作性 - 缺少方法/属性

时间:2010-10-19 10:32:35

标签: c# .net c++ com-interop

我有一个.Net asm,它使用[ComVisible(true)]属性向COM公开了几个接口和类。我生成一个tlb,然后在我的StdAdx文件中在C ++ COM组件中引用它。奇怪的是,由于某种原因,即使非常基本的智能感知(VS6 for C ++)能够看到我的属性和方法,我也会收到编译错误,指出它们不是成员。例如:

[Guid("88E58BE4-E0CB-4d1b-9553-A5431E7A0BEA")]
[ComVisible(true)]
public interface ISupplierPayment : IBusinessObjectPersist
{
   String Comment
    {
        get;
        set;
    }

在c ++生成的tlh中:

struct __declspec(uuid("e94bd31e-327c-33c8-8a55-b693ccf1ed96"))
struct __declspec(uuid("e94bd31e-327c-33c8-8a55-b693ccf1ed96"))

ISupplierPayment : IDispatch
{
    //
    // Raw methods provided by interface
    //

    virtual HRESULT __stdcall get_Comment (
        BSTR * pRetVal ) = 0;

最后在代码中尝试使用它时出错:

D:\ MR ... File.cpp(647):错误C2039:'评论':不是'ISupplierPayment'的成员         d:\ mr ... projectdir \ release \ TheDotNetClasses.tlh(758):参见'ISupplierPayment'的声明

我接下来应该注意什么?如果它在tlh并且intellisense识别它并且它在tlb的OLEView中存在,我不确定什么可能是错误的...感谢提前看看

更新 相关问题的进一步示例: C#

    [Guid("3BE93D52-86B7-42e6-BAE6-29037F6BC9C4")]
    [ComVisible(true)]
public interface IDataStoreFactory
{
        void TestMethod(String test);

C ++ TLH

struct __declspec(uuid("3be93d52-86b7-42e6-bae6-29037f6bc9c4"))
IDataStoreFactory : IDispatch
{
    //
    // Raw methods provided by interface
    //

    virtual HRESULT __stdcall TestMethod (
        BSTR dataStoreAssembly ) = 0;
            void TestMethod(String test);

C ++方法调用

spDataStoreFactory->TestMethod("test");

C ++编译错误

'TestMethod':无法将参数1从'char [5]'转换为'unsigned short *'         指向的类型是无关的;转换需要reinterpret_cast,C风格的转换或函数式转换

咦!?它不是短暂的BSTR ......现在很困惑

1 个答案:

答案 0 :(得分:0)

您没有显示生成错误消息的调用。我想你直接使用了属性名称。

您需要使用方法get_Comment而不是简单的Comment属性。生成的tlh指的是该方法。您是否使用了raw_interfaces_only指令的#import属性?

稍后编辑BSTR:

BSTR是wchar_t*的typedef。所以请使用spDataStoreFactory->TestMethod( SysAllocString(L"test"));

相关问题