锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国内久久精品,亚洲一区二区不卡免费,亚洲人午夜精品免费http://www.shnenglu.com/mzty/category/695.html<br/> <br/> <a href = "http://www.shnenglu.com/mzty/archive/2007/03/02/19109.html"><font size = 5 color ="#00FFFF">{C++ 鍩虹}<font/></a> <a href = "http://www.shnenglu.com/mzty/archive/2007/08/13/29922.html"><font size = 5 color ="#00FFFF">{C++ 楂樼駭}<font/></a> <a href = "http://www.shnenglu.com/mzty/archive/2008/07/18/56532.html"><font size = 5 color ="#00FFFF">{C++/CLI}<font/></a> <a href = "http://www.shnenglu.com/mzty/archive/2008/07/18/56533.html"><font size = 5 color ="#00FFFF">{VC Team涓枃}<font/></a> <a href = "http://www.shnenglu.com/mzty/archive/2007/04/16/22064.html"><font size = 5 color ="#00FFFF">{C#鐣岄潰錛孋++鏍稿績綆楁硶}<font/></a> <a href = "http://www.shnenglu.com/mzty/archive/2007/03/04/19163.html"><font size = 5 color ="#00FFFF">{璁捐妯″紡}<font/></a> <a href = " http://www.shnenglu.com/mzty/archive/2007/03/04/19167.html"><font size = 5 color ="#FF0000">{C#鍩虹}<font/></a> zh-cnSat, 19 Jul 2008 00:57:57 GMTSat, 19 Jul 2008 00:57:57 GMT60瓚呰秺 C++ 涓嬩竴浠?C++ 鈥?C++/CLI綆浠嬶紙杞澆錛?/title><link>http://www.shnenglu.com/mzty/archive/2008/07/18/56536.html</link><dc:creator>姊﹀湪澶╂動</dc:creator><author>姊﹀湪澶╂動</author><pubDate>Fri, 18 Jul 2008 14:28:00 GMT</pubDate><guid>http://www.shnenglu.com/mzty/archive/2008/07/18/56536.html</guid><wfw:comment>http://www.shnenglu.com/mzty/comments/56536.html</wfw:comment><comments>http://www.shnenglu.com/mzty/archive/2008/07/18/56536.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/mzty/comments/commentRss/56536.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mzty/services/trackbacks/56536.html</trackback:ping><description><![CDATA[<span id="hvzpftn" class=a14c id=zoom> <br> <p><strong>涓銆佺華璁?/strong> <p>褰撳井杞帹鍑篤S.NET7瀹炵幇浜嗗彲鎵╁睍鐨勬墭綆++鍚庯紝C++紼嬪簭鍛樹滑鍙嶆槧涓嶄竴銆傚敖綆″ぇ閮ㄥ垎鐨勭▼搴忓憳瀵逛簬鑳藉緇х畫浣跨敤C++鎰熷埌寰堟鎱幫紝浣嗗嚑涔庢墍鏈夌殑浜哄浜庢墭綆++鎻愪緵鐨勬櫐娑╄娉曟劅鍒板緢鐥涜嫤銆傚井杞槑鏄句粠鍙嶉涓劅瑙夊埌鎵樼C++涓嶆槸閭d箞鎴愬姛銆? <p>2003騫?0鏈?鏃ワ紝ECMA錛堟媧茶綆楁満鍒墮犲晢鍗忎細錛夊甯冩垚绔嬩笓瀹剁粍錛岃礋璐g粨鍚圛SO鏍囧噯C++涓庨氱敤璇█錛屽紑鍙戜竴涓彲鎵╁睍璇█鐨勬爣鍑嗭紝榪欎釜鏂扮殑鍙墿灞曡璦琚О涓篊++/CLI鏍囧噯銆傝繖涓爣鍑嗗皢琚玍S.NET2005鐨凜++緙栬瘧鍣ㄦ敮鎸併? <p><strong>浜屻佽佽娉曞瓨鍦ㄧ殑闂</strong> <p>1銆佹櫐娑╃箒鐞愮殑璇硶鍜屾枃娉?-榪欎袱涓?鍙岄噸搴曠嚎"闂鍔犻噸浜嗛槄璇葷殑璐熸媴銆? <p>2銆佷簩嫻佺殑CLI鏀寔--鐩稿涓嶤#涓嶸B.NET錛孧C++浣跨敤涓嶆柟渚跨殑宸ヤ綔鍖烘潵鎻愪緵CLI鏀寔錛屼緥濡傦紝瀹冩病鏈変竴涓竴涓瀵瑰簲鐨勭粨鏋勬潵鍒椾婦.NET鐨勯泦鍚堛? <p>3銆丆++涓?NET綺楅檵鍦扮粨鍚?-瀵逛簬CLI綾誨瀷錛屼綘涓嶈兘浣跨敤C++鐨勭壒鑹詫紝渚嬪妯℃澘錛涘悓鏍鳳紝瀵逛簬C++綾誨瀷錛屼綘涓嶈兘浣跨敤CLI鐨勭壒鑹詫紝渚嬪紕庣墖甯愰泦銆? <p>4銆佷護浜烘販娣嗙殑鎸囬拡--闈炴墭綆$殑C++鐨勬寚閽堝強鎵樼鐨勫紩鐢ㄦ寚閽堥兘浣跨敤*璇硶錛岃繖闈炲父浠や漢娣鋒穯錛屽洜涓?gc鎸囬拡涓庢墭綆℃寚閽堝湪鏈川鍜岃涓轟笂瀹屽叏涓嶅悓銆? <p>5銆丮FC緙栬瘧鍣ㄤ笉鑳戒駭鐢熷彲鏍¢獙鐨勪唬鐮併? <p><strong>涓夈丆++/CLI緇欐垜浠彁渚涗簡浠涔堬紵</strong> <p>1銆佷紭闆呮祦鐣呯殑璇硶鍜屾枃娉?-C++/CLI涓篊++寮鍙戜漢鍛樹功鍐欐墭綆′唬鐮佹彁渚涗簡涓縐嶉潪甯歌嚜鐒剁殑鎰熻錛屽茍涓斿畠鎻愪緵浜嗛潪鎵樼浠g爜鍒版墭綆′唬鐮佺殑騫蟲粦榪囧害銆備互鍓嶆墍璋撶殑"鍙岄噸搴曠嚎"闂鐜板湪宸茬粡鑽$劧鏃犲瓨銆? <p>2銆佷竴嫻佺殑CLI鏀寔--CLI鐗硅壊錛屼緥濡傚睘鎬с佺鐗囬泦鍚堝拰灞炵被寰楀埌浜嗙洿鎺ユ敮鎸侊紝姝ゅ錛孋++/CLI榪樺噯璁稿皢榪欎簺鐗硅壊鐢ㄤ簬鏈湴闈炴墭綆$殑綾匯? <p>3銆佷竴嫻佺殑C++綾繪敮鎸?-C++鐗硅壊錛屼緥濡傛ā鏉垮拰鏋愭瀯鍑芥暟瀵逛簬鎷栫鍜岄潪鎷栫綾葷戶緇湁鏁堛傚疄闄呬笂錛孋++/CLI鏄綘鍙互"琛ㄩ潰涓?鍦ㄦ爤鎴朇++鏈湴鍫嗕笂澹版槑涓涓?NET綾誨瀷鍞竴鐨?NET璇█銆? <p>4銆佸湪.NET涓嶤++涔嬮棿鐨勬矡澹戜笂鏋惰搗浜嗕竴搴фˉ姊?-C++寮鍙戜漢鍛樺湪鎶ㄥ嚮BCL鏃朵笉鍐嶈薄紱誨紑姘寸殑楸箋? <p>5銆丆++/CLI緙栬瘧鍣ㄤ駭鐢熺殑鍙墽琛屾枃浠跺畬鍏ㄦ槸鍙牎楠岀殑銆? <p><strong>鍥涖?Hello World"灝忕▼搴?/strong> <p><ccid_nobr> <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tbody> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <pre><ccid_code>using namespace System; void _tmain() { Console::WriteLine("Hello World"); }</ccid_code></pre> </td> </tr> </tbody> </table> </ccid_nobr> <p>涓婅堪浠g爜闄や簡涓嶉渶瑕佸紩鐢╩scorlib.dll搴撳錛屼笌鑰佺殑璇硶娌℃湁澶ぇ鐨勫尯鍒紝鍥犱負鏃犺浣犱粈涔堟椂鍊欎嬌鐢?clr榪涜緙栬緫錛岀紪璇戝櫒閮藉彲浠ユ殫涓繘琛屽紩鐢紙鐜板湪榛樿鐨勬槸/clr:newSyntax錛夈? <p><strong>浜斻佸彞鏌?/strong> <p>涓庤佺殑璇硶涓昏鐨勬販娣嗘槸鎴戜滑涔犳儻浜庝嬌鐢?絎﹀彿鏉ュ0鏄庢嫋綆″紩鐢ㄦ垨闈炴嫋綆℃寚閽堬紝鍦–++/CLI閲屽井杞紩鍏ヤ簡鍙ユ焺鐨勬蹇點? <p><ccid_nobr> <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tbody> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <pre><ccid_code>void _tmain() { //The ^ punctuator represents a handle String^ str = "Hello World"; Console::WriteLine(str); }</ccid_code></pre> </td> </tr> </tbody> </table> </ccid_nobr> <p>^絎﹀彿浠h〃涓涓墭綆″璞★紙澹版槑鏃剁湅涓婂幓璞′釜甯藉瓙錛夛紝鎸夌収CLI鐨勮瀹氾紝鍙ユ焺浠h〃涓涓嫋綆″璞$殑寮曠敤銆傚彞鏌勫湪CLI涓槸鏂扮殑璇硶錛岀浉褰撲簬C++涓殑-gc鎸囬拡銆傚彞鏌勪笌鎸囬拡涓嶅啀娣鋒穯錛屽湪鏈川涓婁袱鑰呭畬鍏ㄤ笉鍚屻? <p><strong>鍏佸彞鏌勪笌鎸囬拡鏄庢牱鍖哄垎寮鏉ョ殑錛?/strong> <p>1銆佹寚閽堝0鏄庢椂浣跨敤*絎﹀彿錛岃屽彞鏌勪嬌鐢╚絎﹀彿銆? <p>2銆佸彞鏌勬槸閽堝鎷栫鍫嗕笂瀵硅薄鐨勬嫋綆″紩鐢紝鑰屾寚閽堜粎浠呮寚鍚戝唴瀛樹腑鐨勪竴涓湴鍧銆? <p>3銆佹寚閽堝緢紼沖畾錛孏C寰幆涓嶄細褰卞搷鍒板畠錛涘彞鏌勫湪鍩轟簬GC鎴栧唴瀛樼揣寮犵殑鎯呭喌涓嬶紝鍙互鎸囧悜涓嶅悓鐨勫唴瀛樹綅緗? <p>4銆佸浜庢寚閽堬紝紼嬪簭寮鍙戜漢鍛樺繀欏?鏄懼紡"鍦板垹闄わ紝鍚﹀垯浼氶潰涓存硠闇茬殑鍗遍櫓錛岃屽浜庡彞鏌勶紝鏄惁榪涜鏄懼紡鍒犻櫎鍒欏畬鍏ㄦ牴鎹▼搴忎漢鍛樼殑鐖卞ソ浜嗐? <p>5銆佸彞鏌勪竴瀹氳鎸囧悜涓涓叿浣撶殑綾誨瀷錛屽嵆鎵璋撶殑綾誨瀷瀹夊叏鎬э紝鑰屾寚閽堟槑鏄句笉鏄繖鏍鳳紝浣犲喅涓嶅彲浠ュ皢涓涓彞鏌勬寚鍚慥oid^綾誨瀷銆? <p>6銆佹濡俷ew鎿嶄綔絎﹁繑鍥炰竴涓寚閽堜竴鏍鳳紝gcnew榪斿洖涓涓彞鏌勩? <p><strong>涓冦丆LR瀵硅薄紺轟緥</strong> <p><ccid_nobr> <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tbody> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <pre><ccid_code>void _tmain() { String^ str = gcnew String("Hello World"); Object^ o1 = gcnew Object(); Console::WriteLine(str); }</ccid_code></pre> </td> </tr> </tbody> </table> </ccid_nobr> <p>鍏抽敭瀛梘cnew鐢ㄦ潵瀹炰緥鍖栦竴涓狢LI瀵硅薄錛岃屼笖瀹冭繑鍥炰竴涓寚鍚戝湪CLR鍫嗕笂鐨勫璞$殑鍙ユ焺錛実cnew鐨勪紭鐐瑰湪浜庡畠鍙互鏂逛究鐨勮鎴戜滑鍖哄垎鎷栫鍜岄潪鎷栫鐨勫疄渚嬪璞°? <p>澶ч儴鍒嗘儏鍐典笅錛実cnew鍏抽敭瀛楀拰^鎿嶄綔絎︽彁渚涗簡浣犵敤鏉ヨ繘琛孊CL鐨勪竴鍒囨墜孌碉紝浣嗘槸寰堟槑鏄句綘闇瑕佸垱寤哄拰澹版槑灞炰簬鑷繁鐨勬嫋綆$被鍜屾帴鍙c? <p><strong>鍏佸0鏄庣被鍨?</strong> <p>CLR綾誨瀷鏈変竴涓艦瀹硅瘝鍓嶇紑鐢ㄦ潵璇存槑綾誨瀷鐨勭綾伙紝涓嬮潰鏄疌++/CLI涓殑綾誨瀷澹版槑紺轟緥錛? <p><ccid_nobr> <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tbody> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <pre><ccid_code>1銆?CLR types o Reference types § ref class RefClass; § ref struct RefClass; 2銆?Value types § value class ValClass; § value struct ValClass; o Interfaces § interface class IType; § interface struct IType; o Enumerations § enum class Color; § enum struct Color; 3銆?Native types o class Native; o struct Native; 紺轟緥錛? using namespace System; interface class IDog { void Bark(); }; ref class Dog : IDog { public: void Bark() { Console::WriteLine("Bow wow wow"); } }; void _tmain() { Dog^ d = gcnew Dog(); d->Bark(); }</ccid_code></pre> </td> </tr> </tbody> </table> </ccid_nobr> <p>涓婅堪紼嬪簭涓殑浠g爜涓庤佺殑C++璇█鐩告瘮鐪嬩笂鍘婚潪甯哥畝媧侊紝鍦ㄤ互寰鐨凜++浠g爜涓紝鑷沖皯瑕佺敤鍒?gc鍜?interface榪欎袱涓叧閿瘝銆? <p><strong>涔濄佽綆?鎷嗙鎿嶄綔</strong> <p>鍦–++/CLI涓紝鍔犵鏄殣鍚殑錛岃屼笖綾誨瀷鏄畨鍏ㄧ殑錛屼竴涓簩榪涘埗鐨勬嫹璐濊鎵ц騫跺湪CLR鍫嗕笂褰㈡垚涓涓璞★紝鍘葷鏄樉寮忕殑錛屼粎浠呴渶瑕佷嬌鐢╮einterpret_cast鎿嶄綔絎︽潵瑙i櫎寮曠敤銆? <p><ccid_nobr> <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tbody> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <pre><ccid_code>void _tmain() { int z = 44; Object^ o = z; //implicit boxing int y = *reinterpret_cast<int^>(o); //unboxing Console::WriteLine(" ",o,z,y); z = 66; Console::WriteLine(" ",o,z,y); } // 杈撳嚭緇撴灉濡備笅錛? // 44 44 44 // 44 66 44</ccid_code></pre> </td> </tr> </tbody> </table> </ccid_nobr> <p>鍦ㄤ笂榪頒唬鐮佷腑錛?o"瀵硅薄鏄竴涓姞綆辯殑鎷瘋礉錛屼粠絎簩涓鍙onsole::WriteLine.鐨勮緭鍑哄彲浠ュ緢鏄庢樉鍦扮湅鍒幫紝瀹冨茍娌℃湁娑夊強鍒癷nt綾誨瀷鐨勬暣鏁板箋? <p>褰撲綘瀵逛竴縐嶆暟鍊肩被鍨嬭繘琛屽姞綆辨搷浣滄椂錛岃繑鍥炵殑瀵硅薄璁頒綇浜嗘渶鍒濈殑鏁板肩被鍨嬨? <p><ccid_nobr> <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tbody> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <pre><ccid_code>void _tmain() { int z = 44; float f = 33.567; Object^ o1 = z; Object^ o2 = f; Console::WriteLine(o1->GetType()); Console::WriteLine(o2->GetType()); } // Output // System.Int32 // System.Single</ccid_code></pre> </td> </tr> </tbody> </table> </ccid_nobr> <p>鍥犳涓嶈兘瀵逛笉鍚岀被鍨嬬殑瀵硅薄榪涜鍘葷鎿嶄綔銆? <p><ccid_nobr> <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tbody> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <pre><ccid_code>void _tmain() { int z = 44; float f = 33.567; Object^ o1 = z; Object^ o2 = f; int y = *reinterpret_cast<int^>(o2);//System.InvalidCastException float g = *reinterpret_cast<float^>(o1);//System.InvalidCastException }</ccid_code></pre> </td> </tr> </tbody> </table> </ccid_nobr> <p>濡傛灉浣犻潪灝濊瘯榪欎箞鍋氾紝閭d箞浣犲皢寰楀埌涓涓猄ystem.InvalidCastException銆傝鎴戜滑鏉ユ帰璁ㄤ竴涓嬪畬緹庣殑綾誨瀷瀹夊叏鎬э紝濡傛灉浣犺鐪嬪唴閮ㄤ唬鐮侊紝浣犲皢鐪嬪埌寰蔣鐨勫唴閮ㄧ鍦ㄥ疄闄呬腑鐨勮繍鐢ㄣ備緥濡傦細 <p><ccid_nobr> <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tbody> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <pre><ccid_code>void Box2() { float y=45; Object^ o1 = y; }</ccid_code></pre> </td> </tr> </tbody> </table> </ccid_nobr> <p>緙栬瘧鍚庣殑浠g爜鏄細 <p><ccid_nobr> <table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tbody> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> <pre><ccid_code>.maxstack 1 .locals (float32 V_0, object V_1) ldnull stloc.1 ldc.r4 45. stloc.0 ldloc.0 box [mscorlib]System.Single stloc.1 ret</ccid_code></pre> </td> </tr> </tbody> </table> </ccid_nobr> <p>鏍規嵁寰蔣鐨勫唴閮ㄦ枃妗o紝綆辨搷浣滃皢鏈姞宸ョ殑綾誨瀷杞崲涓轟竴涓叿浣撶被鍨嬬殑瀹炰緥錛岃繖欏瑰伐浣滅殑瀹屾垚閫氳繃鍒涘緩涓涓柊鐨勫璞″茍灝嗘暟鎹嫹璐濆埌榪欎釜鏂板垎閰嶇殑瀵硅薄銆? <p><strong>鍗併佸啓鍦ㄥ悗闈㈢殑璇?/strong> <p>涓轟粈涔堝緢澶氫漢宸茬粡鍙互浣跨敤C銆丆++銆?NET鏉ュ紑鍙戠▼搴忎絾榪樺湪縐瀬瀛︿範C++/CLI鍛紝鎴戞兂鏈夊洓涓柟闈㈢殑鍘熷洜錛? <p>1銆佷粠緙栬瘧鍣ㄧ洿鍒板唴灞傞兘榪樺湪鏀寔C++浠g爜錛? <p>2銆丆++/CLI瀵逛簬鍏朵粬鏍囧噯鏉ヨ鏃犳剰鏄叿鏈夋瘉鐏у湴錛? <p>3銆佷笌鐢熶勘鏉ョ殑鍐呴儴鏀寔鑳滆繃鎵鏈夊叾浠朇LI璇█ <p>4銆佹墍鏈夊湪MFC涓嚭鐜扮殑涓嬪垝綰塊兘宸蹭笉鍐嶅瓨鍦ㄣ?/p> </span><img src ="http://www.shnenglu.com/mzty/aggbug/56536.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mzty/" target="_blank">姊﹀湪澶╂動</a> 2008-07-18 22:28 <a href="http://www.shnenglu.com/mzty/archive/2008/07/18/56536.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>C++/CLI 涓枃http://www.shnenglu.com/mzty/archive/2008/07/18/56532.html姊﹀湪澶╂動姊﹀湪澶╂動Fri, 18 Jul 2008 14:18:00 GMThttp://www.shnenglu.com/mzty/archive/2008/07/18/56532.htmlhttp://www.shnenglu.com/mzty/comments/56532.htmlhttp://www.shnenglu.com/mzty/archive/2008/07/18/56532.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/56532.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/56532.htmlC++/CLI涓枃鏂囩珷錛?br>

涓錛?br>
C++/CLI錛氱涓嫻佺殑CLI璇█(杞澆)

浜岋紝

姊﹀湪澶╂動 2008-07-18 22:18 鍙戣〃璇勮
]]>
C++/CLI錛氱涓嫻佺殑CLI璇█(杞澆)http://www.shnenglu.com/mzty/archive/2008/07/18/56531.html姊﹀湪澶╂動姊﹀湪澶╂動Fri, 18 Jul 2008 14:12:00 GMThttp://www.shnenglu.com/mzty/archive/2008/07/18/56531.htmlhttp://www.shnenglu.com/mzty/comments/56531.htmlhttp://www.shnenglu.com/mzty/archive/2008/07/18/56531.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/56531.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/56531.html 

C++/CLI錛氱涓嫻佺殑CLI璇█
2005-08-25 11:25 浣滆咃細 鏈卞厛蹇犵紪璇?鍑哄錛?澶╂瀬緗?璐d換緙栬緫錛氭柟鑸?

1. 綆浠?br>
銆銆鏈枃騫朵笉鏄負浜嗗鎵緾++/CLI鐨勮緣鐓岋紝涔熶笉鏄負浜嗚船浣庡叾瀹冨C#鎴栬匳B.NET絳夎璦錛岀浉鍙嶏紝榪欏彧鏄竴涓潪瀹樻柟鐨勩佷互涓涓枩嬈㈣繖縐嶈璦鐨勯潪寰蔣闆囧憳韜喚鏉ヨ璇丆++/CLI鏈夊畠鐨勮嚜宸辯殑鍞竴鐨勮鑹詫紝鍙綔涓虹涓嫻佺殑.NET緙栫▼璇█銆?br>
銆銆涓涓笉鏂湪鏂伴椈緇勫拰鎶鏈鍧涗笂鍑虹幇鐨勯棶棰樻槸錛屽綋璞#鍜孷B.NET榪欐牱鐨勮璦鏇撮傚悎浜庤繖縐嶇敤閫旀椂錛屼負浠涔堣浣跨敤C++鏉ュ紑鍙?NET搴旂敤杞歡銆傞氬父榪欐牱涓浜涢棶棰樺悗闈㈢殑璇勮璇存槸錛孋++璇硶鏄庢牱鐨勫鏉傚拰浠や漢璐硅В錛孋++鐜板湪鏄庢牱涓縐嶈繃鏃剁殑璇█錛岃繕鏈変粈涔圴S.NET璁捐鑰呭凡涓嶅啀鍍忔敮鎸丆#鍜孷B.NET涓鏍風戶緇敮鎸丆++銆傚叾涓竴浜涚寽鐤戞槸瀹屽叏鑽掕艾鐨勶紝浣嗘湁浜涜娉曢儴鍒嗘紜傚笇鏈涙湰鏂囨湁鍔╀簬婢勬竻鎵鏈夎繖浜涘洿緇旵++/CLI璇█鍙婂叾鍦╒S.NET璇█灞傛涓殑鍦頒綅鐨勭枒鎯戯紝紲炵鍜屼笉淇′換銆傝璁頒綇錛屾湰浣滆呮棦涓嶄負寰蔣宸ヤ綔涔熸病鏈変粠寰蔣閭i噷鍙栧緱鎶ラ叕錛屽彧鏄兂浠庢妧鏈笂瀵笴++/CLI浣滀竴璇勫垽銆?br>
銆銆2. 蹇熺畝媧佺殑鏈満interop

銆銆闄や簡P/Invoke鏈哄埗鍙敤鍦ㄥ彟澶栫殑璞#鎴朧B.NET榪欐牱鐨勮璦澶栵紝C++鎻愪緵浜嗕竴縐嶇嫭鏈夌殑interop鏈哄埗錛岀О浣淐++ interop銆侰++ interop姣擯/Invoke鐩磋寰楀錛屽洜涓轟綘鍙槸綆鍗曞湴#include闇瑕佺殑澶存枃浠訛紝騫朵笌闇瑕佺殑搴撹繘琛岄摼鎺ュ氨鑳借薄鍦ㄦ湰鏈篊++涓竴鏍瘋皟鐢ㄤ換浣曞嚱鏁般傚彟澶栵紝瀹冩瘮P/Invoke閫熷害蹇?-榪欐槸寰堝鏄撹兘璇佹槑鐨勩傜幇鍦紝鍙簤杈╃殑鏄湪瀹為檯搴旂敤杞歡鐨勫紑鍙戜腑錛岀粡鐢盋++ interop鑾峰緱鐨勬ц兘濂藉涓庤姳鍦ㄧ敤鎴鋒帴鍙d氦浜掋佹暟鎹簱瀛樺彇銆?a class=bluekey target=_blank>緗戠粶鏁版嵁杞偍銆佸鏉傛暟瀛︾畻娉曠瓑鏂歸潰鐨勬椂闂寸浉姣斿彲浠ヨ蹇界暐錛屼絾鏄簨瀹炴槸鍦ㄦ湁浜涙儏鍐典笅錛岀敋鑷抽氳繃姣忔interop璋冪敤鑺傜渷鐨勫嚑涓撼縐掍篃鑳界粰鍏ㄥ眬搴旂敤紼嬪簭鎬ц兘/鍝嶅簲閫犳垚宸ㄥぇ褰卞搷錛岃繖鏄粷瀵逛笉鑳借蹇借鐨勩備笅闈㈡湁涓ら儴鍒嗕唬鐮佺墖鏂?涓涓槸浣跨敤P/Invoke鏈哄埗鐨凜#紼嬪簭錛屼竴涓槸浣跨敤C++ Interop鏈哄埗鐨凜++紼嬪簭)錛屾垜鍒嗗埆璁板綍浜嗗叾鍚勮嚜浠g爜閲嶅鎵ц娑堣楃殑鏃墮棿(姣)銆備笉綆′綘濡備綍瑙i噴榪欎簺鏁版嵁錛屼笉綆¤繖浼氬浣犵殑搴旂敤紼嬪簭浜х敓浠涔堝獎鍝嶏紝鍏ㄦ槸浣犵殑浜嬨傛垜浠呮墦綆椾簨瀹炴у湴鎸囧嚭錛孋++浠g爜鐨勬墽琛岄熷害瑕佹瘮C#(鍏朵腑浣跨敤浜嗚緝澶氱殑鏈満interop璋冪敤)蹇?br>
銆銆1) C#紼嬪簭(浣跨敤P/Invoke)

