} void Unmanaged :: UnmanagedFunc2() { } 非常感谢我能得到的所有帮助。 如果我还需要将一个字符串作为参数传递给 ManagedFunction()。在非托管中,它是char *或CString。如何进行 转换以将其传递给托管。 感谢您的帮助。Hi,I have my main application class(unmanaged) that has a none static memberfunction 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 parametersof this C# method is a delegate. I need to pass the none static memberfunction 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//.hclass Unmanaged{public:Unmanaged();~Unmanaged();void UnmanagedFunc1();void UnmanagedFunc2();}*.cppusing 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 toManagedFunction(). In unmanaged it is a char* or a CString. How can I do theconversion 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 memberfunction 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课程)。 TomAfter some discussion of this topic (see http://tinyurl.com/h2kry), Icame up with two very simple examples demonstrating managed->unmangedand unmanaged->managed event handling: http://tweakbits.com/ManagedToUnmanagedCallback.cpp http://tweakbits.com/UnmanagedToManagedCallback.cppWhat 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, compiledin mixed-mode. It looks like your unmanaged class is written in VC6,which requires yet another level of interaction. In other words, theThunk class (see my example) will call a VC6 DLL inside.In addition to my example, Microsoft has an official guide about thissubject too: http://msdn2.microsoft.com/en-us/library/367eeye0.aspxFinally, you have to take care of your argument marshaling, such asconverting String^ to const char* / const wchar_t*. You can useStringToHGlobalAnsi/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 thetips. I will work on it."Tamas Demjen" wrote: Tamas Demjen写道:Tamas Demjen wrote: 需要另一个级别的互动。 which requires yet another level of interaction. *间接,我的意思。* indirection, I meant. 这篇关于将无静态成员函数作为回调参数传递给manag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 02:55