※ 引述《absoo ( )》之銘言:
: 是用MFC嗎? 還是只能呼叫windowsAPI
: 還是 他自己有一套建構視窗的方法
: 新手笨問題,多多包含^^||
既不是 mfc, 也不是 win32 api,
比它們都要容易許多,
說起來, 應該是跟 vb 或 java 比較像吧...
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 140.113.128.142
> -------------------------------------------------------------------------- <
作者: freaky (jon) 看板: C_Sharp
標題: Re: C#的視窗程式設計
時間: Mon Nov 11 12:03:32 2002
※ 引述《absoo ( )》之銘言:
: 是用MFC嗎? 還是只能呼叫windowsAPI
: 還是 他自己有一套建構視窗的方法
: 新手笨問題,多多包含^^||
用 .NET 的 base class library (BCL) 和 Windows Forms,
既不能用 MFC 也不可以呼叫 Windows API.
現在只有 Visual C++.NET 可以混用 native 和 managed code.
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 61.222.173.66
> -------------------------------------------------------------------------- <
作者: Action (雪...) 看板: C_Sharp
標題: Re: C#的視窗程式設計
時間: Mon Nov 11 12:22:11 2002
※ 引述《freaky (jon)》之銘言:
: 用 .NET 的 base class library (BCL) 和 Windows Forms,
: 既不能用 MFC 也不可以呼叫 Windows API.
硬是要用 System.Runtime.InteropServices.DllImport 也沒什麼不可吧?
只是好端端的 System.Windows.Forms 不用似乎是自找麻煩...
: 現在只有 Visual C++.NET 可以混用 native 和 managed code.
您說的 "混用" 指的是...?
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 140.113.214.133
> -------------------------------------------------------------------------- <
作者: freaky (jon) 看板: C_Sharp
標題: Re: C#的視窗程式設計
時間: Mon Nov 11 16:02:21 2002
※ 引述《Action (雪...)》之銘言:
: ※ 引述《freaky (jon)》之銘言:
: : 用 .NET 的 base class library (BCL) 和 Windows Forms,
: : 既不能用 MFC 也不可以呼叫 Windows API.
: 硬是要用 System.Runtime.InteropServices.DllImport 也沒什麼不可吧?
: 只是好端端的 System.Windows.Forms 不用似乎是自找麻煩...
System.Runtime.InteropServices 裡面的 classes 主要是拿來
存取 COM 物件和 native APIs, 但這還是屬於 BCL 的部份, 和
VC.NET 裡可以直接存取不一樣. 我的重點是, 語言本身不支援創造
unmanaged 物件.
: : 現在只有 Visual C++.NET 可以混用 native 和 managed code.
: 您說的 "混用" 指的是...?
在同一個 source file 內可以同時 manipulate managed 和 unmanaged 物件.
像下面這樣:
#using <mscorlib.dll>
using namespace System;
__nogc class NoGCclass {
public:
void Hello() { Console::WriteLine(S"Hello, from NoGCclass!"); }
};
__gc class GCclass {
public:
void Hello() { Console::WriteLine(S"Hello, from GCclass!"); }
};
int main() {
// creates an object on the C++ heap
NoGCclass *pNoGC = new NoGCclass();
pNoGC->Hello();
// needed because unmanaged objects are not garbage collected
delete pNoGC;
// creates an object on the GC heap
GCclass *pGC = new GCclass();
pGC->Hello();
// delete not needed because the garbage collector reclaims the memory
}
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 61.222.173.66
> -------------------------------------------------------------------------- <
作者: Action (雪...) 看板: C_Sharp
標題: Re: C#的視窗程式設計
時間: Mon Nov 11 17:26:01 2002
※ 引述《freaky (jon)》之銘言:
: System.Runtime.InteropServices 裡面的 classes 主要是拿來
: 存取 COM 物件和 native APIs, 但這還是屬於 BCL 的部份, 和
: VC.NET 裡可以直接存取不一樣. 我的重點是, 語言本身不支援創造
: unmanaged 物件.
真的是直接存取嗎?
還是透過 System.Runtime.CompilerServices.CallConvThiscall 之類的
去呼叫呢?
: 在同一個 source file 內可以同時 manipulate managed 和 unmanaged 物件.
我不否認目前 C# compiler 並沒有產生 native code 的能力,
不然早就可以直接執行了, 也不需要裝什麼的...
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 140.113.214.133
> -------------------------------------------------------------------------- <
作者: freaky (jon) 看板: C_Sharp
標題: Re: C#的視窗程式設計
時間: Mon Nov 11 19:17:21 2002
※ 引述《Action (雪...)》之銘言:
: ※ 引述《freaky (jon)》之銘言:
: : System.Runtime.InteropServices 裡面的 classes 主要是拿來
: : 存取 COM 物件和 native APIs, 但這還是屬於 BCL 的部份, 和
: : VC.NET 裡可以直接存取不一樣. 我的重點是, 語言本身不支援創造
: : unmanaged 物件.
: 真的是直接存取嗎?
: 還是透過 System.Runtime.CompilerServices.CallConvThiscall 之類的
: 去呼叫呢?
C++ 當然可以直接創造 COM 物件, 也可以直接呼叫 APIs.
如果還要透過 BCL, MC++ 就不叫 "唯一" 可以混用 managed 和
unmanaged code 的 .NET 語言. 只有當 unmanaged code 要儲存 managed 物件
的時候, 才需要藉由 System.Runtime.InteropServices 裡的 GCHandle 完成.
: : 在同一個 source file 內可以同時 manipulate managed 和 unmanaged 物件.
: 我不否認目前 C# compiler 並沒有產生 native code 的能力,
: 不然早就可以直接執行了, 也不需要裝什麼的...
C# 設計上本來就是要在 CLR 環境下執行的.
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 61.222.173.66
> -------------------------------------------------------------------------- <
作者: Action (雪...) 看板: C_Sharp
標題: Re: C#的視窗程式設計
時間: Tue Nov 12 00:23:20 2002
※ 引述《freaky (jon)》之銘言:
: C++ 當然可以直接創造 COM 物件, 也可以直接呼叫 APIs.
: 如果還要透過 BCL, MC++ 就不叫 "唯一" 可以混用 managed 和
: unmanaged code 的 .NET 語言. 只有當 unmanaged code 要儲存 managed 物件
: 的時候, 才需要藉由 System.Runtime.InteropServices 裡的 GCHandle 完成.
將剛剛你的那個程式, 用 ildasm 來看:
.method public static int32 modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
main() cil managed
{
.vtentry 1 : 1
// Code size 35 (0x23)
.maxstack 1
.locals (valuetype NoGCclass* V_0,
class GCclass V_1)
IL_0000: ldnull
IL_0001: stloc.1
IL_0002: ldc.i4.1
IL_0003: call void* modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) new(unsigned int32)
IL_0008: stloc.0
IL_0009: ldloc.0
IL_000a: call void modopt([mscorlib]System.Runtime.CompilerServices.CallConvThiscall) NoGCclass.Hello(valuetype NoGCclass* modopt([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier) modopt([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier))
IL_000f: ldloc.0
IL_0010: call void modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) delete(void*)
IL_0015: newobj instance void GCclass::.ctor()
IL_001a: stloc.1
IL_001b: ldloc.1
IL_001c: call instance void GCclass::Hello()
IL_0021: ldc.i4.0
IL_0022: ret
} // end of method 'Global Functions'::main
由此可知:
1. main 是 managed
2. 其中的 unmanaged 是透過 CompilerServices.
: : 我不否認目前 C# compiler 並沒有產生 native code 的能力,
: : 不然早就可以直接執行了, 也不需要裝什麼的...
: C# 設計上本來就是要在 CLR 環境下執行的.
要是單只是不要 gc 的話, C# 只要加個 unsafe 就行了...
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 218.184.81.80
> -------------------------------------------------------------------------- <
作者: freaky (jon) 看板: C_Sharp
標題: Re: C#的視窗程式設計
時間: Tue Nov 12 22:42:05 2002
※ 引述《Action (雪...)》之銘言:
: ※ 引述《freaky (jon)》之銘言:
: : C++ 當然可以直接創造 COM 物件, 也可以直接呼叫 APIs.
: : 如果還要透過 BCL, MC++ 就不叫 "唯一" 可以混用 managed 和
: : unmanaged code 的 .NET 語言. 只有當 unmanaged code 要儲存 managed 物件
: : 的時候, 才需要藉由 System.Runtime.InteropServices 裡的 GCHandle 完成.
: 將剛剛你的那個程式, 用 ildasm 來看:
: .method public static int32 modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
: main() cil managed
: {
: .vtentry 1 : 1
: // Code size 35 (0x23)
: .maxstack 1
: .locals (valuetype NoGCclass* V_0,
: class GCclass V_1)
: IL_0000: ldnull
: IL_0001: stloc.1
: IL_0002: ldc.i4.1
: IL_0003: call void* modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) new(unsigned int32)
: IL_0008: stloc.0
: IL_0009: ldloc.0
: IL_000a: call void modopt([mscorlib]System.Runtime.CompilerServices.CallConvThiscall) NoGCclass.Hello(valuetype NoGCclass* modopt([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier) modopt([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier))
: IL_000f: ldloc.0
: IL_0010: call void modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl) delete(void*)
: IL_0015: newobj instance void GCclass::.ctor()
: IL_001a: stloc.1
: IL_001b: ldloc.1
: IL_001c: call instance void GCclass::Hello()
: IL_0021: ldc.i4.0
: IL_0022: ret
: } // end of method 'Global Functions'::main
: 由此可知:
: 1. main 是 managed
: 2. 其中的 unmanaged 是透過 CompilerServices.
我這樣問好了. 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 執行.
: : C# 設計上本來就是要在 CLR 環境下執行的.
: 要是單只是不要 gc 的話, C# 只要加個 unsafe 就行了...
__nogc 在 MC++ 裡就是 unmanaged 的意思, 可以不用寫出來.
剛才說了, unmanaged code 會被 compile 成 native code, C# 的 unsafe
仍然是 managed code.
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 61.219.78.128
> -------------------------------------------------------------------------- <
作者: 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
> -------------------------------------------------------------------------- <
作者: freaky (jon) 看板: C_Sharp
標題: Re: C#的視窗程式設計
時間: Wed Nov 13 09:52:34 2002
※ 引述《Action (雪...)》之銘言:
: ※ 引述《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,
: 那可不可以請教你, 它到底是怎麼做的呢?
顯然我們的討論沒有交集. 我從來沒有說過 managed code 不需要 PInvoke
Service 就能呼叫執行 native code. 但呼叫 native APIs 和 create COM
object 為什麼一定要用 managed code 來做? 這是為什麼 MC++ 設計上
要方便混用 managed 和 unmanaged code. 你自己引用的資料不也寫了,
可以利用 managed wrapper class 讓 managed code invoke unmanaged
method, 在 MC++ 裡這是建議的作法, 因為可以選擇性 expose method.
重點是, unmanaged code 呼叫 native function 難道需要 PInvoke
Service 嗎?
: : __nogc 在 MC++ 裡就是 unmanaged 的意思, 可以不用寫出來.
: : 剛才說了, unmanaged code 會被 compile 成 native code, C# 的 unsafe
: : 仍然是 managed code.
: 如我之前所說, C# 沒有產生 native code 的能力.
: 另外, 拿 gc 來當 managed code 和 native code 的區別並不妥當.
: 除此之外, C++ 還可以用 #pragma managed/unmanaged 來明示.
那個 keyword 不是我發明的, 只要不寫 __gc compiler 就會視為 unmanaged.
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 61.222.173.66