[SuppressUnmanagedCodeSecurity]
[DllImport("kernel32.dll")]
static extern uint GetTickCount();
[SuppressUnmanagedCodeSecurity]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint GetWindowsDirectory(
[Out] StringBuilder lpBuffer, uint uSize);
static void Test(int x)
{
StringBuilder sb = new StringBuilder(512);
for (int i = 0; i < x; i++)
GetWindowsDirectory(sb, 511);
}
static void DoTest(int x)
{
uint init = GetTickCount();
Test(x);
uint tot = GetTickCount() - init;
Console.WriteLine("Took {0} milli-seconds for {1} iterations",tot, x);
}
static void Main(string[] args)
{
DoTest(50000);DoTest(500000);DoTest(1000000);DoTest(5000000);
Console.ReadKey(true);
}

銆銆2) C++紼嬪簭(浣跨敤C++ Interop)

void Test(int x)
{
TCHAR buff[512];
for(int i=0; i<x; i++)
GetWindowsDirectory(buff, 511);
}
void DoTest(int x)
{
DWORD init = GetTickCount();
Test(x);
DWORD tot = GetTickCount() - init;
Console::WriteLine("Took {0} milli-seconds for {1} iterations",tot, x);
}
int main(array<System::String ^> ^args)
{
DoTest(50000);DoTest(500000);DoTest(1000000);DoTest(5000000);
Console::ReadKey(true);
return 0;
}

銆銆3) 閫熷害姣旇緝

閲嶅嬈℃暟 C# 紼嬪簭 C++紼嬪簭
50,000 61 10
500,000 600 70
1,000,000 1162 140
5,000,000 6369 721
銆銆
銆銆鍏舵ц兘宸埆鐪熸槸浠や漢鎯婃剷錛佽繖鐨勭‘鏄鏄庝負浠涔堣浣跨敤C++/CLI鐨勪竴涓ソ鐞嗙敱錛屽鏋滀綘鍦ㄤ嬌鐢ㄦ湰鏈篿nterop榪涜寮鍙戯紝閭d箞鎬ц兘錛佸畬鍏ㄧ敱浜庢ц兘錛屾垜灝卞皢琚揩鍊熷姪鏈満interop鏉ュ疄鐜板茍闈炲熀浜巜eb鐨?NET搴旂敤紼嬪簭銆傚綋鐒訛紝涓轟粈涔堟垜鎯寵浣跨敤.NET鏉ュ紑鍙戦渶瑕佸ぇ閲忔湰鏈篿nterop鎶鏈殑搴旂敤紼嬪簭瀹屽叏鏄彟澶栦竴涓棶棰樸?br>
銆銆濡傛灉浣犱粛鎬鐤戣繖縐嶆ц兘浼樺娍錛屾湁鍙﹀鐨勭悊鐢辨潵璇存槑浣犱負浠涔堜笉寰椾笉浣跨敤C++/CLI鑰屼笉鏄疌#鎴朧B.NET鈥斺旀簮鐮佽啫鑳錛佷笅闈㈡槸涓涓狢++鍑芥暟鐨勪緥瀛愶紝瀹冧嬌鐢ㄤ簡IP甯姪鑰匒PI鏉ユ灇涓句竴鍙版満鍣ㄤ笂鐨勭綉緇滈傞厤鍣ㄥ茍涓斿垪鍑轟笌姣忎釜閫傞厤鍣ㄧ浉鑱旂郴鐨勬墍鏈塈P鍦板潃銆?br>
銆銆4) 鏋氫婦n/w閫傞厤鍣ㄧ殑C++浠g爜

void ShowAdapInfo()
{
PIP_ADAPTER_INFO pAdapterInfo = NULL;
ULONG OutBufLen = 0;
//寰楀埌闇瑕佺殑緙撳啿鍖哄ぇ灝?br>if(GetAdaptersInfo(NULL,&OutBufLen)==ERROR_BUFFER_OVERFLOW)
{
int divisor = sizeof IP_ADAPTER_INFO;
#if _MSC_VER >= 1400
if( sizeof time_t == 8 ) divisor -= 8;
#endif
pAdapterInfo = new IP_ADAPTER_INFO[OutBufLen/divisor];
//鍙栧緱閫傞厤鍣ㄤ俊鎭?br>if( GetAdaptersInfo(pAdapterInfo, &OutBufLen) != ERROR_SUCCESS )
{//璋冪敤澶辮觸 }
else
{
int index = 0;
while(pAdapterInfo)
{
Console::WriteLine(gcnew String(pAdapterInfo->Description));
Console::WriteLine("IP Address list : ");
PIP_ADDR_STRING pIpStr = &pAdapterInfo->IpAddressList;
while(pIpStr)
{
Console::WriteLine(gcnew tring(pIpStr->IpAddress.String));
pIpStr = pIpStr->Next;
}
pAdapterInfo = pAdapterInfo->Next;
Console::WriteLine();
}
}
delete[] pAdapterInfo;
}
}

銆銆鐜板湪璁╂垜浠湅涓涓嬌鐢≒/Invoke鐨凜#鐗堟湰銆?br>
銆銆5) 浣跨敤P/Invoke鎶鏈殑C#鐗堟湰

const int MAX_ADAPTER_NAME_LENGTH = 256;
const int MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
const int MAX_ADAPTER_ADDRESS_LENGTH = 8;
const int ERROR_BUFFER_OVERFLOW = 111;
const int ERROR_SUCCESS = 0;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct IP_ADDRESS_STRING
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string Address;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct IP_ADDR_STRING
{
public IntPtr Next;
public IP_ADDRESS_STRING IpAddress;
public IP_ADDRESS_STRING Mask;
public Int32 Context;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct IP_ADAPTER_INFO
{
public IntPtr Next;
public Int32 ComboIndex;
[MarshalAs(UnmanagedType.ByValTStr,
SizeConst = MAX_ADAPTER_NAME_LENGTH + 4)]
public string AdapterName;
[MarshalAs(UnmanagedType.ByValTStr,
SizeConst = MAX_ADAPTER_DESCRIPTION_LENGTH + 4)]
public string AdapterDescription;
public UInt32 AddressLength;
[MarshalAs(UnmanagedType.ByValArray,
SizeConst = MAX_ADAPTER_ADDRESS_LENGTH)]
public byte[] Address;
public Int32 Index;
public UInt32 Type;
public UInt32 DhcpEnabled;
public IntPtr CurrentIpAddress;
public IP_ADDR_STRING IpAddressList;
public IP_ADDR_STRING GatewayList;
public IP_ADDR_STRING DhcpServer;
public bool HaveWins;
public IP_ADDR_STRING PrimaryWinsServer;
public IP_ADDR_STRING SecondaryWinsServer;
public Int32 LeaseObtained;
public Int32 LeaseExpires;
}
[DllImport("iphlpapi.dll", CharSet = CharSet.Ansi)]
public static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref int pBufOutLen);
static void ShowAdapInfo()
{
int OutBufLen = 0;
//寰楀埌闇瑕佺殑緙撳啿鍖哄ぇ灝?br>if( GetAdaptersInfo(IntPtr.Zero, ref OutBufLen) ==
ERROR_BUFFER_OVERFLOW )
{
IntPtr pAdapterInfo = Marshal.AllocHGlobal(OutBufLen);
//鍙栧緱閫傞厤鍣ㄤ俊鎭?br>if( GetAdaptersInfo(pAdapterInfo, ref OutBufLen) != ERROR_SUCCESS )
{ //璋冪敤澶辮觸浜?}
else{
while(pAdapterInfo != IntPtr.Zero)
{
IP_ADAPTER_INFO adapinfo =
(IP_ADAPTER_INFO)Marshal.PtrToStructure(
pAdapterInfo, typeof(IP_ADAPTER_INFO));
Console.WriteLine(adapinfo.AdapterDescription);
Console.WriteLine("IP Address list : ");
IP_ADDR_STRING pIpStr = adapinfo.IpAddressList;
while (true){
Console.WriteLine(pIpStr.IpAddress.Address);
IntPtr pNext = pIpStr.Next;
if (pNext == IntPtr.Zero)
break;
pIpStr = (IP_ADDR_STRING)Marshal.PtrToStructure(
pNext, typeof(IP_ADDR_STRING));
}
pAdapterInfo = adapinfo.Next;
Console.WriteLine();
}
}
Marshal.FreeHGlobal(pAdapterInfo);
}
}


3. 鏍堣涔夊拰紜畾鎬х殑鏋愭瀯

銆銆C++緇忕敱鏍堣涔夋ā浠跨粰浜嗘垜浠‘瀹氭х殑鏋愭瀯銆傜畝璦涔嬶紝鏍堣涔夋槸Dispose妯″紡鐨勮壇濂界殑璇硶鏇夸唬鍝併備絾鏄畠鍦ㄨ涔変笂姣擟# using鍧楄娉曟洿鐩磋浜涖傝鐪嬩笅鍒楃殑C#鍜孋++浠g爜孌碉紙閮藉仛涓鏍風殑浜嬫儏-榪炴帴涓や釜鏂囦歡鐨勫唴瀹瑰茍鎶婂畠鍐欏埌絎笁涓枃浠朵腑錛夈?br>
銆銆1) C#浠g爜--浣跨敤鍧楄涔?br>
public static void ConcatFilestoFile(String file1, String file2, String outfile)
{
String str;
try{
using (StreamReader tr1 = new StreamReader(file1))
{
using (StreamReader tr2 = new StreamReader(file2))
{
using (StreamWriter sw = new StreamWriter(outfile))
{
while ((str = tr1.ReadLine()) != null)
sw.WriteLine(str);
while ((str = tr2.ReadLine()) != null)
sw.WriteLine(str);
}
}
}
}
catch (Exception e)
{ Console.WriteLine(e.Message); }
}

銆銆2) C++浠g爜--鏍堣涔?br>
static void ConcatFilestoFile(String^ file1, String^ file2, String^ outfile)
{
String^ str;
try{
StreamReader tr1(file1);
StreamReader tr2(file2);
StreamWriter sw(outfile);
while(str = tr1.ReadLine())
sw.WriteLine(str);
while(str = tr2.ReadLine())
sw.WriteLine(str);
}
catch(Exception^ e)
{ Console::WriteLine(e->Message); }
}

銆銆C#浠g爜涓庣浉絳夌殑C++ 浠g爜鐩告瘮涓嶄粎鍏嶄笉浜嗗啑闀匡紝鑰屼笖using鍧楄娉曡紼嬪簭鍛樿嚜宸辨槑紜湴鎸囧畾浠栨兂鍦ㄥ摢鍎胯皟鐢―ispose錛坲sing鍧楃殑緇撴潫澶勶級錛岃屼嬌鐢–++/CLI鐨勬爤璇箟錛屽彧闇璁╃紪璇戝櫒浣跨敤甯歌鐨勮寖鍥磋鍒欐潵澶勭悊瀹冨嵆鍙備簨瀹炰笂錛岃繖浣垮緱鍦–#涓慨鏀逛唬鐮佹瘮鍦–++涓洿涔忓懗-浣滀負涓瀹炰緥錛岃鎴戜滑淇敼榪欎簺浠g爜浠ヤ究鍗充嬌浠呭瓨鍦ㄤ竴涓緭鍏ユ枃浠朵篃鑳藉垱寤鴻緭鍑烘枃浠躲傝鐪嬩笅闈慨鏀瑰悗鐨凜#鍜孋++浠g爜銆?br>
銆銆3) 淇敼鍚庣殑C#浠g爜

public static void ConcatFilestoFile(String file1, String file2, String outfile)
{
String str;
try{
using (StreamWriter sw = new StreamWriter(outfile))
{
try{
using (StreamReader tr1 = new StreamReader(file1))
{
while ((str = tr1.ReadLine()) != null)
sw.WriteLine(str);
}
}
catch (Exception) { }
using (StreamReader tr2 = new StreamReader(file2))
{
while ((str = tr2.ReadLine()) != null)
sw.WriteLine(str);
}
}
}
catch (Exception e){ }
}

銆銆鎶婇拡瀵筍treamWriter鐨剈sing鍧楁斁鍒伴《灞傞渶瑕侀噸鏂拌皟鏁磚sing鍧楃粨鏋?-榪欏湪涓婇潰鎯呭喌涓嬫樉鐒朵笉鏄釜澶ч棶棰橈紝浣嗘槸瀵逛簬瀹為檯寮鍙戜腑鐨勪慨鏀癸紝榪欏彲鑳芥槸鐩稿綋妯$硦鐨勪笖鏄撳鑷撮昏緫閿欒鐨勩?br>
銆銆4) 淇敼鍚庣殑C++浠g爜

static void ConcatFilestoFile(String^ file1, String^ file2, String^ outfile)
{
String^ str;
try{
StreamWriter sw(outfile);
try{
StreamReader tr1(file1);
while(str = tr1.ReadLine())
sw.WriteLine(str);
}
catch(Exception^){}
StreamReader tr2(file2);
while(str = tr2.ReadLine())
sw.WriteLine(str);
}
catch(Exception^){}
}

銆銆榪欐牱涓嶆槸姣斿湪C#涓殑鍋氭洿瀹規槗浜涘悧錛熸垜鎭板ソ鎶奡treamWriter澹版槑縐誨埌浜嗛《閮ㄥ茍澧炲姞浜嗕竴涓澶栫殑try鍧楋紝灝辮繖浜涖傜敋鑷沖浜庤薄鍦ㄦ垜鐨勭ず渚嬩唬鐮佺墖鏂腑鐨勭悙紕庝簨鎯咃紝濡傛灉鎵娑夊強鐨勫鏉傛у湪C++涓ぇ澶у噺灝戯紝閭d箞錛屽綋浣犲伐浣滀簬鏇村ぇ鐨勫伐紼嬫椂浣犺兘鎯寵薄浣跨敤鏍堣涔夊浣犵殑緙栫爜鏁堢巼鍗冧竾鐨勫獎鍝嶃?br>
銆銆榪樹笉紜俊錛熷ソ錛岃鎴戜滑鐪嬩竴涓嬫垚鍛樺璞″拰瀹冧滑鐨勬瀽鏋勫惂銆侷magine CLI GC綾籖1鍜孯2錛屼簩鑰呴兘瀹炵幇浜咺disposable鎺ュ彛涓旈兘鏈夊嚱鏁癋()錛岃繕鏈変竴涓狢LI GC綾籖錛屽畠鏈塕1鍜孯2鎴愬憳鍜屼竴涓嚱鏁癋()-瀹冨唴閮ㄥ湴璋冪敤R1鍜孯2涓婄殑F()鎴愬憳鍑芥暟銆傝鎴戜滑鍏堢湅C#瀹炵幇銆?br>
銆銆5) 涓涓猟isposable綾葷戶鎵垮眰嬈$殑C#瀹炵幇

class R1 : IDisposable{
public void Dispose() { }
public void F() { }
}
class R2 : IDisposable{
public void Dispose() { }
public void F() { }
}
class R : IDisposable{
R1 m_r1 = new R1();
R2 m_r2 = new R2();
public void Dispose()
{
m_r1.Dispose();
m_r2.Dispose();
}
public void F()
{
m_r1.F();
m_r2.F();
}
public static void CallR()
{
using(R r = new R())
{r.F();}
}
}

銆銆榪欓噷鏈夊嚑浠朵簨鎯呰鍋氾細蹇呴』涓烘瘡涓猟isposable 綾繪墜宸ュ疄鐜癐Disposable鎺ュ彛錛屽浜庡叿鏈夋垚鍛楻1鍜孯2鐨勭被R錛孌ispose鏂規硶涔熼渶瑕佽皟鐢ㄦ垚鍛樼被涓婄殑Dispose銆傜幇鍦ㄨ鎴戜滑鍒嗘瀽涓婇潰鍑犱釜綾葷殑C++瀹炵幇銆?br>
銆銆6) 絳変環鐨凜++瀹炵幇

ref class R1
{
public:
~R1(){}
void F(){}
};
ref class R2
{
public:
~R2(){}
void F(){}
};
ref class R
{
R1 m_r1;
R2 m_r2;
public:
~R(){}
void F()
{
m_r1.F();
m_r2.F();
}
static void CallR()
{
R r;
r.F();
}
};

銆銆娉ㄦ剰錛岃繖閲屼笉鍐嶆湁鎵嬪伐鐨処disposable鎺ュ彛瀹炵幇(鎴戜滑鐨勭被涓粎寤虹珛浜嗘瀽鏋勫櫒)鑰屼笖鏈濂界殑閮ㄥ垎--綾籖鐨勬瀽鏋勫櫒(Dispose鏂規硶)騫舵病鏈夊湪璇ョ被鍙兘鍚湁鐨勫彲閲婃斁鐨勬垚鍛樹笂璋冪敤Dispose-瀹冩病鏈夊繀瑕佽繖鏍峰仛錛岀紪璇戝櫒鑷姩涓轟箣鐢熸垚鎵鏈夌殑浠g爜錛?

4. 娣峰悎綾誨瀷

銆銆鎴戜滑鐭ラ亾錛孋++鏀寔鏈満綾誨瀷-鎬繪槸濡傛錛汣++鏀寔CLI綾誨瀷-鏈枃姝f槸鐗瑰埆寮鴻皟榪欎竴鐐癸紱瀹冭繕鏀寔娣峰悎綾誨瀷-鍏鋒湁CLI鎴愬憳鐨勬湰鏈虹被鍨嬪拰鍏鋒湁鏈満鎴愬憳鐨凜LI綾誨瀷錛佽灝界鑰冭檻鎵鏈変綘鑳界殑鍙兘闇姹傘?br>
銆銆娉ㄦ剰錛岃皥鍒癢hidbey錛屾販鍚堢被鍨嬪疄鐜拌繕涓嶅畬鏁達紱灝辨垜浠嶣randon錛孒erb鍜孯onald鍙戣〃鐨勬潗鏂欑殑鐞嗚В寰楃煡錛屽瓨鍦ㄨ繖縐嶇浉褰撻叿鐨勭被鍨?-緇熶竴妯″瀷錛屽畠灝嗗湪Orcas涓疄鐜?-浣犺兘澶熷湪鏈満C++鍫嗕笂new/delete CLI綾誨瀷錛岃屼笖涔熻兘澶熷湪CLI鍫嗕笂gcnew/delete鏈満綾誨瀷銆備絾鏃㈢劧榪欐槸Whidbey浠ュ悗鐨勪笢瑗匡紝鏈枃涓嶈璁虹粺涓妯″瀷銆?br>
銆銆鍦ㄦ垜璋堣浣犱綍鏃朵嬌鐢ㄦ販鍚堢被鍨嬩互鍓嶏紝鎴戞兂鍚戜綘璇存槑浠涔堟槸娣峰悎綾誨瀷銆傚鏋滀綘鐞嗚В娣峰悎綾誨瀷錛岃璺寵繃涓嬮潰鍑犳銆傝繖閲屽紩鐢˙randon Bray鐨勮娉曪細"涓縐嶆販鍚堢被鍨嬶紝鎴栬呮槸鏈満綾籸ef綾伙紙闇瑕佹湁瀵硅薄鎴愬憳錛夛紝鎴栬呮槸閫氳繃澹版槑鎴栫戶鎵胯鍒嗛厤鍦ㄥ瀮鍦懼洖鏀跺爢鎴栨湰鏈哄爢涓婄殑銆?鍥犳濡傛灉浣犳湁涓涓墭綆$被鍨嬫垨鑰呮湁涓涓湁鎵樼鎴愬憳鐨勬湰鏈虹被鍨嬶紝浣犲氨鏈変簡涓涓販鍚堢被鍨嬨俈C++ Whidbey涓嶇洿鎺ユ敮鎸佹販鍚堢被鍨?緇熶竴綾誨瀷妯″瀷鏄竴縐峎hidbey涔嬪悗鐨勬蹇?錛屼絾鏄畠緇欐垜浠垝瀹氫簡瀹炵幇娣峰悎綾誨瀷鐨勬潯浠躲傝鎴戜滑寮濮嬭璁哄寘鍚墭綆℃垚鍛樼殑鏈満綾誨瀷銆?br>
ref class R
{
public:
void F(){}
//鍋囧畾 non-trivial ctor/dtor
R(){}
~R(){}
};

銆銆鍦ㄦ垜鐨勪緥瀛愪腑錛岃鎯寵鎵樼綾誨瀷R鏈変竴涓猲on-trivial鏋勯犲櫒鍜屼竴涓猲on-trivial鏋愭瀯鍣ㄣ?br>
class Native
{
private:
gcroot<R^> m_ref;
public:
Native():
m_ref(gcnew R()){}
~Native()
{ delete m_ref; }
void DoF()
{ m_ref->F(); }
};

銆銆鏃㈢劧錛屾垜涓嶈兘鍦ㄦ垜鐨勭被涓嫢鏈変竴涓猂鎴愬憳錛屾垜浣跨敤浜唃croot妯℃澘綾?鍦╣croot.h涓0鏄庯紝浣嗘槸浣犺鐢?#include vcclr.h")錛屽畠鍖呰浜哠ystem::Runtime::InteropServices::GCHandle緇撴瀯銆傚畠鏄釜璞$被涓鏍風殑鐏墊晱鎸囬拡錛屽畠閲嶈澆浜嗚繍綆楃->浠ヨ繑鍥炵敤浣滄ā鏉垮弬鏁扮殑鎵樼綾誨瀷銆傚洜姝ゅ湪涓婇潰綾諱腑錛屾垜鍙互浣跨敤m_ref錛屽氨濂借薄鎴戝凡緇忓0鏄庡畠鏄疪^錛岃屼笖浣犺兘鍦―oF鍑芥暟涓湅鍒拌繖姝e湪璧蜂綔鐢ㄣ傚疄闄呬笂浣犲彲浠ヨ妭鐪乨elete錛岃繖鍙互閫氳繃浣跨敤auto_gcroot(綾諱技浜巗td::auto_ptr錛屽湪msclr\auto_gcroot.h鏂囦歡涓0鏄?浠f浛gcroot鏉ュ疄鐜般備笅闈㈡槸涓涓洿濂戒簺鐨勪嬌鐢╝uto_gcroot鐨勫疄鐜般?br>
class NativeEx
{
private:
msclr::auto_gcroot<R^> m_ref;
public:
NativeEx() : m_ref(gcnew R()){}
void DoF()
{ m_ref->F(); }
};

銆銆涓嬮潰璁╂垜浠湅鐩稿弽鐨勬儏褰細涓涓狢LI綾葷殑鏈満鎴愬憳銆?br>
ref class Managed
{
private:
Native* m_nat;
public:
Managed():m_nat(new Native()){ }
~Managed()
{ delete m_nat; }
!Managed()
{ delete m_nat;
#ifdef _DEBUG
throw gcnew Exception("Oh, finalizer got called!");
#endif
}
void DoF()
{ m_nat->DoF(); }
};

銆銆鎴戜笉鑳藉畾涔変竴涓狽ative瀵硅薄鏉ヤ綔涓轟竴涓猺ef綾繪垚鍛橈紝鍥犳闇瑕佷嬌鐢ㄤ竴涓狽ative*瀵硅薄鏉ヤ唬鏇褲傛垜鍦ㄦ瀯閫犲櫒涓璶ew璇ative瀵硅薄錛岀劧鍚庡湪鏋愭瀯鍣ㄥ拰finalizer涓璬elete瀹冦傚鏋滀綘榪愯璇ュ伐紼嬬殑璋冭瘯鐗堬紝鍦ㄦ墽琛屽埌finalizer鏃跺皢鎶涘嚭涓涓紓甯? 鍥犳寮鍙戣呭彲浠ラ┈涓婃坊鍔犱竴涓delete鐨勮皟鐢ㄦ垨涓轟粬鐨凜LI綾誨瀷浣跨敤鏍堣涔夋妧鏈傚鎬殑鏄紝搴撳紑鍙戝皬緇勬病鏈夊緩绔嬩竴涓猤croot鐨勫弽鍚戝疄鐜?浣嗚繖涓嶆槸涓ぇ闂錛屾垜浠彲浠ヨ嚜宸卞啓銆?br>
template<typename T> ref class nativeroot
{
T* m_t;
public:
nativeroot():m_t(new T){}
nativeroot(T* t):m_t(t){}
T* operator->()
{ return m_t; }
protected:
~nativeroot()
{ delete m_t; }
!nativeroot()
{
delete m_t;
#ifdef _DEBUG
throw gcnew Exception("Uh oh, finalizer got called!");
#endif
}
};

銆銆榪欎粎鏄釜鐩稿綋綆鍗曠殑鐏墊晱鎸囬拡瀹炵幇錛屽氨璞′竴涓礋璐f湰鏈哄璞″垎閰?鍥炴敹鐨剅ef綾匯備笉綆℃庢牱錛屽熷姪nativeroot妯℃澘綾伙紝鎴戜滑鍙互濡備笅淇敼鎵樼綾伙細

ref class ManagedEx
{
private:
nativeroot<Native> m_nat;
public:
void DoF()
{ m_nat->DoF(); }
};

