void Log(LPCTSTR szFormat, ...) { CString strMessage; va_list argList; va_start( argList, szFormat ); strMessage.FormatV( szFormat, argList ); va_end( argList ); CString strIndent(' ', g_indent*2); _tprintf(_T("%s%s\n"), (LPCTSTR)strIndent, (LPCTSTR)strMessage); ATLTRACE(_T("%s%s\n"), (LPCTSTR)strIndent, (LPCTSTR)strMessage); }
static HRESULT FormatDateTime( SYSTEMTIME *pSystemTime, char *pResult, size_t max ) { GetDateFormat( GetThreadLocale(), DATE_SHORTDATE, pSystemTime, NULL, pResult, (int)max ); size_t len = _tcslen( pResult ); if ( (max - len) < 2 ) return E_FAIL; // if not enough room in buffer pResult[ len ] = ' '; len++; GetTimeFormat( GetThreadLocale(), TIME_NOSECONDS, pSystemTime, NULL, pResult + len, (int)(max - len) ); return S_OK; }
#include "stdafx.h" //#include <iostream.h> int main(void) { //reinterpret_cast //将一个类型指针转换为另一个类型指针,这种在转换不修改指针变量值数据存放格式 //只需在编译时重新解释指针的类型,他可以将指针转化为一个整型数但不能用于非指针的转换 double d=9.3; double* pd = &d; int* pi = reinterpret_cast<int *> (pd); class A{}; class B{}; A* pa = new A; B* pb=reinterpret_cast<B*>(pa); //将pa 转为B long j=reinterpret_cast<long> (pa);//指针转换为整数 // int i=9; // double x=reinterpret_cast<double>(i); //reinterpret_cast不能用于非指针的转换 //const_cast //1.用于去除指针变量的常属性,将它转换为一个对应指针类型的普通变量, //2.反过来也可以将一个非常量指针转换为一个常量指针变量 //3.他无法将一个非指针的常量转换为普通变量 //example: const i=10; // int j=const_cast<int>(i); //无法转换 const int ppp=998; const int* pca=&ppp; int* p=const_cast<int*>(pca);//将它转换为一个对应指针类型的普通变量,去除了const; const A* paa=new A; A * ppppa=const_cast<A*> (paa);//它转换为一个对应指针类型的普通变量,去除了const; int * pii=0;//反过来也可以将一个非常量指针转换为一个常量指针变量 const int* piiic=const_cast<const int *>(pii); ////////////////////////////////////////////////////////////////////////////////// //static_cast //用于转换基本类型和具有继承关系的类新之间转换 //static_cast不太用于指针类型的之间的转换,他的效率没有reinterpret_cast的效率高 int in=99; double dn=static_cast<double> (in);//用于转换基本类型和具有继承关系的类新之间转换 class Base{}; class derv:public Base{}; derv dd; Base bbbb=static_cast<Base>(dd);//具有继承关系的类型之间转换 //static_cast不太用于指针类型的之间的转换,他的效率没有reinterpret_cast的效率高 Base *pb1=new Base; derv *pder=static_cast<derv*>(pb1);//基类转继承类 derv* pder1=new derv; Base* pbase1=static_cast<Base*>(pder1);//继承类指针转父类指针 //dynamic_cast //1.只能在继承类对象的指针之间或引用之间进行类型转换 //2.这种转换并非在编译时,而是在运行时,动态的 //3.没有继承关系,但被转换的类具有虚函数对象的指针进行转换 derv* dp=new derv; Base* bv=dynamic_cast<Base *>(dp);//继承类对象的指针之间进行类型转换 derv dpp;//继承类对象引用之间进行类型转换 Base &b=dynamic_cast<Base&>(dpp); class AA{ virtual int fun(){ return 0;} virtual ~AA(){} }; class BB{ }; //没有继承关系,但被转换的类具有虚函数对象的指针进行转换,编译可通过 AA* pAA=new AA; BB* pBB=dynamic_cast<BB *>(pAA); //没有继承关系,被转换的类也没有有虚函数对象的指针进行转换,编译不能通过 //BB* pBBB=new BB; //AA* pAAA=dynamic_cast<AA*>(pBBB); return 1; }
CString GetCurDir() { TCHAR sDrive[_MAX_DRIVE]; TCHAR sDir[_MAX_DIR]; TCHAR sFilename[_MAX_FNAME],Filename[_MAX_FNAME]; TCHAR sExt[_MAX_EXT]; GetModuleFileName(AfxGetInstanceHandle(), Filename,_MAX_PATH); _tsplitpath(Filename,sDrive,sDir,sFilename,sExt); CString homeDir(CString(sDrive)+CString(sDir)); int nLen = homeDir.GetLength(); if(homeDir.GetAt(nLen-1)!=_T('\\')) homeDir+=_T('\\'); return homeDir; }
//获取程序所在目录 CString GetDir() { CString strPath; ::GetModuleFileName(NULL,strPath.GetBuffer(MAX_PATH),MAX_PATH); strPath.ReleaseBuffer(); strPath = strPath.Left(strPath.ReverseFind(_T('\\')) +1 ); return strPath; }
CFileDialog dlg( TRUE , "(*.exe)|*.exe","*.*" ,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT ,"程序文件(*.exe)"); if(dlg.DoModal()==IDOK) { SetDlgItemText(IDC_EDIT_FILE_PATH,dlg.GetPathName()); }
//通过窗体名关闭进程 HWND hNoteWnd = FindWindow(NULL, "无标题 - 记事本"); if ( hNoteWnd == NULL ) { return -1; } DWORD dwNotePid = 0; GetWindowThreadProcessId(hNoteWnd, &dwNotePid); if ( dwNotePid == 0 ) { return -1; } HANDLE hNoteHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwNotePid); if ( hNoteHandle == NULL ) { return -1; } TerminateProcess(hNoteHandle, 0); CloseHandle(hNoteHandle);
void GetSysInfo() { char szComputerName[MAXBYTE] = { 0 }; char szUserName[MAXBYTE] = { 0 }; unsigned long nSize = MAXBYTE; OSVERSIONINFO OsVer; OsVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&OsVer); if ( OsVer.dwPlatformId == VER_PLATFORM_WIN32_NT ) { if ( OsVer.dwMajorVersion == 5 && OsVer.dwMinorVersion == 1 ) { printf("Windows XP %s \r\n", OsVer.szCSDVersion); } else if ( OsVer.dwMajorVersion == 5 && OsVer.dwMinorVersion == 0) { printf("Windows 2K \r\n"); }else{ printf("%d %d\n",OsVer.dwMajorVersion,OsVer.dwMinorVersion); } }else { printf("Ohter System \r\n"); } GetComputerName(szComputerName, &nSize); printf("Computer Name is %s \r\n", szComputerName); nSize = MAXBYTE; GetUserName(szUserName, &nSize); printf("User Name is %s \r\n", szUserName); }
//isNumber("1024",0); int isNumber(const char *z, int *realnum){ if( *z=='-' || *z=='+' ) z++; if( !isdigit(*z) ){ return 0; } z++; if( realnum ) *realnum = 0; while( isdigit(*z) ){ z++; } if( *z=='.' ){ z++; if( !isdigit(*z) ) return 0; while( isdigit(*z) ){ z++; } if( realnum ) *realnum = 1; } if( *z=='e' || *z=='E' ){ z++; if( *z=='+' || *z=='-' ) z++; if( !isdigit(*z) ) return 0; while( isdigit(*z) ){ z++; } if( realnum ) *realnum = 1; } return *z==0; }
//int=>string //1、使用sprintf 2、_itoa //<sstream.h> stringstream() int hello=4; stringstream ss; ss<<hello; string s=ss.str(); //调用string的方法 cout<<s.c_str()<<endl;
void WriteLog(char *fmt,...) { FILE *fp; va_list args; char modname[128]; if((fp =fopen("c:\\hookapi.log", "a")) !=NULL) { va_start(args,fmt); GetModuleFileName(NULL, modname, sizeof(modname)); fprintf(fp, "%s: ", modname); // fprintf(fp,"Netxx: "); vfprintf(fp, fmt, args); fprintf(fp, "\n"); fclose(fp); va_end(args); } } char * GetErrString(char *str, DWORD errcode) { LPVOID lpbuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpbuf, 0, NULL )) { lstrcpy(str, (char *)lpbuf); LocalFree(lpbuf); } return str; }
ansic和Unicode转换函数:
char *UNICODEconvertANSI(LPWSTR wText,int targetCodePage) { //wchar_t wText[20] = {L"宽字符转换实例!OK!"}; int dwNum = WideCharToMultiByte(targetCodePage,NULL,wText,-1,NULL,0,NULL,FALSE); char *psText; psText = new char[dwNum+1]; memset(psText,0,dwNum); WideCharToMultiByte (targetCodePage,NULL,wText,-1,psText,dwNum+1,NULL,FALSE); return psText; } LPWSTR ANSIConvertUNCOIDE(char* aText,int sourceCodePage) { int len=MultiByteToWideChar(sourceCodePage,NULL,aText,-1,NULL,0); wchar_t *psText=new wchar_t[len+1]; memset(psText,0,len); MultiByteToWideChar(sourceCodePage,NULL,aText,-1,psText,len+1); //psText[len]='\0'; return psText; }字符连接
wchar_t *STR2WSTR(char *pmb) //narrow string to wide string { size_t requiredSize = mbstowcs(NULL, pmb, 0); wchar_t *pwc = (wchar_t *)malloc( (requiredSize + 1) * sizeof( wchar_t )); mbstowcs( pwc, pmb, requiredSize + 1); return pwc; } char *cs(char *str1, char *str2) //connect string { size_t newstrlen=strlen(str1)+strlen(str2)+1; char *newstr=(char*)malloc(newstrlen); memcpy(newstr,str1,strlen(str1)); memcpy(newstr+strlen(str1),str2,strlen(str2)+1); return newstr; }