| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #pragma once
- #include "../pch/pch.h"
- class CSystem
- {
- public:
- CSystem();
- ~CSystem();
- static int get_CPU_core_num();
- //程序休眠X秒
- static void my_sleep(int second);
- static std::wstring getExePath()
- {
- wchar_t exeFullPath[MAX_PATH]; // Full path
- std::wstring strPath = L"";
- GetModuleFileName(NULL, exeFullPath, MAX_PATH);
- strPath = (wstring)exeFullPath; // Get full path of the file
- return strPath;
- }
- static std::wstring GetProgramDir()
- {
- wchar_t exeFullPath[MAX_PATH]; // Full path
- std::wstring strPath = L"";
- GetModuleFileName(NULL, exeFullPath, MAX_PATH);
- strPath = (wstring)exeFullPath; // Get full path of the file
- int pos = strPath.find_last_of('\\', strPath.length());
- return strPath.substr(0, pos); // Return the directory without the file name
- }
- // 程序开机自动启动
- static void autostart()
- {
- HKEY hKey;
- wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
- //1、找到系统的启动项
- if(RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) ///打开启动项
- {
- //2、得到本程序自身的全路径
- TCHAR strExeFullDir[MAX_PATH];
- GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
- //3、添加一个子Key,并设置值,"GISRestart"是应用程序名字(不加后缀.exe)
- RegSetValueEx(hKey, L"智铺子收银插件", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1) * sizeof(TCHAR));
- //4、关闭注册表
- RegCloseKey(hKey);
- }
- else
- {
- cout << "系统参数错误, 不能随系统启动";
- }
- }
- // 取消开机自动启动
- static void cancelAutoStart()
- {
- HKEY hKey;
- wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
- //1、找到系统的启动项
- if(RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
- {
- //2、删除值
- RegDeleteValue(hKey, L"智铺子收银插件");
- //3、关闭注册表
- RegCloseKey(hKey);
- }
- }
- static bool IsAutoStart()
- {
- HKEY hKey;
- wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
- //1、找到系统的启动项
- if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
- {
- char dwValue[256];
- DWORD dwSzType = REG_SZ;
- DWORD dwSize = sizeof(dwValue);
- if (::RegQueryValueEx(hKey, _T("智铺子收银插件"), 0, &dwSzType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
- {
- return false;
- }
- //关闭注册表
- RegCloseKey(hKey);
- return true;
- }
- return false;
- }
- // 判断文件是否存在
- static BOOL IsFileExist(const wstring& csFile)
- {
- DWORD dwAttrib = GetFileAttributes(csFile.c_str());
- return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
- }
- // 判断文件夹是否存在
- static BOOL IsDirExist(const wstring& csDir)
- {
- DWORD dwAttrib = GetFileAttributes(csDir.c_str());
- return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
- }
- // 判断文件或文件夹是否存在
- static BOOL IsPathExist(const wstring& csPath)
- {
- DWORD dwAttrib = GetFileAttributes(csPath.c_str());
- return INVALID_FILE_ATTRIBUTES != dwAttrib;
- }
- static std::string GetVersion()
- {
- std::string r = "";
- UINT sz = GetFileVersionInfoSize(getExePath().c_str(), 0);
- if(sz != 0)
- {
- r.resize(sz, 0);
- char* pBuf = NULL;
- pBuf = new char[sz];
- VS_FIXEDFILEINFO* pVsInfo;
- if(GetFileVersionInfo(getExePath().c_str(), 0, sz, pBuf))
- {
- if(VerQueryValue(pBuf, L"\\", (void**)&pVsInfo, &sz))
- {
- sprintf(pBuf, "%d.%d.%d.%d", HIWORD(pVsInfo->dwFileVersionMS), LOWORD(pVsInfo->dwFileVersionMS), HIWORD(pVsInfo->dwFileVersionLS), LOWORD(pVsInfo->dwFileVersionLS));
- r = pBuf;
- }
- }
- delete pBuf;
- }
- return r;
- }
- static BOOL IsRunasAdmin();
- };
|