锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
: 姣斿鎴戞湁涓涓狢MyButton鐨勭被錛屾垜鐜板湪鏈変粬鐨勪竴涓猦andle
: 緙栬瘧鍣ㄦ庝箞鏍規嵁榪欎釜鍙ユ焺鎵懼埌CMyButton鐨勪唬鐮佺殑錛?/em>
銆?鍦?鏌愭煇 鐨勫ぇ浣滀腑鎻愬埌: 銆?br>: 榪欎釜鍜孫S/Compiler娌″叧緋伙紝鏄簱璧風殑浣滅敤
: 浠ヤ粠鏌愪釜鏂囩珷閲岀湅鐨勶紝璇碝FC鐢ㄤ簡涓涓ぇmap錛屾病楠岃瘉榪?br>: 鏈夋湰璁睪DI鐨勪功閲岋紝鐢ㄤ簡WNDCLASS閲岀殑extra bytes鏉ュ疄鐜扮殑榪欎釜鏄犲皠
MFC鐨勫簲鐢ㄩ噷錛屾瘡涓狹FC綰跨▼錛堝繀欏昏浣跨敤MFC鏂瑰紡鍚姩鐨勭嚎紼嬶級閮界淮鎶ゆ湁涓涓狹FC object鍜孒WND涔嬮棿鐨?/p>
mapping錛屾暣涓狹FC妗嗘灦灝辨槸浣跨敤榪欎釜鏈哄埗鏉ュ疄鐜板簲鐢ㄧ駭C++瀵硅薄鍜岀郴緇熺駭鍘熺敓紿楀彛鍐呮牳瀵硅薄涔嬮棿鐨勫叧鑱旓紱
鍥犱負榪欎釜mapping鏄互綰跨▼涓哄崟浣嶆潵緇存姢鐨勶紝姣忎釜綰跨▼闂翠簰涓嶅叧鑱旓紝鎵浠ワ紝涓涓簲鐢ㄩ噷瀵逛簬娑夊強UI紿楀彛鐨?/p>
浠誨姟鏈濂芥槸閮芥斁鍦ㄥ悓涓涓嚎紼嬮噷闈紝涓鑸氨鏄綋鍓嶈繘紼嬬殑涓葷嚎紼嬶紝鍚﹀垯鍙兘鍑虹幇MFC object鍜孒WND涔嬮棿
鍏寵仈涓嶄笂鐨勯棶棰橈紝鑰屼笖榪欐牱鐨勯棶棰樿繕寰堥殣钄姐?br>
鑷充簬WNDCLASS緇撴瀯鑷甫鐨別xtra bytes鍩燂紝鏄互鍓嶇己涔忓簲鐢ㄦ鏋剁殑鏃朵唬錛屼嬌鐢╓in32 API鐩存帴寮鍙戞椂錛岃姣忎釜
紿楀彛綾伙紙榪欓噷鐨勭被錛屼笉鏄疌++ class鐨勬蹇碉紝鑰屾槸Windows緋葷粺紿楀彛瀹氫箟鏃剁殑涓縐嶆暟鎹粨鏋勶級閮借兘鏈変釜闄?/p>
甯︿竴浜涢澶栫殑鑷畾涔夋暟鎹殑絀洪棿錛岃繖涓┖闂村線寰琚敤鏉ュ瓨鏀句笌褰撳墠紿楀彛綾葷浉鍏崇殑鐢ㄦ埛鏁版嵁錛岄氬父鏄寚鍚?/p>
鏌愪釜鍐呭瓨鍖哄煙鐨勬寚閽堬紝褰撶▼搴忔搷浣滆繖涓睘浜庤繖涓獥鍙g被鐨勭獥鍙f椂灝卞彲浠ユ牴鎹繖涓檮甯︾殑鑷畾涔夋暟鎹紙鎴?/p>
鑰呮寚閽堬級鏉ユ搷浣滃搴旂殑鍏寵仈鑷畾涔夋暟鎹紱寰堝鍚庢潵鍑虹幇鐨勬鏋訛紝涔熼兘浣跨敤浜嗚繖涓猠xtra bytes鍩燂紝鏉ュ瓨鏀?/p>
妗嗘灦鏈韓鐨勪竴浜涘拰紿楀彛綾葷浉鍏寵仈鐨勬暟鎹粨鏋勩備粠鐩墠瓚嬪娍鐪嬶紝鐩存帴浣跨敤WNDCLASS浠ュ強extra bytes鐨勫彲鑳?/p>
鎬ф槸寰箮鍏跺井浜嗭紝浣嗘槸濡傛灉瑕佸仛濂藉師鐢熷簲鐢ㄧ殑寮鍙戯紝寰堝搴曞眰鐨勫疄鐜扮粏鑺傛渶瑕佽繕鏄鐭ラ亾涓涓嬶紝浠ヤ究浜?/p>
浼樺寲緇撴瀯鍜屾ц兘錛屼互鍙婂嚭閿欐椂鐨勮皟璇曞鐞嗭紱鍥犱負鏃犺鏄疻inform/WPF錛岃繕鏄法騫沖彴鐨刉TL/QT/WxWindows絳?/p>
絳夋柊鍨嬬殑鏈哄埗鎴栬呮鏋躲佺被搴擄紝鍙鏄湪Windows騫沖彴涓婃惌寤虹殑錛岄偅閮芥槸鍩轟簬鍓嶉潰璇磋繃鐨勮繖濂楁渶鍩烘湰涔熸槸
鏈鏍稿績鐨刉in32 API鍩虹涔嬩笂銆?/p>
CRT incorrectly set a lock at _global_unlock which resulted in such issue.
In CLR-mixed mode, during the inialization of static local object, CRT would call _atexit_m(_CPVFV func) in msilexit.cpp to register a special __clrcall callback function which would be called back to destroy such static object when the current AppDomain quited.
In the multithread environment, _atexit_helper which was invoked by _atexit_m, could register such callbace function successfully because it had been guarded by __global_lock() and __global_unlock(). But in the same environment, the _atexit_m would fail to assign the correct value to __onexitbegin_m and __onexitend_m.
__onexitbegin_m and __onexitend_m were shared by the different threads; It's the key point of such issue. For example, the following statements,
__onexitbegin_m = (_CPVFV *)_encode_pointer(onexitbegin_m);
__onexitend_m = (_CPVFV *)_encode_pointer(onexitend_m);
should also guarded by __global_lock() and __global_unlock() or other syn primitives.
__global_lock();
__onexitbegin_m = (_CPVFV *)_encode_pointer(onexitbegin_m);
__onexitend_m = (_CPVFV *)_encode_pointer(onexitend_m);
__global_unlock();
extern "C" int __clrcall _atexit_m(_CPVFV func)
{
MANAGED_ASSERT(AppDomain::CurrentDomain->IsDefaultAppDomain(), "This fuction must be called in the default domain");
__global_lock();
_CPVFV* onexitbegin_m = (_CPVFV*)_decode_pointer(__onexitbegin_m);
_CPVFV* onexitend_m = (_CPVFV*)_decode_pointer(__onexitend_m);
__global_unlock();
int retval = _atexit_helper((_CPVFV)_encode_pointer(func), &__exit_list_size, &onexitend_m, &onexitbegin_m);
__global_lock();
__onexitbegin_m = (_CPVFV*)_encode_pointer(onexitbegin_m);
__onexitend_m = (_CPVFV*)_encode_pointer(onexitend_m);
__global_unlock();
return retval;
}
The error comes directly from the CLR when a type has multiple definitions that are not consistent based upon the ODR, one-definition-rule for types. And, the linker itself isn't involved.
For example, with one module compile with /D_SECURE_SCL=0, while another is compiled with _SECURE_SCL=1;
At first, it's found that with _SECURE_SCL, the only thing that could be different as following,
#if _SECURE_SCL
typedef _Range_checked_iterator_tag _Checked_iterator_category;
#endif
But, actually, it's not the typedef that changed the layout the these iterators (istreambuf_iterator/ostreambuf_iterator), and further they don't really use the extra pointer that _SECURE_SCL adds.
Finally, it's found the root cause is that, these iterators, istreambuf_iterator/ostreambuf_iterator had been moved from <xutility> to <streambuf>, and their ultimate base class had been changed from _Iterator_base_secure to _Iterator_base. And, the layout of _Iterator_base would be different between HID and no-HID, and between SCL and no-SCL. It is the cause where the issue comes from.
What we can learn from such issue,
These iterators really shouldn't derive from either _Iterator_base_secure or _Iterator_base, because these classes contain data members (pointers) which are entirely unused. It would result in unnecessary bloat and extra work being performed in ctor/dtor etc.
Introduce a new class, _Iterator_base_universal, which is defined identically regardless of HID/no-HID and SCL/no-SCL. It would contains the three internal typedefs that all standard iterators need to have, and nothing else. And _Iterator_base (in all of its variants) and _Iterator_base_secure now should derive from _Iterator_base_universal to get these typedefs.
Now, when an iterator wants these typedefs, but not the data members of _Iterator_base and _Iterator_base_secure, it could derive from _Iterator_base_universal. And istreambuf_iterator and ostreambuf_iterator are now as small as possible, and keep identical representations or layout across HID/no-HID, SCL/no-SCL.
铏界劧瀵逛簬鍏ㄦ柊鐨勫伐紼嬮」鐩紝鎺ㄨ崘閫氳繃.net瀹炵幇錛屼絾鏄紝鍙浣犲伐浣滃湪Windows騫沖彴涓婏紝蹇呯劧浼氶亣鍒板拰COM鐩稿叧鐨勬妧鏈拰鏈哄埗錛屾棤璁烘槸澶ч噺鐨刲egacy鐨勫伐紼嬪拰浠g爜錛岃繕鏄綔涓篛S閲嶈鍔熻兘浠ュ強native緇勪歡鐨勯閫変氦浜掑艦寮忓拰鎺ュ彛鏆撮湶鏂瑰紡錛屾瘮濡侱irectX API錛屾瘮濡備竴浜沇MI鐨凙PI錛涙渶鏈夎叮鐨勬槸錛屽嵆浣挎槸.net鐨勬牳蹇僀LR鏈韓涔熸槸涓涓狢OM緇勪歡錛屽彲浠ラ氳繃Host鐩稿叧鎺ュ彛璁﹏ative搴旂敤鏉ュ姞杞斤紝浠ュ湪褰撳墠榪涚▼涓惎鍔ㄦ暣涓狢LR鐨勮櫄鎷熸墽琛岀幆澧冩垨鑰呭彨鎵樼鎵ц鐜(managed executive environment)銆?/p>
鎶婃彙COM鏈変袱鐐瑰緢鍏抽敭錛?br>1錛塈nterface-based design錛屼粠璁捐鍜岀紪鐮佹濊礬涓婂氨鏄瀹屽叏鍩轟簬鎺ュ彛錛?br>2錛塚irtualTable-based binary compatibility, 瀹炵幇涓婃棤璁轟綍縐嶈璦鎴栬呮満鍒訛紝鍙絎﹀悎鍩轟簬铏氳〃鐨勪簩榪涘埗鍏煎瑙勮寖錛屽氨閮藉彲浠ュ疄鏂斤紱
COM浠呬粎鏄釜瑙勮寖錛屽熀浜嶤OM鐨勫叿浣撴妧鏈潪甯鎬箣澶氾紝OLE錛孉utomation錛孲tructural storage錛孉ctiveX...姹楃墰鍏呮爧錛岃繕鏈塁OM+錛岃繖涓槸鎻愪緵浼佷笟綰у紑鍙戝繀澶囩殑涓浜涘熀紜鍔熻兘鍜岃鏂斤紝姣斿錛屼簨鍔$鐞嗘満鍒訛紝瀵硅薄姹狅紝瀹夊叏綆$悊錛屾秷鎭槦鍒?..闇瑕佹寚鍑猴紝鐩墠鍗充究鏄?net Framework涔熸病鏈夊疄鐜癈OM+鎵鎻愪緵榪欎簺鏈哄埗錛屽彧鏄畝鍗曠殑灝佽浜嗗悗鑰呫?/p>
COM鎶鏈腑鍙兘鏈変竴浜涙瘮杈冨洶闅劇殑鍦版柟錛屾帴鍙g殑涓鑷存э紝瀵硅薄鐨勮仛鍚堝拰鐢熷懡鍛ㄦ湡錛屽闂達紝璺ㄥ闂寸殑鎺ュ彛璁塊棶錛屽悕瀛楀璞★紝絳夌瓑錛涜繖浜涘茍涓嶆槸COM瑙勮寖浜轟負鍒墮犵殑鍥伴毦錛岃屾槸涓轟簡璁捐鍜屾彁渚涳紝鍙互璺ㄨ繘紼嬪拰鏈哄櫒杈圭晫錛岃法寮傛瀯騫沖彴錛堝綋鐒跺繀欏誨疄鐜頒簡COM鎵瑙勫畾鐨勫熀紜鏈嶅姟錛夛紝閫忔槑鍖栧叿浣撳璞$被鍨嬪強瀵硅薄鐢熷懡鍛ㄦ湡錛屼究浜庣粺涓閮ㄧ講鍜岀増鏈鐞嗙殑緇勪歡鎶鏈紝鎵蹇呴』浠樺嚭鐨勪唬浠鳳紝榪欎釜浠d環浠庡紑鍙戜漢鍛樿搴︾湅鍏蜂綋琛ㄧ幇涓猴紝姒傚康鐞嗚В鐨勫洶闅句互鍙婂叿浣撲簩榪涘埗瀹炵幇鐨勫洶闅撅紱
涓嶈繃浠庡彟涓涓搴︾湅錛孋OM宸茬粡寰堝鏄撲簡錛?br>a) COM瑙勮寖宸叉妸瑕佽揪鑷磋繖浜涚洰鏍囩殑緋葷粺錛屾墍蹇呴』鎻愪緵鐨勬帴鍙e拰鐗規ф娊璞′簡鍑烘潵錛屽彧涓嶈繃涓轟簡琛ㄨ揪榪欎簺鎶借薄鐨勬蹇佃屾柊閫犵殑鏈鍚嶈瘝鏈変簺闄岀敓鍜岀獊鍏錛涘鏋滆閬囧埌鐩鎬技闂鐨勬瘡涓涓璁″拰寮鍙戜漢鍛橀兘鑷繁鏉ュ仛鎶借薄錛屾湭蹇呬細鐢熸垚鏇村ソ鐨勬柟妗堬紱
b) 涓轟簡甯姪璁捐鍜屽紑鍙戜漢鍛橈紝浜轟滑鎻愪緵浜嗗緢澶氱殑寮鍙戝簱錛屼互鎻愰珮COM寮鍙戠殑姝g‘鎬у拰鏁堢巼錛涙渶鏄捐憲鐨勫氨鏄疢FC涓叧浜嶤OM/OLE鐨勮緟鍔╃被鍜屽嚱鏁幫紝浠ュ強涓轟簡COM鑰岀敓鐨凙TL錛涗粠鏈川涓婄湅錛岃繖浜涚被搴撻兘鏄妸COM瑙勮寖涓繀欏誨疄鐜扮殑錛學indows騫沖彴鏈韓娌℃湁鎻愪緵錛屽叿浣撹璁″拰寮鍙戜漢鍛樺疄闄呭疄鏂芥椂浼氶噸澶嶅疄鐜扮殑錛屽悓鏃跺張闈炲父瀹規槗鍑洪敊鐨勯偅閮ㄥ垎鍔熻兘錛岄泦涓埌浜嗚繖浜涚被搴撻噷緇熶竴瀹炵幇錛岃鍏蜂綋璁捐鍜屽紑鍙戜漢鍛樹互浠g爜閲嶇敤鐨勫艦寮忔潵瀹炵幇COM瑙勮寖錛?/p>
褰撶劧浜轟滑涔熸剰璇嗗埌浜咰OM榪欐牱鐨勪竴浜涢棶棰橈紝鐗瑰埆鏄叿浣撳疄鐜版椂璁捐鍜屽紑鍙戜漢鍛樺繀欏昏鍏蟲敞鍑犱箮鎵鏈夌殑浜岃繘鍒剁粏鑺傦紝浜庢槸.net灝辮癁鐢熶簡錛屾妸榪欎簺瑙勮寖鐨勮澶氬鏉傛ч兘灝佽鍦ㄤ簡铏氭嫙鏈洪噷闈紝鎶婅繖浜涚洰鏍囧姛鑳斤紙璺ㄨ竟鐣屻侀忔槑鎬х瓑絳夛級閫氳繃涓鑷磋屽張騫蟲粦鐨勫鉤鍙版帴鍙e拰鑷弿榪扮殑meta data錛屼互涓縐嶈璁捐鍜屽紑鍙戜漢鍛樻洿鏄撴帴鍙楃殑椋庢牸寮鏀句簡鍑烘潵錛?/p>
COM鐨勫獎鍝嶆槸闈炲父騫垮ぇ鐨勶紝姣斿XPCOM 錛孎irefox涓婄殑涓縐嶆彃浠舵妧鏈爣鍑嗭紝灝辨槸鏍規嵁COM鐨勬濇兂鍜屽師鍒欏埗瀹氱殑錛涜澶氳瘎璁鴻錛孎irefox鐨勬垚鍔熸槸鍥犱負瀹冩彃浠舵槸濡傛鐨勬垚鍔燂紝榪欎篃綆楁槸COM鏈韓鎵鎰忔枡涓嶅埌鐨勮礎鐚箣涓銆?/p>
鍦?net鐨勫鉤鍙頒笂錛屽嵆浣挎槸.net CLR/SSCLI鐨勫叿浣撳疄鐜頒篃澶ч噺榪愮敤浜咰OM鐨勬濇兂鍜屾満鍒訛紝鍙互璇?net灝辨槸鎼緩鍦–OM浜岃繘鍒剁粍浠跺鉤鍙頒箣涓婄殑铏氭嫙鏈烘墭綆″鉤鍙般?/p>
鏈鍚庯紝.net寮濮嬫椂鐨勫唴閮ㄧ紪鍙鋒槸COM 2.0
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*) 鍏充簬“鍚嶈瘝澶”
榪欐槸瑕佸疄鐜板彲浠ヨ法榪涚▼鍜屾満鍣ㄨ竟鐣岋紝璺ㄥ紓鏋勫鉤鍙幫紙褰撶劧蹇呴』瀹炵幇浜咰OM鎵瑙勫畾鐨勫熀紜鏈嶅姟錛夛紝閫忔槑鍖栧叿浣撳璞$被鍨嬪強瀵硅薄鐢熷懡鍛ㄦ湡錛屼究浜庣粺涓閮ㄧ講鍜岀増鏈鐞嗙殑緇勪歡鎶鏈紝鎵蹇呴』浠樺嚭鐨勪唬浠楓?/p>
COM瑙勮寖宸叉妸瑕佽揪鑷磋繖浜涚洰鏍囩殑緋葷粺錛屾墍蹇呴』鎻愪緵鐨勬帴鍙e拰鐗規ф娊璞′簡鍑烘潵錛屽彧涓嶈繃涓轟簡琛ㄨ揪榪欎簺鎶借薄鐨勬蹇佃屾柊閫犵殑鏈鍚嶈瘝鏈変簺闄岀敓鍜岀獊鍏錛涘鏋滆閬囧埌鐩鎬技闂鐨勬瘡涓涓璁″拰寮鍙戜漢鍛橀兘鑷繁鏉ュ仛鎶借薄錛屾湭蹇呬細鐢熸垚鏇村ソ鐨勬柟妗堬紱
涓句釜渚嬪瓙錛宎partment錛屽闂達紝灝辨槸涓轟簡鎶借薄浼犵粺OS涓繘紼嬪拰綰跨▼鐨勫疄鐜拌屾柊閫犵殑鏈鍚嶈瘝鍜屾蹇碉紱浠諱綍浜鴻鎶借薄榪欐牱鐨勪竴浜涙蹇碉紝涓嶆柊閫犳湳璇紝鏄潪甯稿洶闅劇殑錛屽姣?net錛屽悗鑰呯敤浜咰LR铏氭嫙鏈烘潵灝佽浜嗗ぇ澶氭暟鐨勫疄鐜扮粏鑺傦紝騫剁敤璁╀漢鏇村鏄撴帴鍙楃殑椋庢牸鏉ュ紑鏀炬帴鍙o紝鍙簨瀹炰笂浠嶇劧鏂伴犱簡涓浜涘悕璇嶅拰姒傚康錛屽綾諱技鑼冪暣鐨凙ppDomain錛?/p>
*) 鍏充簬“鍩轟簬ATL鐨勫疄鐜版瘮杈冮毦鎳?#8221;
ATL涓昏浣跨敤浜唗emplate鎶鏈紝COM鎺ュ彛鏅鴻兘鎸囬拡錛岀敤闈欐佽漿鎹㈡潵妯℃嫙鍔ㄦ佺粦瀹氾紝絳夌瓑錛屽疄闄呭茍涓嶆槸寰堝鏉傦紝鍙兘綆梒++瀹炵幇鏈哄埗鐨勪腑絳夐毦搴︼紝涓昏娑夊強Modern C++ design涓涔︿腑涓浜涚浉鍏寵璁$悊蹇電殑榪愮敤銆傚姣擝oost涓煇浜涘簱鐨勫疄鐜幫紝ATL寰堜漢閬撲簡銆?/p>
*) 鍏充簬“榪欏茍涓嶆槸COM鏈韓澶嶆潅錛岃屾槸C++宸茬粡钀藉悗浜庢椂浠d簡”
棣栧厛COM鐨勮鑼冪殑紜槸澶嶆潅鐨勶紝涓哄暐錛熺涓鐐瑰凡緇忚浜嗭紝灝辨槸涓轟簡瑕佹娊璞″嚭璺ㄨ竟鐣屽拰瀵硅薄閫忔槑鐨勭粍浠舵妧鏈紱.net琛ㄨ薄涓婄湅姣旇緝“綆鍗曞鏄?#8221;錛岄鏍間翰榪戣璁″拰寮鍙戜漢鍛橈紝瀹為檯涓婂鏉備簨鍔″拰瀹炵幇緇嗚妭閮借鍒掑垎鍒癈LR閭d釜灞傞潰涓婂幓瀹炵幇浜嗭紱鍘葷湅涓涓婥LR鐨勫紑婧愬疄鐜癝SCLI錛屼綘浼氬彂鐜幫紝鏁翠釜铏氭嫙鏈哄鉤鍙扮殑瀹炵幇錛屽ぇ閲忚繍鐢ㄤ簡COM鐨勬濇兂鍜屾満鍒訛紝灝辨槸涓涓法鍨嬬郴緇熷鉤鍙扮駭鐨凜OM server錛?/p>
鍏舵錛孋OM瑙勮寖鏈韓鏄嫭绔嬩簬瀹炵幇璇█鐨勶紝鍙鏋勫緩鍑虹殑緇勪歡絎﹀悎瑙勮寖鍒跺畾鐨勪簩榪涘埗鍏煎錛岀郴緇熷氨鍙互榪愪綔錛岃繖鍜孋++鏄惁钀藉悗鏃朵唬娌℃湁鍏崇郴銆傚鏋滃紑鍙戜漢鍛樿涓猴紝.net鎵嶅鍏堣繘錛屼篃瀹屽叏鍙互鐢?net涓殑鎵樼璇█錛屽C#鏉ュ疄鐜癈OM緇勪歡錛?/p>
鏈鍚庯紝姣忕璇█閮芥湁鍏墮傜敤鐨勮寖鍥達紝鐜板湪鍙互榪欎箞璇?#8220;濡傛灉鏈変竴涓叏鏂扮殑欏圭洰闇姹傦紝瑕佽揪鑷磋法杈圭晫鍜屽璞¢忔槑緇勪歡錛屽茍涓旀病鏈夊お榪囦弗鑻涚殑鎬ц兘闇姹傦紝閭d箞.net騫沖彴鍙婂叾涓婄殑鎵樼璇█鏉ュ疄鐜幫紝姣旂敤C++鍙婄浉鍏寵緟鍔╃被搴撴潵浠OM緇勪歡褰㈠紡鏉ュ疄鐜幫紝瑕佹洿鍚堥傦紝涔熸洿蹇熶究鎹峰拰鑺傜渷棰勭畻銆?#8221;浣嗘槸錛屽湪榪欎釜鍒ゆ柇涓婃垜浠姞浜嗗緢澶氫弗鏍肩殑綰︽潫錛屼竴鏃﹂渶姹傚彉鏇達紝鐗瑰埆鏄」鐩殑闈炲姛鑳芥ч渶姹傦紝瑕佹眰楂樻ц兘榪愮畻鎴栬呮洿欏虹晠鐨勪笌legacy鐨刵ative緋葷粺鐩鎬簰錛岄偅涔?#8220;浣跨敤native璇█鏉ュ疄鐜版ц兘鍏抽敭浠ュ強legacy浜や簰鍔熻兘錛岄氳繃COM灝佽錛屽啀鐢盋OMInterop浜?net鎵樼搴旂敤璋冪敤”鍙兘鏄洿鐜板疄鐨勬柟妗堛侰++鏄竴闂ㄦ椿鐨勮璦錛屼笉鏂彂灞曠殑璇█錛屽嵆浣垮湪鏈鏂扮殑鎵樼鏃朵唬閲岋紝C#鎴愪負鏍囧噯涓繪祦錛屼絾C++/CLI浠嶇劧鏄墭綆¤璦閲屽姛鑳芥渶瀹屾暣鐨勮璦銆?/p>
In the fusion, or any other components or modules, how to retrieve the execution engine instance and how to generate such engine?
UtilExecutionEngine, implemented as COM object, support Queryinterface/AddRef/Release, and exposed via interface IExecutionEngine.
With SELF_NO_HOST defined,
BYTE g_ExecutionEngineInstance[sizeof(UtilExecutionEngine)];
g_ExecutionEngineInstance would be the singleton instance of current execution engine,
otherwise, without SELF_NO_HOST, the 'sscoree' dll would be loaded and try to get the exported function, which is named 'IEE' from such dll. Here, it is the well-known shim, in .net CLR, such module is named 'mscoree'. Further, if 'IEE' could not be found in such dll, system would try to locate another exported function, named 'LoadLibraryShim', and use such function to load the 'mscorwks' module, and try to locate the 'IEE' exportd functionin it.
It's very obvious that Rotor has implemented its own execution engine, but it also gives or make space for implementation of execution engine from 3rd party. Here, .net CLR is a good candidate definitely, Rotor might load the mscorwks.dll module for its usage.
PAL, PALAPI, for example, HeapAlloc, one famous WIN32 API, has been implemented as one PALAPI (defined in Heap.c), to make it possible that the CLI/Rotor be ported smoothly to other OS, such freebsd/mac os.
CRT routines are also reimplemented, such as memcpy, it has been implemented as GCSafeMemCpy
There're many macros in fuctions, such as SCAN_IGNORE_FAULT/STATIC_CONTRACT_NOTHROW/STATIC_CONTRACT_NOTRIGGER, they are for static analysis tool to scan, analyse and figour out the potential issues in code.
From view point of the execution model by CLI, the act of compiling (including JIT) high-level type descriptions would be separated from the act of turning these type descriptions into processor-specific code and memory structures.
And such executino model, in other word, the well-known 'managed execution', would defer the loading, verification and compilation of components until runtime really needs; At the same time, the type-loading is the key trigger that causes CLI's tool chain to be engaged at runtime. Deferred compilation(lead to JIT)/linking/loading would get better portability to different target platform and be ready for version change; The whole deferred process would driven by well-defined metadata and policy, and it would be very robust for building a virtual execution environment;
At the top of such CLI tool chain, fusion is reponsible for not only finding and binding related assemblies, which are via assembly reference defined in assembly, fusion also takes another important role, loader, and its part of functionality is implemented in PEAssembly, ClassLoader classes. For example, ClassLoader::LoadTypeHandleForTypeKey.
For types in virtual execution environment of CLI, rotor defines four kinds of elements for internal conducting,
ELEMENT_TYPE_CLASS for ordinary classes and generic instantiations(including value types);
ELEMENT_TYPE_ARRAY AND ELEMENT_TYPE_SZARRAY for array types
ELEMENT_TYPE_PRT and ELEMENT_TYPE_BYREF for pointer types
ELEMENT_TYPE_FNPTR for function pointer types
every type would be assigned unique ulong-typed token, and such token would be used to look up in m_TypeDefToMethodTableMap (Linear mapping from TypeDef token to MethodTable *)which is maintained by current module; If there it is, the pointer to method table of such type would be retrieved, or it would look up in the loader module, where the method table should exist in while it's JIT loaded, not launched from NGEN image;
And all the unresolved typed would be maintained in a hash table, PendingTypeLoadTable; Types and only those types that are needed, such as dependencies, including parent types, are loaded in runtime, such type is fully loaded and ready for further execution, and other unresolved types would be kept in the previous hash table.
2) 鍐嶄粠澶у鏁板簲鐢ㄤ腑甯歌鐨勭被緇ф壙浣撶郴涓婄湅錛?br>闄や簡鏁翠釜緇ф壙浣撶郴鎵緇熶竴寮鏀懼嚭鏉ョ殑鎺ュ彛闆嗭紙涔熷氨鏄敱铏氬嚱鏁版墍緇勬垚錛夛紝鍦ㄧ戶鎵夸綋緋葷殑姣忎釜灞傞潰鍙﹀浼氭湁澶ч噺鐨勫叾浠栬緟鍔╂垚鍛樺嚱鏁幫紙鍏舵暟閲忛氬父姣旇櫄鍑芥暟澶氱殑澶氾級錛岃繖浜涙垚鍛樺嚱鏁板畬鍏ㄦ病蹇呰璁捐鎴愯櫄鍑芥暟錛?/p>
3) 浠庡叾浠栬璦鐪嬶紝
鍗充嬌杈冩柊鐨勮櫄鎷熸満璇█C#(Java綆楁槸杈冭佺殑铏氭嫙鏈鴻璦),鍙嶈屽畾涔変簡姣擟++鏇翠負涓ユ牸鏇翠負鏄懼紡鐨勬垚鍛樻柟娉曞疄鐜版垨瑕嗙洊鎴栭噸杞芥垨鏂板緩鐨勮鍒欙紱榪欐槸闈炲父閲嶈鐨勫C++浠ュ強Java璁捐鎬濇兂鐨勫弽鎬濄?/p>
4) 浠庤璦鐨勯傜敤鍦哄悎鐪嬶紝
鎴戜滑鐜板湪鐨勮璁猴紝緇濆ぇ澶氭暟鎯呭喌涓嬪甫鏈変竴涓潪甯擱噸瑕佺殑榛樿鍓嶆彁錛岄偅灝辨槸鍦ㄧ敤鎴鋒佹ā寮忎笅浣跨敤C++錛屽鏋滄斁瀹借繖涓害鏉燂紝鍦ㄥ唴鏍告ā寮忎笅浣跨敤C++錛岄偅鎯呭喌鍙堝畬鍏ㄤ笉鍚屼簡銆?br>寮曠敤涓嬮潰榪欎釜鏂囨。鐨勮鐐癸紝http://www.microsoft.com/china/whdc/driver/kernel/KMcode.mspx
棣栧厛錛岀敤鎴鋒佷笅闈炲父寤変環鍑犱箮涓嶇敤鑰冭檻鐨勮祫婧愶紝鍦ㄥ唴鏍鎬腑鏄潪甯告槀璐電殑錛屾瘮濡傚唴鏍稿爢鏍堜竴鑸氨3涓猵age錛?/p>
鍦ㄥ唴鏍鎬笉鑳藉垎欏?paging)鏃跺繀欏諱繚璇佸皢琚墽琛岀殑鎵鏈変唬鐮佸拰鏁版嵁蹇呴』鏈夋晥鐨勯┗鐣欏湪鐗╃悊鍐呭瓨涓紝濡傛灉榪欐椂闇瑕佸椹葷暀鍑犲紶铏氳〃浠ュ強铏氳〃鎸囬拡閭h繕鏄樉寰楅潪甯告槀璐電殑錛屽悓鏃剁紪璇戝櫒涓鴻櫄鍑芥暟錛屾ā鏉跨瓑鐢熸垚浠g爜鐨勬柟寮忥紝璁╁紑鍙戜漢鍛樺緢闅劇‘瀹氳鎵ц涓涓嚱鏁版墍闇瑕佺殑鎵鏈変唬鐮佺殑鎵鍦ㄤ綅緗紝鍥犳涔熸棤娉曠洿鎺ユ帶鍒剁敤浜庡畨緗繖浜涗唬鐮佺殑鑺傦紙涓漢璁や負鍙兘閫氳繃progma segment/datasegment/codesegment瀵逛簬浠g爜鍜屾暟鎹繘琛岄泦涓帶鍒訛級錛屽洜姝ゅ湪闇瑕佽繖浜涗唬鐮佹椂錛屽彲鑳藉凡緇忚page out浜嗭紱
鎵鏈夋秹鍙婄被灞傛緇撴瀯錛屾ā鏉匡紝寮傚父絳夌瓑榪欐牱鐨勪竴浜涜璦緇撴瀯鍦ㄥ唴鏍告佷腑閮藉彲鑳芥槸涓嶅畨鍏ㄧ殑錛屾渶濂芥槸鎶婄被鐨勪嬌鐢ㄩ檺瀹氫負POD綾伙紝鍥炲埌鎴戜滑鐨勪富棰樿櫄鍑芥暟錛屼篃灝辨槸璇村唴鏍告佷笅綾昏璁′腑娌℃湁铏氬嚱鏁般?/p>