銆銆濂斤紝鍏充簬娣峰悎綾誨瀷鐨勬渶澶ч棶棰樻槸浠涔堝憿錛熶綘鍙兘闂傛渶澶ч棶棰樻槸錛岀幇鍦ㄤ綘鑳芥販鍚堜嬌鐢ㄤ綘鐨凪FC銆丄TL銆乄TL銆丼TL浠g爜浠撳簱鍜?NET妗嗘灦錛屽茍鐢ㄥ彲鑳界殑鏈鐩存帴鐨勬柟寮?鍙渶鍐欎綘鐨勬販鍚堟ā寮忎唬鐮佸茍緙栬瘧瀹炵幇!浣犲彲浠ュ緩绔嬪湪涓涓狣LL搴撲腑寤虹珛MFC 綾伙紝鐒跺悗寤虹珛涓涓?NET搴旂敤紼嬪簭鏉ヨ皟鐢ㄨ繖涓狣LL錛岃繕闇瑕佹妸.NET綾繪垚鍛樻坊鍔犲埌浣犵殑MFC綾伙紙涔熷疄鐜板彲浠ョ浉鍙嶇殑鎯呭喌錛夈?br>
銆銆浣滀負涓渚嬶紝璁炬兂浣犳湁涓MFC瀵硅瘽妗?-瀹冮氳繃涓涓琛岀殑緙栬緫妗嗘帴鍙楁潵鑷敤鎴風殑鏁版嵁-鐜板湪錛屼綘鏈変竴鏂扮殑瑕佹眰-鏄劇ず涓涓彧璇葷紪杈戞錛屽畠灝嗘樉紺哄綋鍓嶅湪璇ュ琛岀紪杈戞涓枃鏈殑md5鍝堝笇緇撴灉銆備綘鐨勯槦鍙嬫鍦ㄦ偛鍙逛粬浠皢蹇呴』鑺辮垂鍑犱釜灝忔椂閽葷爺crypto API錛岃屼綘鐨勪笂鍙稿湪鎷呭咖浣犱滑鍙兘蹇呴』瑕佷拱涓涓涓夋柟鍔犲瘑搴擄紱閭f鏄綘鍦ㄤ粬浠潰鍓嶆爲绔嬪艦璞$殑鏃跺欙紝浣犲甯冧綘灝嗗湪15鍒嗛挓鍐呭仛瀹岃繖欏逛換鍔°備笅闈㈡槸瑙e喅鐨勫姙娉曪細

銆銆娣誨姞涓涓柊鐨勭紪杈戞鍒頒綘鐨勫璇濇璧勬簮涓紝騫朵笖娣誨姞鐩稿簲鐨凞DX鍙橀噺銆傞夋嫨/clr緙栬瘧妯″紡騫朵笖娣誨姞涓嬪垪浠g爜鍒頒綘鐨勫璇濇鐨勫ご鏂囦歡涓細

#include <msclr\auto_gcroot.h>
using namespace System::Security::Cryptography;

銆銆浣跨敤auto_gcroot妯℃澘鏉ュ0鏄庝竴涓狹D5CryptoServiceProvider鎴愬憳錛?br>
protected:
msclr::auto_gcroot<MD5CryptoServiceProvider^> md5;

銆銆鍦∣nInitDialog榪囩▼涓紝gcnew MD5CryptoServiceProvider鎴愬憳銆?br>
md5 = gcnew MD5CryptoServiceProvider();

銆銆騫朵笖涓哄琛岀紪杈戞娣誨姞涓涓狤N_CHANGE澶勭悊鍣細

void CXxxxxxDlg::OnEnChangeEdit1()
{
using namespace System;
CString str;
m_mesgedit.GetWindowText(str);
array<Byte>^ data = gcnew array<Byte>(str.GetLength());
for(int i=0; i<str.GetLength(); i++)
data[i] = static_cast<Byte>(str[i]);
array<Byte>^ hash = md5->ComputeHash(data);
CString strhash;
for each(Byte b in hash)
{
str.Format(_T("%2X "),b);
strhash += str;
}
m_md5edit.SetWindowText(strhash);
}

銆銆榪欓噷浣跨敤浜嗘販鍚堢被鍨嬶細涓涓湰鏈篊dialog媧劇敓綾伙紝璇ョ被鍚湁涓涓狹D5CryptoServiceProvider鎴愬憳(CLI綾誨瀷)銆備綘鍙互杞繪槗鍦拌瘯楠岀浉鍙嶇殑鎯呭喌錛堝鏃╂湡鐨勪唬鐮佺墖鏂凡鏄劇ず鐨勶級鈥斺斿彲浠ュ緩绔嬩竴涓猈indows琛ㄥ崟搴旂敤紼嬪簭鑰屼笖鍙兘鎯沖埄鐢ㄤ竴涓湰鏈虹被搴?-榪欎笉鎴愰棶棰橈紝浣跨敤涓婇潰瀹氫箟鐨勬ā鏉縩ativeroot鍗沖彲銆?br>
5. 鎵樼妯℃澘

銆銆涔熻浣犲娉涘瀷鐨勬蹇靛凡寰堟竻妤氫簡錛屽畠甯姪浣犻伩鍏嶈繘鍏++鐨勬ā鏉挎ⅵ欖囷紝瀹冩槸瀹炵幇妯℃澘鐨勬渶浣蟲柟寮忥紝絳夌瓑銆傚ソ錛屽亣璁捐繖浜涘叏閮ㄦ紜紝C++/CLI鏀寔娉涘瀷灝辮薄浠諱綍鍏跺畠CLI璇█涓鏍?浣嗘槸瀹冩湁鑰屽叾瀹冧竴浜汣LI璇█榪樻病鏈夌殑鏄畠榪樻敮鎸佹墭綆℃ā鏉?涔熷氨鏄ā鏉垮寲鐨剅ef鍜寁alue綾匯傚鏋滀綘浠ュ墠浠庢湭浣跨敤榪囨ā鏉匡紝浣犱笉鑳戒竴涓嬫璧忚繖涔堝浼樼偣錛屼絾鏄鏋滀綘鏈夋ā鏉夸嬌鐢ㄨ儗鏅屼笖浣犲凡鍙戠幇浜嗘硾鍨嬩腑瀛樺湪鐨勫彲鑳介檺鍒朵綘緙栫爜鐨勬柟寮忥紝鎵樼妯℃澘灝嗕細澶уぇ鍑忚交浣犵殑璐熸媴銆備綘鑳借仈鍚堜嬌鐢ㄦ硾鍨嬪拰妯℃澘- 浜嬪疄涓婃湁鍙兘鐢ㄤ竴涓墭綆$被鍨嬬殑妯℃澘鍙傛暟鏉ュ疄渚嬪寲涓涓硾鍨嬬被鍨?灝界鐩稿弽鐨勬儏褰㈡槸涓嶅彲鑳界殑錛屽洜涓鴻繍琛屾椂鍒誨疄渚嬪寲鐢辨硾鍨嬫墍鐢?銆係TL.NET (鎴朣TL/CLR)浠ュ悗璁ㄨ錛岃寰堝ソ鍦板埄鐢ㄦ硾鍨嬪拰鎵樼妯℃澘鐨勬販鍚堢紪紼嬪惂銆?br>
銆銆娉涘瀷浣跨敤鐨勫瓙綾誨瀷綰︽潫鏈哄埗灝嗛槻姝綘鍐欏嚭涓嬮潰鐨勪唬鐮侊細

generic<typename T> T Add(T t1, T t2)
{ return t1 + t2; }

銆銆緙栬瘧閿欒錛?br>
error C2676: binary ’+’ : ’T’ does not define this operator or a conversion to a type acceptable to the predefined operator

銆銆鐜板湪璇風湅鐩稿簲鐨勬ā鏉跨増鏈細

template<typename T> T Add(T t1, T t2)
{ return t1 + t2; }

銆銆閭d箞灝卞彲浠ヨ繖鏍峰仛錛?br>
int x1 = 10, x2 = 20;
int xsum = Add<int>(x1, x2);

銆銆榪樺彲浠ヨ繖鏍峰仛錛?br>
ref class R
{
int x;
public:
R(int n):x(n){}
R^ operator+(R^ r)
{ return gcnew R(x + r->x); }
};
//...
R^ r1 = gcnew R(10);
R^ r2 = gcnew R(20);
R^ rsum = Add<R^>(r1, r2);

銆銆榪欏湪涓涓薄int鐨勬湰鏈虹被鍨嬩互鍙婁竴涓猺ef綾誨瀷(鍙ref綾誨瀷鏈変竴涓?榪愮畻絎?鎯呭喌涓嬮兘鑳藉伐浣滆壇濂姐傝繖涓硾鍨嬬己鐐逛笉鏄竴涓皟璇曢敊璇垨緙洪櫡-瀹冩槸璁捐閫犳垚鐨勩傛硾鍨嬬殑瀹炰緥鍖栨槸鍦ㄨ繍琛屾椂閫氳繃璋冪敤閰嶄歡闆嗗疄鐜扮殑錛屽洜姝ょ紪璇戝櫒涓嶈兘紜煡涓鐗瑰畾鎿嶄綔鑳借鏂借浜庝竴涓硾鍨嬪弬鏁幫紝闄ら潪瀹冨尮閰嶄竴涓瓙綾誨瀷綰︽潫錛屽洜姝ょ紪璇戝櫒鍦ㄥ畾涔夋硾鍨嬫椂瑙e喅榪欎釜闂銆傚綋浣犱嬌鐢ㄦ硾鍨嬫椂鐨勫彟澶栦竴涓Θ紕嶆槸錛屽畠涓嶄細鍏佽浣犱嬌鐢ㄩ潪綾誨瀷鍙傛暟銆備笅鍒楁硾鍨嬬被瀹氫箟涓嶄細緙栬瘧錛?br>
generic<typename T, int x> ref class G{};

銆銆緙栬瘧閿欙細

error C2978: syntax error : expected ’typename’ or ’class’; found type ’int’; non-type parameters are not supported in generics

銆銆涓庢墭綆℃ā鏉跨浉姣旇緝錛?br>
template<typename T, int x = 0> ref class R{};

銆銆濡傛灉浣犲紑濮嬫劅嬋C++鍚戜綘鎻愪緵浜嗘硾鍨嬪拰鎵樼妯℃澘錛岄偅涔堣鐪嬩笅闈㈣繖涓涓緥瀛愶細

template<typename T> ref class R{
public:
void F()
{ Console::WriteLine("hey"); }
};
template<> ref class R<int>
{
public:
void F()
{ Console::WriteLine("int"); }
};

銆銆浣犱笉鑳界敤娉涘瀷榪欐牱緙栫爜錛涘惁鍒欙紝灝嗕駭鐢燂細

銆銆緙栬瘧閿欙細error C2979: explicit specializations are not supported in generics

銆銆浣嗗彲浠ュ湪緇ф壙閾句腑娣峰悎浣跨敤妯℃澘鍜屾硾鍨嬶細

generic<typename T> ref class Base
{
public:
void F1(T){}
};
template<typename T> ref class Derived : Base<T>
{
public:
void F2(T){}
};
//...
Derived<int> d;
d.F1(10);
d.F2(10);

銆銆鏈鍚庯紝浣犱笉鑳戒粠涓涓硾鍨嬪弬鏁扮被鍨嬫淳鐢熶竴涓硾鍨嬬被銆?br>
銆銆涓嬪垪浠g爜涓嶄細鎴愬姛緙栬瘧錛?br>
generic<typename T> ref class R : T
{};

銆銆error C3234: a generic class may not derive from a generic type parameter

銆銆妯℃澘璁╀綘榪欐牱鍋?濂藉儚浣犺繕涓嶇煡閬撹繖浜?錛?br>
ref class Base{
public:
void F(){}
};
generic<typename T> ref class R : T
{};
//...
R<Base> r1;
r1.F();

銆銆榪欐牱錛屽綋浣犱笅嬈¢亣鍒板娉涘瀷鐨勮船璋ゆ椂錛屼綘灝辯煡閬撹鎬庝箞鍋氫簡銆?br>
銆銆6. STL/CLR

銆銆褰撳ぇ閲忎嬌鐢⊿TL鐨凜++寮鍙戣呰漿鍚?NET1/1.1鏃朵竴瀹氭劅瑙夐潪甯稿埆鎵紝浠栦滑涓殑璁稿鍙兘浼氭斁寮冨茍杞洖鍒板師鏉ョ殑鏈満緙栫爜銆備粠鎶鏈笂璁詫紝浣犺兘緇撳悎.NET綾誨瀷(using gcroot)浣跨敤鏈満STL錛屼絾鏄駭鐢熺殑緇撴灉浠g爜鍙兘鐩稿綋浣庢晥錛屾洿涓嶇敤璇存槸涓戦檵浜嗭細

std::vector< gcroot<IntPtr> >* m_vec_hglobal;
//...
for each(gcroot<IntPtr> ptr in *m_vec_hglobal)
{ Marshal::FreeHGlobal(ptr);}

銆銆澶фVC++灝忕粍鑰冭檻鍒頒簡榪欎簺騫跺喅瀹氬湪Whidbey浠ュ悗錛屼粬浠細鎻愪緵STL.NET錛堟垨STL/CLR錛夊茍鍙互鍗曠嫭浠庣綉涓婁笅杞姐?br>
銆銆浣犲彲鑳介棶涓轟粈涔堬紵Stan Lippman錛屽湪浠栫殑MSDN鏂囩珷(STL.NET Primer)涓粰鍑轟簡3鏉″師鍥狅細

銆銆·鍙墿灞曟?-STL璁捐鎶婄畻娉曞拰瀹瑰櫒闅旂鍒拌嚜宸辯殑搴旂敤絀洪棿-涔熷氨鏄綘鍙互鏈変竴緇勫鍣ㄥ拰涓緇勭畻娉曪紝騫朵笖浣犺兘鍦ㄤ換浣曚竴涓鍣ㄤ笂浣跨敤榪欎簺綆楁硶錛涘悓鏃朵綘鑳藉湪浠諱綍涓涓畻娉曚腑浣跨敤榪欎簺瀹瑰櫒銆傚洜姝わ紝濡傛灉浣犳坊鍔犱竴縐嶆柊鐨勭畻娉曪紝浣犺兘鍦ㄤ換浣曚竴縐嶅鍣ㄤ腑浣跨敤瀹冿紱鍚屾牱錛屼竴涓柊鐨勫鍣ㄤ篃鍙互涓庣幇鏈夌畻娉曢厤鍚堜嬌鐢ㄣ?br>
銆銆·緇熶竴鎬?-鎵鏈夋牳蹇僀++寮鍙戣呴泦涓湪涓璧鳳紝姹囬泦璧蜂粬浠簿濡欑殑STL涓撻暱錛屽啀浣跨敤浠栦滑鐨勪笓闀垮垯杞昏濺鐔熻礬銆傝杈冨ソ鍦頒嬌鐢⊿TL闇瑕佽姳璐規椂闂?鐒惰屼竴鏃︿綘鎺屾彙浜嗗畠錛屼綘灝辨湁浜嗗湪.NET涓栫晫涓嬌鐢ㄤ綘鐨勬妧宸х殑鏄庢樉浼樺娍銆備笉鏄悧錛?br>
銆銆·鎬ц兘--STL.NET閫氳繃浣跨敤瀹炵幇娉涘瀷鎺ュ彛鐨勬墭綆℃ā鏉垮疄鐜般傚茍涓旀棦鐒跺畠鐨勬牳蹇冨凡鐢–++鍜屾墭綆℃ā鏉跨紪鐮侊紝鍙互鏈熺浖瀹冩瘮鍦˙CL涓婁嬌鐢ㄧ殑娉涘瀷瀹瑰櫒鏇村叿鏈夋ц兘浼樺娍銆?br>
銆銆浣跨敤榪嘢TL鐨勪漢涓嶉渶瑕佷換浣曠ず鑼冿紝鎵浠ヤ笅闈唬鐮佹湁鐩婁簬浠ュ墠娌℃湁浣跨敤榪嘢TL鐨勪漢銆?br>
vector<String^> vecstr;
vecstr.push_back("wally");
vecstr.push_back("nish");
vecstr.push_back("smitha");
vecstr.push_back("nivi");
deque<String^> deqstr;
deqstr.push_back("wally");
deqstr.push_back("nish");
deqstr.push_back("smitha");
deqstr.push_back("nivi");

銆銆鎴戜嬌鐢ㄤ簡涓や釜STL.NET瀹瑰櫒-vector鍜宒eque錛屽茍瑁呮弧涓や釜瀹瑰櫒錛屼嬌鍏剁湅璧鋒潵鐩稿悓(鍦ㄤ袱涓鍣ㄤ腑閮戒嬌鐢ㄤ簡push_back)銆傜幇鍦紝鎴戝皢鍦ㄤ袱涓鍣ㄤ笂浣跨敤replace綆楁硶-鎴戜滑鍐嶆鐪嬪埌錛岃繖浜涗唬鐮佹槸寰堢浉鍚岀殑銆?br>
replace(vecstr.begin(), vecstr.end(),
gcnew String("nish"), gcnew String("jambo"));
replace(deqstr.begin(), deqstr.end(),
gcnew String("nish"), gcnew String("chris"));

銆銆榪欓噷鐗瑰埆瑕佹敞鎰忕殑鏄垜浣跨敤浜?鍚屾牱"鐨勭畻娉?-replace騫跺湪涓や釜涓嶅悓STL瀹瑰櫒涓婁嬌鐢ㄧ浉鍚岀殑鍑芥暟璋冪敤銆傝繖鏄綋Stan璋堝強"鍙墿灞曟?鏃剁殑鎰忔濄備笅闈㈡垜鐢ㄤ竴涓畝鍗曞嚱鏁版潵璇佹槑錛?br>
template<typename ForwardIterator> void Capitalize(
ForwardIterator first錛孎orwardIterator end)
{
for(ForwardIterator it = first; it < end; it++)
*it = (*it)->ToUpper();
}

銆銆瀹冮亶鍘嗕竴涓猄ystem::String^瀹瑰櫒騫舵妸鍏朵腑鐨勬瘡涓瓧絎︿覆杞寲涓哄ぇ鍐欍?br>
Capitalize(vecstr.begin(), vecstr.end());
Capitalize(deqstr.begin(), deqstr.end());
for(vector<String^>::iterator it = vecstr.begin();
it < vecstr.end(); it++)
Console::WriteLine(*it);
Console::WriteLine();
for(deque<String^>::iterator it = deqstr.begin();
it < deqstr.end(); it++)
Console::WriteLine(*it);

銆銆涓婇潰鎴戠殑綆楁硶鑳藉涓巚ector鍜宒eque瀹瑰櫒宸ヤ綔鑹ソ銆傝嚦姝わ紝涓嶅啀緇嗚皥錛涘惁鍒欙紝guru绔欎笂鐨凷TL鐖卞ソ鑰呬滑浼氬鎴戠兢璧鋒敾鍑伙紝鑰岄潪STL浜哄彲鑳芥劅鍒板帉鐑︺傚鏋滀綘榪樻病浣跨敤榪嘢TL錛屽彲浠ュ弬鑰冩湁鍏寵祫鏂欍?br>
7. 鐔熸倝鐨勮娉?br>
銆銆寮鍙戣呯粡甯歌糠鎭嬩粬浠墍鐢ㄧ殑緙栫▼璇█錛岃屽緢灝戞槸鍑轟簬瀹炵敤鐨勭洰鐨勩傝繕璁板緱褰撳井杞甯冧笉鍐嶄負VB6鎻愪緵瀹樻柟鏀寔鏃訛紝VB6浜虹殑鍙嶆姉鍚楋紵闈濾B6浜哄姝ゅ彲鑳介潪甯擱渿鎯婏紝鑰岃侀亾鐨刅B6浜烘棭宸蹭負浠栦滑鐨勮璦浣滃ソ钁ぜ鍑嗗浜嗐備簨瀹炰笂錛屽鏋淰B.NET浠庢潵娌¤鍙戞槑錛屽鏁癡B6浜哄皢浼氱寮.NET錛屽洜涓篊#灝嗕細瀵逛粬浠潪甯擱檶鐢燂紝鑰屽畠鐨勭鍏堝氨鏄疌++銆傚浠婏紝璁稿VB.NET浜哄彲鑳藉凡緇忚漿鍚戜簡C#錛屼絾鏄粬浠笉浼氫粠VB6鐩存帴杞悜C#錛沄B.NET璧峰埌涓涓ˉ姊佷綔鐢ㄨ浠栦滑鐨勬濇兂鑴辯寮鍘熸潵VB6鎬濇兂銆傜浉搴斿湴錛屽鏋滃井杞粎鍙戣VB.NET(鑰屾病鏈塁#)錛岄偅涔?NET鍙兘鎴愪負浜嗘柊鐨勯潰鍚戝璞B錛屼笖甯︽湁涓涓洿澶х殑綾誨簱-C++紺懼洟鐨勪漢鍙兘瀵規鍡や箣浠ラ薊-浠栦滑鐢氳嚦涓嶄細楹葷儲鍦版楠?NET鍩虹綾誨簱銆備負浠涔堜換浣曚嬌鐢ㄤ竴縐嶇壒瀹氳璦鐨勫紑鍙戣呬細瀵瑰彟澶栦竴涓洟浣撶殑浣跨敤鍙﹀寮鍙戣璦鐨勫紑鍙戣呭棨涔嬩互榧伙紵榪欎笉鏄垜瑕佸洖絳旂殑闂銆?-瑕佸洖絳旇闂涔熻瑕佸厛鍥炵瓟涓轟粈涔堟湁鐨勪漢鍠滄濞佸+蹇岋紝鏈夌殑浜哄枩嬈㈠彲鍙e彲涔愶紝鑰岃繕鏈変漢鍠滄鐗涘ザ銆傛墍鏈夋垜瑕佽鐨勬槸錛屽寮鍙戣呮潵璇達紝璇硶瀹舵棌鏄釜澶ч棶棰樸?br>
銆銆浣犺涓哄浜庝竴涓叿鏈塁++鑳屾櫙鐨勪漢錛屼笅闈㈢殑浠g爜鍏鋒湁鎬庢牱鐨勭洿瑙夋э紵

char[] arr =new char[128];

銆銆浠?濂瑰彲鑳藉洖絳旂殑絎竴浠朵簨鏄紝鏂規嫭鍙鋒斁閿欎簡浣嶇疆銆備笅闈㈣繖鍙ュ張濡備綍錛?br>
int y=arr.Length;

銆銆"鍛!"-鏈鍙兘鐨勫弽鏄犮傜幇鍦ㄦ妸涓嬮潰涓庡墠闈㈢浉姣旇緝錛?br>
char natarr[128];
array<char>^ refarr=gcnew array<char>(128);
int y=refarr->Length;

銆銆璇鋒敞鎰忓0鏄庝竴涓湰鏈烘暟緇勫拰涓涓墭綆℃暟緇勬椂鐨勮娉曞尯鍒傝繖閲屼笉鍚岀殑妯℃澘褰㈠紡鐨勮娉曞彲瑙嗗寲鍦板憡璇紑鍙戣呰繖涓浜嬪疄--refarr騫朵笉鏄吀鍨嬬殑C++鏁扮粍鑰屼笖瀹冨彲鑳芥槸鏌愮CLI綾葷殑媧劇敓鐗?浜嬪疄涓婄‘鏄姝?錛屾墍浠ユ瀬鏈夊彲鑳藉彲浠ユ妸鏂規硶鍜屽睘鎬у簲鐢ㄤ簬瀹冦?br>
銆銆C#鐨刦inalizer璇硶鏈鏈夊彲鑳藉紩璧瘋漿鍚慍#鐨凜++紼嬪簭鍛樼殑娣鋒穯銆傝鐪嬭涓嬪垪C#浠g爜錛?br>
class R
{ ~R(){} }

銆銆濂斤紝榪欐牱~R鐪嬭搗鏉ヨ薄涓涓瀽鏋勫櫒浣嗗疄闄呮槸涓猣inalizer銆備負浠涔堬紵璇蜂笌涓嬮潰鐨凜++浠g爜姣旇緝錛?br>
ref class R
{
~R(){ }
!R(){ }
};

銆銆榪欓噷~R鏄瀽鏋勫櫒錛堝疄闄呬笂絳変環浜庝竴涓瀽鏋勫櫒鐨凞ispose妯″紡-浣嗗C++浜哄憳鏉ヨ錛岃繖瀹冪殑琛屼負璞′釜鏋愭瀯鍣級鑰屾柊鐨?R璇硶鏄負finalizer寤虹珛鐨?榪欐牱灝變笉鍐嶆湁娣鋒穯鑰屼笖璇硶鐪嬩笂鍘諱篃涓庢湰鏈篊++鐩稿尮閰嶃?br>
銆銆璇風湅涓涓婥#娉涘瀷璇硶錛?br>
class R<T>{};

銆銆鍐嶈鐪嬩竴涓婥++鐨勮娉曪細

generic<typename T> ref class R{};

銆銆鏇劇粡浣跨敤榪囨ā鏉跨殑浜洪┈涓婂氨鐪嬪嚭榪欑C++璇硶錛岃孋#璇硶涓嶈兘淇濊瘉鍏舵病鏈夋販娣嗘т笖涔熶笉鐩磋銆傛垜鐨勮鐐規槸錛屽鏋滀綘浠ュ墠鍏鋒湁C++鑳屾櫙錛孋++/CLI璇硶灝嗘渶璐磋繎浜庝綘浠ュ墠鎵鐢ㄣ侰#(浠ュ強J#)鐪嬩笂鍘昏薄C++錛屼絾鏄繕鏈夌浉褰撳鐨勬瀬涓轟嬌浜虹儲鐏殑濂囨涔夊樊鍒屼笖濡傛灉浣犳病鏈夊畬鍏ㄦ斁寮僀++錛岃娉曞樊鍒皢姘歌繙涓嶅仠鍦板甫緇欎綘娣蜂貢鍜屾尗鎶樸備粠榪欑鎰忎箟涓婅錛屾垜璁や負VB.NET鏇村ソ浜涳紝鑷沖皯瀹冩湁鑷繁鍞竴鐨勮娉曪紝鎵浠ラ偅浜涘叡鐢–++鍜孷B.NET鐨勪漢涓嶄細浜х敓璇硶娣蜂貢銆?br>
銆銆8. 緇撹

銆銆鏈鍚庯紝鑷充簬浣犵敤浠涔堣璦緙栫▼錛岃繖鍙兘渚濊禆璁稿鍥犵礌鈥斺斿錛氫綘鍦ㄥ鏍″涔犵殑鏄粈涔堣璦錛屼綘鏄敤浠涔堣璦寮鍙戠殑鐜版湁浠g爜浠撳簱錛屾槸鍚︿綘鐨勫鎴峰浣犳湁鍏蜂綋鐨勮璦瑕佹眰絳夈傛湰鏂囩殑涓昏鐩殑鏄府鍔╀綘紜畾浣跨敤C++/CLI鐨勫嚑涓槑紜殑鍦烘墍--榪欓噷錛屽畠姣斿彟澶朇LI璇█鏇村叿鏈夋槑鏄句紭鍔褲傚鏋滀綘寮鍙戠殑搴旂敤紼嬪簭鏈?0%鐨勪嬌鐢ㄦ椂闂存槸娑夊強鏈満interop錛屼負浣曡繕瑕佽冭檻浣跨敤鍙﹀鐨勮屼笉鏄疌++錛熷鏋滀綘鎯沖紑鍙戜竴涓氱敤闆嗗悎錛屼負浠涔堜粎鎶婁綘鑷繁闄愬埗鍦ㄦ硾鍨嬩笂錛岃屼笉鏄粨鍚堟硾鍨嬪拰妯℃澘鐨勪紭鍔垮憿錛熷鏋滀綘宸茬粡鐢–++宸ヤ綔錛屼負浠涔堣繕瑕佸涔犱竴縐嶆柊鐨勮璦?鎴戝父瑙夊緱璞#鍜孷B.NET榪欐牱鐨勮璦鎬繪槸灝介噺鍚戝紑鍙戣呴殣钘廋LR錛岃孋++涓嶄粎璁╀綘鍝佸懗CLR錛岀敋鑷沖彲浠ヨ浣犲幓浜插惢CLR! 



姊﹀湪澶╂動 2008-07-18 22:12 鍙戣〃璇勮
]]>
CLI涓嶤#姣旇緝http://www.shnenglu.com/mzty/archive/2008/06/02/51878.html姊﹀湪澶╂動姊﹀湪澶╂動Mon, 02 Jun 2008 06:17:00 GMThttp://www.shnenglu.com/mzty/archive/2008/06/02/51878.htmlhttp://www.shnenglu.com/mzty/comments/51878.htmlhttp://www.shnenglu.com/mzty/archive/2008/06/02/51878.html#Feedback2http://www.shnenglu.com/mzty/comments/commentRss/51878.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/51878.html鏉ヨ嚜錛歍he Most Powerful Language for .NET Framework Programming 

            http://msdn.microsoft.com/en-us/library/ms379617(VS.80).aspx


