作者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