将COM方法添加到现有COM模块会导致未处理的异常? [英] Adding COM methods to an existing COM module results in unhandled exception?

查看:52
本文介绍了将COM方法添加到现有COM模块会导致未处理的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的用.NET C#应用程序访问的COM。



COM最初是由其他人编写的,并且已作为产品推出几年前。



现在,我不得不对它进行改动。



我添加了一些接口,这似乎工作。



然后我添加了一些方法到我继承这个工作之前存在的接口。访问它的C#应用​​程序认为它被写入受保护的内存并在运行时失败。



C#位肯定知道新方法存在。



这不是COM中的一些内存分配问题,因为我已经从COM端调试了它并没有达到目标调用COM方法。



我错过了什么吗?



 // MyCOM.idl 
[
对象,
uuid(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX),
oleautomation,
pointer_default(唯一)
]
接口IMyInterface:IUnknown
{
[helpstring(方法Method1)HRESULT Method1();
[helpstring(方法MethodNewlyAdded)HRESULT MethodNewlyAdded();
}

[
object,
uuid(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX),
oleautomation,
pointer_default(unique )
]
接口INewInterface:IUnknown
{
[helpstring(方法NewMethod)HRESULT NewMethod();
}

coclass MyCoClass
{
interface IMyInterface;
接口INewInterface;
}





 // MyApp.cs 
public void MyAppMethod()
{
MyCOM.IMyInterface myCOM = new MyCOM.IMyInterface;
myCOM.Method1(); // OK
myCOM.MethodNewlyAdded(); //未处理的内存异常!


MyCOM.INewInterface myNewCOM = new MyCOM.INewInterface;
myNewCOM.NewMethod(); // OK
}

解决方案

我认为你遗失的是 id



尝试枚举界面中的所有方法并指定ID,如下所示:



  interface  IMyInterface:IUnknown 
{
[id( 1 ),helpstring( 方法Method1)HRESULT Method1();
[id( 2 ),helpstring( 方法MethodNewlyAdded)HRESULT MethodNewlyAdded();
}





此外,尝试让你的界面双重,如下所示:



 [
object,
uuid (?????),
dual,
helpstring( IMyInterface Interface),
pointer_default(唯一的)
]


还有一个包装器模块可以实现存根。我忘了将新方法添加到存根列表中。 :DOH:

I have a COM written in C++ accessed by .NET C# application.

The COM was originally written by someone else and has already been launched as a product many years ago.

Now, I''ve had to make alterations to it.

I added a few interfaces and this seems to work.

I then added a few methods to an interface that existed before I inherited this job. The C# application accessing it thinks that it is made to write into protected memory and fails at runtime.

The C# bit definitely knows that the new methods exist.

It''s not some memory allocation problem within the COM as I''ve debugged from the COM side as well and it doesn''t get as far as calling the COM methods.

Have I missed something?

// MyCOM.idl
[ 
 object, 
 uuid(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX),
 oleautomation,
 pointer_default(unique)
]
interface IMyInterface: IUnknown
{
	[helpstring("method Method1") HRESULT Method1();
	[helpstring("method MethodNewlyAdded") HRESULT MethodNewlyAdded();
}

[ 
 object, 
 uuid(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX),
 oleautomation,
 pointer_default(unique)
]
interface INewInterface: IUnknown
{
	[helpstring("method NewMethod") HRESULT NewMethod();
}

coclass MyCoClass
{
	interface IMyInterface;
	interface INewInterface;
}



// MyApp.cs
public void MyAppMethod()
{
	MyCOM.IMyInterface myCOM = new MyCOM.IMyInterface;
	myCOM.Method1(); // OK
	myCOM.MethodNewlyAdded(); // Unhandled memory exception!


	MyCOM.INewInterface myNewCOM = new MyCOM.INewInterface;
	myNewCOM.NewMethod(); // OK
}

解决方案

I think what''s your missing is id

Try to enumerate all your methods in the interface and assigns the ids, something like this:

interface IMyInterface: IUnknown
{
	[id(1), helpstring("method Method1") HRESULT Method1();
	[id(2), helpstring("method MethodNewlyAdded") HRESULT MethodNewlyAdded();
}



Also, try to make your interface dual, like this:

[
    object,
    uuid(?????),
    dual,
    helpstring("IMyInterface Interface"),
    pointer_default(unique)
]


There is a wrapper module further up that implements stubs. I forgot to add the new methods to the list of stubs. :doh:


这篇关于将COM方法添加到现有COM模块会导致未处理的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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