姣旇緝錛?br>
Description C++/CLI C#
Allocate reference type ReferenceType^ h = gcnew ReferenceType; ReferenceType h = new ReferenceType();
Allocate value type ValueType v(3, 4); ValueType v = new ValueType(3, 4);
Reference type, stack semantics ReferenceType h; N/A
Calling Dispose method ReferenceType^ h = gcnew ReferenceType;

delete h;

ReferenceType h = new ReferenceType();

((IDisposable)h).Dispose();

Implementing Dispose method ~TypeName() {} void IDisposable.Dispose() {}
Implementing Finalize method !TypeName() {} ~TypeName() {}
Boxing int^ h = 123; object h = 123;
Unboxing int^ hi = 123;

int c = *hi;

object h = 123;

int i = (int) h;

Reference type definition ref class ReferenceType {};

ref struct ReferenceType {};

class ReferenceType {}
Value type definition value class ValueType {};

value struct ValueType {};

struct ValueType {}
Using properties h.Prop = 123;

int v = h.Prop;

h.Prop = 123;

int v = h.Prop;

Property definition property String^ Name
{
    String^ get()
    {
        return m_value;
    }
    void set(String^ value)
    {
        m_value = value;
    }
}
string Name
{
    get
    {
        return m_name;
    }
    set
    {
        m_name = value;
    }
}

thanks

姊﹀湪澶╂動 2008-06-02 14:17 鍙戣〃璇勮
]]>
CLI涓璶ative鐨剆tring鍜孲ystem::String杞寲http://www.shnenglu.com/mzty/archive/2008/03/12/44280.html姊﹀湪澶╂動姊﹀湪澶╂動Wed, 12 Mar 2008 08:47:00 GMThttp://www.shnenglu.com/mzty/archive/2008/03/12/44280.htmlhttp://www.shnenglu.com/mzty/comments/44280.htmlhttp://www.shnenglu.com/mzty/archive/2008/03/12/44280.html#Feedback4http://www.shnenglu.com/mzty/comments/commentRss/44280.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/44280.html闃呰鍏ㄦ枃

姊﹀湪澶╂動 2008-03-12 16:47 鍙戣〃璇勮
]]>
C#鐨凢orm閫氳繃CLI璋冪敤C++鐨凞LLhttp://www.shnenglu.com/mzty/archive/2007/12/25/39610.html姊﹀湪澶╂動姊﹀湪澶╂動Tue, 25 Dec 2007 11:14:00 GMThttp://www.shnenglu.com/mzty/archive/2007/12/25/39610.htmlhttp://www.shnenglu.com/mzty/comments/39610.htmlhttp://www.shnenglu.com/mzty/archive/2007/12/25/39610.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/39610.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/39610.html闃呰鍏ㄦ枃

姊﹀湪澶╂動 2007-12-25 19:14 鍙戣〃璇勮
]]>
NativeC++閫氳繃CLI璋冪敤C#鐨凢ormhttp://www.shnenglu.com/mzty/archive/2007/12/25/39555.html姊﹀湪澶╂動姊﹀湪澶╂動Tue, 25 Dec 2007 02:51:00 GMThttp://www.shnenglu.com/mzty/archive/2007/12/25/39555.htmlhttp://www.shnenglu.com/mzty/comments/39555.htmlhttp://www.shnenglu.com/mzty/archive/2007/12/25/39555.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/39555.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/39555.html涓 璋冪敤鏂規硶

      Native C++鐨刾roject璋冪敤C#鐨凞LL錛屼竴鑸湁3涓柟娉曪細
      1錛夐氳繃COM灝佽
      2錛夐氳繃CLI/C++鐨刉rapper
      3)  鍦╒S涓彲浠ョ洿鎺ヤ慨鏀筃ativeC++鐨刾roject鎴栨槸閮ㄥ垎鏂囦歡涓轟嬌鐢–LR鏉ヨ皟鐢–#鐨凞LL

浜?瀹炰緥
1錛塁#鐨勪竴涓狹yForm綾伙紝鏈塸ublic鍑芥暟ShowMyForm錛堬級
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace CsharpDLL
{
    
public class MyForm : Form
    
{
        
private Button button1;

        
public void ShowMyForm()
        
{
            
this.ShowDialog();
        }

        
public MyForm()
        
{
            InitializeComponent();
            
this.Text = "MyForm";
            
this.StartPosition = FormStartPosition.CenterScreen;
        }

        
private void InitializeComponent()
        
{
            
this.button1 = new System.Windows.Forms.Button();
            
this.SuspendLayout();
            
// 
            
// button1
            
// 
            this.button1.Location = new System.Drawing.Point(11074);
            
this.button1.Name = "button1";
            
this.button1.Size = new System.Drawing.Size(7523);
            
this.button1.TabIndex = 0;
            
this.button1.Text = "TestMessageBox";
            
this.button1.UseVisualStyleBackColor = true;
            
this.button1.Click += new System.EventHandler(this.button1_Click);
            
// 
            
// MyForm
            
// 
            this.ClientSize = new System.Drawing.Size(292266);
            
this.Controls.Add(this.button1);
            
this.Name = "MyForm";
            
this.ResumeLayout(false);

        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            MessageBox.Show(
"Hello, i am a Csharp Form!");
        }

    }

}


2錛塁#鐨別xe璋冪敤
using System;
using System.Collections.Generic;
using System.Text;

namespace CsharpTest
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            System.Console.WriteLine(
"Csharp main funtion start:");
            CsharpDLL.MyForm myForm 
= new CsharpDLL.MyForm();
            myForm.ShowMyForm();
            System.Console.WriteLine(
"Csharp main function end!");
        }

    }

}


3錛塁LI/C++鐨剋arpper錛孌LLexport鍑芥暟CallCsharpForm錛堬級錛屾鍑芥暟涓皟鐢–#鐨凪yForm
CPPCLIPROXYDLLFORCSHARPDLL_API void CallCsharpForm();

#
using "../debug/CsharpDll.dll"

CPPCLIPROXYDLLFORCSHARPDLL_API 
void CallCsharpForm()
{
    CsharpDLL::MyForm
^ myForm = gcnew CsharpDLL::MyForm();
    myForm
->ShowMyForm();
}

4錛塏ativeC++鐨別xe璋冪敤CLI鐨刉rapper鏉ラ棿鎺ョ殑璋冪敤MyForm
#include "stdafx.h"
#include 
<iostream>
#pragma comment(lib, 
"../debug/CppCLIProxyDLLForCsharpDLL.lib")

void CallCsharpForm();

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout
<<" Cpp Main function start:"<<std::endl;
    CallCsharpForm();
    std::cout
<<" Cpp Main function end!"<<std::endl;
    
return 0;
}

涓?鎬葷粨
瑕佽皟璇曟渶濂戒慨鏀圭紪璇戦夐」涓篗ixed銆?br>
浠g爜涓嬭澆錛?http://www.shnenglu.com/Files/mzty/CppCallCsharpByCLI.rar


姊﹀湪澶╂動 2007-12-25 10:51 鍙戣〃璇勮
]]>
CLI/C++涓販鍚堢被鐨勪嬌鐢?/title><link>http://www.shnenglu.com/mzty/archive/2007/12/24/39517.html</link><dc:creator>姊﹀湪澶╂動</dc:creator><author>姊﹀湪澶╂動</author><pubDate>Mon, 24 Dec 2007 09:47:00 GMT</pubDate><guid>http://www.shnenglu.com/mzty/archive/2007/12/24/39517.html</guid><wfw:comment>http://www.shnenglu.com/mzty/comments/39517.html</wfw:comment><comments>http://www.shnenglu.com/mzty/archive/2007/12/24/39517.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.shnenglu.com/mzty/comments/commentRss/39517.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mzty/services/trackbacks/39517.html</trackback:ping><description><![CDATA[     鎽樿: 涓 娣峰悎綾繪墍璋撴販鍚堢被鏄寚CLI/C++涓璶ative鐨凜lass涓彲浠ュ寘鍚獵LR瀵硅薄錛孋LR鐨刢lass涔熷彲浠ュ寘鍚玁aitve鐨勫璞°?1錛塶ative鐨刢lass涓寘鍚獵LR瀵硅薄錛屽繀欏婚氳繃gcroot<>鎴朼uto_gcroot<>銆?錛塁LR涓殑class涓寘鍚玭ative鐨勫璞★紝蹇呴』鏄寚閽堬紝涔熷彲浠ヤ嬌鐢ㄩ珮鎵嬪啓鐨凜AutoNativePtr鏅鴻兘鎸囬拡銆?娉ㄦ剰錛欳#涓笉鑳?..  <a href='http://www.shnenglu.com/mzty/archive/2007/12/24/39517.html'>闃呰鍏ㄦ枃</a><img src ="http://www.shnenglu.com/mzty/aggbug/39517.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mzty/" target="_blank">姊﹀湪澶╂動</a> 2007-12-24 17:47 <a href="http://www.shnenglu.com/mzty/archive/2007/12/24/39517.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>涓涓狢LI/C++鐨凞LL鍚屾椂琚獵#鐨別xe鍜孨ativeC++鐨別xe璋冪敤http://www.shnenglu.com/mzty/archive/2007/12/20/39165.html姊﹀湪澶╂動姊﹀湪澶╂動Thu, 20 Dec 2007 14:47:00 GMThttp://www.shnenglu.com/mzty/archive/2007/12/20/39165.htmlhttp://www.shnenglu.com/mzty/comments/39165.htmlhttp://www.shnenglu.com/mzty/archive/2007/12/20/39165.html#Feedback4http://www.shnenglu.com/mzty/comments/commentRss/39165.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/39165.html 涓 寮曞瓙

      1錛変嬌鐢╒S2005錛孨ative C++錛孋#錛孋LI/C++銆?br>      2錛変富瑕佷粙緇岰LI/C++鍜孨ative C++, C#鐨勪氦浜掞紝鏈鎬昏兘澶熷疄鐜頒互CLI/C++涓轟腑浠嬬殑Native C++涓嶤#鐨勪簰璋冦?br>
 浜?瀹炰緥
      
      1錛夊疄鐜頒竴涓狢LI/C++鐨凞ll錛屽湪姝ll涓寘鍚竴涓猂efClass鑳藉琚獵#鐨別xe璋冪敤銆傦紙瀹炵幇榪囩▼錛氬湪VS涓緩绔婥++ CLR Class library 鐨勪竴涓狿roject錛岀劧鍚庡鍔犱竴涓被RefClass錛変唬鐮佸涓嬶細

      refclass.h鏂囦歡錛?br>      
#pragma once 

namespace ClrCppDll 
{
    
public ref class RefClass
    
{
    
public:
        RefClass()
{}
        RefClass(
int _x, int _y) : x(_x), y(_y)
        
{
        }

        
void PrintSelf();
    
    
private:
        
int x;
        
int y;
    }
;
}

      refclass.cpp鏂囦歡錛?br>
#include "stdafx.h"
#include 
"refclass.h"

using namespace System;
using namespace ClrCppDll;

void RefClass::PrintSelf()
{
    System::Console::WriteLine(
"hello, i am a ref cpp class");
    System::Console::WriteLine(
"x is {0}",x);
    System::Console::WriteLine(
"y is {0}",y);
}

      2錛夎兘澶熻皟鐢ㄤ笂闈㈢1錛夋涓殑CLI/C++鐨凞ll涓璫lass鐨凜#鐨別xe銆傦紙瀹炵幇榪囩▼錛氬湪VS寤虹珛C#鐨刢onsole Application錛岀劧鍚巖eference 鍓嶉潰鐨?)鐢熸垚鐨凞ll錛変唬鐮佸涓嬶細

         Program.cs鏂囦歡錛?br>
using System;
using System.Collections.Generic;
using System.Text;

namespace CsharpTest
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            ClrCppDll.RefClass refClass 
= new ClrCppDll.RefClass(2,4);
            refClass.PrintSelf();
        }

    }

}

            涓婇潰2姝ョ殑浠g爜涓嬭澆錛?a href="http://www.shnenglu.com/Files/mzty/CSharpCPPCLI1.rar">http://www.shnenglu.com/Files/mzty/CSharpCPPCLI1.rar 錛堝疄鐜癈LI/C++鐨刣ll鑳藉琚獵#璋冪敤錛?br>


     3錛夊1錛夊疄鐜扮殑CLI/C++鐨凞ll涓鍔犺兘澶熻Native C++璋冪敤鐨凬ativeClass銆備唬鐮佸涓嬶細

      NativeClass.h鏂囦歡錛?br>
#pragma once

namespace ClrCppDll
{
    
public class NativeClass
    
{        
        
public:
            NativeClass()
{}
            NativeClass(
int _x, int _y) : x(_x), y(_y)
            
{}

            
void printSelf();

        
private:
            
int x;
            
int y;    
    }
;
}

      NativeClass.cpp鏂囦歡錛?br>
#include "stdafx.h"
#include 
<iostream>
#include 
"nativeclass.h"

using namespace ClrCppDll;

void NativeClass::printSelf()
{
    std::cout
<<"hello,i am a native cpp class!"<<std::endl;
    std::cout
<<"x is "<<x<<std::endl;
    std::cout
<<"y is "<<y<<std::endl;                
}


      闂錛?/span> 濡傛灉鎴戜滑鐩存帴鍦∟ativeC++鐨別xe璋冪敤涓婇潰CLI/C++涓殑NativeClass錛屼細鏈夐棶棰橈紝Error 1 error C3381: 'CppClrDll::NativeClass' : assembly access specifiers are only available in code compiled with a /clr option d:\cppandcsharpinteractivetest\csharpcppcli\clrcppdll\nativeclass.h 8錛岃繖鏄負浠涔堥偅錛屾垜浠兂鎯蟲垜浠竴鑸殑Native C++鐨凞LL鐨勮皟鐢紝閮借灝嗚琚皟鐢ㄧ殑Class鎴杅untion瀵煎嚭鎵嶅彲浠ヨ皟鐢紝鎯沖埌榪欓噷鎴戜滑涔熷鎴戜滑鐨凬ativeClass榪涜瀵煎嚭銆傦紙鎬庝箞瀵煎嚭鍙互鐪嬩笅闈㈢殑浠g爜錛変絾鏄綋澧炲姞浜嗗NativeClass鐨勫鍑猴紝璋冪敤鐨勬椂鍊欎粛鐒舵湁涓婇潰鐨別rror錛孍rror 1 error C3381: 'CppClrDll::NativeClass' : assembly access specifiers are only available in code compiled with a /clr option d:\cppandcsharpinteractivetest\csharpcppcli\clrcppdll\nativeclass.h 9錛屾渶鍚庡彧鏈夋煡鎵捐祫鏂欙紙娌℃湁鎵懼埌錛夐棶鍒漢錛堟病鏈夐棶鍒幫級錛屾渶鍚庢兂鍒頒嬌鐢ㄥ鍑哄嚱鏁拌瘯璇曪紝涓璇曟灉鐒跺彲琛屽晩錛屽搱鍝堝搱錛宻o Happy錛?span style="COLOR: #00ccff">鍏跺疄鍚庢潵鍙戠幇鏄垜鍔犵殑瀵煎嚭__declspec(dllexport)鐨勪綅緗笉瀵癸紝搴旇鏄湪class鍏抽敭瀛楀悗闈紝鎬諱箣瀵煎嚭綾諱篃鏄彲浠ョ殑銆?/span>瀵煎嚭綾葷殑浠g爜鍙互涓嬭澆鏂扮殑sample錛屽鍔犲鍑哄拰瀵煎嚭鐨勫嚱鏁頒唬鐮佸涓嬶細

      (瀵煎嚭涓嶄箙闇瑕佷笅闈㈢殑2鏂囦歡錛岃繕鐨勫畾涔夊畯 CLR_CPP_DLL鍦ㄧ紪璇戦夐」涓紝鎴栫洿鎺ュ啓鍦ㄦ枃浠朵腑)
      clrcppdll.h
// ClrCppDll.h

#pragma once

#ifdef CLR_CPP_DLL
#define DLLIMPEXP __declspec(dllexport)
#else
#define DLLIMPEXP
#endif

namespace ClrCppDll 
{
    DLLIMPEXP 
void  CallNativeClassPrintSelf();    
}

      clrcppdll.cpp鏂囦歡錛?br>
// This is the main DLL file.

#include 
"stdafx.h"
#include 
"NativeClass.h"
#include 
"ClrCppDll.h"

DLLIMPEXP 
void  ClrCppDll::CallNativeClassPrintSelf()
{
    ClrCppDll::NativeClass test(
10,20);
    test.printSelf();
}

      4錛夊緩绔嬩竴涓狽ativeC++鐨別xe鏉ヨ皟鐢?錛夌敓鎴愮殑CLI/C++鐨凞ll涓殑3錛夊鍔犵殑NativeClass錛堝疄鐜拌繃紼嬶細寤虹珛涓涓狽ative C++鐨刢onsole application錛変唬鐮佸涓嬶細

       cpptest.cpp鏂囦歡錛氾紙緇忚繃涓婁竴姝ラ棶棰樼殑鍙嶅鎶樼(錛岀粓浜庢悶瀹氾級錛堝悓鏃跺埆蹇樹簡瑕佸湪緙栬瘧閫夐」涓寚瀹歭ib鍜宭ib鐨勮礬寰勶級
// CppTest.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"
#include 
"../ClrCppDll/clrcppdll.h"

int _tmain(int argc, _TCHAR* argv[])
{
    
/*ClrCppDll::NativeClass test(5,6);
    test.PrintSelf();
*/


    ClrCppDll::CallNativeClassPrintSelf();
    
return 0;
}


   涓婇潰鍥涙鎬諱唬鐮佷笅杞斤細http://www.shnenglu.com/Files/mzty/CSharpCPPCLI2.rar 錛堝疄鐜癈LI/C++鐨刣ll鍚屾椂琚玁ative C++鍜孋#鐨勮皟鐢級

      5錛夋墿灞曪紝澧炲姞鍦∟ativeClass涓皟鐢≧efClass鐨刦untion, 鍜孯efClass涓皟鐢∟ativeClass鐨刦unction銆?br>
      浠g爜涓嬭澆錛?a href="http://www.shnenglu.com/Files/mzty/CSharpCPPCLI3.rar">http://www.shnenglu.com/Files/mzty/CSharpCPPCLI3.rar 錛堟渶鍚庝唬鐮佷笅杞斤紝鍖呭惈NativeClass涓嶳efClass鐨勪簰璋冿級
                           http://www.shnenglu.com/Files/mzty/CSharpCPPCLI32.zip 錛堝寘鍚玭ative class鐨勫鍑轟篃璋冪敤錛?br>

      涓?鎬葷粨

      1) CLI/C++涓鏋滄兂瀵煎嚭璁㎞ativeC++浣跨敤錛屽垯鏈濂藉鍑哄嚱鏁?涔熷彲浠ュ鍑虹被銆?br>      2) CLI/C++鐨凞LL鍙互娌℃湁鍍忎竴鑸殑NativeC++鐨凞ll涓殑DllMain錛堬級鍑芥暟銆?br>      3) CLI/C++濡傛灉鎯沖鍑鴻C#浣跨敤錛屽垯涓鑸槸ref class銆?br>








]]>
C#閫氳繃C++/CLI璋冪敤C++DLLhttp://www.shnenglu.com/mzty/archive/2007/06/04/25524.html姊﹀湪澶╂動姊﹀湪澶╂動Mon, 04 Jun 2007 14:59:00 GMThttp://www.shnenglu.com/mzty/archive/2007/06/04/25524.htmlhttp://www.shnenglu.com/mzty/comments/25524.htmlhttp://www.shnenglu.com/mzty/archive/2007/06/04/25524.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/25524.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/25524.html鍙浣犱細C++/CLI灝辨瘮杈冨鏄撳摝錛屾垨鑰呰鍙浣犱細C++鍜孋#瀛﹁搗鏉ヤ篃鏄緢蹇殑鍝︼紒锝?br>


澶у鍙互鍏堝弬鑰冿紝浠ュ悗鏈夋椂闂村姞鍐嶅啓鍝︼綖


鍙傝冿細http://www.codeproject.com/csharp/marshalCPPclass.asp




姊﹀湪澶╂動 2007-06-04 22:59 鍙戣〃璇勮
]]>
C++涓?NET涓熀紜綾誨瀷鐨勫搴斿強杞寲http://www.shnenglu.com/mzty/archive/2007/05/29/25062.html姊﹀湪澶╂動姊﹀湪澶╂動Tue, 29 May 2007 05:55:00 GMThttp://www.shnenglu.com/mzty/archive/2007/05/29/25062.htmlhttp://www.shnenglu.com/mzty/comments/25062.htmlhttp://www.shnenglu.com/mzty/archive/2007/05/29/25062.html#Feedback3http://www.shnenglu.com/mzty/comments/commentRss/25062.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/25062.html闃呰鍏ㄦ枃

姊﹀湪澶╂動 2007-05-29 13:55 鍙戣〃璇勮
]]>
C++/CLIhttp://www.shnenglu.com/mzty/archive/2007/05/29/25059.html姊﹀湪澶╂動姊﹀湪澶╂動Tue, 29 May 2007 05:38:00 GMThttp://www.shnenglu.com/mzty/archive/2007/05/29/25059.htmlhttp://www.shnenglu.com/mzty/comments/25059.htmlhttp://www.shnenglu.com/mzty/archive/2007/05/29/25059.html#Feedback1http://www.shnenglu.com/mzty/comments/commentRss/25059.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/25059.html闃呰鍏ㄦ枃

姊﹀湪澶╂動 2007-05-29 13:38 鍙戣〃璇勮
]]>
C#鐣岄潰錛孋++綆楁硶http://www.shnenglu.com/mzty/archive/2007/04/16/22064.html姊﹀湪澶╂動姊﹀湪澶╂動Mon, 16 Apr 2007 14:41:00 GMThttp://www.shnenglu.com/mzty/archive/2007/04/16/22064.htmlhttp://www.shnenglu.com/mzty/comments/22064.htmlhttp://www.shnenglu.com/mzty/archive/2007/04/16/22064.html#Feedback16http://www.shnenglu.com/mzty/comments/commentRss/22064.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/22064.html闃呰鍏ㄦ枃

姊﹀湪澶╂動 2007-04-16 22:41 鍙戣〃璇勮
]]>
Managed c++涓巆++\CLI鐨勫尯鍒?瀹炰緥)http://www.shnenglu.com/mzty/archive/2006/08/18/11397.html姊﹀湪澶╂動姊﹀湪澶╂動Fri, 18 Aug 2006 02:58:00 GMThttp://www.shnenglu.com/mzty/archive/2006/08/18/11397.htmlhttp://www.shnenglu.com/mzty/comments/11397.htmlhttp://www.shnenglu.com/mzty/archive/2006/08/18/11397.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/11397.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/11397.html闃呰鍏ㄦ枃

