看板 C_Sharp 關於我們 聯絡資訊
※ 引述《freaky (jon)》之銘言: : 我這樣問好了. managed 物件要怎麼執行 native code? : CompilerServices.CallConvCdel 是告訴 CLR 要使用什麼 calling convention. : 這個例子並沒有告訴你 MC++ 需要 PInvoke (Platform Invoke) Service 來 : 呼叫 native function. 當然也可以這麼做, 但這是不被建議的作法. : MC++ 裡的 unmanaged code 可以直接呼叫 native function, 而且是 compile : 成 native code. 同樣也可以 create COM 物件, 不需要 tlbexp.exe 或 : tlbimp.exe, 兩種方法在效率上是有差別的. : main() 是 managed 沒錯, 但 mainCRTStartup() 是 unmanaged, 而 native : code 是靠 C runtime 執行. 不然你認為 managed code 是要怎麼 calling native code 的呢? 在 .net framework on windows 中, Managed Code Calling Native Code 有兩種方法: COM interop: Managed wrapper classes allow managed code to instantiate unmanage COM classes and invoke methods. Platform invoke: Managed code can call directly to stdcall or cdecl C-style functions exported by dynamic libraries implemented in native code. 而 Native Code Calling Managed Code 有三種: COM interop: Unmanaged code can use COM wrapper classes to instantiate managed classes and invoke methods. Managed Extensions for C++: Native C++ classes can be compiled in the same assembly as managed classes and can interop directly. Managed methods can be exported directly as dynamic library entry points. Delegates: Managed code implements a delegate which is exported as a function pointer to native code. 如果你說 managed code calling native code 不是用 PInvoke, 那可不可以請教你, 它到底是怎麼做的呢? : __nogc 在 MC++ 裡就是 unmanaged 的意思, 可以不用寫出來. : 剛才說了, unmanaged code 會被 compile 成 native code, C# 的 unsafe : 仍然是 managed code. 如我之前所說, C# 沒有產生 native code 的能力. 另外, 拿 gc 來當 managed code 和 native code 的區別並不妥當. 除此之外, C++ 還可以用 #pragma managed/unmanaged 來明示. -- ※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw) ◆ From: 218.184.81.80