作者percyy (serene ice)
看板C_Sharp
标题Re: [问题] Callback 函式 (C# 呼叫 C++ dll)
时间Wed Sep 16 00:37:17 2009
用 Delegate 的写法
我写到这样 还是不会出现 haha
第一次接触 应该有错待改
请大家不吝指教 谢谢 <(_ _)>
dll 说明
 ̄ ̄ ̄ ̄
int __stdcall SKQuoteLib_AttachConnectionCallBack( [in]
long lCallBack )
// lCallBack 带入 Call back 的函式位址。当状况发生时即可呼叫此函式进行处理
// 函式的宣告必须符合以下格式:
// typedef void ( __stdcall* FOnNotifyConnection)( int nKind, int nCode )
C# 部份
 ̄ ̄ ̄ ̄
// delegate
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void FOnNotifyConnection(
int nKind,
int nCode );
// dll,参数部份 到底该怎样才能与 C++ 的 long 相容呢?
[DllImport("SKQuoteLib.dll")]
extern static int SKQuoteLib_AttachConnectionCallBack(
[MarshalAs(UnmanagedType.FunctionPtr)]
FOnNotifyConnection Delegate_AttachConnection );
// main
static void Main(string[] args)
{
FOnNotifyConnection Delegate_AttachConnection
= new
FOnNotifyConnection(
CBFunc_AttachConnection );
SKQuoteLib_AttachConnectionCallBack( Delegate_AttachConnection );
}
// 还没办法进到这个函式
static void CBFunc_AttachConnection(
int nKind,
int nCode )
{
Console.WriteLine("haha");
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.32.91.117
1F:推 cole945:没有 exception吗 @@? 09/16 01:39
2F:→ percyy:嗯...什麽意思? @@" 09/16 09:08
3F:推 cplusplus:基本上 unmanaged C++ 不能直接call C#的method... 09/16 19:22
4F:→ cplusplus:除非你做一个COM或是用managed C++ 已经写好的应该不行 09/16 19:22
5F:→ percyy:囧" 这还真是个天大的坏消息 感谢楼上~ 09/16 23:30