姊﹀湪澶╂動 2006-08-18 10:58 鍙戣〃璇勮
]]>
Visual C++ .NET緙栫▼錛氭墭綆++姒傝堪http://www.shnenglu.com/mzty/archive/2006/08/18/11393.html姊﹀湪澶╂動姊﹀湪澶╂動Fri, 18 Aug 2006 02:18:00 GMThttp://www.shnenglu.com/mzty/archive/2006/08/18/11393.htmlhttp://www.shnenglu.com/mzty/comments/11393.htmlhttp://www.shnenglu.com/mzty/archive/2006/08/18/11393.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/11393.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/11393.html
銆銆Microsoft鐨?NET鎴樼暐鎰忓懗鐫錛歁icrosoft浠ュ強鍦∕icrosoft騫沖彴涓婄殑寮鍙戣呭皢浼氬埗閫犳湇鍔★紝鑰屼笉鏄埗閫犺蔣浠躲傚湪鏈潵鍑犲勾涔嬪唴錛孧icrosoft灝嗛檰緇彂甯冩湁鍏?NET鐨勫鉤鍙板拰宸ュ叿錛岀敤浜庡湪鍥犵壒緗戜笂寮鍙慦eb鏈嶅姟銆傞偅鏃訛紝宸ヤ綔鍦?NET涓婄殑鐢ㄦ埛銆佸紑鍙戜漢鍛樺拰IT宸ヤ綔浜哄憳閮戒笉鍐嶈喘涔拌蔣浠躲佸畨瑁呰蔣浠跺拰緇存姢杞歡銆傚彇鑰屼唬涔嬬殑鏄紝浠栦滑灝嗗畾鍒舵湇鍔★紝杞歡浼氳嚜鍔ㄥ畨瑁咃紝鎵鏈夌殑緇存姢鍜屽崌綰т篃浼氶氳繃浜掕仈緗戣繘琛屻傗淢icrosoft.NET 浠h〃浜嗕竴涓泦鍚堛佷竴涓幆澧冦佷竴涓彲浠ヤ綔涓哄鉤鍙版敮鎸佷笅涓浠nternet鐨勫彲緙栫▼緇撴瀯銆傗濊繖灝辨槸椴嶅皵榛樺.NET鐨勬弿榪般?br />

銆銆浣滀負.NET鐨勬渶鏂扮壒鎬х粍鎴愰儴鍒嗭紝Microsoft .NET Framework鏄竴涓敤浜庢瀯寤猴紝閮ㄧ講鍜岃繍琛學eb鏈嶅姟鍙婂簲鐢ㄧ▼搴忕殑騫沖彴銆傚畠涓哄皢鐜版湁鎶曡祫涓庝笅涓浠e簲鐢ㄧ▼搴忓拰鏈嶅姟鐨勯泦鎴愭彁渚涗簡楂樹駭鐨勶紝鍩轟簬鏍囧噯鐨勶紝澶氳璦鐜錛屽悓鏃跺畠榪樼敤浜庤В鍐矷nternet綰у簲鐢ㄧ▼搴忕殑閮ㄧ講鍜屾搷浣滈棶棰樸?NET妗嗘灦鍖呭惈涓変釜涓昏閮ㄥ垎錛氶氱敤璇█榪愯鏃訛紝涓緇勫眰嬈″寲鐨勭粺涓鐨勭被搴擄紝鍙婄粍浠跺寲鐗堟湰鐨勫姩鎬佹湇鍔″櫒涓婚〉(縐頒負ASP.NET)銆?br />
銆銆鐢ㄤ簬寮鍙?NET Framework鐨勮璦鏈塚isual C#銆乂B.NET鍜孋++鎵樼鎵╁睍(Managed Extensions for C++)銆傚叾涓瑿#鏄紑鍙?NET鐨勫厓璇█錛岃孋++鎵樼鎵╁睍鏄湪C++鍩虹涓婂緩绔嬭搗鏉ョ殑錛岀敤鏉ヤ負Visual C++紼嬪簭鍛樺紑鍙?NET妗嗘灦搴旂敤紼嬪簭鑰岃璁°備負鍙欒堪鏂逛究錛屾垜浠皢C++鎵樼鎵╁睍灝辯О涔嬩負鈥滄墭綆++鈥濄?br />涓轟簡甯姪C/C++浠ュ強Visual C++紼嬪簭鍛樻垨鐖卞ソ鑰呭揩閫熶嬌鐢ㄦ墭綆++寮鍙?NET Framework紼嬪簭錛屾垜浠皢闄嗙畫鎺ㄥ嚭鐩稿叧鐨勪竴緋誨垪鏂囩珷銆?br />
銆銆鏈瘒鈥滄墭綆++姒傝堪鈥濅富瑕佽榪頒簡浠涔堟槸鎵樼C++銆佸紑鍙?NET Framework(妗嗘灦)鐨勯」鐩被鍨嬩互鍙婁笌鏍囧噯C++涔嬮棿鐨勫尯鍒?br />
銆銆1銆佷粈涔堟槸鎵樼C++錛?/font>

銆銆鍦ㄥ洖絳旇繖涓棶棰橈紝棣栧厛瑕佹悶娓呮浠涔堟槸鈥滄墭綆♀?Managed)銆傛墭綆℃槸.NET鐨勪竴涓笓闂ㄦ蹇碉紝瀹冩槸铻嶄簬閫氱敤璇█榪愯鏃?CLR)涓殑涓縐嶆柊鐨勭紪紼嬬悊蹇碉紝鍥犳鎴戜滑瀹屽叏鍙互鎶娾滄墭綆♀濊涓衡?NET鈥濄傞偅涔堜粈涔堟槸鈥滈氱敤璇█榪愯鏃垛濓紵閫氱敤璇█榪愯鏃舵槸.NET 妗嗘灦搴旂敤紼嬪簭鐨勬墽琛屽紩鎸氥傚畠鎻愪緵浜嗚澶氭湇鍔★紝鍏朵腑鍖呮嫭錛氫唬鐮佺鐞?瑁呭叆鍜屾墽琛?銆佺被鍨嬪畨鍏ㄦч獙璇併佸厓鏁版嵁(楂樼駭綾誨瀷淇℃伅)璁塊棶銆佷負綆$悊瀵硅薄綆$悊鍐呭瓨銆佺鐞嗕唬鐮侊紝COM瀵硅薄鍜岄鐢熸垚鐨凞LLs(闈炵鐞嗕唬鐮佸拰鏁版嵁)鐨勪氦浜掓搷浣滄с佸寮鍙戜漢鍛樻湇鍔$殑鏀寔絳夌瓑銆?br />
銆銆涔熷氨鏄錛屼嬌鐢ㄦ墭綆++鎰忓懗鐫錛屾垜浠殑浠g爜鍙互琚獵LR鎵綆$悊錛屽茍鑳藉紑鍙戝嚭鍏鋒湁鏈鏂扮壒鎬у鍨冨溇鑷姩鏀墮泦銆佺▼搴忛棿鐩鎬簰璁塊棶絳夌殑.NET妗嗘灦搴旂敤紼嬪簭銆?br />
銆銆鐢辨墭綆℃蹇墊墍寮曞彂鐨凜++搴旂敤紼嬪簭鍖呮嫭鎵樼浠g爜銆佹墭綆℃暟鎹拰鎵樼綾諱笁涓粍鎴愰儴鍒嗐傘銆

銆銆(1) 鎵樼浠g爜錛?Net鐜鎻愪緵浜嗚澶氭牳蹇冪殑榪愯(RUNTIME)鏈嶅姟錛屾瘮濡傚紓甯稿鐞嗗拰瀹夊叏絳栫暐銆備負浜嗚兘浣跨敤榪欎簺鏈嶅姟錛屽繀欏昏緇欒繍琛岀幆澧冩彁渚涗竴浜涗俊鎭唬鐮?鍏冩暟鎹?錛岃繖縐嶄唬鐮佸氨鏄墭綆′唬鐮併傛墍鏈夌殑C#銆乂B.NET銆丣Script.NET榛樿鏃墮兘鏄墭綆$殑錛屼絾Visual C++榛樿鏃朵笉鏄墭綆$殑錛屽繀欏誨湪緙栬瘧鍣ㄤ腑浣跨敤鍛戒護琛岄夐」(/CLR)鎵嶈兘浜х敓鎵樼浠g爜銆?br />
銆銆(2) 鎵樼鏁版嵁錛氫笌鎵樼浠g爜瀵嗗垏鐩稿叧鐨勬槸鎵樼鏁版嵁銆傛墭綆℃暟鎹槸鐢卞叕鍏辮璦榪愯鐨勫瀮鍦懼洖鏀跺櫒榪涜鍒嗛厤鍜岄噴鏀劇殑鏁版嵁銆傞粯璁ゆ儏鍐典笅錛孋#銆乂isual Basic 鍜?JScript.NET 鏁版嵁鏄墭綆℃暟鎹備笉榪囷紝閫氳繃浣跨敤鐗規畩鐨勫叧閿瓧錛孋# 鏁版嵁鍙互琚爣璁頒負闈炴墭綆℃暟鎹俈isual C++鏁版嵁鍦ㄩ粯璁ゆ儏鍐典笅鏄潪鎵樼鏁版嵁錛屽嵆浣垮湪浣跨敤 /CLR 寮鍏蟲椂涔熶笉鏄墭綆$殑銆?br />
銆銆(3) 鎵樼綾?/b>錛氬敖綆isual C++鏁版嵁鍦ㄩ粯璁ゆ儏鍐典笅鏄潪鎵樼鏁版嵁錛屼絾鏄湪浣跨敤C++鐨勬墭綆℃墿灞曟椂錛屽彲浠ヤ嬌鐢ㄢ淿_gc鈥濆叧閿瓧灝嗙被鏍囪涓烘墭綆$被銆傚氨鍍忚鍚嶇О鎵鏄劇ず鐨勯偅鏍鳳紝瀹冭〃紺虹被瀹炰緥鐨勫唴瀛樼敱鍨冨溇鍥炴敹鍣ㄧ鐞嗐傚彟澶栵紝涓涓墭綆$被涔熷畬鍏ㄥ彲浠ユ垚涓?.NET 妗嗘灦鐨勬垚鍛橈紝鐢辨鍙互甯︽潵鐨勫ソ澶勬槸錛屽畠鍙互涓庡叾浠栬璦緙栧啓鐨勭被姝g‘鍦拌繘琛岀浉浜掓搷浣滐紝濡傛墭綆$殑C++綾誨彲浠ヤ粠Visual Basic綾葷戶鎵跨瓑銆備絾鍚屾椂涔熸湁涓浜涢檺鍒訛紝濡傛墭綆$被鍙兘浠庝竴涓熀綾葷戶鎵跨瓑銆傞渶瑕佽鏄庣殑鏄紝鍦ㄦ墭綆++搴旂敤紼嬪簭涓棦鍙嬌鐢ㄦ墭綆$被涔熷彲浠ヤ嬌鐢ㄩ潪鎵樼綾匯傝繖閲岀殑闈炴墭綆$被涓嶆槸鎸囨爣鍑咰++綾伙紝鑰屾槸浣跨敤鎵樼C++璇█涓殑__nogc鍏抽敭瀛楃殑綾匯?br />
2銆佺敤鎵樼C++鍙互寮鍙?NET妗嗘灦鐨勯」鐩被鍨?br />
銆銆浣跨敤鎵樼C++搴旇鏄疌++紼嬪簭鍛樼紪鍐?NET妗嗘灦搴旂敤紼嬪簭鏈濂界殑涓縐嶉夋嫨錛岄氳繃闆嗘垚鍦╒isual Studio.NET寮鍙戠幆澧冪殑鎵樼C++鍚戝錛屾垜浠彲浠ュ垱寤轟互涓嬪嚑縐嶅紑鍙?NET妗嗘灦鐨勯」鐩被鍨嬶細

銆銆(1) 鎵樼C++搴旂敤紼嬪簭:鐢ㄦ潵鍒涘緩涓涓敮鎸佹墭綆℃墿灞曠殑鍗曠嫭C++搴旂敤紼嬪簭錛屼嬌鐢ㄥ畠榪樺彲鍒涘緩浠諱綍綾誨瀷鐨勫簲鐢ㄧ▼搴忥紝鍖呮嫭.NET妗嗘灦瀹㈡埛搴旂敤紼嬪簭銆?br />
銆銆(2) 鎵樼C++綾誨簱:鐢ㄦ潵鍒涘緩涓涓敮鎸佹墭綆℃墿灞曠殑C++DLL錛屼嬌鐢ㄥ畠鍙互鐢熸垚涓涓兘琚?NET妗嗘灦搴旂敤紼嬪簭璋冪敤鐨勬墭綆$被鍨嬬殑緇勪歡銆?br />
銆銆(3) 鎵樼C++絀洪」鐩細鐢ㄦ潵鍒涘緩涓涓┖鐨勬墭綆¢」鐩紝璇ラ」鐩彧鍚湁鏀寔鎵樼鎵╁睍鐨勬紜紪璇戝拰閾炬帴鐨勫紑鍏抽夐」銆備嬌鐢ㄥ畠鑳藉皢涓涓凡鏈夌殑C++婧愭枃浠惰繘鍏ュ埌涓涓墭綆$幆澧冧腑銆?br />
銆銆(4) 鎵樼C++ Web鏈嶅姟錛氱敤浜庡垱寤轟袱涓」鐩紝涓涓槸C++鎵樼鎵╁睍欏圭洰錛屽彟涓涓槸閮ㄧ講欏圭洰銆?br />
銆銆3銆佹墭綆++涓庢爣鍑咰++鐨勪富瑕佸尯鍒?/font>

銆銆灝界鎵樼C++鏄粠鏍囧噯C++寤虹珛鑰屾潵鐨勶紝浣嗗畠涓庢爣鍑咰++鏈夌潃鏈川涓婄殑鍖哄埆錛岃繖涓昏浣撶幇鍦ㄤ互涓嬪嚑涓柟闈細

銆銆(1) 騫挎硾閲囩敤鈥滃悕縐扮┖闂粹?namespace)

銆銆鍚嶇О絀洪棿鏄被鍨嬬殑涓縐嶉昏緫鍛藉悕鏂規錛?NET浣跨敤璇ュ懡鍚嶆柟妗堢敤浜庡皢綾誨瀷鎸夌浉鍏沖姛鑳界殑閫昏緫綾誨埆榪涜鍒嗙粍錛屽埄鐢ㄥ悕縐扮┖闂村彲浠ヤ嬌寮鍙戜漢鍛樻洿瀹規槗鍦ㄤ唬鐮佷腑嫻忚鍜屽紩鐢ㄧ被鍨嬨傚綋鐒訛紝鎴戜滑涔熷彲灝嗗悕縐扮┖闂寸悊瑙f垚鏄竴涓滅被搴撳悕鈥濄?

銆銆灝界寰堟棭Microsoft灝卞湪Visual C++涓敮鎸佸悕縐扮┖闂寸殑緙栫▼鏂瑰紡錛屼絾鏄緢灝戝紩璧稸isual C++紼嬪簭鍛樼殑鏅亶鍏蟲敞銆傜幇鍦ㄥ湪鎵樼C++紼嬪簭涓紝鎴戜滑蹇呴』浣跨敤榪欎竴鏂瑰紡錛屽嵆浣跨敤#using鍜寀sing鍏抽敭瀛椼備緥濡備笅闈㈢殑綆鍗曠▼搴忎唬鐮佹槸鍦ㄦ帶鍒跺彴涓婅緭鍑衡淗ello World鈥濓細

#using
using namespace System;
int main(void)
{
Console::WriteLine(S"Hello World");
return 0;
}

銆銆浠g爜涓紝#using鏄敤鏉ュ皢涓涓厓鏁版嵁鏂囦歡杈撳叆鍒版墭綆++紼嬪簭涓紝榪欎簺鏂囦歡鍙互鏄寘鍚墭綆℃暟鎹拰緇撴瀯鐨凪SIL (Microsoft intermediate language錛屽井杞腑闂磋璦)鏂囦歡錛屽DLL銆丒XE銆丱BJ鏂囦歡絳夈俶scorlib.dll鏄?NET妗嗘灦鐨勪竴涓牳蹇冪被搴擄紝鍖呭惈涓昏鐨勫悕縐扮┖闂碨ystem銆傜▼搴忕殑絎簩琛屼唬鐮佲渦sing namespace System;鈥濈敤鏉ヤ嬌鐢⊿ystem鍚嶇О絀洪棿銆係ystem鏄?NET妗嗘灦鏍瑰悕縐扮┖闂達紝鍖呭惈鏈鍩烘湰鐨勭被鍨嬶紝濡傜敤浜庢暟鎹祦鐨勮緭鍏?杈撳嚭鐨凷ystem::IO絳夈?br />
銆銆鍦ㄥ鎵樼C++紼嬪簭寮鍙戠殑涓嶆柇娣卞叆錛屾垜浠笉涔呭氨浼氬彂鐜幫紝璁稿綾誨瀷鐨勫紩鐢ㄩ兘瑕佸湪紼嬪簭鐨勫墠闈嬌鐢?using鍜寀sing鏉ヨ繘琛屻?br />
銆銆(2) 鍩烘湰鏁版嵁綾誨瀷鐨勫彉鍖?/b>

