110,590
社区成员
发帖
与我相关
我的任务
分享
interface INotifyCallback : IDispatch //<---
{
virtual void __stdcall MyEventCallback(int nMSG);
virtual void __stdcall MyEventCallback2(int nMSG);
};
extern "C"
{
_declspec(dllexport) VOID _stdcall RegNotify(INotifyCallback* p)
{
p->MyEventCallback(123);
}
}
[ClassInterface(ClassInterfaceType.None)]
public partial class Form1 : Form, INotifyCallback
{
public Form1()
{
InitializeComponent();
RegNotify(this);
}
public void MyEventCallback(int nMSG) { MessageBox.Show("hello" + nMSG); }
public void MyEventCallback2(int nMSG){}
[DllImport("Mydll.dll")]
static extern bool RegNotify([MarshalAs(UnmanagedType.Interface)]INotifyCallback a);
}
[InterfaceType( ComInterfaceType.InterfaceIsIDispatch)] //<---
interface INotifyCallback
{
void MyEventCallback(int nMSG);
void MyEventCallback2(int nMSG);
}
public delegate void CallbackDelegate(int msg);
public interface INotifyCallback
{
void MyEventCallback(int nMSG);
void MyEventCallback2(int nMSG);
}
public class Test : INotifyCallback
{
#region INotifyCallback Members
public void MyEventCallback(int nMSG)
{
}
public void MyEventCallback2(int nMSG)
{
}
#endregion
}
static void Main(string[] args)
{
INotifyCallback inc = new Test();
CallbackDelegate del1 = inc.MyEventCallback;
CallbackDelegate del2 = inc.MyEventCallback2;
IntPtr funcPointer1 = Marshal.GetFunctionPointerForDelegate(del1);
IntPtr funcPointer2 = Marshal.GetFunctionPointerForDelegate(del1);
PhoneLib.RegNotify1(funcPointer1);
PhoneLib.RegNotify2(funcPointer2);
}