【问题标题】:How to use ComImport?如何使用 ComImport?
【发布时间】:2014-12-14 07:33:36
【问题描述】:

我正在学习 com 并在基于 .net 的应用程序中使用它。

我创建了一个简单的 MathFunction dll,它有一个函数 Add 2 numbers。

然后我使用 ComImport 将其加载到 Windows 窗体应用程序中。一切正常,没有错误。当我运行应用程序时,我得到的结果是添加 2 个数字为零。

我向函数传递了 2 个参数。

IMathFunc mathFunc = GetMathFunc();
int res = mathFunc.Add(10, 20);

现在我得到的结果为 0。这里 IMathFunc 是 IUnkown 类型的接口。

[ComImport]
    [Guid("b473195c-5832-4c19-922b-a1703a0da098")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IMathFunc
    {
        int Add(int a, int b);
        void ThrowError();
    }

当我为函数调试它时

int Add(int a,int b)

它在“a”中显示一些大数字,在“b”中显示 10。

这是我的数学函数库

#include<windows.h>
using namespace System;

static const GUID IID_MathFunc =
{ 0xb473195c, 0x5832, 0x4c19, { 0x92, 0x2b, 0xa1, 0x70, 0x3a, 0xd, 0xa0, 0x98 } };

struct IMathFunc:public IUnknown
{
    virtual int Add(int a, int b) = 0;

    STDMETHOD(ThrowError)() = 0;
};

namespace MathFuncLibrary {

    class MathFunc: public IMathFunc
    {
        volatile long refcount_;
    public:

        MathFunc();

        int Add(int a, int b);
        STDMETHODIMP_(ULONG) Release();

        STDMETHODIMP_(ULONG) AddRef();
        STDMETHODIMP  QueryInterface(REFIID guid, void **pObj);

        STDMETHODIMP ThrowError();

    };

}

int MathFunc::Add(int a, int b)
{
    return a + b;
}

STDMETHODIMP_(ULONG) MathFunc::Release()
{
    ULONG result = InterlockedDecrement(&refcount_);
    if(result==0)delete this;
    return result;
}

STDMETHODIMP MathFunc::QueryInterface(REFIID guid, void **pObj)
{
    if (pObj == NULL) {
        return E_POINTER;
    }
    else if (guid == IID_IUnknown) {
        *pObj = this;
        AddRef();
        return S_OK;
    }
    else if (guid == IID_MathFunc) {
        *pObj = this;
        AddRef();
        return S_OK;
    }
    else {
        // always set [out] parameter
        *pObj = NULL;
        return E_NOINTERFACE;
    }
}

STDMETHODIMP_(ULONG) MathFunc::AddRef()
{
    return InterlockedIncrement(&refcount_);
}

STDMETHODIMP MathFunc::ThrowError()
{
    return E_FAIL;
}

MathFunc::MathFunc() :refcount_(1)
{

}



extern "C" __declspec(dllexport) LPUNKNOWN __stdcall GetMathFunc()
{
    return new MathFunc();
}

我是否遗漏了导致此错误的任何内容,或者我做错了所有事情......?

【问题讨论】:

  • 与您拥有的其他函数相比,您肯定会注意到 Add() 的一些奇怪之处。是的,STDMETHOD 不是一个小细节。 COM 函数必须返回 HRESULT。
  • 好的,我将返回类型 int 更改为 STDMETHODIMP_(UINT32)。现在我得到了正确的参数。但是你如何接收结果(int 和 object 不起作用)。?
  • 好的,我明白了。如果我使用 [PreserveSig] int Add(int a,int b) 它可以工作。
  • 好吧,不要那样做。 QueryInterface() 是一个有返回值的方法,注意它是怎么做的。
  • 你的意思是使用out参数吗?它返回 HRESULT。

标签: c# c++ com


【解决方案1】:

我是在 Hans Passant 的建议下得到它的。

你需要改变方法

virtual int Add(int a, int b) = 0;

virtual STDMETHOD_(UINT32,Add)(int a, int b) = 0;

然后在 ComImport 界面中

int Add(int a, int b);

 [PreserveSig]int Add(int a,int b);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多