銆銆鎴戜滑鐭ラ亾錛屾爣鍑咰++璇█鐨勬暟鎹被鍨嬫槸闈炲父涓板瘜鐨勩傝屾墭綆++鐨勬暟鎹被鍨嬫洿鍔犱赴瀵岋紝涓嶄粎鍖呭惈浜嗘爣鍑咰++涓殑鏁版嵁綾誨瀷錛岃屼笖鏂板浜哶_int64(64浣嶆暣鍨?銆丏ecimal(96浣嶅崄榪涘埗鏁?銆丼tring*(瀛楃涓茬被鍨?鍜孫bject*(瀵硅薄綾誨瀷)絳夌被鍨嬶紝琛?-1鍒楀嚭瀹冧滑鍚勮嚜鏁版嵁綾誨瀷銆?br />
綾誨瀷鎻忚堪鏍囧噯C++綾誨瀷鍚?/td>鎵樼C++綾誨瀷鍚?/td>闀垮害(浣?
甯冨皵鍨?/td>boolbool8
瀛楃鍨?/td>charsigned char8
鏃犵鍙峰瓧絎﹀瀷unsigned charchar8
鐭暣鍨?/td>short [int]short16
鏃犵鍙風煭鏁村瀷unsigned short [int]unsigned short16
鏁村瀷intint 鎴?long32
鏃犵鍙鋒暣鍨?/td>unsigned [int]unsigned int 鎴?long32
闀挎暣鍨?/td>long [int]long32
鏃犵鍙烽暱鏁村瀷unsigned long [int]unsigned long32
鍗曠簿搴︽誕鐐瑰瀷floatfloat32
鍙岀簿搴︽誕鐐瑰瀷doubledouble64
闀垮弻綺懼害嫻偣鍨?/td>long double--64
Unicode瀛楃--wchar_t16
64浣嶆暣鍨?/td>--__int6464
鏃犵鍙?4浣嶆暣鍨?/td>--unsigned __int6464
96浣嶅崄榪涘埗鍊?/td>--Decimal96
瀵硅薄綾誨瀷--Object*32
瀛楃涓茬被鍨?/td>--String*--


銆銆闇瑕佹敞鎰忕殑鏄紝String鍜孫bject鍦ㄥ畾涔変竴涓彉閲忔椂錛屾敞鎰忚鏈夋槦鍙?鈥?鈥?錛屼絾榪欎釜鍙橀噺涓嶆槸鎸囬拡鍙橀噺錛岃繖涓庢爣鍑咰++鐨勫惈涔夋槸涓嶄竴鏍風殑銆備緥濡備笂闈㈢殑浠g爜鍙互鏀逛負錛?br />
#using
using namespace System;
int main(void)
{
String* hello = S"Hello World";
Console::WriteLine(hello);
return 0;
}
(3) 鏂板涓変釜鎵樼C++綾誨瀷錛歘_gc class銆乢_value class鍜宊_gc interface

銆銆涓涓猒_gc綾繪垨緇撴瀯鎰忓懗鐫璇ョ被鎴栫粨鏋勭殑鐢熷懡鍛ㄦ湡鏄敱.NET寮鍙戝鉤鍙拌嚜鍔ㄧ鐞嗗強鍨冨溇鑷姩鏀墮泦錛岀敤鎴蜂笉蹇呰嚜宸插幓璋冪敤delete鏉ュ垹闄ゃ傚畾涔変竴涓猒_gc綾繪垨緇撴瀯鍜屾爣鍑咰++鍩烘湰鐩鎬技錛屾墍涓嶅悓鐨勬槸鍦╟lass鎴杝truct鍓嶅姞涓奯_gc錛屼緥濡備笅闈㈢殑浠g爜錛?br />
__gc class G {
public:
int k;
int sum(int);
};

G::sum(int i) {return i*(i + 1)/2;}
int main()
{
G * g = new G;
Console::WriteLine(g->sum(4)); // 緇撴灉杈撳嚭10
return 0;
}

銆銆浣嗚娉ㄦ剰錛?/font>

銆銆A. 涓涓猒_gc綾諱笉鑳戒粠涓涓潪鎵樼綾諱腑緇ф壙錛屼笖涓嶈兘鍖呭惈浠庡畠媧劇敓鐨勯潪鎵樼綾匯備絾涓涓猒_gc綾繪渶澶氬彲浠ヤ粠涓涓墭綆$被涓戶鎵褲?br />
銆銆B. 涓涓猒_gc綾諱笉鑳藉畾涔夋垚涓涓弸鍏冪被鎴栧寘鍚竴涓弸鍏冩垚鍛樺嚱鏁般傛墍璋撳弸鍏冨嚱鏁幫紝鏄敤鏉ヨ澶栭儴鍑芥暟璁塊棶綾諱腑鐨勭鏈夊拰淇濇姢綾誨瀷鎴愬憳銆?br />
銆銆C. 涓涓猒_gc綾諱笉鑳藉0鏄庢垨瀹氫箟浠ュ強閲嶈澆new鎴杁elete鎿嶄綔浠ュ強涓嶈兘鍖呭惈using絳夊0鏄庛?br />
銆銆__value綾繪槸鐢ㄦ潵浣跨敤鍏鋒湁鐭敓鍛芥湡鐨勫皬鍨嬫暟鎹」錛屽畠涓嶅悓浜巁_gc綾匯俖_gc綾繪暟鎹垎閰嶅湪CLR鍫嗕腑錛岃宊_value綾誨璞℃槸鍦ㄨ繍琛屾爤鎴栫О涓篘DP(.NET Developer Platform錛?NET寮鍙戣呭鉤鍙?鍫嗕腑鍒涘緩鐨勶紝浠庤岄伩鍏嶄簡鍨冨溇鍥炴敹鍣ㄤ笉鏂垎閰嶅拰閲婃斁絀洪棿鑰屽甫鏉ョ殑寮閿銆備竴涓猒_value綾誨彲浠ュ0鏄庢垚涓轟竴涓眬閮ㄥ彉閲忋佸弬鏁板拰榪斿洖鍊鹼紝涔熷彲宓屽叆鍒頒竴涓猒_gc綾諱腑鎴栨槸浣滀負涓涓潤鎬佸彉閲忔垨鍦–++鍫嗕腑鍒嗛厤鐨勫彉閲忋備緥濡備笅闈㈢殑浠g爜錛?br />
#using
using namespace System;
__value struct V { int i; };
__gc struct G { V v; }; // 宓屽叆鍒癬_gc綾諱腑
V f(V v) { // 瀹氫箟涓涓叏灞鍑芥暟錛屽叾鍊煎瓨鍌ㄥ湪榪愯鏍堜腑
v.i += 1; // 涓嶅獎鍝嶅師鏉ュ艦鍙倂鐨勫?br />return v; // 榪斿洖V緇撴瀯綾誨瀷鐨勫?br />}
int main(void)
{
V v1 = {10}; // 鍦ㄨ繍琛屾爤涓0鏄庡茍鍒濆鍖?br />V v2 = f(v1); // 璋冪敤f鍑芥暟錛屾鏃秜1涓殑i涓?0錛岃寁2涓殑i涓?1
G *pG = new G; // 涓篏瀹炰緥鍒嗛厤鍫嗙┖闂?br />pG->v = v1; // pG鐨剉涓殑i涓?0
pG->v.i += v2.i; // pG鐨剉涓殑i涓?0+11=21
Console::WriteLine(v1.i); // 杈撳嚭緇撴灉涓?0
Console::WriteLine(v2.i); // 杈撳嚭緇撴灉涓?1
Console::WriteLine(pG->v.i); // 杈撳嚭緇撴灉涓?1
return 0;
}

銆銆闄ゆ涔嬪錛屾墍鏈夌殑__gc瀵硅薄閮芥槸浠庣被System::Object媧劇敓鑰屾潵錛屽洜鑰岃兘澶熷緢瀹規槗浣跨敤浣滅敤鍦╛_gc綾諱腑鐨勯泦鍚堝拰鏄犲皠鍔熻兘銆傜劧鑰宊_value綾誨瀷騫舵病鏈変笌榪欎釜鍩虹被鎵鍏變韓錛屽洜鑰屼笉鑳界洿鎺ュ皢__value浣滀負鍑芥暟涓殑Object*瀹炲弬銆備負浜嗚В鍐寵繖涓棶棰橈紝.NET鍏佽鎴戜滑浣跨敤__box鍏抽敭瀛楀皢涓涓猒_value綾誨瀷瑙嗕負涓涓猒_gc瀵硅薄銆傛鏃禵_value綾誨瀷琚皝瑁呮垚涓涓猒_gc綾繪々瀛?Stub)錛屽茍琚鍒跺埌NDP鍫嗕腑銆傜敱浜庡湪鎵樼C++涓紝box涓嶅叿澶囬殣寮忚漿鎹㈢殑鍔熻兘錛屽洜姝ゅ湪杞崲鏃跺繀欏繪寚鏄庤漿鎹㈢殑綾誨瀷銆?br />
銆銆鎵樼C++涓殑__gc鎺ュ彛鏈鑳戒綋鐜癈OM鎺ュ彛鐨勬濇兂錛屽畠鐨勫畾涔夊拰澹版槑鏄潪甯哥畝鍗曠殑錛屽畠闄や簡鍏抽敭瀛椾笉鍚屽錛屼笌涓涓猒_gc綾葷殑澹版槑鏋佷負鐩鎬技銆備緥濡備笅闈㈢殑浠g爜瀹氫箟浜嗕竴涓帴鍙MyBase錛屽叾涓寘鍚簡涓涓猣鐨勬柟娉曪細

__gc __interface Ibase {
void f();
};

銆銆闇瑕佽鏄庣殑鏄紝鎺ュ彛涓墍鏈夌殑鏂規硶榛樿鏃墮兘鏄函铏氱殑涓旈兘鏄叕鏈夌殑錛屾垜浠笉闇瑕佸湪鏂規硶涔嬪墠浣跨敤virtual鍏抽敭瀛楁垨鍦ㄦ柟娉曚箣鍚庡姞涓娾? 0鈥濄傚叾嬈★紝鍦ㄤ竴涓猒_gc鎺ュ彛涓笉鑳藉寘鍚暟鎹垚鍛樹互鍙婇潤鎬佹垚鍛橈紝涔熶笉鑳藉寘鍚換浣曠被鐨勫0鏄庛備笅闈婦涓涓ず渚嬫潵璇存槑__gc鎺ュ彛鐨勪嬌鐢細

#using
using namespace System;

__gc __interface Ibase1 {
int f(int);
};
__gc __interface Ibase2 {
int f(int);
};
__gc struct C: Ibase1, Ibase2 {
int f(int i) { // 鎺ュ彛鏂規硶鐨勫疄鐜?br />return 2*i-1;
};
};

int main(void){
C* c = new C;
Console::WriteLine((c -> f(1)).ToString()); // 杈撳嚭緇撴灉涓?
Console::WriteLine((__try_cast (c)->f(2)).ToString());
// 杈撳嚭緇撴灉涓?

Console::WriteLine((__try_cast (c)->f(3)).ToString());
// 杈撳嚭緇撴灉涓?

return 0;
}

銆銆浠g爜涓紝__try_cast鐢ㄦ潵灝嗘煇涓璞¤漿鎹㈡垚涓涓寚瀹氱被鍨嬶紝騫跺綋綾誨瀷杞崲澶辮觸鏃惰嚜鍔ㄥ鐞嗙敱姝や駭鐢熺殑寮傚父銆俆oString鐢ㄦ潵灝嗗璞℃弿榪版垚涓涓瓧絎︿覆銆?br />(4) 綆鍖栧睘鎬ф搷浣?br />
銆銆鍦╛_gc綾諱腑鍙互浣跨敤.NET鐨勫睘鎬э紝榪欎釜灞炴х畝鍖栦簡灞炴у嚱鏁扮殑璋冪敤鎿嶄綔錛岃繖涓庢爣鍑咰++涓殑灞炴т笉涓鏍楓傚湪鏍囧噯C++涓垎鍒氳繃get_鍜宲ut_鎴愬憳鍑芥暟鏉ヨ緗垨鑾峰彇鐩稿叧灞炴х殑鍊箋傜幇鍦紝鎵樼C++涓殑灞炴ф搷浣滃氨濂芥瘮鏄涓涓睘鎬у彉閲忚繘琛屾搷浣滐紝渚嬪涓嬪垪浠g爜錛?br />
#using
using namespace System;

__gc class G {
public:
__property int get_Size() {
Console::WriteLine(S"get_灞炴?);
return nSize;
};
__property void set_Size(int i) {
Console::WriteLine(S"set_灞炴?);
nSize = i;
};
private:
int nSize;
};

int main() {
G * pG = new G;
pG->Size = 10; // 璋冪敤set_Size
int i = pG->Size; // 璋冪敤get_Size
Console::WriteLine(i);
}

銆銆紼嬪簭緇撴灉涓猴細

銆銆銆set_灞炴?br />
銆銆銆get_灞炴?br />
銆銆銆10

銆銆闇瑕佽鏄庣殑鏄紝鎵樼C++浣跨敤__property鍏抽敭瀛楁潵瀹氫箟涓涓睘鎬х殑鎴愬憳鍑芥暟銆備粠浠g爜涓彲浠ョ湅鍑鴻緗拰鑾峰彇灞炴х殑鎴愬憳鍑芥暟鍚嶇О涓垎鍒嬌鐢ㄤ簡set_鍜実et_錛岃繖鏍風紪璇戝櫒浼氳嚜鍔ㄧ敓鎴愪竴涓吉鎴愬憳鍙橀噺Size錛岃繖涓彉閲忓悕鏄痵et_鍜実et_鎴愬憳鍑芥暟鍚庨潰鐨勫悕縐般傛敞鎰忎笉瑕佸啀鍦╣et_鎴愬憳鍑芥暟浠g爜涓嬌鐢ㄨ繖涓吉鎴愬憳鍙橀噺Size錛屽畠浼氬紩璧瘋鍑芥暟鐨勯掑綊璋冪敤銆?br />
銆銆(5) 鎵樼C++鐨勫媧?/b>

銆銆鍦–/C++涓紝涓涓嚱鏁扮殑鍦板潃灝辨槸鍐呭瓨鍦板潃銆傝繖涓湴鍧涓嶄細甯︽湁浠諱綍鍏跺畠闄勫姞淇℃伅錛屽鍑芥暟鐨勫弬鏁頒釜鏁般佸弬鏁扮被鍨嬨佸嚱鏁扮殑榪斿洖鍊肩被鍨嬩互鍙婅繖涓嚱鏁扮殑璋冪敤瑙勮寖絳夈傛諱箣錛孋/C++鐨勫洖璋冨嚱鏁頒笉鍏峰綾誨瀷瀹夊叏鎬с傝?NET妗嗘灦鍦ㄥ洖璋冨嚱鏁扮殑鍩虹澧炲姞浜嗘彁渚涚被鍨嬪畨鍏ㄧ殑鏈哄埗錛岀О涓哄媧俱?br />
銆銆鎵樼C++鐨勫媧炬柟娉曚笉鍍廋#閭d箞澶嶆潅錛屽畠綆鍖栦簡濮旀淳緇濆ぇ閮ㄥ垎鐨勫唴閮ㄦ満鍒訛紝鍥犺屼嬌寰楀畠鐨勪嬌鐢ㄥ彉鎴愰潪甯哥畝鍗曞鏄撱備緥濡備笅闈㈢殑浠g爜錛?br />
#using
using namespace System;

__delegate int GetDayOfWeek(); // 濮旀淳鏂規硶鐨勫0鏄?br />__gc class MyCalendar
{
public:
MyCalendar() : m_nDayOfWeek(4) {}
int MyGetDayOfWeek() {
Console::WriteLine("闈為潤鎬佹柟娉?);
return m_nDayOfWeek;
}
static int MyStaticGetDayOfWeek() {
Console::WriteLine("闈欐佹柟娉?);
return 6;
}
private:
int m_nDayOfWeek;
};

int main(void)
{
GetDayOfWeek * pGetDayOfWeek; // 澹版槑濮旀淳綾誨瀷鍙橀噺
int nDayOfWeek;

// 灝嗙被鐨勯潤鎬佹柟娉昅yStaticGetDayOfWeek緇戝畾鎴愬媧?br />pGetDayOfWeek = new GetDayOfWeek(0, &MyCalendar::MyStaticGetDayOfWeek);
nDayOfWeek = pGetDayOfWeek->Invoke(); // 濮旀淳鐨勮皟鐢?br />Console::WriteLine(nDayOfWeek);

// 灝嗕竴涓被鐨勫疄渚嬬粦瀹氭垚濮旀淳
MyCalendar * pcal = new MyCalendar();
pGetDayOfWeek =
static_cast(Delegate::Combine(pGetDayOfWeek,
new GetDayOfWeek(pcal, &MyCalendar::MyGetDayOfWeek)));
nDayOfWeek = pGetDayOfWeek->Invoke();
Console::WriteLine(nDayOfWeek);

// 鍒犻櫎緇戝畾濮旀淳鐨勭被瀹炰緥
pGetDayOfWeek =
static_cast(Delegate::Remove(pGetDayOfWeek,
new GetDayOfWeek(pcal, &MyCalendar::MyGetDayOfWeek)));

return 0;
}

銆銆杈撳嚭緇撴灉鏄細

銆銆銆闈欐佹柟娉?br />銆銆
銆銆銆6

銆銆銆闈欐佹柟娉?br />
銆銆銆闈為潤鎬佹柟娉?br />
銆銆銆4

銆銆4銆佺粨閫熻

銆銆鎬諱箣錛屼嬌鐢ㄦ墭綆++鏄疌++紼嬪簭鍛樼紪鍐?NET妗嗘灦搴旂敤紼嬪簭鏈濂界殑涓縐嶉夋嫨錛屽湪鍏呭垎鐞嗚В.NET妗嗘灦鍩虹涓婏紝閬垮厤浜嗕嬌鐢ㄥ叾浠栬璦濡侰#銆乂B.NET鎵甯︽潵鐨勯澶栧紑閿銆?br />

]]>
c++\CLI涓巆#http://www.shnenglu.com/mzty/archive/2006/08/17/11358.html姊﹀湪澶╂動姊﹀湪澶╂動Thu, 17 Aug 2006 08:46:00 GMThttp://www.shnenglu.com/mzty/archive/2006/08/17/11358.htmlhttp://www.shnenglu.com/mzty/comments/11358.htmlhttp://www.shnenglu.com/mzty/archive/2006/08/17/11358.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/11358.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/11358.html Description C++/CLI C# Allocate reference type ReferenceType^ h = gcnew ReferenceType; ReferenceType h = new ReferenceType(); Allocate value type ValueType v(3, 4); ValueType v = new ValueType(3, 4); Reference type, stack semantics ReferenceType h; N/A Calling Dispose method ReferenceType^ h = gcnew ReferenceType;

delete h;

ReferenceType h = new ReferenceType();

((IDisposable)h).Dispose();

Implementing Dispose method ~TypeName() {} void IDisposable.Dispose() {} Implementing Finalize method !TypeName() {} ~TypeName() {} Boxing int^ h = 123; object h = 123; Unboxing int^ hi = 123;

int c = *hi;

object h = 123;

int i = (int) h;

Reference type definition ref class ReferenceType {};

ref struct ReferenceType {};

class ReferenceType {} Value type definition value class ValueType {};

value struct ValueType {};

struct ValueType {} Using properties h.Prop = 123;

int v = h.Prop;

h.Prop = 123;

int v = h.Prop;

Property definition property String^ Name
{
聽聽聽 String^ get()
聽聽聽 {
聽聽聽聽聽聽聽 return m_value;
聽聽聽 }
聽聽聽 void set(String^ value)
聽聽聽 {
聽聽聽聽聽聽聽 m_value = value;
聽聽聽 }
}
string Name
{
聽聽聽 get
聽聽聽 {
聽聽聽聽聽聽聽 return m_name;
聽聽聽 }
聽聽聽 set
聽聽聽 {
聽聽聽聽聽聽聽 m_name = value;
聽聽聽 }


]]>
Native c++ 鍜孧anaged 鐨?interophttp://www.shnenglu.com/mzty/archive/2006/08/17/11354.html姊﹀湪澶╂動姊﹀湪澶╂動Thu, 17 Aug 2006 08:26:00 GMThttp://www.shnenglu.com/mzty/archive/2006/08/17/11354.htmlhttp://www.shnenglu.com/mzty/comments/11354.htmlhttp://www.shnenglu.com/mzty/archive/2006/08/17/11354.html#Feedback5http://www.shnenglu.com/mzty/comments/commentRss/11354.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/11354.html4縐嶆柟娉?

There are four main ways to do interop in .NET between the managed and native worlds. COM interop can be accomplished using Runtime Callable Wrappers (RCW) and COM Callable Wrappers (CCW). The common language runtime (CLR) is responsible for type marshaling (except in the rare scenarios where a custom marshaler is used) and the cost of these calls can be expensive. You need to be careful that the interfaces are not too chatty; otherwise a large performance penalty could result. You also need to ensure that the wrappers are kept up to date with the underlying component. That said, COM interop is very useful for simple interop scenarios where you are attempting to bring in a large amount of native COM code.

The second option for interop is to use P/Invoke. This is accomplished using the DllImport attribute, specifying the attribute on the method declaration for the function you want to import. Marshaling is handled according to how it has been specified in the declaration. However, DllImport is only useful if you have code that exposes the required functions through a DLL export.

When you need to call managed code from native code, CLR hosting is an option. In such a scenario, the native application has to drive all of the execution: setting up the host, binding to the runtime, starting the host, retrieving the appropriate AppDomain, setting up the invocation context, locating the desired assembly and class, and invoking the operation on the desired class. This is definitely one of the most robust solutions in terms of control over what and when things happen, but it is also incredibly tedious and requires a lot of custom code.

The fourth option, and quite possibly the easiest and the most performant, is to use the interop capabilities in C++. By throwing the /clr switch, the compiler generates MSIL instead of native machine code. The only code that is generated as native machine code is code that just can't be compiled to MSIL, including functions with inline asm blocks and operations that use CPU-specific intrinsics such as Streaming SIMD Extensions (SSE). The /clr switch is how the Quake II port to .NET was accomplished. The Vertigo software team spent a day porting the original C code for the game to code that successfully compiled as C++ and then threw the /clr switch. In no time they were up and running on the .NET Framework. Without adding any additional binaries and simply by including the appropriate header files, managed C++ and native C++ can call each other without any additional work on the part of the developer. The compiler handles the creation of the appropriate thunks to go back and forth between the two worlds.



]]>
Visual C++ 緙栬瘧鍣ㄩ夐」http://www.shnenglu.com/mzty/archive/2006/08/17/11336.html姊﹀湪澶╂動姊﹀湪澶╂動Thu, 17 Aug 2006 04:16:00 GMThttp://www.shnenglu.com/mzty/archive/2006/08/17/11336.htmlhttp://www.shnenglu.com/mzty/comments/11336.htmlhttp://www.shnenglu.com/mzty/archive/2006/08/17/11336.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/11336.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/11336.html 鏉ヨ嚜msdn:http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/vccore/html/_core_compiler_options_listed_by_category.asp

浼樺寲

/O1 鍒涘緩灝忎唬鐮?/td>
/O2 鍒涘緩蹇熶唬鐮?/td>
/Oa 鍋囪娌℃湁鍒悕
/Ob 鎺у埗鍐呰仈灞曞紑
/Od 紱佺敤浼樺寲
/Og 浣跨敤鍏ㄥ眬浼樺寲
/Oi 鐢熸垚鍐呴儴鍑芥暟
/Op 鏀瑰杽嫻偣鏁頒竴鑷存?/td>
/Os 浠g爜澶у皬浼樺厛
/Ot 浠g爜閫熷害浼樺厛
/Ow 鍋囧畾鍦ㄥ嚱鏁拌皟鐢ㄤ腑浣跨敤鍒悕
/Ox 浣跨敤鏈澶т紭鍖?(/Ob2gity /Gs)
/Oy 鐪佺暐妗嗘灦鎸囬拡

浠g爜鐢熸垚

/arch 浣跨敤 SSE 鎴?SSE2 鎸囦護鐢熸垚浠g爜
/clr 鍚敤 C++ 鐨勬墭綆℃墿灞曞茍浜х敓鍦ㄥ叕鍏辮璦榪愯搴撲笂榪愯鐨勮緭鍑烘枃浠?/td>
/EH 鎸囧畾寮傚父澶勭悊妯″瀷
/G3 浼樺寲浠g爜浠ヤ紭閫?386 澶勭悊鍣ㄣ傚湪 Visual C++ 5.0 涓凡緇忓仠鐢紝緙栬瘧鍣ㄥ皢蹇界暐姝ら夐」
/G4 浼樺寲浠g爜浠ヤ紭閫?486 澶勭悊鍣ㄣ傚湪 Visual C++ 5.0 涓凡緇忓仠鐢紝緙栬瘧鍣ㄥ皢蹇界暐姝ら夐」
/G5 浼樺寲浠g爜浠ヤ紭閫?Pentium
/G6 浼樺寲浠g爜浠ヤ紭閫?Pentium Pro銆丳entium II 鍜?Pentium III 澶勭悊鍣?/td>
/G7 閽堝 Pentium 4 鎴?Athlon 浼樺寲浠g爜銆?/td>
/GB 涓?/G6 絳夋晥錛涘皢 _M_IX86 鐨勫艱緗負 600
/Gd 浣跨敤 __cdecl 璋冪敤綰﹀畾
/Ge 嬋媧誨爢鏍堟帰嫻?/td>
/GF
/Gf
鍚敤瀛楃涓叉睜
/Gh 璋冪敤鎸傞挬鍑芥暟 _penter
/GH 璋冪敤鎸傞挬鍑芥暟 _pexit
/GL 鍚敤鍏ㄧ▼搴忎紭鍖?/td>
/Gm 鍚敤鏈灝忛噸鏂扮敓鎴?/td>
/GR 鍚敤榪愯鏃剁被鍨嬩俊鎭?(RTTI)
/Gr 浣跨敤 __fastcall 璋冪敤綰﹀畾
/Gs 鎺у埗鍫嗘爤鎺㈡祴
/GT 鏀寔浣跨敤闈欐佺嚎紼嬫湰鍦板瓨鍌ㄥ尯鍒嗛厤鐨勬暟鎹殑綰ょ▼瀹夊叏
/GX 鍚敤鍚屾寮傚父澶勭悊
/Gy 鍚敤鍑芥暟綰ч摼鎺?/td>
/Gz 浣跨敤 __stdcall 璋冪敤綰﹀畾
/MD 浣跨敤 MSVCRT.lib 鍒涘緩澶氱嚎紼?DLL
/MDd 浣跨敤 MSVCRTD.lib 鍒涘緩璋冭瘯澶氱嚎紼?DLL
/ML 浣跨敤 LIBC.lib 鍒涘緩鍗曠嚎紼嬪彲鎵ц鏂囦歡
/MLd 浣跨敤 LIBCD.lib 鍒涘緩璋冭瘯鍗曠嚎紼嬪彲鎵ц鏂囦歡
/MT 浣跨敤 LIBCMT.lib 鍒涘緩澶氱嚎紼嬪彲鎵ц鏂囦歡
/MTd 浣跨敤 LIBCMTD.lib 鍒涘緩璋冭瘯澶氱嚎紼嬪彲鎵ц鏂囦歡

杈撳嚭鏂囦歡

/FA
/Fa
鍒涘緩鍒楄〃鏂囦歡
璁劇疆鍒楄〃鏂囦歡鍚?/td>
/Fd 閲嶅懡鍚嶇▼搴忔暟鎹簱鏂囦歡
/Fe 閲嶅懡鍚嶅彲鎵ц鏂囦歡
/Fm 鍒涘緩鏄犲皠鏂囦歡
/Fo 鍒涘緩瀵硅薄鏂囦歡
/Fp 鎸囧畾棰勭紪璇戝ご鏂囦歡鍚?/td>
/FR
/FR
鐢熸垚嫻忚鍣ㄦ枃浠?/td>
/Fx 灝嗘彃鍏ョ殑浠g爜涓庢簮鏂囦歡鍚堝茍

璋冭瘯

/GS 緙撳啿鍖哄畨鍏ㄦ鏌?/td>
/GZ 涓?/RTC1 鐩稿悓
/RTC 鍚敤榪愯鏃墮敊璇鏌?/td>
/Wp64 媯嫻?64 浣嶅彲縐繪鎬ч棶棰?/td>
/Yd 灝嗗畬鏁寸殑璋冭瘯淇℃伅鏀懼湪鎵鏈夊璞℃枃浠朵腑
/Yl 鍒涘緩璋冭瘯搴撴椂鎻掑叆 PCH 寮曠敤
/Z7 鐢熸垚涓?C 7.0 鍏煎鐨勮皟璇曚俊鎭?/td>
/Zd 鐢熸垚琛屽彿
/Zi 鐢熸垚瀹屾暣鐨勮皟璇曚俊鎭?/td>

棰勫鐞嗗櫒

/AI 鎸囧畾鍦ㄨВ鏋愪紶閫掑埌 #using 鎸囦護鐨勬枃浠跺紩鐢ㄦ椂鎼滅儲鐨勭洰褰?/td>
/C 鍦ㄩ澶勭悊鏈熼棿淇濈暀娉ㄩ噴
/D 瀹氫箟甯告暟鍜屽畯
/E 灝嗛澶勭悊鍣ㄨ緭鍑哄鍒跺埌鏍囧噯杈撳嚭
/EP 灝嗛澶勭悊鍣ㄨ緭鍑哄鍒跺埌鏍囧噯杈撳嚭
/Fl 棰勫鐞嗘寚瀹氱殑鍖呭惈鏂囦歡
/FU 寮哄埗浣跨敤鏂囦歡鍚嶏紝灝卞儚瀹冨凡琚紶閫掑埌 #using 鎸囦護涓鏍?/td>
/I 鍦ㄧ洰褰曚腑鎼滅儲鍖呭惈鏂囦歡
/P 灝嗛澶勭悊鍣ㄨ緭鍑哄啓鍏ユ枃浠?/td>
/U 縐婚櫎棰勫畾涔夊畯
/u 縐婚櫎鎵鏈夌殑棰勫畾涔夊畯
/X 蹇界暐鏍囧噯鍖呭惈鐩綍
/ZI 灝嗚皟璇曚俊鎭寘鍚湪涓庘滅紪杈戝茍緇х畫鈥濆吋瀹圭殑紼嬪簭鏁版嵁搴撲腑

璇█

/vd 鍙栨秷鎴栧惎鐢ㄩ殣钘忕殑 vtordisp 綾繪垚鍛?/td>
/vmb 瀵規寚鍚戞垚鍛樼殑鎸囬拡浣跨敤鏈浣崇殑鍩?/td>
/vmg 瀵規寚鍚戞垚鍛樼殑鎸囬拡浣跨敤瀹屽叏涓鑸?/td>
/vmm 澹版槑澶氶噸緇ф壙
/vms 澹版槑鍗曚竴緇ф壙
/vmv 澹版槑铏氭嫙緇ф壙
/Za 紱佺敤璇█鎵╁睍
/Zc 鍦?/Ze 涓嬫寚瀹氭爣鍑嗚涓?/td>
/Ze 鍚敤璇█鎵╁睍
/Zg 鐢熸垚鍑芥暟鍘熷瀷
/Zl 浠?.obj 鏂囦歡涓Щ闄ら粯璁ゅ簱鍚?/td>
/Zp n 灝佽緇撴瀯鎴愬憳
/Zs 鍙鏌ヨ娉?/td>

閾炬帴

/F 璁劇疆鍫嗘爤澶у皬
/LD 鍒涘緩鍔ㄦ侀摼鎺ュ簱
/LDd 鍒涘緩璋冭瘯鍔ㄦ侀摼鎺ュ簱
/link 灝嗘寚瀹氱殑閫夐」浼犻掔粰 LINK
/MD 浣跨敤 MSVCRT.lib 緙栬瘧浠ュ垱寤哄綰跨▼ DLL
/MDd 浣跨敤 MSVCRTD.lib 緙栬瘧浠ュ垱寤鴻皟璇曞綰跨▼ DLL
/ML 浣跨敤 LIBC.lib 緙栬瘧浠ュ垱寤哄崟綰跨▼鍙墽琛屾枃浠?/td>
/MLd 浣跨敤 LIBCD.lib 緙栬瘧浠ュ垱寤鴻皟璇曞崟綰跨▼鍙墽琛屾枃浠?/td>
/MT 浣跨敤 LIBCMT.lib 緙栬瘧浠ュ垱寤哄綰跨▼鍙墽琛屾枃浠?/td>
/MTd 浣跨敤 LIBCMTD.lib 緙栬瘧浠ュ垱寤鴻皟璇曞綰跨▼鍙墽琛屾枃浠?/td>

棰勭紪璇戝ご

/Y- 蹇界暐褰撳墠鐢熸垚涓殑鎵鏈夊叾浠栭緙栬瘧澶寸紪璇戝櫒閫夐」
/Yc 鍒涘緩棰勭紪璇戝ご鏂囦歡
/Yd 灝嗗畬鏁寸殑璋冭瘯淇℃伅鏀懼湪鎵鏈夊璞℃枃浠朵腑
/Yu 鍦ㄧ敓鎴愭湡闂翠嬌鐢ㄩ緙栬瘧澶存枃浠?/td>
/YX 鑷姩澶勭悊棰勭紪璇戝ご

鏉傞」

@ 鎸囧畾鍝嶅簲鏂囦歡
/? 鍒楀嚭緙栬瘧鍣ㄩ夐」
/c 緙栬瘧浣嗕笉閾炬帴
/H 闄愬埗澶栭儴錛堝叕鍏憋級鍚嶇О鐨勯暱搴?/td>
/HELP 鍒楀嚭緙栬瘧鍣ㄩ夐」
/J 鏇存敼榛樿鐨?char 綾誨瀷
/nologo 鍙栨秷鏄劇ず鐧誨綍鐗堟潈鏍囧織
/QI0f 紜繚 Pentium 0F 鎸囦護娌℃湁闂
/QIfdiv FDIV銆丗PREM銆丗PTAN 鍜?FPATAN 鎸囦護鏈夌己闄風殑 Intel Pentium 寰鐞嗗櫒鐨勫彉閫氭柟娉?/td>
QIfist 褰撻渶瑕佷粠嫻偣綾誨瀷杞崲涓烘暣鍨嬫椂鍙栨秷 Helper 鍑芥暟 _ftol 鐨勮皟鐢?/td>
/showIncludes 鍦ㄧ紪璇戞湡闂存樉紺烘墍鏈夊寘鍚枃浠剁殑鍒楄〃
/Tc
/TC
鎸囧畾 C 婧愭枃浠?/td>
/Tp
/TP
鎸囧畾 C++ 婧愭枃浠?/td>
/V 璁劇疆鐗堟湰瀛楃涓?/td>
/W 璁劇疆璀﹀憡絳夌駭
/w 紱佺敤鎵鏈夎鍛?/td>
/Wall 鍚敤鎵鏈夎鍛婏紝鍖呮嫭榛樿鎯呭喌涓嬬鐢ㄧ殑璀﹀憡
/WL 鍦ㄤ粠鍛戒護琛岀紪璇?C++ 婧愪唬鐮佹椂鍚敤閿欒淇℃伅鍜岃鍛婃秷鎭殑鍗曡璇婃柇
/Zm 鎸囧畾棰勭紪璇戝ご鍐呭瓨鍒嗛厤闄愬埗

璇峰弬瑙?/h4>

C/C++ 鐢熸垚鍙傝?/a> | 緙栬瘧鍣ㄩ夐」 | 璁劇疆緙栬瘧鍣ㄩ夐」



]]>闈炴墭綆$殑vc宸ョ▼涓儴鍒嗘枃浠朵嬌鐢?Net Framworkhttp://www.shnenglu.com/mzty/archive/2006/08/17/11334.html姊﹀湪澶╂動姊﹀湪澶╂動Thu, 17 Aug 2006 04:05:00 GMThttp://www.shnenglu.com/mzty/archive/2006/08/17/11334.htmlhttp://www.shnenglu.com/mzty/comments/11334.htmlhttp://www.shnenglu.com/mzty/archive/2006/08/17/11334.html#Feedback2http://www.shnenglu.com/mzty/comments/commentRss/11334.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/11334.html浣跨敤vs2005鐜?/font> 澧?,涓斿伐紼嬩笉浣跨敤涓庣紪璇戝ご鏂囦歡stdafx.h.(淇濊瘉鍙互,涓嶅彲浠ユ潵鎵炬垜!)

涓,寤虹珛涓鑸殑c++鐨刢onsole32鐨刾roject ,name: Test ,宸ョ▼setting浣跨敤榛樿,涓嶆敼鍙?濡備笅:
r_testclass.jpg
浜?add header file: ManagedCppClass.h
#pragma  once

class ManagedCppClass
{
public:
    
void PrintString();
    
}
;

涓?add cpp file: ManagedCppClass.cpp
#using <mscorlib.dll> 
using namespace System;

#include 
"ManagedCppClass.h"

void ManagedCppClass::PrintString()
{
    Console::WriteLine(S
"Hello, i am a Managed cpp class ");
}
鍥?main鍑芥暟濡備笅:
#include "stdafx.h"
#include 
"ManagedCppClass.h"

int _tmain(int argc, _TCHAR* argv[])
{
    ManagedCppClass test;
    test.PrintString();
    
return 0;
}
浜?淇敼Managedcppclass.cpp鏂囦歡鐨刾roperty setting,
r_managedcppclass.jpg
next
o_managedcppclass2.jpg
鍏?compile->link->run ,it is ok!

]]>
浣跨敤c++\CLI瀹炵幇c++鎵樼涓庨潪鎵樼娣峰悎緙栫▼http://www.shnenglu.com/mzty/archive/2006/08/16/11288.html姊﹀湪澶╂動姊﹀湪澶╂動Wed, 16 Aug 2006 06:23:00 GMThttp://www.shnenglu.com/mzty/archive/2006/08/16/11288.htmlhttp://www.shnenglu.com/mzty/comments/11288.htmlhttp://www.shnenglu.com/mzty/archive/2006/08/16/11288.html#Feedback0http://www.shnenglu.com/mzty/comments/commentRss/11288.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/11288.htmlMixing Native and Managed Types in C++

