Re: [問題] Callback 函式 (C# 呼叫 C++ dll) (用 delegate)

看板C_Sharp作者 (serene ice)時間16年前 (2009/09/16 00:37), 編輯推噓2(203)
留言5則, 3人參與, 最新討論串1/1
用 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

09/16 01:39, , 1F
沒有 exception嗎 @@?
09/16 01:39, 1F

09/16 09:08, , 2F
嗯...什麼意思? @@"
09/16 09:08, 2F

09/16 19:22, , 3F
基本上 unmanaged C++ 不能直接call C#的method...
09/16 19:22, 3F

09/16 19:22, , 4F
除非你做一個COM或是用managed C++ 已經寫好的應該不行
09/16 19:22, 4F

09/16 23:30, , 5F
囧" 這還真是個天大的壞消息 感謝樓上~
09/16 23:30, 5F
文章代碼(AID): #1AhyA-tl (C_Sharp)