??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
用位q算来表C把一个负数{换成zig-zag~码Q就?span class="pun">
int32是:(x)(n << 1) ^ (n >> 31)
int64是:(x)(n << 1) ^ (n >> 63)
也就是说Q如果是负数Q对?2位最多能省去30|其中1格是W号位,另一个代表最?Q此处假?#8220;正负0”不合法)(j)。同理,64位最多能省去62位。当然比较极端的是所有的位数都被用上了?br>
]]>
首先什么是Wow64Q很多朋友一看到64p个方法是判断当前pȝ是否?4bit的,其实不然。Wow64是Windows-On-Windows64的意思,它是指在64位的操作pȝ上(不是?4位的CPUQ运?2位应用程序的兼容q_?/p>
下面是MSDN中一DIsWow64的应用程序:(x)
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; }
下面的代码用来检这个程序的l果Q?/p>
if( IsWow64() == TRUE ) { _tprintf(_T("IsWow64() == TRUE\n")); } else { _tprintf(_T("IsWow64() == FALSE\n")); }
让我们编译一下这个程序?/p>
我们需要的?4位的操作pȝQ比如XP64bitQW(xu)indows 2008 R2{都?4bit操作pȝ?/p>
?4位的操作pȝ上运行的kernel32.dll中,会(x)实现IsWow64ProcessҎ(gu)Q而在32位系l中提供的kernel32.dll中则没有提供相关函数的实现?/p>
比较qh人的则是bIsWow64Q其实仔l看MSDN中的RemarkQ会(x)发现Q?/p>
If the application is a 64-bit application running under 64-bit Windows, the Wow64Process parameter is set to FALSE.也就是说64位应用程序跑?4位的操作pȝ上,bIsWow64的值将是FALSE而不是TRUE?
因此我们需要分别将我们的程序编译成Win32q_和x64q_的,如果你用Visual Studioq行~译Q默认安装则只包?2位的~译?链接器,即便你是?4位操作系l上安装Q也是一L(fng)。你需要在VC++节点下勾选x64选项才可以,Itanium则需要在Serverq_下安装才可勾选。然后在~译的时候,分别选择Win32和x64q行~译?
~译后,q行Q结果如我们分析的一P(x)
?4位系l上q行W(xu)in32~译配置的结果是IsWow64() == TRUEQ而x64~译配置的结果是IsWow64() == FALSE?
如果惌知道当前pȝ是否?4位的Q则可以通过下面的方法:(x)
BOOL Is64bitSystem() { SYSTEM_INFO si; GetNativeSystemInfo(&si); if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 || si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 ) { return TRUE; } else { return FALSE; } }
注意Q需要注意是GetNativeSystemInfo 函数从Windows XP 开始才有, ?IsWow64Process 函数?Windows XP with SP2 以及(qing) Windows Server 2003 with SP1 开始才有?
l论QMAX_PATH代表从盘W开始到文g名结C字符串长度(长度+1Q的最大长度。也是假设C:\a.txt?个字W,长度?QMAX_PATH通常?60Q其中这个文件全名的长度不能大于260?/p>
试代码Q?/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; }
提示Q右键项目属性,字W集讄为“未讄”?/p>
情况是这L(fng)Q通常的应用程序内的事件传递一般场景都是类似在H体A上点?yn)L?Q弹Z个窗体B。面对这L(fng)场景只要::SetWindowActive(HWND wnd)卛_Q这里其实有个隐含的前提Q就是wnd的父H口是出于TOP的状态,也就是它的父H口是置的Q也是我们能够在界面上看到它,否则你的其他|顶H口依旧置?/p>
下面的代码示意了如何窗口置Ӟ因ؓ(f)也没有对各个Ҏ(gu)做深入分析,所以就不多发表aZQ?/p>
/*场景Q? 目标Q将子窗口放到置Ӟ且是HWND_TOPQ模? 说明QhMainWnd是主H口Qm_pMMessageWnd->m_hWnd是子H口 */ /*1、将H口从后台直接提到最前方Q需要先主HUM来?/ 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、将子窗口置?/ 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、放在后面的原因是避免子H口q未弹出Q点ȝ体外的时候被攑ֈ后面去,所以等H口出来后再取消|顶*/ if( hMainWnd != NULL ) { SetWindowPos( hMainWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW ); } /*4、避免被ȝ口给压了*/ 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 ); }
动态连接库的创建步骤:(x)
一、创建Non-MFC DLL动态链接库
1、打开File ?gt; New ?gt; Project选项Q选择Win32 Dynamic-Link Library ?gt;sample project ?gt;工程名:(x)DllDemo
2、新Z?h文gDllDemo.h
#ifdef DllDemo_EXPORTS
#define DllAPI __declspec(dllexport)
#else
#define DllAPI __declspec(dllimport)
extern "C" //原样~译
{
DllAPI int __stdcall Max(int a,int b); //__stdcall佉KC/C++语言内能够调用API
}
#endif
3、在DllDemo.cpp文g中导入DllDemo.h文gQƈ实现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文g创徏动态连接库DllDemo.dll
1、删除DllDemo工程中的DllDemo.h文g?
2、在DllDemo.cpp文g_(d)删除 #include DllDemo.h语句?
3、向该工E中加入一个文本文Ӟ命名为DllDemo.defq写入如下语句:(x)
LIBRARY MyDll
EXPORTS
Max@1
4、编译程序生成动态连接库?
动态链接的调用步骤Q?/strong>
一、隐式调?/strong>
1、徏立DllCnslTest工程
2、将文gDllDemo.dll、DllDemo.lib拯到DllCnslTest工程所在的目录
3、在DllCnslTest.h中添加如下语句:(x) #define DllAPI __declspec(dllimport) 4、在DllCnslTest.cpp文g中添加如下语句:(x) #include "DllCnslTest.h"http://或?#include "DllDemo.h" 5、编译ƈ生成应用E序DllCnslTest.exe
二、显式调?/strong>
1、徏立DllWinTest工程?
2、将文gDllDemo.dll拯到DllWinTest工程所在的目录或Windowspȝ目录下?
3、用vc/bin下的Dumpbin.exe的小E序Q查看DLL文g(DllDemo.dll)中的函数l构?
4、用类型定义关键字typedefQ定义指向和DLL中相同的函数原型指针?
例:(x) typedef int(*lpMax)(int a,int b); //此语句可以放?h文g?/p> 5、通过LoadLibray()DLL加蝲到当前的应用E序中ƈq回当前DLL文g的句柄?
例:(x) HINSTANCE hDll; //声明一个Dll实例文g句柄 6、通过GetProcAddress()函数获取导入到应用程序中的函数指针?
例:(x) lpMax Max; 7、函数调用完毕后Q用FreeLibrary()卸蝲DLL文g?pre> FreeLibrary(hDll); 8、编译ƈ生成应用E序DllWinTest.exe
注:(x)昑ּ链接应用E序~译时不需要用相应的Lib文g?/p>
下蝲Q?font style="background-color: #cccccc">Visual Studio 2008验证通过Q:(x)http://www.shnenglu.com/Files/mymsdn/DllCnsTest.7z
#pragma comment(libQ?DllDemo.lib") //在编辑器linkӞ链接到DllDemo.lib文g
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) \止隐式构造,则可以将默认构造函数隐藏v来,在大多数~译器中也可以对构造函数增加explicit关键字来避免隐式构造?
private: \
TypeName(const TypeName&); \
TypeName& operator=(const TypeName&)
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \更多解释详见《More Effective C++?
private: \
TypeName(); \
DISALLOW_COPY_AND_ASSIGN(TypeName)
// keyword__declspec.cpp : 定义控制台应用程序的入口炏V? // // 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; }