Wow, its turning into a busy month. I just haven鈥檛 had any time to blog despite having a number of interesting topics to cover. I鈥檒l try to get a few of them written soon. Here鈥檚 a topic from one of my regular readers.

The CLR naturally supports mixing managed and native method calls allowing you to easily call native functions from managed functions (which are of course natively compiled before execution) and visa versa. This is all largely transparent in C++. What鈥檚 not as transparent is how to mix managed and native types. The reason is that there is a greater distinction between native and managed types compared with function calls. Beside obvious differences such as those introduced by different calling conventions and virtual invocation, function calls aren鈥檛 all that different. Types however require a bit more help from the programmer/compiler since native and managed types can have very different characteristics. This is very evident in C# as you often need to decorate native type definitions with all kinds of attributes to control memory layout and marshalling. Fortunately for the C++ programmer the compiler takes care of much of this when you define or include native type definitions such as those found in the various Windows header files, but the programmer is still responsible for telling the compiler just how those types are to be used.

Visual C++ provides many of the building blocks for mixing native and managed types but in some cases you need to write a little code to help it along. Fortunately C++/CLI is very capable. Let鈥檚 consider a few different scenarios.

Embed Simple Managed Type in Native Type

Since the CLR needs to keep track of every single instance of a managed type in a process, storing some kind of reference/pointer/handle to a managed object in a native type is not directly supported since instances of native types can be allocated in any region of memory and cast to all kinds of foreign data types that would be completely opaque to the CLR and its services. Instead you need to register such occurrences with the CLR so that it is aware of these 鈥渘ative鈥?references to managed types. This is achieved with the use of the GCHandle type. Internally GCHandle manages a static table of (native) pointers that are used to lookup the objects in the managed heap. Of course using GCHandle directly from C++ can be quite tedious. It鈥檚 a CLS compliant value type which means native pointers are represented by IntPtr values. It also does not preserve static type information so static_casts are inevitable. Fortunately Visual C++ ships with the gcroot native template class that provides a strongly-typed interface over the GCHandle type.

#include <vcclr.h>

ref struct ManagedType
{
聽聽聽 void HelloDotNet()
聽聽聽 {
聽聽聽聽聽聽聽 Console::WriteLine("Hello .NET");
聽聽聽 }
};

struct NativeType
{
聽聽聽 ManagedType m1;聽聽聽聽聽聽聽聽聽 // Error!
聽聽聽
聽聽聽 ManagedType^ m2;聽聽聽聽聽聽聽聽 // Error!
聽聽聽
聽聽聽 gcroot<ManagedType^> m3; // OK
};

void main()
{
聽聽聽 NativeType native;
聽聽聽 native.m3 = gcnew ManagedType;

聽聽聽 native.m3->HelloDotNet();
}

As you can see, gcroot provides a 鈥渟mart鈥?pointer for storing handles in native types. It may be smart but it does not provide automatic cleanup of resources. Specifically, the gcroot destructor makes no attempt to dispose of the managed object鈥檚 resources.

Embed Managed Resource in Native Type

Enter the auto_gcroot class. This native template class wraps a gcroot and provides transfer-of-ownership semantics for managed objects stored in native types. If you鈥檙e looking for a point of reference, think of the auto_ptr template class from the Standard C++ Library which does the same thing for native pointers. The auto_gcroot destructor takes care of 鈥渄eleting鈥?the handle which results in the object鈥檚 IDisposable::Dispose method (if any) being called.

#include <msclr\auto_gcroot.h>

ref struct ManagedType
{
聽聽聽 void HelloDotNet()
聽聽聽 {
聽聽聽聽聽聽聽 Console::WriteLine("Hello .NET");
聽聽聽 }

聽聽聽 ~ManagedType()
聽聽聽 {
聽聽聽聽聽聽聽 Console::WriteLine("dtor");

聽聽聽聽聽聽聽 // Compiler implements Dispose pattern...
聽聽聽 }
};

struct NativeType
{
聽聽聽 msclr::auto_gcroot<ManagedType^> m3; // OK
};

void main()
{
聽聽聽 NativeType native;
聽聽聽 native.m3 = gcnew ManagedType;

聽聽聽 native.m3->HelloDotNet();
}

The NativeType destructor (provided by the compiler) will automatically call the auto_gcroot destructor which will delete the managed object resulting in its destructor being called through its compiler generated Dispose method.

Embed Native Type in Managed Type

Now let鈥檚 turn things around. Let鈥檚 say we want to store a native type as a member of a managed type. The challenge is that the only native type the CLR really supports within managed types is a native pointer. C# programmers use IntPtr but that is only because IntPtr is the CLS compliant way of representing a native pointer and C# tries really hard to remain CLS compliant. The CLR fully supports storing native pointers without losing type information.

struct NativeType
{
};

ref struct ManagedType
{
聽聽聽 NativeType n1; // Error!

聽聽聽 NativeType* n2; // OK
};

That鈥檚 great except that now we have a resource management issue. Recall that C++ does not have the separation of memory and resource management evident in the CLR. The native object pointed to by the ManagedType member needs to be deleted. Here is one solution.

ref struct ManagedType
{
聽聽聽 NativeType* n2; // OK

聽聽聽 ~ManagedType()
聽聽聽 {
聽聽聽聽聽聽聽 if (0 != n2)
聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽 delete n2;
聽聽聽聽聽聽聽聽聽聽聽 n2 = 0;
聽聽聽聽聽聽聽 }
聽聽聽 }
};

Now the ManagedType has a Dispose implementation that will faithfully delete the native object. But this can become tedious and error prone very quickly. A better solution is to use some kind of 鈥渁utomatic鈥?approach. Fortunately C++/CLI support by-value semantics for members so all we need is a managed auto-pointer template class. With such a class the ManagedType becomes really simple.

ref struct ManagedType
{
聽聽聽 AutoPtr<NativeType> n2; // OK
};

ManagedType stores a pointer to a native object and its destructor automatically deletes the object. Woohoo!

The C++ compiler really takes care of a lot of boilerplate code. If you鈥檙e not sure just how much code the compiler is taking care of for you then聽take a look at the compiled assembly in a disassembler.

Although Visual C++ does not provide a managed AutoPtr class, it is reasonably simple to write one. Here is a basic implementation.

template <typename T>
ref struct AutoPtr
{
聽聽聽 AutoPtr() : m_ptr(0)
聽聽聽 {
聽聽聽聽聽聽聽 // Do nothing
聽聽聽 }
聽聽聽 AutoPtr(T* ptr) : m_ptr(ptr)
聽聽聽 {
聽聽聽聽聽聽聽 // Do nothing
聽聽聽 }
聽聽聽 AutoPtr(AutoPtr<T>% right) : m_ptr(right.Release())
聽聽聽 {
聽聽聽聽聽聽聽 // Do nothing
聽聽聽 }
聽聽聽 ~AutoPtr()
聽聽聽 {
聽聽聽聽聽聽聽 if (0 != m_ptr)
聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽 delete m_ptr;
聽聽聽聽聽聽聽聽聽聽聽 m_ptr = 0;
聽聽聽聽聽聽聽 }
聽聽聽 }
聽聽聽 T& operator*()
聽聽聽 {
聽聽聽聽聽聽聽 return *m_ptr;
聽聽聽 }
聽聽聽 T* operator->()
聽聽聽 {
聽聽聽聽聽聽聽 return m_ptr;
聽聽聽 }
聽聽聽 T* Get()
聽聽聽 {
聽聽聽聽聽聽聽 return m_ptr;
聽聽聽 }
聽聽聽 T* Release()
聽聽聽 {
聽聽聽聽聽聽聽 T* released = m_ptr;
聽聽聽聽聽聽聽 m_ptr = 0;
聽聽聽聽聽聽聽 return released;
聽聽聽 }
聽聽聽 void Reset()
聽聽聽 {
聽聽聽聽聽聽聽 Reset(0);
聽聽聽 }
聽聽聽 void Reset(T* ptr)
聽聽聽 {
聽聽聽聽聽聽聽 if (0 != m_ptr)
聽聽聽聽聽聽聽 {
聽聽聽聽聽聽聽聽聽聽聽 delete m_ptr;
聽聽聽聽聽聽聽 }
聽聽聽聽聽聽聽 m_ptr = ptr;
聽聽聽 }
private:
聽聽聽 T* m_ptr;
};

In a future post I may聽provide a few realistic examples of mixing native and managed code, but I hope this introduction has given you a few ideas on how to mix native and managed code and types effectively in C++.


鏉ヨ嚜: http://weblogs.asp.net/kennykerr/archive/2005/07/12/Mixing-Native-and-Managed-Types-in-C_2B002B00_.aspx



]]>
A first look at C++/CLI(C++鐨勭鐞嗘墿灞?http://www.shnenglu.com/mzty/archive/2005/12/29/2240.html姊﹀湪澶╂動姊﹀湪澶╂動Thu, 29 Dec 2005 07:14:00 GMThttp://www.shnenglu.com/mzty/archive/2005/12/29/2240.htmlhttp://www.shnenglu.com/mzty/comments/2240.htmlhttp://www.shnenglu.com/mzty/archive/2005/12/29/2240.html#Feedback2http://www.shnenglu.com/mzty/comments/commentRss/2240.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/2240.htmlIntroduction

When Microsoft brought out the Managed Extensions to C++ with VS.NET 7, C++ programmers accepted it with mixed reactions. While most people were happy that they could continue using C++, nearly everyone was unhappy with the ugly and twisted syntax offered by Managed C++. Microsoft obviously took the feedback it got very seriously and they decided that the MC++ syntax wasn't going to be much of a success.

On October 6th 2003, the ECMA announced the creation of a new task group to oversee development of a standard set of language extensions to create a binding between the ISO standard C++ programming language and Common Language Infrastructure (CLI). It was also made known that this new set of language extensions will be known as the C++/CLI standard, which will be supported by the VC++ compiler starting with the Whidbey release (VS.NET 2005).

Problems with the old syntax

  • Ugly and twisted syntax and grammar - All those double underscores weren't exactly pleasing to the eye.
  • Second class CLI support - Compared to C# and VB.NET, MC++ used contorted workarounds to provide CLI support, for e.g. it didn't have a for-each construct to enumerate .NET collections.
  • Poor integration of C++ and .NET - You couldn鈥檛 use C++ features like templates on CLI types and you couldn鈥檛 use CLI features like garbage collection on C++ types.
  • Confusing pointer usage - Both unmanaged C++ pointers and managed reference pointers used the same * based syntax which was quite confusing because __gc pointers were totally different in nature and behavior from unmanaged pointers.
  • The MC++ compiler could not produce verifiable code

What C++/CLI gives us?

  • Elegant syntax and grammar -This gave a natural feel for C++ developers writing managed code and allowed a smooth transition from unmanaged coding to managed coding. All those ugly double underscores are gone now.
  • First class CLI support - CLI features like properties, garbage collection and generics are supported directly. And what's more, C++/CLI allows jus to use these features on native unmanaged classes too.
  • First class C++ support - C++ features like templates and deterministic destructors work on both managed and unmanaged classes. In fact C++/CLI is the only .NET language where you can *seemingly* declare a .NET type on the stack or on the native C++ heap.
  • Bridges the gap between .NET and C++ - C++ programmers won't feel like a fish out of water when they attack the BCL
  • The executable generated by the C++/CLI compiler is now fully verifiable.

Hello World

				using
				namespace System;

void _tmain()
{
    Console::WriteLine("Hello World");
}

Well, that doesn't look a lot different from old syntax, except that now you don't need to add a reference to mscorlib.dll because the Whidbey compiler implicitly references it whenever you compile with /clr (which now defaults to /clr:newSyntax).

Handles

One major confusion in the old syntax was that we used the * punctuator with unmanaged pointers and with managed references. In C++/CLI Microsoft introduces the concept of handles.

				void _tmain()
{
    //The ^ punctuator represents a handle
    String^ str = "Hello World";
    Console::WriteLine(str);
}

The ^ punctuator (pronounced as cap) represents a handle to a managed object. According to the CLI specification a handle is a managed object reference. Handles are the new-syntax equivalent of __gc pointers in the MC++ syntax. Handles are not to be confused with pointers and are totally different in nature from pointers.

How handles differ from pointers?

  • Pointers are denoted using the * punctuator while handles are denoted using the ^ punctuator.
  • Handles are managed references to objects on the managed heap, pointers just point to a memory address.
  • Pointers are stable and GC cycles do not affect them, handles might keep pointing to different memory locations based on GC and memory compactions.
  • For pointers, the programmer must deleteexplicitly or else suffer a leak. For handles delete is optional.
  • Handles are type-safe while pointers are most definitely not. You cannot cast a handle to a void^.
  • Just as a new returns a pointer, a gcnew returns a handle.

Instantiating CLR objects

				void _tmain()
{
    String^ str = gcnew String("Hello World");
    Object^ o1 = gcnew Object();
    Console::WriteLine(str);
}

The gcnew keyword is used to instantiate CLR objects and it returns a handle to the object on the CLR heap. The good thing about gcnew is that it allows us to easily differentiate between managed and unmanaged instantiations.

Basically, the gcnew keyword and the ^ operator offer just about everything you need to access the BCL. But obviously you'd need to create and declare your own managed classes and interfaces.

Declaring types

CLR types are prefixed with an adjective that describes what sort of type it is. The following are examples of type declarations in C++/CLI :-

  • CLR types
    • Reference types
      • refclass RefClass{...};
      • refstruct RefClass{...};
    • Value types
      • value class ValClass{...};
      • value struct ValClass{...};
    • Interfaces
      • interfaceclass IType{...};
      • interfacestruct IType{...};
    • Enumerations
      • enumclass Color{...};
      • enumstruct Color{...};
  • Native types
    • class Native{...};
    • struct Native{...};
				using
				namespace System;

interfaceclass IDog
{
    void Bark();
};

refclass Dog : IDog
{
public:
    void Bark()
    {
        Console::WriteLine("Bow wow wow");
    }
};

void _tmain()
{
    Dog^ d = gcnew Dog();
    d->Bark();
}

There, the syntax is now so much more neater to look at than the old-syntax where the above code would have been strewn with double-underscored
聽keywords like __gc and __interface.

Boxing/Unboxing

Boxing is implicit (yaay!) and type-safe. A bit-wise copy is performed and an Object is created on the CLR heap. Unboxing is explicit - just do a reinterpret_cast and then dereference.

				void _tmain()
{
    int z = 44;
    Object^ o = z; //implicit boxingint y = *reinterpret_cast<int^>(o); //unboxing

    Console::WriteLine("{0} {1} {2}",o,z,y);

    z = 66; 
    Console::WriteLine("{0} {1} {2}",o,z,y);
}

// Output// 44 44 44// 44 66 44

The Objecto is a boxed copy and does not actually refer the int value-type which is obvious from the output of the second Console::WriteLine.

When you box a value-type, the returned object remembers the original value type.

				void _tmain()
{
    int z = 44;
    float f = 33.567;

    Object^ o1 = z; 
    Object^ o2 = f; 

    Console::WriteLine(o1->GetType());
    Console::WriteLine(o2->GetType());    
}

// Output// System.Int32// System.Single

Thus you cannot try and unbox to a different type.

				void _tmain()
{
    int z = 44;
    float f = 33.567;

    Object^ o1 = z; 
    Object^ o2 = f;

    int y = *reinterpret_cast<int^>(o2);//System.InvalidCastExceptionfloat g = *reinterpret_cast<float^>(o1);
//System.InvalidCastException }

If you do attempt to do so, you'll get a System.InvalidCastException. Talk about perfect type-safety! If you look at the IL generated, you'll see the MSIL box instruction in action. For example :-

				void Box2()
{
    float y=45;
    Object^ o1 = y;
}

gets compiled to :-

.maxstack  1
.locals (float32 V_0, object V_1)

  ldnull
  stloc.1ldc.r4     45.
  stloc.0ldloc.0
  box   [mscorlib]System.Single
  stloc.1ret

According to the MSIL docs, "The box instruction converts the 鈥榬aw鈥?valueType (an unboxed value type) into an instance of type Object (of type O). This is accomplished by creating a new object and copying the data from valueType into the newly allocated object."

Further reading

Conclusion

Alright, so why would anyone want to use C++/CLI when they can use C#, J# and that VB thingie for writing .NET code? Here are the four reasons I gave during my talk at DevCon 2003 in Trivandrum (Dec 2003).

  • Compile existing C++ code to IL (/clr magic)
  • Deterministic destruction
  • Native interop support that outmatches anything other CLI languages can offer
  • All those underscores in MC++ are gone ;-)

About Nishant Sivakumar



Editor
Site Builder
Nish is a real nice guy living in Toronto who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he has moved to Toronto so he can enjoy the cold winter and get to play in some snow.

He works for The Code Project and handles the Dundas MFC products Ultimate Toolbox, Ultimate Grid and Ultimate TCP/IP that are sold exclusively through The Code Project Storefront. He frequents the CP discussion forums when he is not coding, reading or writing. Nish hopes to visit at least three dozen countries before his human biological mechanism stops working (euphemism used to avoid the use of the d-word here), and regrets that he hasn't ever seen snow until now (will be rectified this December). Oh btw, it must be mentioned that normally Nish is not inclined to speak about himself in the 3rd person.

Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com

Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book 鈥?Extending MFC applications with the .NET Framework.

Click here to view Nishant Sivakumar's online profile.



C++鐨勭鐞嗘墿灞?/h1>

綆浠?/b>

C++綆$悊鎵╁睍鏄竴緇勮璦鎵╁睍錛屽畠甯姪Microsoft Visual C++寮鍙戜漢鍛樹負寰蔣.NET緙栧啓搴旂敤紼嬪簭銆?

綆$悊鎵╁睍鏄湁鐢ㄧ殑錛屽鏋滀綘錛?

  • 甯屾湜鎻愰珮寮鍙戜漢鍛樹嬌鐢–++緙栧啓.NET搴旂敤紼嬪簭鐨勭敓浜х巼
  • 瑕佸垎闃舵鍦板皢涓澶ф浠g爜浠庨潪綆$悊C++涓Щ妞嶅埌.NET騫沖彴涓?
  • 鎯充粠.NET Framework搴旂敤紼嬪簭涓嬌鐢ㄥ凡鏈夌殑闈炵鐞咰++緇勪歡銆?
  • 鎯充粠闈炵鐞咰++涓嬌鐢?NET Framework緇勪歡
  • 鍦ㄥ悓涓搴旂敤紼嬪簭涓販鍚堥潪綆$悊C++浠g爜鍜?NET浠g爜

C++綆$悊鎵╁睍涓哄紑鍙戜漢鍛樺畾浣?NET Framework鎻愪緵浜嗘棤姣旂殑鐏墊椿鎬с備紶緇熺殑闈炵鐞咰++鍜岀鐞咰++浠g爜鍙互鑷敱鍦版販鍚堝湪涓涓?br />搴旂敤紼嬪簭涓傜敤綆$悊鎵╁睍緙栧啓鐨勫簲鐢ㄧ▼搴忓彲浠ュ埄鐢ㄤ袱縐嶄唬鐮佺殑浼樼偣銆備嬌鐢ㄧ鐞嗘墿灞曪紝鐜版湁緇勪歡鍙互鏂逛究鍦板皝瑁呭埌.NET緇勪歡涓紝
鍦ㄤ笌.NET闆嗘垚鐨勫悓鏃朵繚鐣欏師鏈夋姇璧勩?

浠涔堟槸綆$悊鎵╁睍?
鎵╁睍鍏佽浣犲湪C++涓紪鍐欏湪.NET Framework鎺у埗涓嬭繍琛岀殑綆$悊錛堟垨.NET錛夌被銆傦紙闈炵鐞咰++綾昏繍琛屽湪浼犵粺鐨勫井杞熀浜嶹indows?
鐨勭幆澧冧腑銆傦級涓涓鐞嗙被鏄竴涓唴緗殑.NET綾伙紝鍙互瀹屽叏鍒╃敤.NET Framework銆?

綆$悊鎵╁睍鏄疺isual C++寮鍙戠郴緇熺殑鏂板叧閿瓧鍜屽睘鎬с傚畠浠厑璁稿紑鍙戜漢鍛樺喅瀹氬摢浜涚被鎴栧嚱鏁扮紪璇戜負綆$悊鎴栭潪綆$悊浠g爜銆?br />榪欎簺閮ㄥ垎鐒跺悗灝卞彲浠ュ鉤婊戝湴涓庡叾瀹冮儴鍒嗘垨澶栭儴搴撲氦浜掋?

綆$悊鎵╁睍涔熺敤浜庡湪C++婧愪唬鐮佷腑琛ㄧず.NET綾誨瀷鍜屾蹇點傝繖灝卞厑璁稿紑鍙戜漢鍛樺鏄撳湴緙栧啓.NET搴旂敤紼嬪簭錛岃屾棤闇緙栧啓棰濆浠g爜銆?

涓昏浣跨敤鐜

  • 灝嗙幇鏈変唬鐮佸鉤婊戝湴縐繪鍒?.NET
    濡傛灉浣犲湪C++浠g爜涓婃湁澶ч噺鎶曡祫錛岀鐞嗘墿灞曞皢甯綘灝嗗畠浠鉤婊戝湴杞Щ鍒?NET騫沖彴涓傚洜涓轟綘鍙互鍦ㄤ竴涓簲鐢ㄧ▼搴?-
  • 鐢氳嚦鏄悓涓鏂囦歡涓販鍚堢鐞嗗拰闈炵鐞嗕唬鐮侊紝浣犲彲浠ョ敤寰堥暱鏃墮棿杞Щ浠g爜錛屼竴涓粍浠舵帴涓涓粍浠跺湴杞崲鍒?NET涓?
  • 鎴栦綘鍙互緇х畫鍦ㄩ潪綆$悊C++涓紪鍐欑粍浠訛紝浠ュ埄鐢ㄨ璇█鐨勫己澶у姛鑳藉拰鐏墊椿鎬э紝鍙敤綆$悊鎵╁睍緙栧啓灝戦噺鐨勯珮鎬ц兘鐨?
  • 灝佽鍣紙瀹冧嬌浣犵殑浠g爜鍙互浠?NET緇勪歡涓皟鐢級銆?
  • 浠?.NET璇█涓闂瓹++緇勪歡
    綆$悊鎵╁睍鍏佽浣犱粠浠諱綍.NET璇█涓皟鐢–++綾匯備綘闇瑕佺敤鎵╁睍緙栧啓綆鍗曠殑灝佽鍣紝瀹冨皢浣犵殑C++綾誨拰鏂規硶鏆撮湶涓?
  • 綆$悊綾匯傚皝瑁呭櫒鏄畬鍏ㄧ殑綆$悊綾伙紝鍙互浠庝換浣?NET璇█涓皟鐢ㄣ傚皝瑁呭櫒綾繪槸浣滀負浜嗙鐞嗙被涓庨潪綆$悊C++綾婚棿鐨?
  • 鏄犲皠灞傘傚畠綆鍗曞湴灝嗘柟娉曡皟鐢ㄧ洿鎺ヤ紶閫掑埌闈炵鐞嗙被涓傜鐞嗘墿灞曞彲鐢ㄤ簬璋冪敤浠諱綍鍐呯疆鐨勫姩鎬侀摼鎺ュ簱錛圖LL錛夊強
  • 鍐呯疆綾匯?
  • 浠庡唴緗唬鐮佷腑璁塊棶.NET 綾?
    浣跨敤綆$悊鎵╁睍錛屼綘鍙互鍒涘緩騫朵粠C++浠g爜涓洿鎺ヨ皟鐢?NET綾匯備綘鍙互緙栧啓灝?NET緇勪歡褰撲綔浠諱綍鍏跺畠綆$悊C++綾葷殑
  • C++浠g爜銆備綘鍙互浣跨敤.NET Framework涓唴緗殑COM璋冪敤.NET綾匯備綘浣跨敤COM榪樻槸浣跨敤綆$悊鎵╁睍璁塊棶.NET緇勪歡
  • 瑕佷緷璧栦簬浣犵殑宸ョ▼銆傚湪涓浜涙儏鍐典笅錛屽埄鐢ㄧ幇鏈夌殑COM鏀寔鏄渶濂界殑閫夋嫨銆傚湪鍙︿竴浜涙儏鍐典笅錛屼嬌鐢ㄧ鐞嗘墿灞曞彲鑳?
  • 浼氬鍔犳ц兘鍜屽紑鍙戣呯殑鐢熶駭鐜囥?
  • 鍦ㄥ悓涓鍙墽琛屾枃浠朵腑鐨勭鐞嗗拰鍐呯疆浠g爜
    Visual C++緙栬瘧鍣ㄨ兘鍦ㄧ鐞嗗拰闈炵鐞嗕笂涓嬫枃涓嚜鍔ㄨ岄忔槑鐨勭炕璇戞暟鎹佹寚閽堝拰鎸囦護嫻併傝繖涓繃紼嬫槸鍏佽綆$悊鎵╁睍
  • 鏃犵紳鍦頒笌闈炵鐞嗕唬鐮佷氦浜掔殑榪囩▼銆傚紑鍙戜漢鍛樿兘澶熸帶鍒朵粈涔堟牱鐨勬暟鎹拰浠g爜鍙互綆$悊銆傞夋嫨姣忎釜綾繪垨鍑芥暟鏄鐞?
  • 榪樻槸闈炵鐞嗙殑鑳藉姏涓哄紑鍙戜漢鍛樻彁渚涗簡鏇村ぇ鐨勭伒媧繪с備竴浜涗唬鐮佹垨鏁版嵁綾誨瀷鍦ㄩ潪綆$悊鐜涓墽琛屽緱瑕佹瘮杈冨ソ銆?
  • 鍙︿竴鏂歸潰錛岀鐞嗕唬鐮佺敱浜庡紕庣墖鏀墮泦鍜岀被搴撶瓑鐗規э紝瀹冩彁楂樹簡寮鍙戜漢鍛樼殑鐢熶駭鐜囥傜幇鏈夐潪綆$悊浠g爜鍙互涓嬈?
  • 涓閮ㄥ垎鍦拌漿鍖栦負綆$悊浠g爜錛屽洜姝や繚鐣欎簡宸叉湁鐨勬姇璧勩?/li>



]]>Managed, Unmanaged, Native: What Kind of Code Is This?錛堣漿錛?/title><link>http://www.shnenglu.com/mzty/archive/2005/11/21/1213.html</link><dc:creator>姊﹀湪澶╂動</dc:creator><author>姊﹀湪澶╂動</author><pubDate>Mon, 21 Nov 2005 02:47:00 GMT</pubDate><guid>http://www.shnenglu.com/mzty/archive/2005/11/21/1213.html</guid><wfw:comment>http://www.shnenglu.com/mzty/comments/1213.html</wfw:comment><comments>http://www.shnenglu.com/mzty/archive/2005/11/21/1213.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/mzty/comments/commentRss/1213.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mzty/services/trackbacks/1213.html</trackback:ping><description><![CDATA[     鎽樿: Managed, Unmanaged, Native: What Kind of Code Is This?  <a href='http://www.shnenglu.com/mzty/archive/2005/11/21/1213.html'>闃呰鍏ㄦ枃</a><img src ="http://www.shnenglu.com/mzty/aggbug/1213.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mzty/" target="_blank">姊﹀湪澶╂動</a> 2005-11-21 10:47 <a href="http://www.shnenglu.com/mzty/archive/2005/11/21/1213.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>娉涘瀷緙栫▼ 錛岀煡澶氬皯錛?/title><link>http://www.shnenglu.com/mzty/archive/2005/11/18/1188.html</link><dc:creator>姊﹀湪澶╂動</dc:creator><author>姊﹀湪澶╂動</author><pubDate>Fri, 18 Nov 2005 05:51:00 GMT</pubDate><guid>http://www.shnenglu.com/mzty/archive/2005/11/18/1188.html</guid><wfw:comment>http://www.shnenglu.com/mzty/comments/1188.html</wfw:comment><comments>http://www.shnenglu.com/mzty/archive/2005/11/18/1188.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/mzty/comments/commentRss/1188.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mzty/services/trackbacks/1188.html</trackback:ping><description><![CDATA[     鎽樿: 褰掔撼璧鋒潵錛屾硾鍨嬫瘮闈炴硾鍨嬪叿鏈変笅闈袱涓紭鐐癸細 1銆?nbsp; 鏇村姞瀹夊叏 鍦ㄩ潪娉涘瀷緙栫▼涓紝铏界劧鎵鏈夌殑涓滆タ閮藉彲浠ヤ綔涓篛bject浼犻掞紝浣嗘槸鍦ㄤ紶閫掔殑榪囩▼涓厤涓嶄簡瑕佽繘琛岀被鍨嬭漿鎹€傝岀被鍨嬭漿鎹㈠湪榪愯鏃舵槸涓嶅畨鍏ㄧ殑銆備嬌鐢ㄦ硾鍨嬬紪紼嬪皢鍙互鍑忓皯涓嶅繀瑕佺殑綾誨瀷杞崲錛屼粠鑰屾彁楂樺畨鍏ㄦс?2銆?nbsp; 鏁堢巼鏇撮珮 鍦ㄩ潪娉涘瀷緙栫▼涓紝灝嗙畝鍗曠被鍨嬩綔涓篛bject浼犻掓椂浼氬紩璧稡oxing鍜孶nboxing鎿嶄綔錛岃繖涓や釜榪囩▼閮芥槸鍏鋒湁...  <a href='http://www.shnenglu.com/mzty/archive/2005/11/18/1188.html'>闃呰鍏ㄦ枃</a><img src ="http://www.shnenglu.com/mzty/aggbug/1188.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mzty/" target="_blank">姊﹀湪澶╂動</a> 2005-11-18 13:51 <a href="http://www.shnenglu.com/mzty/archive/2005/11/18/1188.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鍏充簬 鈥滄墭綆′笌闈炴墭綆?鈥?澶у閮界煡閬撻偅浜涳紝鏉ヨ璇達紵http://www.shnenglu.com/mzty/archive/2005/11/18/1186.html姊﹀湪澶╂動姊﹀湪澶╂動Fri, 18 Nov 2005 05:36:00 GMThttp://www.shnenglu.com/mzty/archive/2005/11/18/1186.htmlhttp://www.shnenglu.com/mzty/comments/1186.htmlhttp://www.shnenglu.com/mzty/archive/2005/11/18/1186.html#Feedback1http://www.shnenglu.com/mzty/comments/commentRss/1186.htmlhttp://www.shnenglu.com/mzty/services/trackbacks/1186.html涓庨潪鎵樼浠g爜浜掓搷浣?

