在本机C ++ dll中触发事件并捕获C ++/CLI [英] Fire an event in native C++ dll and catch in C++/CLI
问题描述
大家好!
有没有办法在C ++/CLI中处理本机C ++事件?我应该怎么做钩子?
我几乎整天都在互联网上进行搜索,但没有找到答案.我希望你有什么主意.
Hello guys!
Is there a way to handle a native C++ event in C++/CLI? How should I make the hook?
I searched almost all day on the internet but haven''t found the answer. I hope you have any idea.
class Native{
public:
__event void myEvent(int i);
void fireEvent(){
__raise myEvent(1598);
};
};
public ref class Managed{
Native* nativeObject;
public:
void fireEventHandler(int i){
printf("Event handled!\n");
};
Managed(){
nativeObject=new Native();
__hook(&Native::myEvent, nativeObject, &Managed::fireEventHandler );
};
};
如果我尝试对此进行编译,则会出现错误:
If I try to compile this, I get an error:
error C3731: incompatible event 'void Native::myEvent(int)' and handler 'void Managed::fireEventHandler(int)'; event source and event handler must have the same event type.
1> The event type of 'Native' is 'native'.
1> The event type of 'Managed' is 'managed'.
在我的应用程序中,本机类在没有\ clr选项的情况下在单独的DLL中编译,因此我无法保存对托管对象的引用.
In my application, the native class is compiled in separate DLL without a \clr option, so I cannot hold a reference to managed object.
Thank you in forward!
推荐答案
希望这会有所帮助
http://msdn.microsoft.com/en-us/library/367eeye0 (v = VS.100).aspx [
Hope this will help
http://msdn.microsoft.com/en-us/library/367eeye0(v=VS.100).aspx[^]
谢谢Lakamraju,实际上我昨天晚上自己找到了解决方案,但是这篇文章也对我有所帮助,因为我没考虑过垃圾回收器的迁移.
我的解决方案在这里:
Thank you Lakamraju, actually I''ve found the solution myself yesterday evening, but this article helped me also, because I didn''t think about garbage collector relocation.
My solution is here:
// EventTesting.cpp : main project file.
#include "stdafx.h"
#include "stdio.h"
using namespace System;
using namespace System::Runtime::InteropServices;
class Native{
public:
void (*myEvent)(int i); // function pointer declaration
Native(){
myEvent=NULL;
}
void fireEvent(int i){
if(myEvent!=NULL) myEvent(i);// calling function thru pointer
};
void setmyEventHandler(void (*func)(int i)){
myEvent = func;// Initializing the function pointer with the incoming one
}
};
private delegate void myEventDelegate(int i);
public ref class Managed{
Native* nativeObject;
myEventDelegate ^ del;
GCHandle delH;
public:
void fireEventHandler(int i){
printf("EVENT FIRED %d\n",i);
};
Managed(){
nativeObject=new Native();
del = gcnew myEventDelegate(this, &Managed::fireEventHandler);
delH = GCHandle::Alloc(del);
System::IntPtr d1 = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(del);
nativeObject->setmyEventHandler(( void(*)(int))d1.ToPointer());// Calling unmanged library function to initialize function pointer
};
~Managed(){ //Cleanup
nativeObject->setmyEventHandler(NULL);
delete nativeObject;
delH.Free();
}
void testEvent(){
nativeObject->fireEvent(1598);
}
};
int main(array<system::string xmlns:system="#unknown"> ^args)
{
Managed^ managedClass = gcnew Managed;
GC::Collect();
managedClass->testEvent();
return 0;
}</system::string>
此解决方案的唯一问题是我们只能注册"一个事件处理程序" ...实际上这不是一个事件,只是一个回调.
再次感谢您!
The only problem with this solution is that we can "register" only one "event handler"... actually this is not an event, just a callback.
Thank you once again!
这篇关于在本机C ++ dll中触发事件并捕获C ++/CLI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!