作者Action (雪...)
看板C_Sharp
标题Re: C#的视窗程式设计
时间Wed Nov 13 00:34:15 2002
※ 引述《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