涓轟簡浣垮緱鐜板瓨鐨勯潪.NET浠g爜閮藉彲鐢紝寰蔣鐨勮璁′嬌寰桟LR鍙互鍦ㄥ簲鐢ㄧ▼搴忛噷鍚屾椂鍖呭惈鎵樼鍜岄潪鎵樼浠g爜銆侰LR鏀寔涓夌浜掓搷浣滐細

鎵樼浠g爜璋冪敤DLL錛堝氨鏄垜浠鐨勫姩鎬侀摼鎺ュ簱錛変腑鐨勯潪鎵樼鍑芥暟

鎵樼浠g爜浣跨敤鐜板瓨鐨凜OM緇勫緩錛堥潪鎵樼緇勫緩浣滀負COM鏈嶅姟鍣級

闈炴墭綆′唬鐮佷嬌鐢ㄦ墭綆$被鍨嬶紙鎵樼綾誨瀷浣滀負COM鏈嶅姟鍣級


浜掓搷浣滄?BR>鏄惁鍙互鍦?.NET 妗嗘灦紼嬪簭涓嬌鐢?COM 瀵硅薄錛?BR>鏄傛偍鐜板湪閮ㄧ講鐨勪換浣?COM 緇勪歡閮藉彲浠ュ湪鎵樼浠g爜涓嬌鐢ㄣ傞氬父鎯呭喌涓嬶紝鎵闇鐨勮皟鏁存槸瀹屽叏鑷姩榪涜鐨勩?/P>

鐗瑰埆鏄紝鍙互浣跨敤榪愯鏃跺彲璋冪敤鍖呰 (RCW) 浠?.NET 妗嗘灦璁塊棶 COM 緇勪歡銆傛鍖呰灝?COM 緇勪歡鎻愪緵鐨?COM 鎺ュ彛杞崲涓轟笌 .NET 妗嗘灦鍏煎鐨勬帴鍙c傚浜?OLE 鑷姩鍖栨帴鍙o紝RCW 鍙互浠庣被鍨嬪簱涓嚜鍔ㄧ敓鎴愶紱瀵逛簬闈?OLE 鑷姩鍖栨帴鍙o紝寮鍙戜漢鍛樺彲浠ョ紪鍐欒嚜瀹氫箟 RCW錛屾墜鍔ㄥ皢 COM 鎺ュ彛鎻愪緵鐨勭被鍨嬫槧灝勪負涓?.NET 妗嗘灦鍏煎鐨勭被鍨嬨?/P>

鏄惁鍙互鍦?COM 紼嬪簭涓嬌鐢?.NET 妗嗘灦緇勪歡錛?BR>鏄傛偍鐜板湪鍒涘緩鐨勬墭綆$被鍨嬮兘鍙互閫氳繃 COM 璁塊棶銆傞氬父鎯呭喌涓嬶紝鎵闇鐨勯厤緗槸瀹屽叏鑷姩榪涜鐨勩傛墭綆″紑鍙戠幆澧冪殑鏌愪簺鏂扮壒鎬т笉鑳藉湪 COM 涓闂備緥濡傦紝涓嶈兘鍦?COM 涓嬌鐢ㄩ潤鎬佹柟娉曞拰鍙傛暟鍖栨瀯閫犲嚱鏁般備竴鑸紝鎻愬墠紜畾緇欏畾綾誨瀷鎵閽堝鐨勭敤鎴鋒槸涓縐嶈緝濂界殑鍔炴硶銆傚鏋滅被鍨嬮渶瑕佸湪 COM 涓嬌鐢紝鎮ㄥ皢琚檺鍒跺湪浣跨敤 COM 鍙闂殑鐗規с?/P>

榛樿鎯呭喌涓嬶紝鎵樼綾誨瀷鍙兘鏄彲瑙佺殑錛屼篃鍙兘鏄笉鍙鐨勶紝榪欑敱鐢ㄤ簬緙栧啓鎵樼綾誨瀷鐨勮璦鍐沖畾銆?/P>

鐗瑰埆鏄紝鍙互浣跨敤 COM 鍙皟鐢ㄥ寘瑁?(CCW) 浠?COM 璁塊棶 .NET 妗嗘灦緇勪歡銆傝繖涓?RCW錛堣鍙傞槄涓婁竴涓棶棰橈級鐩鎬技錛屼絾瀹冧滑鐨勬柟鍚戠浉鍙嶃傚悓鏍鳳紝濡傛灉 .NET 妗嗘灦寮鍙戝伐鍏蜂笉鑳借嚜鍔ㄧ敓鎴愬寘瑁咃紝鎴栬呭鏋滆嚜鍔ㄦ柟寮忎笉鏄偍鎵闇瑕佺殑錛屽垯鍙互寮鍙戣嚜瀹氫箟鐨?CCW銆?/P>

 鏄惁鍙互鍦?.NET 妗嗘灦紼嬪簭涓嬌鐢?Win32 API錛?BR>鏄備嬌鐢?P/Invoke錛?NET 妗嗘灦紼嬪簭鍙互閫氳繃闈欐?DLL 鍏ュ彛鐐圭殑鏂瑰紡鏉ヨ闂湰鏈轟唬鐮佸簱銆?/P>

涓嬮潰鏄?C# 璋冪敤 Win32 MessageBox 鍑芥暟鐨勭ず渚嬶細

using System;
using System.Runtime.InteropServices;

class MainApp
{
    [DllImport("user32.dll", EntryPoint="MessageBox")]
    public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType);

    public static void Main()
    {
        MessageBox( 0, "鎮ㄥソ錛岃繖鏄?PInvoke錛?, ".NET", 0 );
    }
}

-----------------------------------------------------------------------------
浠涔堟槸鎵樼浠g爜鍜屾墭綆℃暟鎹紵

鎵樼浠g爜鏄負闈㈠悜鍏叡璇█榪愯搴撶殑鏈嶅姟緙栧啓鐨勪唬鐮侊紙璇峰弬闃呪滀粈涔堟槸鍏叡璇█榪愯搴擄紵鈥濓級銆備負浜嗛潰鍚戣繖浜涙湇鍔★紝璇ヤ唬鐮佸繀欏誨悜榪愯搴撴彁渚涙渶浣庣駭鍒殑淇℃伅錛堝厓鏁版嵁錛夈傞粯璁ゆ儏鍐典笅錛屾墍鏈?C#銆乂isual Basic .NET 鍜?JScript .NET 浠g爜閮藉彈鎵樼銆傞粯璁ゆ儏鍐典笅錛孷isual Studio .NET C++ 浠g爜涓嶅彈鎵樼錛屼絾緙栬瘧鍣ㄥ彲浠ラ氳繃鎸囧畾鍛戒護琛屽紑鍏?(/CLR) 鏉ヤ駭鐢熸墭綆′唬鐮併?
涓庢墭綆′唬鐮佸瘑鍒囩浉鍏崇殑鏄墭綆℃暟鎹紙鐢卞叕鍏辮璦榪愯搴撶殑鍨冨溇鍥炴敹鍣ㄥ垎閰嶅拰閲婃斁鐨勬暟鎹級銆傞粯璁ゆ儏鍐典笅錛孋#銆乂isual Basic 鍜?JScript .NET 鏁版嵁鍙楁墭綆°傜劧鑰岋紝閫氳繃浣跨敤鐗規畩鐨勫叧閿瓧鍙互灝?C# 鏁版嵁鏍囪涓洪潪鎵樼銆傞粯璁ゆ儏鍐典笅錛孷isual Studio .NET C++ 鏁版嵁涓嶅彈鎵樼錛堝嵆浣垮湪浣跨敤 /CLR 寮鍏蟲椂錛夛紝浣嗗湪浣跨敤 C++ 鎵樼鎵╁睍鏃訛紝鍙互閫氳繃浣跨敤 __gc 鍏抽敭瀛楀皢綾繪爣璁頒負鎵樼銆傛濡傚悕縐版墍鏆楃ず鐨勶紝榪欐剰鍛崇潃鐢ㄤ簬綾誨疄渚嬬殑鍐呭瓨鍙楀瀮鍦懼洖鏀跺櫒鐨勬墭綆°傚彟澶栵紝綾繪垚涓?.NET 妗嗘灦紺懼尯鐨勪竴鍚嶅畬鍏ㄥ弬涓庣殑鎴愬憳錛岃繖鏃㈠甫鏉ヤ簡濂藉錛屼篃甯︽潵浜嗛檺鍒躲傚叾涓竴涓ソ澶勬槸涓庣敤鍏朵粬璇█緙栧啓鐨勭被鐨勬紜簰鎿嶄綔鎬э紙渚嬪錛屾墭綆?C++ 綾誨彲浠ヤ粠 Visual Basic 綾葷戶鎵匡級銆傚叾涓竴涓檺鍒舵槸鎵樼綾誨彧鑳戒粠鍩虹被緇ф壙銆?/P>

--------------------------------------------------------------------------------
浠涔堟槸鍏叡璇█榪愯鏃?(CLR)錛?BR>鍏叡璇█榪愯鏃舵槸 .NET 妗嗘灦搴旂敤紼嬪簭鐨勬墽琛屽紩鎿庛?/P>

瀹冩彁渚涜澶氭湇鍔★紝鍖呮嫭錛?/P>

浠g爜綆$悊錛堝姞杞藉拰鎵ц錛?/P>


搴旂敤紼嬪簭鍐呭瓨闅旂


綾誨瀷瀹夊叏楠岃瘉


IL 鍒版湰鏈轟唬鐮佺殑杞崲


鍏冩暟鎹紙澧炲己鐨勭被鍨嬩俊鎭級璁塊棶


涓烘墭綆″璞$鐞嗗唴瀛?/P>


寮哄埗浠g爜璁塊棶瀹夊叏


寮傚父澶勭悊錛屽寘鎷法璇█寮傚父


鎵樼浠g爜銆丆OM 瀵硅薄鍜岀幇鏈?DLL錛堥潪鎵樼浠g爜鍜屾暟鎹級涔嬮棿鐨勪簰鎿嶄綔


鑷姩榪涜瀵硅薄甯冨眬


瀵瑰紑鍙戜漢鍛樻湇鍔★紙閰嶇疆銆佽皟璇曠瓑錛夌殑鏀寔

浠涔堟槸鍏叡綾誨瀷緋葷粺 (CTS)錛?BR>鍏叡綾誨瀷緋葷粺鏄淇℃伅綾誨瀷緋葷粺錛屽畠琚唴緗湪鍏叡璇█榪愯鏃朵腑錛屾敮鎸佸ぇ澶氭暟緙栫▼璇█涓殑綾誨瀷鍜屾搷浣溿傚叕鍏辯被鍨嬬郴緇熸敮鎸佸ぇ閲忕紪紼嬭璦鐨勫畬鍏ㄥ疄鐜般?/P>

 

浠涔堟槸鍏叡璇█瑙勮寖 (CLS)錛?BR>鍏叡璇█瑙勮寖鏄竴緇勭粨鏋勫拰闄愬埗錛岀敤浣滃簱緙栧啓鑰呭拰緙栬瘧鍣ㄧ紪鍐欒呯殑鎸囧崡銆傚畠浣夸換浣曟敮鎸?CLS 鐨勮璦閮藉彲浠ュ畬鍏ㄤ嬌鐢ㄥ簱錛屽茍涓斾嬌榪欎簺璇█鍙互鐩鎬簰闆嗘垚銆傚叕鍏辮璦瑙勮寖鏄叕鍏辯被鍨嬬郴緇熺殑瀛愰泦銆傚浜庨偅浜涢渶瑕佺紪鍐欎唬鐮佷緵鍏朵粬寮鍙戜漢鍛樹嬌鐢ㄧ殑搴旂敤紼嬪簭寮鍙戜漢鍛橈紝鍏叡璇█瑙勮寖涔熼潪甯擱噸瑕併傚鏋滃紑鍙戜漢鍛橀伒寰?CLS 瑙勫垯鏉ヨ璁″叕鍏辮闂殑 API錛岄偅涔堝氨鍙互鍦ㄦ敮鎸佸叕鍏辮璦榪愯鏃剁殑浠諱綍鍏朵粬緙栫▼璇█涓緢瀹規槗鍦頒嬌鐢ㄨ繖浜?API銆?/P>

 

浠涔堟槸 Microsoft 涓棿璇█ (MSIL)錛?BR>MSIL 鏄笌 CPU 鏃犲叧鐨勬寚浠ら泦銆?NET 妗嗘灦紼嬪簭琚紪璇戞垚 MSIL銆傚畠鍖呭惈鍔犺澆銆佸瓨鍌ㄣ佸垵濮嬪寲鍜岃皟鐢ㄥ璞℃柟娉曠殑鎸囦護銆?/P>

涓庡厓鏁版嵁鍜屽叕鍏辯被鍨嬬郴緇熺粨鍚堬紝MSIL 鍏佽鐪熸鐨勮法璇█闆嗘垚銆?/P>

MSIL 鍦ㄦ墽琛屽墠琚漿鎹負鏈哄櫒浠g爜錛岃屼笉鏄竴杈硅В閲婁竴杈規墽琛屻?/P>

 

 



]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              亚洲免费网站| 亚洲一区二区欧美| 久久免费视频一区| 久久精品免费播放| 精品999网站| 欧美激情精品久久久久久蜜臀 | 亚洲国产精品久久| 久久免费视频在线观看| 最新日韩av| 99精品国产在热久久下载| 国产精品美女久久| 久久久久九九九九| 麻豆精品一区二区av白丝在线| 亚洲人体大胆视频| 亚洲午夜一区| 亚洲高清视频一区二区| 99精品国产高清一区二区| 国产日产高清欧美一区二区三区| 蜜桃av噜噜一区| 欧美三级电影精品| 久久国产精品亚洲va麻豆| 裸体丰满少妇做受久久99精品| 在线视频免费在线观看一区二区| 亚洲欧美美女| 日韩一区二区精品视频| 亚洲欧美中文在线视频| 亚洲黄色精品| 午夜国产精品视频| 一本色道久久88亚洲综合88| 午夜精品久久久久久久99热浪潮| 亚洲精品欧美日韩| 午夜精品久久久久久久久久久久| 亚洲国产婷婷香蕉久久久久久99| 久久精品久久综合| 亚洲午夜极品| 欧美激情在线狂野欧美精品| 国产麻豆9l精品三级站| 亚洲精品视频在线观看网站| 一区二区视频欧美| 亚洲日韩第九十九页| 久久激情五月丁香伊人| 亚洲欧美大片| 欧美日韩国产成人在线91| 免费看成人av| 国产主播精品在线| 亚洲欧美日韩综合一区| 亚洲先锋成人| 欧美日韩国产精品一区二区亚洲| 免费欧美在线视频| 国产一区二区按摩在线观看| 亚洲色图在线视频| 亚洲视频免费| 欧美美女操人视频| 亚洲国产老妈| 亚洲精品老司机| 欧美gay视频| 欧美国产日韩a欧美在线观看| 国色天香一区二区| 午夜精品久久久久久久男人的天堂| 亚洲一区自拍| 国产精品福利av| 亚洲最新合集| 亚洲欧美中文另类| 国产精品每日更新| 亚洲视频在线观看网站| 午夜精品福利电影| 国产精品日韩精品欧美精品| 中文精品视频| 欧美在线观看一区二区| 国产三区精品| 久久久女女女女999久久| 久久综合狠狠综合久久综合88| 国产在线精品二区| 久久在线视频| 亚洲精品乱码久久久久久黑人| 99在线精品免费视频九九视| 欧美视频在线观看 亚洲欧| 一本一本久久a久久精品牛牛影视| 亚洲桃花岛网站| 国产精品视区| 久久一区视频| 亚洲美女黄色| 久久精品91久久香蕉加勒比| 伊人久久大香线| 欧美不卡福利| 亚洲在线播放| 米奇777在线欧美播放| 一本大道av伊人久久综合| 国产精品人人做人人爽| 久久国产精品网站| 亚洲国产高清aⅴ视频| 亚洲一区二区不卡免费| 国产在线不卡视频| 欧美日韩免费网站| 久久久99国产精品免费| 欧美成人r级一区二区三区| 中文av字幕一区| 激情综合自拍| 欧美日韩一二区| 久久精品国产一区二区电影 | 欧美午夜精彩| 性色av一区二区三区在线观看| 欧美福利专区| 先锋影音久久| 欧美中文字幕在线观看| 亚洲精选久久| 在线色欧美三级视频| 国产精品大片wwwwww| 蜜臀久久99精品久久久画质超高清| 99在线精品视频| 欧美大学生性色视频| 欧美一级片一区| 在线亚洲电影| 亚洲破处大片| 狠狠狠色丁香婷婷综合激情| 欧美亚洲成人精品| 欧美风情在线| 久久亚洲免费| 欧美自拍偷拍午夜视频| 亚洲丝袜av一区| 99re6这里只有精品视频在线观看| 久久综合色一综合色88| 欧美一级一区| 亚洲欧美综合v| 一区二区三区国产在线观看| 亚洲破处大片| 亚洲国产片色| 亚洲国产成人久久综合| 国产一区自拍视频| 国产日韩欧美在线播放| 国产精品日产欧美久久久久| 国产精品mv在线观看| 欧美女人交a| 欧美激情精品久久久久久黑人 | 国内精品视频一区| 国产欧美日韩精品在线| 国产精品久久久久久福利一牛影视| 欧美成人亚洲成人日韩成人| 麻豆精品在线视频| 久热成人在线视频| 久久躁狠狠躁夜夜爽| 免播放器亚洲| 欧美成人免费播放| 欧美国产日韩在线观看| 欧美丰满高潮xxxx喷水动漫| 欧美高清视频在线| 欧美激情一级片一区二区| 欧美劲爆第一页| 欧美日韩免费高清一区色橹橹| 欧美久久久久久久久| 欧美日韩视频在线| 国产精品久久久久91| 国产欧美精品xxxx另类| 国产视频欧美视频| 激情久久久久久| 亚洲国产视频a| 一级日韩一区在线观看| 亚洲一区免费在线观看| 欧美中文字幕视频在线观看| 久久一区欧美| 亚洲国产精品久久久久久女王| 亚洲人成网站在线播| 亚洲视频你懂的| 欧美在线免费观看视频| 美女福利精品视频| 欧美精品三级在线观看| 国产精品视屏| 亚洲国产精品成人精品| 一区二区三区成人| 久久国产精品久久国产精品| 你懂的视频一区二区| 日韩视频中文字幕| 欧美一区二区三区啪啪| 欧美激情按摩在线| 国产精品亚洲美女av网站| 欧美日韩国产影院| 亚洲欧美日韩在线| 久久深夜福利免费观看| 欧美日韩国产首页| 亚洲伊人第一页| 免费在线欧美黄色| 国产精品一区二区男女羞羞无遮挡| 永久免费视频成人| 中文一区在线| 欧美国产日韩一二三区| 亚洲午夜一级| 欧美日韩99| 一区二区在线免费观看| 亚洲一区国产| 欧美电影在线| 欧美一级黄色录像| 欧美日韩国产综合视频在线观看中文 | 午夜精品久久久久久久99黑人| 欧美激情精品久久久久久蜜臀| 国产日韩精品一区二区| 一区二区精品| 欧美激情第10页| 久久精品国产亚洲a| 国产精品美女主播在线观看纯欲| 亚洲精品乱码久久久久久|