COM和C#.net互操作性 [英] COM and C#.net Interoperability

查看:96
本文介绍了COM和C#.net互操作性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi all,

我正在尝试向COM公开C#对象,在此过程中,我收到了一个查询,不确定如何进一步进行.所以我在这里寻求帮助.

i am trying to expose C# object to COM and in this process i got a query and not sure how to progress further. so here i am seeking for help.

我想以这种方式在C#中定义一个接口,COM对象的使用者应该能够以以下方式使用该方法.

i want to define a interface in C# in such a way the consumer of the COM object should be able to use the method in the following way.

comObject.methodName(123)

上面的语句将返回一个值,假设其中123是可以设置id或类似名称的参数

the above statement will return a value, assume where 123 is the parameter which could id or some thing like that

comObject.methodName(123)= 100&

上面的语句应将值100设置为ID为123的内部成员

the above statement should set the value 100 something internal member having id 123

在以上两个语句中, methodName 是一样.

in both of the above statement the methodName is the same. 

所以问题是我们如何在C#中定义一个可以使用regasm.exe暴露给COM的接口.

so the question is how can we define an interface in C# that can be exposed to COM using regasm.exe. 

我知道可以使用ATL在C ++中定义这种方式,但不确定如何在C#中实现相同的方式.

i know it is possible to define such a way in C++ using ATL but not sure how can i achieve the same in C#.

感谢您的帮助,

Vj.

推荐答案

您不能,至少不能使用第二个示例.第二个示例将要求该方法返回对COM不支持的内部存储的引用.

You can't, at least not with the second example. The second example would require the method to return a reference to internal storage, which COM does not support.

最后,它可能会归结为以下内容:

In the end, it will probably boil down to something like this:

c ++

class __declspec(novtable, uuid("A5208D8D-7287-43A6-AE0F-B5F7E35A0E81")) IDummy : public IUnknown
{
public:
	virtual HRESULT STDMETHODCALLTYPE GetMethodName(DWORD index, DWORD *value) = 0;
	virtual HRESULT STDMETHODCALLTYPE SetMethodName(DWORD index, DWORD value) = 0;
};

C#:

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("A5208D8D-7287-43A6-AE0F-B5F7E35A0E81")]
interface IDummy
{
    // The standard translations provided by the runtime 
    // (if you don't use [PreserveSig]) will convert the return 
    // value to an out argument at the end of the argument
    // list, and the HRESULT to an exception.
    int GetMethodName(int index);

    void GetMethodName(int index, int value);
}






这篇关于COM和C#.net互操作性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