将无静态成员函数作为回调参数传递给manag [英] Passing none static member function as callback parameter to manag

查看:85
本文介绍了将无静态成员函数作为回调参数传递给manag的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的主应用程序类(非托管)有一个非静态成员

函数我需要作为委托传递给管理C#方法。


在这个类的一个方法(unmamanged)中,我调用一个托管的C#

方法(我使用gcnew来实例化管理类)。这个C#方法的一个参数

是一个委托。我需要将无静态成员

函数作为委托(函数指针)作为托管C#

函数调用中的参数传递。


eg


//托管C#

名称空间MySpace

{

公共代表int MyDelegate();


MyDelegate rDelegate;


公共类管理

{

void ManagedFunction(MyDelegate del)

{

//做东西

}


} $ / $
}

//用VC ++ 6.0编写的非托管C ++

//.h

class Unmanaged

{

public:

Unmanaged();

~Unmanaged();


void UnmanagedFunc1();

void UnmanagedFunc2();


}


* .cpp

使用命名空间MySpace;


void Unmanaged :: UnmanagedFunc1()

{

MySpace :: Managed ^ man = gcnew MySpace :: Managed;


//我需要这里的UnmanagedFunc2()作为委托/回调作为ManagedFunction的

//参数。

man-> ManagedFunction(??)

}


void Unmanaged :: UnmanagedFunc2()

{


}


非常感谢我能得到的所有帮助。


如果我还需要将一个字符串作为参数传递给

ManagedFunction()。在非托管中,它是char *或CString。如何进行

转换以将其传递给托管。


感谢您的帮助。

Hi,

I have my main application class(unmanaged) that has a none static member
function that I need to pass as a delegate to managed C# method.

In one of the methods of this class(unmamanged), I am calling a managed C#
method(I use gcnew to instantiate the managed class). One of the parameters
of this C# method is a delegate. I need to pass the none static member
function as a delegate(function pointer) as a parameter in the managed C#
function call.

e.g.

//Managed C#
namespace MySpace
{
public delegate int MyDelegate();

MyDelegate rDelegate;

public class Managed
{
void ManagedFunction(MyDelegate del)
{
//do stuff
}

}
}
//Unmanaged C++ written in VC++ 6.0

//.h
class Unmanaged
{
public:
Unmanaged();
~Unmanaged();

void UnmanagedFunc1();
void UnmanagedFunc2();

}

*.cpp
using namespace MySpace;

void Unmanaged::UnmanagedFunc1()
{
MySpace::Managed ^man = gcnew MySpace::Managed;

//I need to pass UnmanagedFunc2() here as a delegate/callback as a
//parameter to ManagedFunction.
man->ManagedFunction(??)

}

void Unmanaged::UnmanagedFunc2()
{

}

Will appreciate all the help I can get on this.

Also what if I need to pass a string also as a parameter to
ManagedFunction(). In unmanaged it is a char* or a CString. How can I do the
conversion to pass it to managed.

Thanks for the help.

推荐答案

Haxan写道:
Haxan wrote:




我有我的主要应用程序class(unmanaged)有一个非静态成员

函数,我需要作为委托传递给托管C#方法。
Hi,

I have my main application class(unmanaged) that has a none static member
function that I need to pass as a delegate to managed C# method.



在讨论了这个主题后(参见 http://tinyurl.com/h2kry) ,我的b $ b想出了两个非常简单的例子,展示了托管 - >无人管理

和unmanaged-> ;管理事件处理:

http://tweakbits.com/ManagedToUnmanagedCallback .cpp
http://tweakbits.com/UnmanagedToManagedCallback.cpp


你要求的是从非托管代码处理托管事件,

所以我的ManagedToUnmanagedCallback.cpp示例应该给你一个想法。


另一方面,我假设您的代码是用VC ++ 2005编写的,在混合模式下编译为
。看起来你的非托管类是用VC6编写的,

需要另一个级别的交互。换句话说,

Thunk类(参见我的例子)将调用内部的VC6 DLL。


除了我的例子,微软还有一个官方指南关于这个

主题:
http://msdn2.microsoft.com/en-us/library/367eeye0.aspx


最后,你必须照顾你的论证编组,例如

将String ^转换为const char * / const wchar_t *。您可以使用

StringToHGlobalAnsi / StringToHGlobalUni来执行此操作,如下所示:
http://tinyurl.com/kmzmh (参见Jochen的StringConvA / StringConvW课程)。


Tom

After some discussion of this topic (see http://tinyurl.com/h2kry), I
came up with two very simple examples demonstrating managed->unmanged
and unmanaged->managed event handling:

http://tweakbits.com/ManagedToUnmanagedCallback.cpp
http://tweakbits.com/UnmanagedToManagedCallback.cpp

What you''re asking for is handling managed events from unmanaged code,
so my ManagedToUnmanagedCallback.cpp example should give you an idea.

On the other hand, I assume your code is written in VC++ 2005, compiled
in mixed-mode. It looks like your unmanaged class is written in VC6,
which requires yet another level of interaction. In other words, the
Thunk class (see my example) will call a VC6 DLL inside.

In addition to my example, Microsoft has an official guide about this
subject too:
http://msdn2.microsoft.com/en-us/library/367eeye0.aspx

Finally, you have to take care of your argument marshaling, such as
converting String^ to const char* / const wchar_t*. You can use
StringToHGlobalAnsi/StringToHGlobalUni to do that, as shown here:
http://tinyurl.com/kmzmh (see Jochen''s StringConvA / StringConvW classes).

Tom


Tamas Demjen写道:
Tamas Demjen wrote:

这需要另一层次的互动。
which requires yet another level of interaction.



*间接,我的意思。

* indirection, I meant.


非常感谢Tamas。这看起来像我在寻找。感谢

提示。我将继续努力。


" Tamas Demjen"写道:
Thanks very Tamas. This looks like what I was looking for. Appreciate the
tips. I will work on it.

"Tamas Demjen" wrote:

Tamas Demjen写道:
Tamas Demjen wrote:

需要另一个级别的互动。
which requires yet another level of interaction.



*间接,我的意思。

* indirection, I meant.


这篇关于将无静态成员函数作为回调参数传递给manag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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