锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
鐢ㄤ綅榪愮畻鏉ヨ〃紺烘妸涓涓礋鏁拌漿鎹㈡垚zig-zag緙栫爜錛屽氨鏄?span class="pun">
int32鏄細(n << 1) ^ (n >> 31)
int64鏄細(n << 1) ^ (n >> 63)
涔熷氨鏄錛屽鏋滄槸璐熸暟錛屽浜?2浣嶆渶澶氳兘鐪佸幓30鏍鹼紙鍏朵腑1鏍兼槸絎﹀彿浣嶏紝鍙︿竴涓唬琛ㄦ渶灝忓?錛屾澶勫亣璁?#8220;姝h礋0”涓嶅悎娉曪級銆傚悓鐞嗭紝64浣嶆渶澶氳兘鐪佸幓62浣嶃傚綋鐒舵瘮杈冩瀬绔殑鏄墍鏈夌殑浣嶆暟閮借鐢ㄤ笂浜嗐?br>
]]>
棣栧厛浠涔堟槸Wow64錛熷緢澶氭湅鍙嬩竴鐪嬪埌64灝辮涓鴻繖涓柟娉曟槸鍒ゆ柇褰撳墠緋葷粺鏄惁鏄?4bit鐨勶紝鍏跺疄涓嶇劧銆俉ow64鏄疻indows-On-Windows64鐨勬剰鎬濓紝瀹冩槸鎸囧湪64浣嶇殑鎿嶄綔緋葷粺涓婏紙涓嶆槸鎸?4浣嶇殑CPU錛夎繍琛?2浣嶅簲鐢ㄧ▼搴忕殑鍏煎騫沖彴銆?/p>
涓嬮潰鏄疢SDN涓竴孌礗sWow64鐨勫簲鐢ㄧ▼搴忥細
BOOL IsWow64() { typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); LPFN_ISWOW64PROCESS fnIsWow64Process; BOOL bIsWow64 = FALSE; fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle(_T("kernel32")), "IsWow64Process"); if (NULL != fnIsWow64Process) { fnIsWow64Process(GetCurrentProcess(),&bIsWow64); } return bIsWow64; }
涓嬮潰鐨勪唬鐮佺敤鏉ユ嫻嬭繖涓▼搴忕殑緇撴灉錛?/p>
if( IsWow64() == TRUE ) { _tprintf(_T("IsWow64() == TRUE\n")); } else { _tprintf(_T("IsWow64() == FALSE\n")); }
璁╂垜浠紪璇戜竴涓嬭繖涓▼搴忋?/p>
鎴戜滑闇瑕佺殑鏄?4浣嶇殑鎿嶄綔緋葷粺錛屾瘮濡俋P64bit錛學indows 2008 R2絳夐兘鏄?4bit鎿嶄綔緋葷粺銆?/p>
鍦?4浣嶇殑鎿嶄綔緋葷粺涓婅繍琛岀殑kernel32.dll涓紝灝嗕細瀹炵幇IsWow64Process鏂規硶錛岃屽湪32浣嶇郴緇熶腑鎻愪緵鐨刱ernel32.dll涓垯娌℃湁鎻愪緵鐩稿叧鍑芥暟鐨勫疄鐜般?/p>
姣旇緝榪鋒儜浜虹殑鍒欐槸bIsWow64錛屽叾瀹炰粩緇嗙湅MSDN涓殑Remark錛屼細鍙戠幇錛?/p>
If the application is a 64-bit application running under 64-bit Windows, the Wow64Process parameter is set to FALSE.涔熷氨鏄64浣嶅簲鐢ㄧ▼搴忚窇鍦?4浣嶇殑鎿嶄綔緋葷粺涓婏紝bIsWow64鐨勫煎皢鏄疐ALSE鑰屼笉鏄疶RUE銆?
鍥犳鎴戜滑闇瑕佸垎鍒皢鎴戜滑鐨勭▼搴忕紪璇戞垚Win32騫沖彴鍜寈64騫沖彴鐨勶紝濡傛灉浣犵敤Visual Studio榪涜緙栬瘧錛岄粯璁ゅ畨瑁呭垯鍙寘鍚?2浣嶇殑緙栬瘧鍣?閾炬帴鍣紝鍗充究浣犳槸鍦?4浣嶆搷浣滅郴緇熶笂瀹夎錛屼篃鏄竴鏍風殑銆備綘闇瑕佸湪VC++鑺傜偣涓嬪嬀閫墄64閫夐」鎵嶅彲浠ワ紝Itanium鍒欓渶瑕佸湪Server騫沖彴涓嬪畨瑁呮墠鍙嬀閫夈傜劧鍚庡湪緙栬瘧鐨勬椂鍊欙紝鍒嗗埆閫夋嫨Win32鍜寈64榪涜緙栬瘧銆?
緙栬瘧鍚庯紝榪愯錛岀粨鏋滃鎴戜滑鍒嗘瀽鐨勪竴鏍鳳細
鍦?4浣嶇郴緇熶笂榪愯Win32緙栬瘧閰嶇疆鐨勭粨鏋滄槸IsWow64() == TRUE錛岃寈64緙栬瘧閰嶇疆鐨勭粨鏋滄槸IsWow64() == FALSE銆?
濡傛灉鎯寵鐭ラ亾褰撳墠緋葷粺鏄惁鏄?4浣嶇殑錛屽垯鍙互閫氳繃涓嬮潰鐨勬柟娉曪細
BOOL Is64bitSystem() { SYSTEM_INFO si; GetNativeSystemInfo(&si); if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 || si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 ) { return TRUE; } else { return FALSE; } }
娉ㄦ剰錛氶渶瑕佹敞鎰忔槸GetNativeSystemInfo 鍑芥暟浠嶹indows XP 寮濮嬫墠鏈夛紝 鑰?IsWow64Process 鍑芥暟浠?Windows XP with SP2 浠ュ強 Windows Server 2003 with SP1 寮濮嬫墠鏈夈?
緇撹錛歁AX_PATH浠h〃浠庣洏絎﹀紑濮嬪埌鏂囦歡鍚嶇粨灝劇殑C瀛楃涓查暱搴︼紙闀垮害+1錛夌殑鏈澶ч暱搴︺備篃灝辨槸鍋囪C:\a.txt鍏?涓瓧絎︼紝闀垮害涓?錛孧AX_PATH閫氬父涓?60錛屽叾涓繖涓枃浠跺叏鍚嶇殑闀垮害涓嶈兘澶т簬260銆?/p>
嫻嬭瘯浠g爜錛?/p>
#include "stdafx.h" #include <atlbase.h> #include <windows.h> #include <atlfile.h> #include <atlstr.h> #include <iostream> #include <string> BOOL IsFileExist( LPCTSTR lpszFileName ) { DWORD dwAttr = ::GetFileAttributes( lpszFileName ); if ( dwAttr == 0xFFFFFFFF ) { return FALSE; } if ( ( dwAttr & FILE_ATTRIBUTE_DIRECTORY ) > 0 ) { return FALSE; } return TRUE; } BOOL DeleteFiles( LPCTSTR lpszPath ) { TCHAR szFrom[_MAX_PATH+1] = {_T( '\0' )}; lstrcpy( szFrom , lpszPath ); SHFILEOPSTRUCT shf; memset( &shf, 0, sizeof( SHFILEOPSTRUCT ) ); shf.hwnd = NULL; shf.pFrom = szFrom; shf.wFunc = FO_DELETE; shf.fFlags = FOF_NOCONFIRMMKDIR | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT; return SHFileOperation( &shf ) == 0; } BOOL WriteBinaryBytesToFile( LPCTSTR fileName, unsigned char* data, unsigned int datasize , BOOL bAppend ) { CAtlFile file; if( !bAppend && IsFileExist( fileName )) DeleteFiles( fileName ); HRESULT ret = file.Create( fileName, FILE_WRITE_DATA, FILE_SHARE_WRITE, bAppend?OPEN_ALWAYS:CREATE_ALWAYS ); if ( !SUCCEEDED( ret ) ) return FALSE; if( bAppend ) { file.Seek( 0, FILE_END ); } unsigned char *pos = data; while( datasize > 0 ) { DWORD dwWrite = 0; if ( file.Write( pos , datasize , &dwWrite ) != S_OK ) { file.Close(); return FALSE; } datasize -= dwWrite; pos += dwWrite; } file.Close(); return TRUE; } BOOL GCreateFile(std::string& fileNamePath, std::string& fileNameExt, int shortNameLength, std::string& data) { char c[] = "a"; std::string fileName; fileName.append(fileNamePath); while( shortNameLength-- ) { fileName.append(c); ++*c; if( *c > 'z' ) { *c = 'a'; } } fileName.append(fileNameExt); std::cout << "fileName:" << fileName << std::endl; std::cout << "fileName.size():"<< fileName.size() << std::endl; std::cout << "MAX_PATH" << MAX_PATH << std::endl; if( WriteBinaryBytesToFile(fileName.c_str(), (unsigned char*)data.data(), data.size(), FALSE ) ) { std::cout << "++++++++++++++WriteBinaryBytesToFile successful.++++++++++++++" << std::endl; return TRUE; } else { std::cout << "==============WriteBinaryBytesToFile error==============." << std::endl; return FALSE; } } int _tmain(int argc, _TCHAR* argv[]) { std::string fileNamePath = "E:\\MyCPlusProject\\TestFileNameMaxLength\\TestFileNameMaxLength\\Output\\"; std::string fileNameExt = ".txt"; std::string data("This is a content."); int shortNameLength; shortNameLength = MAX_PATH - fileNameExt.size() - fileNamePath.size(); GCreateFile(fileNamePath, fileNameExt, shortNameLength, data); std::cout << "------------------" << std::endl; shortNameLength = MAX_PATH - fileNameExt.size() - fileNamePath.size() - 1; GCreateFile(fileNamePath, fileNameExt, shortNameLength, data); return 0; }
鎻愮ず錛氬彸閿」鐩睘鎬э紝灝嗗瓧絎﹂泦璁劇疆涓衡滄湭璁劇疆鈥濄?/p>
鎯呭喌鏄繖鏍風殑錛岄氬父鐨勫簲鐢ㄧ▼搴忓唴鐨勪簨浠朵紶閫掍竴鑸満鏅兘鏄被浼煎湪紿椾綋A涓婄偣鍑繪寜閽?錛屽脊鍑轟竴涓獥浣揃銆傞潰瀵硅繖鏍風殑鍦烘櫙鍙::SetWindowActive(HWND wnd)鍗沖彲錛岃繖閲屽叾瀹炴湁涓殣鍚殑鍓嶆彁錛屽氨鏄痺nd鐨勭埗紿楀彛鏄嚭浜嶵OP鐨勭姸鎬侊紝涔熷氨鏄畠鐨勭埗紿楀彛鏄疆欏剁殑錛屼篃灝辨槸鎴戜滑鑳藉鍦ㄧ晫闈笂鐪嬪埌瀹冿紝鍚﹀垯浣犵殑鍏朵粬緗《紿楀彛灝嗕緷鏃х疆欏躲?/p>
涓嬮潰鐨勪唬鐮佺ず鎰忎簡濡備綍灝嗙獥鍙g疆欏訛紝鍥犱負涔熸病鏈夊鍚勪釜鏂規硶鍋氭繁鍏ュ垎鏋愶紝鎵浠ュ氨涓嶅鍙戣〃璦璁轟簡錛?/p>
/*鍦烘櫙錛? 鐩爣錛氬皢瀛愮獥鍙f斁鍒扮疆欏訛紙涓旀槸HWND_TOP錛夋ā寮? 璇存槑錛歨MainWnd鏄富紿楀彛錛宮_pMMessageWnd->m_hWnd鏄瓙紿楀彛 */ /*1銆佸皢紿楀彛浠庡悗鍙扮洿鎺ユ彁鍒版渶鍓嶆柟錛岄渶瑕佸厛灝嗕富紿楀彛縐諱笂鏉ャ?/ HWND hMainWnd = CFramework::GetInstance()->GetMainWnd(); if( hMainWnd != NULL ) { SetForegroundWindow(hMainWnd); BringWindowToTop(hMainWnd); SetActiveWindow(hMainWnd); SetWindowPos( hMainWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW ); } /*2銆佸皢瀛愮獥鍙g疆欏?/ if( m_pMMessageWnd != NULL ) /* true */ { SetForegroundWindow(m_pMMessageWnd->m_hWnd); BringWindowToTop(m_pMMessageWnd->m_hWnd); SetActiveWindow(m_pMMessageWnd->m_hWnd); SetWindowPos( m_pMMessageWnd->m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW ); SetWindowPos( m_pMMessageWnd->m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW ); } /*3銆佹斁鍦ㄥ悗闈㈢殑鍘熷洜鏄伩鍏嶅瓙紿楀彛榪樻湭寮瑰嚭錛岀偣鍑葷獥浣撳鐨勬椂鍊欒鏀懼埌鍚庨潰鍘伙紝鎵浠ョ瓑紿楀彛鍑烘潵鍚庡啀鍙栨秷緗《*/ if( hMainWnd != NULL ) { SetWindowPos( hMainWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW ); } /*4銆侀伩鍏嶈涓葷獥鍙g粰鍘嬩簡*/ if( m_pMMessageWnd != NULL ) /* true */ { SetWindowPos( m_pMMessageWnd->m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW ); SetWindowPos( m_pMMessageWnd->m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW ); }
鍔ㄦ佽繛鎺ュ簱鐨勫垱寤烘楠わ細
涓銆佸垱寤篘on-MFC DLL鍔ㄦ侀摼鎺ュ簱
1銆佹墦寮File 鈥?gt; New 鈥?gt; Project閫夐」錛岄夋嫨Win32 Dynamic-Link Library 鈥?gt;sample project 鈥?gt;宸ョ▼鍚嶏細DllDemo
2銆佹柊寤轟竴涓?h鏂囦歡DllDemo.h
#ifdef DllDemo_EXPORTS
#define DllAPI __declspec(dllexport)
#else
#define DllAPI __declspec(dllimport)
extern "C" //鍘熸牱緙栬瘧
{
DllAPI int __stdcall Max(int a,int b); //__stdcall浣塊潪C/C++璇█鍐呰兘澶熻皟鐢ˋPI
}
#endif
3銆佸湪DllDemo.cpp鏂囦歡涓鍏llDemo.h鏂囦歡錛屽茍瀹炵幇Max(int,int)鍑芥暟
#include "DllDemo.h"
DllAPI int __stdcall Max(int a,int b)
{
if(a==b)
return NULL;
else if(a>b)
return a;
else
return b;
}
4銆佺紪璇戠▼搴忕敓鎴愬姩鎬佽繛鎺ュ簱
浜屻佺敤.def鏂囦歡鍒涘緩鍔ㄦ佽繛鎺ュ簱DllDemo.dll
1銆佸垹闄llDemo宸ョ▼涓殑DllDemo.h鏂囦歡銆?
2銆佸湪DllDemo.cpp鏂囦歡澶達紝鍒犻櫎 #include DllDemo.h璇彞銆?
3銆佸悜璇ュ伐紼嬩腑鍔犲叆涓涓枃鏈枃浠訛紝鍛藉悕涓篋llDemo.def騫跺啓鍏ュ涓嬭鍙ワ細
LIBRARY MyDll
EXPORTS
Max@1
4銆佺紪璇戠▼搴忕敓鎴愬姩鎬佽繛鎺ュ簱銆?
鍔ㄦ侀摼鎺ョ殑璋冪敤姝ラ錛?/strong>
涓銆侀殣寮忚皟鐢?/strong>
1銆佸緩绔婦llCnslTest宸ョ▼
2銆佸皢鏂囦歡DllDemo.dll銆丏llDemo.lib鎷瘋礉鍒癉llCnslTest宸ョ▼鎵鍦ㄧ殑鐩綍
3銆佸湪DllCnslTest.h涓坊鍔犲涓嬭鍙ワ細 #define DllAPI __declspec(dllimport) 4銆佸湪DllCnslTest.cpp鏂囦歡涓坊鍔犲涓嬭鍙ワ細 #include "DllCnslTest.h"http://鎴栬?#include "DllDemo.h" 5銆佺紪璇戝茍鐢熸垚搴旂敤紼嬪簭DllCnslTest.exe
浜屻佹樉寮忚皟鐢?/strong>
1銆佸緩绔婦llWinTest宸ョ▼銆?
2銆佸皢鏂囦歡DllDemo.dll鎷瘋礉鍒癉llWinTest宸ョ▼鎵鍦ㄧ殑鐩綍鎴朩indows緋葷粺鐩綍涓嬨?
3銆佺敤vc/bin涓嬬殑Dumpbin.exe鐨勫皬紼嬪簭錛屾煡鐪婦LL鏂囦歡(DllDemo.dll)涓殑鍑芥暟緇撴瀯銆?
4銆佷嬌鐢ㄧ被鍨嬪畾涔夊叧閿瓧typedef錛屽畾涔夋寚鍚戝拰DLL涓浉鍚岀殑鍑芥暟鍘熷瀷鎸囬拡銆?
渚嬶細 typedef int(*lpMax)(int a,int b); //姝よ鍙ュ彲浠ユ斁鍦?h鏂囦歡涓?/p> 5銆侀氳繃LoadLibray()灝咲LL鍔犺澆鍒板綋鍓嶇殑搴旂敤紼嬪簭涓茍榪斿洖褰撳墠DLL鏂囦歡鐨勫彞鏌勩?
渚嬶細 HINSTANCE hDll; //澹版槑涓涓狣ll瀹炰緥鏂囦歡鍙ユ焺 6銆侀氳繃GetProcAddress()鍑芥暟鑾峰彇瀵煎叆鍒板簲鐢ㄧ▼搴忎腑鐨勫嚱鏁版寚閽堛?
渚嬶細 lpMax Max; 7銆佸嚱鏁拌皟鐢ㄥ畬姣曞悗錛屼嬌鐢‵reeLibrary()鍗歌澆DLL鏂囦歡銆?pre> FreeLibrary(hDll); 8銆佺紪璇戝茍鐢熸垚搴旂敤紼嬪簭DllWinTest.exe
娉細鏄懼紡閾炬帴搴旂敤紼嬪簭緙栬瘧鏃朵笉闇瑕佷嬌鐢ㄧ浉搴旂殑Lib鏂囦歡銆?/p>
涓嬭澆錛?font style="background-color: #cccccc">Visual Studio 2008楠岃瘉閫氳繃錛夛細http://www.shnenglu.com/Files/mymsdn/DllCnsTest.7z
#pragma comment(lib錛?DllDemo.lib") //鍦ㄧ紪杈戝櫒link鏃訛紝閾炬帴鍒癉llDemo.lib鏂囦歡
extern "C"
{
DllAPI int __stdcall Max(int a,int b);
}
void main()
{
int value;
value = Max(2,9);
printf("The Max value is %d\n",value);
}
hDll = LoadLibrary("DllDemo.dll");//瀵煎叆DllDemo.dll鍔ㄦ佽繛鎺ュ簱
Max = (lpMax)GetProcAddress(hDLL,"Max");
int value;
value = Max(2,9);
printf("The Max value is %d",value);
]]>
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \紱佹闅愬紡鏋勯狅紝鍒欏彲浠ュ皢榛樿鏋勯犲嚱鏁伴殣钘忚搗鏉ワ紝鍦ㄥぇ澶氭暟緙栬瘧鍣ㄤ腑涔熷彲浠ュ鏋勯犲嚱鏁板鍔爀xplicit鍏抽敭瀛楁潵閬垮厤闅愬紡鏋勯犮?
private: \
TypeName(const TypeName&); \
TypeName& operator=(const TypeName&)
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \鏇村瑙i噴璇﹁銆奙ore Effective C++銆?
private: \
TypeName(); \
DISALLOW_COPY_AND_ASSIGN(TypeName)
// keyword__declspec.cpp : 瀹氫箟鎺у埗鍙板簲鐢ㄧ▼搴忕殑鍏ュ彛鐐廣? // // ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/kernel_d/hh/Kernel_d/64bitAMD_6db3322a-fe6d-4287-9eda-a9c1378e715d.xml.htm // The sizeof value for any structure is the offset of the final member, // plus that member's size, rounded up to the nearest multiple of the largest // member alignment value or the whole structure alignment value, // whichever is greater. #include "stdafx.h" __declspec( align( 32) ) struct Struct__declspec_1 { int a; int b; }; __declspec( align( 32) ) struct Struct__declspec_2 { __declspec( align( 64) ) int a; int b; }; __declspec( align( 8 ) ) struct Struct__declspec_3 { int a; //4 bytes int b; //4 bytes int c; //4 bytes }; __declspec( align( 8 ) ) struct Struct__declspec_4 { int a; //4 bytes int b; //4 bytes }; struct StructNormal { int a; //4 bytes int b; //4 bytes int c; //4 bytes }; int _tmain(int argc, _TCHAR* argv[]) { printf( "sizeof Struct__declspec_1 is %d.\n", sizeof( Struct__declspec_1 )); //32 printf( "sizeof Struct__declspec_2 is %d.\n", sizeof( Struct__declspec_2 )); //64 printf( "sizeof Struct__declspec_3 is %d.\n", sizeof( Struct__declspec_3 )); //16 printf( "sizeof Struct__declspec_4 is %d.\n", sizeof( Struct__declspec_4 )); //8 printf( "sizeof StructNormal is %d.\n", sizeof( StructNormal )); //12 return 0; }