1.一般如果有比较大小的时候。使用unsigned来比较,这个和默认的比较结果相同。
2.定义bool类型的值时。默认的值时ture的。
3.如果有无用的函数参数,可以采用宏命令无视掉。UNREFERENCED_PARAMETER()
4._strlwr(buf);使用_strlwr_s(buf, sizeof(buf));替换
5.sscanf( tokenstring, "%c", &c );用sscanf_s( tokenstring, "%c", &c, sizeof(char) );替换
6.UINT WINAPI GetTempFileName(
__in LPCTSTR lpPathName,//路径
__in LPCTSTR lpPrefixString, //临时文件名的头部
__in UINT uUnique,//0就可以了,有很多好处
__out LPTSTR lpTempFileName
);
创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径。创建带 .TMP 文件扩展名
如果使用 GetTempFileName 方法创建超过 65535 个文件但不删除以前的临时文件,则将引发IOException。
如果没有提供唯一的临时文件名,则GetTempFileName 方法将引发IOException。要解决此错误,请删除所有不需要的临时文件。
失败返回0.
7.DWORD WINAPI GetTempPath(
__in DWORD nBufferLength,
__out LPTSTR lpBuffer
);获取临时文件夹。返回字符串长度。包含“/”符号
void GetMyDocumentFolder(CString &m_Folder)
{
LPITEMIDLIST pidl ;
TCHAR szInitialFolder[MAX_PATH] ;
_tcscpy_s(szInitialFolder, ARRAYSIZE(szInitialFolder), _T(".")) ; // cold position (this may locate install folder)
m_Folder = _T(".") ;
IMalloc *pMalloc;
if ( SUCCEEDED(SHGetMalloc(&pMalloc)) )
{
if( SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl)) )
{
SHGetPathFromIDList(pidl,szInitialFolder);
pMalloc->Free(pidl);
}
pMalloc->Release();
m_Folder = (LPCTSTR)szInitialFolder ;
}
}
获取临时文件夹路径
更新版:
void GetTempFile(
CString strFix, // <IN> Top of 2 characters to be make file name
CString &strTempFileName // <OUT> File name (full path)
)
{
BOOL bSuccess = FALSE ;
TCHAR szTempPath[MAX_PATH] ;
TCHAR szTempFileName[MAX_PATH] ;
int nTempLen ;
nTempLen = ::GetTempPath( sizeof(szTempPath)/sizeof(TCHAR), szTempPath ) ;
strFix.Left(2) ;
if ( (0 < nTempLen) && (nTempLen < MAX_PATH) )
{
if ( 0 != ::GetTempFileName(szTempPath, strFix, 0, szTempFileName) )
{
bSuccess = TRUE ;
}
}
if ( !bSuccess )
{
CString m_Folder ;
BRUtility::GetMyDocumentFolder(m_Folder) ;
GetTempFileName((LPCTSTR)m_Folder, strFix, 0, szTempFileName) ;
}
strTempFileName = (CString)szTempFileName ;
}
8.16进制与10进制互换
int hex2num(char hex) {
int result = -1;
unsigned work = (unsigned)hex;
if(work >= '0' && work <= '9') {
result = work - '0';
}
else if(work >= 'a' && work <= 'f') {
result = 10 + work - 'a';
}
else if(work >= 'A' && work <= 'F') {
result = 10 + work - 'A';
}
return result;
}
char num2hex(int num) {
char result = -1;
unsigned work = (unsigned)num;
if(work >= 0 && work <= 9) {
result = work + '0';
}
else if(work >= 0x0A && work <= 0x0F) {
result = (work - 10) + 'A';
}
return result;
}
//0123456789ABCDEF"→0x01, 0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF
bool str2hex(char* src, char* dst) {
bool result = true;
size_t len = strlen(src)/2;
char *buff = (char*)alloca(len);
for(unsigned i=0 ; i<::strlen(src) ; i += 2) {
int h = hex2num(src[i]);
int l = hex2num(src[i+1]);
if(h < 0 || l < 0) {
result = false;
break;
}
buff[i/2] = (h<<4) + l;
}
if(result)
memcpy(dst, buff, len);
return result;
}
//0x01, 0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF→"0123456789ABCDEF"
bool hex2str(char* src, char* dst, unsigned len) {
bool result = true;
char *buff = (char*)alloca(len * 2);
for(unsigned i=0 ; i<len ; i++) {
int h = num2hex((src[i] & 0xF0) >> 4);
int l = num2hex(src[i] & 0x0F);
if(h < 0 || l < 0) {
result = false;
break;
}
buff[i*2] = h;
buff[i*2 + 1] = l;
}
if(result)
memcpy(dst, buff, len * 2);
return result;
}
9.判断版本
int DecideOSVersion()
{
OSVERSIONINFO info;
info.dwOSVersionInfoSize = sizeof(info);
if( GetVersionEx(&info) ) {
if( 6 <= info.dwMajorVersion ){ // Vista or later
}
}
return info.dwMajorVersion ;
}
新版本
OSVERSIONINFOEX sVerInfo;
DWORDLONG dwlConditionMask = 0;
sVerInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
// Initialize the OSVERSIONINFOEX structure.
ZeroMemory(&sVerInfo, sizeof(OSVERSIONINFOEX));
sVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
sVerInfo.dwMajorVersion = 5; // XP
sVerInfo.dwMinorVersion = 1; // XP
//sVerInfo.wServicePackMajor = 0;
// Initialize the condition mask.
VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL );
VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, VER_GREATER_EQUAL );
VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL );
if (VerifyVersionInfo(&sVerInfo, VER_MAJORVERSION
| VER_MINORVERSION
| VER_SERVICEPACKMAJOR,
dwlConditionMask) != FALSE) {
Xp以后
}else{
Xp之前
}
10.strtok使用
char *cp = NULL;
char *next_token = NULL;
cp = strtok_s(FilterData2->IpAddressList, "\n", &next_token);
while ( cp != NULL)
{
FilterData.IpAddressList.push_back(cp) ;
cp = strtok_s(NULL, "\n", &next_token) ;
}
11.默认debug时候的字符串填充是fe,(MSDN上有描述,为了更好的找到错误)如果要忽略这个技能,
可以设置成自己想要的填充符,如果要填充0,定义下面的宏
_CrtSetDebugFillThreshold( 0 );
12.时间函数的理解(个人,非官方)
time_t time( time_t *timer );获取当前系统时间,跟时区有关。
struct tm *gmtime( const time_t *timer );获取time_t的时间到tm类型。会根据系统减去多余的时区时间恢复到UTC时间。
time_t mktime( struct tm *timeptr );把当地时间以格式化输出。
13Mutex运用(CreateMutex权限不够会返回NULL)
int GetMutexObject( HANDLE* h_mutex, char * MutexName, bool * isAlreadyExist )
{
int status = 0;
(*isAlreadyExist) = false;
// Make Mutex-Name
char mutex_name[MAX_PATH];
OSVERSIONINFO OsVersionInfo;
OsVersionInfo.dwOSVersionInfoSize = sizeof(OsVersionInfo);
GetVersionEx(&OsVersionInfo);
// Windows 2000 or later
if( 5 <= OsVersionInfo.dwMajorVersion )
{
strcpy_s(mutex_name, sizeof(mutex_name), "Global\\");
strncat_s(mutex_name, sizeof(mutex_name), MutexName, _TRUNCATE);
}
else
{
strcpy_s(mutex_name, sizeof(mutex_name), MutexName);
}
*h_mutex = CreateMutex(
NULL,
FALSE,
mutex_name
);
DWORD err = ::GetLastError();
if(( err == ERROR_ALREADY_EXISTS )||( err == ERROR_ACCESS_DENIED))
{
if(NULL != *h_mutex)
{
CloseHandle(*h_mutex);
}
(*isAlreadyExist) = true;
}
return status;
}
void ReleaseMutexObject( HANDLE* h_mutex )
{
ReleaseMutex(*h_mutex);
CloseHandle(*h_mutex);
}
外部使用范例
HANDLE hd2;
bool bFlag = FALSE;
GetMutexObject(&hd2, MUTEX_ALL, &bFlag);
if(!bFlag){
ReleaseMutexObject(&hd2);
}