Linux and windows Porting

// 根据本人的经验胡乱写写: // linux下获得可用磁盘空间大小采用: string sCmd = "df -m /usr/local | awk '{ if(NR == 2) print $4 }'"; string sResult = pipeIPC(sCmd); pipeIPC()如下: string pipeIPC(const string& command) { string sResult = ""; char *s = new char[2048]; bzero(s, 2047); FILE *fp = NULL; if((fp = popen(command.c_str(), "r")) == NULL) { cout << "Failed to open pipe!" << endl; } while(fgets(s, 2048, fp) != NULL) { sResult += s; }; pclose(fp); delete[] s; return sResult; } // 在windows中采用: string GetMyPointDiscInfo(char Discname) { char dic[4] = {Discname, ':', '//'}; __int64 FreeBytesToCaller; __int64 TotlaBytes; __int64 FreeBytes; BOOL fResult = GetDiskFreeSpaceEx( dic, (PULARGE_INTEGER)&FreeBytesToCaller, (PULARGE_INTEGER)&TotlaBytes, (PULARGE_INTEGER)&FreeBytes); char cTstr[64], cFstr[64]; _i64toa(TotlaBytes/1024/1024, cTstr, 10); string sResult = string("Total work space: ") + cTstr; _i64toa(FreeBytes/1024/1024, cFstr, 10); sResult += string("/nFree work space: ") + cFstr; return sResult; } int GetMyPointDiscFree(char Discname) { char dic[4] = {Discname, ':', '//'}; __int64 FreeBytesToCaller; __int64 TotlaBytes; __int64 FreeBytes; BOOL fResult = GetDiskFreeSpaceEx( dic, (PULARGE_INTEGER)&FreeBytesToCaller, (PULARGE_INTEGER)&TotlaBytes, (PULARGE_INTEGER)&FreeBytes); int iReturn = int(FreeBytes/1024/1024); return iReturn; } // 获得CPU信息: linux: sCmd = "cat /proc/cpuinfo | grep 'model name'"; sResult = pipeIPC(sCmd); windows: string GetCPUInfo() { HKEY hKEY; LPCSTR data_str = "HARDWARE//DESCRIPTION//System//CentralProcessor//0//"; LONG ret0 =(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_str, 0, KEY_READ , &hKEY)); if(ret0 == ERROR_SUCCESS) { LPBYTE cpuinfo_Get=new BYTE[80]; DWORD type_1=REG_SZ ; DWORD cbData_1=80; long ret1=::RegQueryValueEx(hKEY, "ProcessorNameString", NULL,&type_1,cpuinfo_Get, &cbData_1); string cpuinfo = (char*)cpuinfo_Get; return cpuinfo; delete[] cpuinfo_Get; } else { return NULL; } ::RegCloseKey(hKEY); } // 获得内存: linux: sCmd = "cat /proc/meminfo | grep 'MemTotal//|MemFree//|SwapTotal//|SwapFree'"; sResult = pipeIPC(sCmd); if( (nStartPos = (int)sResult.find("MemTotal")) >= 0 ) sResult.replace(nStartPos, 9, "Total Memory: "); if( (nStartPos = (int)sResult.find("MemFree")) >= 0 ) sResult.replace(nStartPos, 8, "Free Memory: "); if( (nStartPos = (int)sResult.find("SwapTotal")) >= 0 ) sResult.replace(nStartPos, 10, "Total Swap: "); if( (nStartPos = (int)sResult.find("SwapFree")) >= 0 ) sResult.replace(nStartPos, 9, "Free Swap: "); win: MEMORYSTATUS memoryinfo; memoryinfo.dwLength = sizeof(memoryinfo); GlobalMemoryStatus(&memoryinfo); char cTpstr[32], cApstr[32], cTvstr[32], cAvstr[32]; _itoa(memoryinfo.dwTotalPhys/1024, cTpstr, 10); sResult = string("Total Physic Memory : ") + cTpstr + "KB/n"; _itoa(memoryinfo.dwAvailPhys/1024, cApstr, 10); sResult += string("Free Physic Memory : ") + cApstr + "KB/n"; _itoa(memoryinfo.dwTotalVirtual/1024, cTvstr, 10); sResult += string("Total Virtual Memory : ") + cTvstr + "KB/n"; _itoa(memoryinfo.dwAvailVirtual/1024, cAvstr, 10); sResult += string("Free Virtual Memory : ") + cAvstr + "KB/n"; // 操作系统: linux] sCmd = "cat /proc/version"; sResult = pipeIPC(sCmd) windows: string GetOSInfomation() { HKEY hKEY; LPCSTR data_str = "SOFTWARE//Microsoft//Windows NT//CurrentVersion//"; LONG ret0 =(::RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_str, 0, KEY_READ , &hKEY)); if(ret0 == ERROR_SUCCESS) { LPBYTE osname_Get=new BYTE[80]; DWORD type_1=REG_SZ ; DWORD cbData_1=80; long ret1=::RegQueryValueEx(hKEY, "ProductName", NULL,&type_1,osname_Get, &cbData_1); string osname = (string)(char*)osname_Get; LPBYTE osversion_Get=new BYTE [80]; DWORD type_2=REG_SZ; DWORD cbData_2=80; long ret2 = ::RegQueryValueEx(hKEY,"CSDVersion", NULL,&type_2,osversion_Get, &cbData_2); osname += string("(") + (char*)osversion_Get + string(")"); return osname; delete[] osname_Get; delete[] osversion_Get; } else { return NULL; } ::RegCloseKey(hKEY); } // 获得进程ID(通过进程名) string GetProcessID(const string process_name) { HANDLE handle=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); PROCESSENTRY32* info=new PROCESSENTRY32; info->dwSize = sizeof(PROCESSENTRY32); if(Process32First(handle,info)) { if(GetLastError()==ERROR_NO_MORE_FILES ) { cout << "No More Process" << endl; } else { if(strcmp(info->szExeFile, process_name.c_str()) == 0) { char cstr[64]; _i64toa((_int64)info->th32ProcessID, cstr, 10); string sstr = (string)cstr; return sstr; } while(Process32Next(handle, info) != FALSE) { if(strcmp(info->szExeFile, process_name.c_str()) == 0) { char cstr[64]; _i64toa((_int64)info->th32ProcessID, cstr, 10); string sstr = (string)cstr; return sstr; } } } } CloseHandle(handle); } // 删除非空目录: bool __stdcall deleteDirectory(const char* pszDir) { WIN32_FIND_DATA fd; char szTempFileFind[MAX_PATH] = { 0 }; bool bIsFinish = false; ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); sprintf(szTempFileFind, "%s//*.*", pszDir); HANDLE hFind = FindFirstFile(szTempFileFind, &fd); if(hFind == INVALID_HANDLE_VALUE) return false; while(!bIsFinish) { bIsFinish = (FindNextFile(hFind, &fd)) ? false : true; if((strcmp(fd.cFileName, ".") != 0) && (strcmp(fd.cFileName, "..") != 0)) { char szFoundFileName[MAX_PATH] = { 0 }; strcpy(szFoundFileName, fd.cFileName); if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { char szTempDir[MAX_PATH] = { 0 }; sprintf(szTempDir, "%s//%s", pszDir, szFoundFileName); deleteDirectory(szTempDir); } else { char szTempFileName[MAX_PATH] = { 0 }; sprintf(szTempFileName, "%s//%s", pszDir, szFoundFileName); DeleteFile(szTempFileName); } } } FindClose(hFind); if(!RemoveDirectory(pszDir)) { return false; } return true; } 

 

你可能感兴趣的:(windows,linux,String,null,Integer)