C++常用操作函数.docx
- 文档编号:7592998
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:27
- 大小:22.78KB
C++常用操作函数.docx
《C++常用操作函数.docx》由会员分享,可在线阅读,更多相关《C++常用操作函数.docx(27页珍藏版)》请在冰豆网上搜索。
C++常用操作函数
C++常用操作函数
载入动态库
boolLoadDll()
{
HttpDownDll=LoadLibrary("HTTPDOWNDLL.dll");
if(HttpDownDll==NULL)
{
MessageBox(0,"载?
入?
动ˉ态?
库a错洙?
误ó!
?
","错洙?
误ó",MB_OK|MB_ICONWARNING);
FreeLibrary(HttpDownDll);
returnfalse;
}
SendCommand=(SendCommandFunc)GetProcAddress(HttpDownDll,"SendCommand");
if(SendCommand==NULL)
{
MessageBox(0,"GetProcAddress错洙?
误ó!
?
","错洙?
误ó",MB_OK|MB_ICONWARNING);
FreeLibrary(HttpDownDll);
returnfalse;
}
returntrue;
}
卸载动态库
boolFreeDll()
{
FreeLibrary(HttpDownDll);
SendCommand=NULL;
returntrue;
}
字符串分割
std:
:
vector : string>split(std: : stringstr,std: : stringpattern) { std: : string: : size_typepos; std: : vector : string>result; str+=pattern;//扩? 展1字? 符? 串? 以? 方? 便? 操ù作痢? intsize=str.size(); for(inti=0;i { pos=str.find(pattern,i); if(pos { std: : strings=str.substr(i,pos-i); result.push_back(s); i=pos+pattern.size()-1; } }returnresult; } UTF8转GBK voidUTF8ToGBK(std: : string&s) { charszOut[256]; memset(szOut,0,256); strcpy(szOut,s.c_str()); unsignedshort*wszGbk=NULL; char*szGbk=NULL; //长度 intlen=MultiByteToWideChar(CP_UTF8,0,(LPCSTR)szOut,-1,NULL,0); wszGbk=newunsignedshort[len+1]; memset(wszGbk,0,len*2+2); MultiByteToWideChar(CP_UTF8,0,(LPCSTR)szOut,-1,(LPWSTR)wszGbk,len); //长度 len=WideCharToMultiByte(CP_ACP,0,(LPWSTR)wszGbk,-1,NULL,0,NULL,NULL); szGbk=newchar[len+1]; memset(szGbk,0,len+1); WideCharToMultiByte(CP_ACP,0,(LPWSTR)wszGbk,-1,szGbk,len,NULL,NULL); //szOut=szGbk;//这样得到的szOut不正确,因为此句意义是将szGbk的首地址赋给szOut, //当delete[]szGbk执行后szGbk的内存空间将被释放,此时将得不到szOut的内容 memset(szOut,'\0',strlen(szGbk)+1);//改将szGbk的内容赋给szOut,这样即使szGbk被释放也能得到正确的值 memcpy(szOut,szGbk,strlen(szGbk)); s=szOut; delete[]szGbk; delete[]wszGbk; } 删除文件 intDeleteFileFunc(stringv_SourceFile) { intl_Result=0; /*_access(char*,int)判断文件是否存在 存在返回0;不存在返回-1. _access(constchar*path,intmode) mode的值: 00是否存在 02写权限 04读权限 06读写权限 */ charl_SourceFile[256]={0}; strcpy(l_SourceFile,v_SourceFile.c_str()); if(! _access(l_SourceFile,0))//如果文件存在: 文件为只读无法删除 { //去掉文件只读属性 SetFileAttributes(l_SourceFile,0); if(DeleteFile(l_SourceFile))//删除成功 { l_Result=0; } else//无法删除: 文件只读或无权限执行删除 { //cout< 文件为只读属性或无删除权限."< l_Result=1; } } returnl_Result; } 重命名文件 intRenameFileFunc(stringv_SourceFile,stringv_NewFileName) { intl_Result=0; charl_SourceFile[256]={0}; charl_NewFileName[256]={0}; strcpy(l_SourceFile,v_SourceFile.c_str()); strcpy(l_NewFileName,v_NewFileName.c_str()); if(! _access(l_SourceFile,0))//如果文件存在: { if(! rename(l_SourceFile,l_NewFileName))//删除成功 { l_Result=0; } else//无法重命名: 文件打开或无权限执行重命名 { //cout<<"文件无法重命名(可能原因如下): "< //cout<<"\t"<<"1."< //<<"\t"<<"2."< //<<"\t"<<"3."<<"你没有权限重命名此文件."< l_Result=1; } } else//文件不存在 { l_Result=1; } returnl_Result; } 设置系统语言 intSetLanguageThrd() { BOOLbXPLaterOS=FALSE;//是否为xp以后版本的os OSVERSIONINFOovs; ovs.dwOSVersionInfoSize=sizeof(OSVERSIONINFO); if(: : GetVersionEx(&ovs)) { bXPLaterOS=(ovs.dwPlatformId==VER_PLATFORM_WIN32_NT &&ovs.dwMajorVersion>=6 &&ovs.dwMinorVersion>=0);//vistaorlater } else { returnFALSE; } intlang=0; //charl_Language[10]={0}; char*l_Language=ReadXmlFile("System","Language",""); if(stricmp(l_Language,"en")==0) lang=1; if(lang==1) { if(bXPLaterOS) SetThreadUILanguage(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US)); else SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT)); } elseif(lang==0) { if(bXPLaterOS) SetThreadUILanguage(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED)); else SetThreadLocale(MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_DEFAULT)); } else { returnFALSE; } returnTRUE; } 读写xml文件 char*ReadXmlFile(char*v_FatherNodeName,char*v_NodeName,char*v_Default) { charl_ComName[20]={0}; charl_TempStr[20]={0}; charl_XmlPath[256]={0}; charl_ExePath[256]={0}; GetModuleFileName(NULL,l_ExePath,256); intl_ExePathLen=strlen(l_ExePath); for(inti=l_ExePathLen-1;i>=0;i--) { if(l_ExePath[i]=='\\') { memcpy(l_XmlPath,l_ExePath,i); } } strcat(l_XmlPath,g_XmlFileName); CTinyXMLLibl_TinyXml; strcpy(l_TinyXml.m_XmlPath,l_XmlPath); intl_Result=l_TinyXml.LoadXml(); if(l_Result==0) { l_TinyXml.ReadXmlNode(v_FatherNodeName,v_NodeName,v_Default,l_TempStr); } else { strcpy(l_TempStr,v_Default); } returnl_TempStr; } char*ReadXmlFile(char*v_XmlFileName,char*v_FatherNodeName,char*v_NodeName,char*v_Default) { charl_XmlPath[256]={0}; charl_TempStr[20]={0}; charl_ExePath[256]={0}; GetModuleFileName(NULL,l_ExePath,256); intl_ExePathLen=strlen(l_ExePath); for(inti=l_ExePathLen-1;i>=0;i--) { if(l_ExePath[i]=='\\') { memcpy(l_XmlPath,l_ExePath,i); } } strcat(l_XmlPath,"\\"); strcat(l_XmlPath,v_XmlFileName); CTinyXMLLibl_TinyXml; strcpy(l_TinyXml.m_XmlPath,l_XmlPath); intl_Result=l_TinyXml.LoadXml(); if(l_Result==0) { l_TinyXml.ReadXmlNode(v_FatherNodeName,v_NodeName,v_Default,l_TempStr); } else { strcpy(l_TempStr,v_Default); } returnl_TempStr; } intWriteXmlFile(char*v_XmlFileName,char*v_FatherNodeName,char*v_NodeName,char*v_Value) { charl_ComName[20]={0}; charl_XmlPath[256]={0}; charl_ExePath[256]={0}; GetModuleFileName(NULL,l_ExePath,256); intl_ExePathLen=strlen(l_ExePath); for(inti=l_ExePathLen-1;i>=0;i--) { if(l_ExePath[i]=='\\') { memcpy(l_XmlPath,l_ExePath,i); } } strcat(l_XmlPath,"\\"); strcat(l_XmlPath,v_XmlFileName); CTinyXMLLibl_TinyXml; strcpy(l_TinyXml.m_XmlPath,l_XmlPath); l_TinyXml.LoadXml(); l_TinyXml.WriteXmlNode(v_FatherNodeName,v_NodeName,v_Value); return0; } 读ini文件 intCCommServerDlg: : ReadIniFile() { charl_TempStr[2048]={0}; charl_CurrentDir[256]={0}; l_TempStr,2048,g_config_name); l_TempStr,2048,g_config_name); GetCurrentDirectory(256,l_CurrentDir); strcat(l_CurrentDir,"\\Download"); CheckFolderExist(l_CurrentDir); GetPrivateProfileString("Application","LocalFileList",l_FileListPath,l_TempStr,2048,g_config_name); GetPrivateProfileString("Application","LocalFileDir",l_LocalFileDir,l_TempStr,2048,g_config_name); return0; } 读文件 voidRead(char*v_Path,char*v_Buffer,constintv_BufferLen) { HANDLEhFile=: : CreateFile(v_Path,GENERIC_READ,0,NULL,OPEN_EXISTING,NULL,NULL); if(hFile! =INVALID_HANDLE_VALUE) { //SetFilePointer(hFile,-1,NULL,FILE_END); SetFilePointer(hFile,81920,NULL,FILE_BEGIN); charl_Buffer[5]={0}; DWORDnNumberOfBytesRead; BOOLbRet=: : ReadFile(hFile,l_Buffer,v_BufferLen,&nNumberOfBytesRead,NULL); memcpy(v_Buffer,l_Buffer,v_BufferLen); //TODO... CloseHandle(hFile); } } 写文件 voidWrite(char*v_Path,char*v_Content,constintv_ContentSize) { HANDLEhFile=: : CreateFile(v_Path,GENERIC_WRITE,0,NULL,OPEN_EXISTING,NULL,NULL); if(hFile! =INVALID_HANDLE_VALUE) { //SetFilePointer(hFile,-1,NULL,FILE_END); SetFilePointer(hFile,81920,NULL,FILE_BEGIN); DWORDnNumberOfBytesWritten; : : WriteFile(hFile,v_Content,v_ContentSize,&nNumberOfBytesWritten,NULL); //TODO... CloseHandle(hFile); } } 查找进程 BOOLFindProcess() { inti=0; PROCESSENTRY32pe32; pe32.dwSize=sizeof(pe32); HANDLEhProcessSnap=: : CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(hProcessSnap==INVALID_HANDLE_VALUE) { i+=0; } BOOLbMore=: : Process32First(hProcessSnap,&pe32); while(bMore) { //printf("进程名称: %s\n",pe32.szExeFile); if(stricmp("RWPEPROC.exe",pe32.szExeFile)==0) { //printf("进程运行中"); i+=1; } bMore=: : Process32Next(hProcessSnap,&pe32); } if(i>1){//大于1,排除自身 returntrue; }else{ returnfalse; } } 路径操作 charl_ExePath[256]={0}; charl_MainExePath[256]={0}; //GetCurrentDirectory(256,l_ExePath); GetModuleFileName(NULL,l_ExePath,256); intl_ExePathLen=strlen(l_ExePath); for(inti=l_ExePathLen-1;i>=0;i--) { if(l_ExePath[i]=='\\') { memcpy(l_MainExePath,l_ExePath,i); } } strcat(l_MainExePath,"\\RWPEPROC.exe"); BOOLAPIENTRYDllMain(HMODULEhModule, DWORDul_reason_for_call, LPVOIDlpReserved ) { switch(ul_reason_for_call) { caseDLL_PROCESS_ATTACH: { char*l_Buffer; l_Buffer=_getcwd(NULL,0); _getcwd(DllPath,MAXPATH); HMODULEmodule=GetModuleHandle(0); charszBuff[MAX_PATH]={0}; GetModuleFileName(module,szBuff,sizeof(szBuff)); intl_Pause=0; } caseDLL_THREAD_ATTACH: caseDLL_THREAD_DETACH: caseDLL_PROCESS_DETACH: break; } returnTRUE; } 查找指定进程 ///判断指定进程是否存在 intIsProcExist(charFileName[50],boolIsDelete) { charNameBuff[MAX_PATH]; char*pFileName;//pFileName转化为大写后的字符串指针 char*pNameBuff;//NameBuff转化为大写后的字符串指针 BOOLbRes;//返回结果 HINSTANCEhInstance;//动态库句柄 HANDLEhHandle;//进程句柄 inti,j;//循环变量 intlen; funEnumPorcessEnumProcesses; funGetModuleFileNameExGetModuleFileNameEx; DWORD*buffer,maxNum,factNum; maxNum=MAXPROCESS; factNum=MAXPROCESS; buffer=(DWORD*)malloc(maxNum); hInstance=LoadLibrary("psapi.dll");//装载psapi.dll if(hInstance) { EnumProcesses=(funEnumPorcess)GetProcAddress(hInstance,"EnumProcesses"); if(EnumProcesses) { bRes=EnumProcesses(buffer,maxNum,&factNum);//枚举进程,得到进程名称和实际进程总大小 if(bRes)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 常用 操作 函数
![提示](https://static.bdocx.com/images/bang_tan.gif)