CSystem.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #pragma once
  2. #include "../pch/pch.h"
  3. class CSystem
  4. {
  5. public:
  6. CSystem();
  7. ~CSystem();
  8. static int get_CPU_core_num();
  9. //程序休眠X秒
  10. static void my_sleep(int second);
  11. static std::wstring getExePath()
  12. {
  13. wchar_t exeFullPath[MAX_PATH]; // Full path
  14. std::wstring strPath = L"";
  15. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  16. strPath = (wstring)exeFullPath; // Get full path of the file
  17. return strPath;
  18. }
  19. static std::wstring GetProgramDir()
  20. {
  21. wchar_t exeFullPath[MAX_PATH]; // Full path
  22. std::wstring strPath = L"";
  23. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  24. strPath = (wstring)exeFullPath; // Get full path of the file
  25. int pos = strPath.find_last_of('\\', strPath.length());
  26. return strPath.substr(0, pos); // Return the directory without the file name
  27. }
  28. // 程序开机自动启动
  29. static void autostart()
  30. {
  31. HKEY hKey;
  32. wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  33. //1、找到系统的启动项
  34. if(RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) ///打开启动项
  35. {
  36. //2、得到本程序自身的全路径
  37. TCHAR strExeFullDir[MAX_PATH];
  38. GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
  39. //3、添加一个子Key,并设置值,"GISRestart"是应用程序名字(不加后缀.exe)
  40. RegSetValueEx(hKey, L"智铺子收银插件", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1) * sizeof(TCHAR));
  41. //4、关闭注册表
  42. RegCloseKey(hKey);
  43. }
  44. else
  45. {
  46. cout << "系统参数错误, 不能随系统启动";
  47. }
  48. }
  49. // 取消开机自动启动
  50. static void cancelAutoStart()
  51. {
  52. HKEY hKey;
  53. wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  54. //1、找到系统的启动项
  55. if(RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
  56. {
  57. //2、删除值
  58. RegDeleteValue(hKey, L"智铺子收银插件");
  59. //3、关闭注册表
  60. RegCloseKey(hKey);
  61. }
  62. }
  63. static bool IsAutoStart()
  64. {
  65. HKEY hKey;
  66. wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  67. //1、找到系统的启动项
  68. if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
  69. {
  70. char dwValue[256];
  71. DWORD dwSzType = REG_SZ;
  72. DWORD dwSize = sizeof(dwValue);
  73. if (::RegQueryValueEx(hKey, _T("智铺子收银插件"), 0, &dwSzType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS)
  74. {
  75. return false;
  76. }
  77. //关闭注册表
  78. RegCloseKey(hKey);
  79. return true;
  80. }
  81. return false;
  82. }
  83. // 判断文件是否存在
  84. static BOOL IsFileExist(const wstring& csFile)
  85. {
  86. DWORD dwAttrib = GetFileAttributes(csFile.c_str());
  87. return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  88. }
  89. // 判断文件夹是否存在
  90. static BOOL IsDirExist(const wstring& csDir)
  91. {
  92. DWORD dwAttrib = GetFileAttributes(csDir.c_str());
  93. return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  94. }
  95. // 判断文件或文件夹是否存在
  96. static BOOL IsPathExist(const wstring& csPath)
  97. {
  98. DWORD dwAttrib = GetFileAttributes(csPath.c_str());
  99. return INVALID_FILE_ATTRIBUTES != dwAttrib;
  100. }
  101. static std::string GetVersion()
  102. {
  103. std::string r = "";
  104. UINT sz = GetFileVersionInfoSize(getExePath().c_str(), 0);
  105. if(sz != 0)
  106. {
  107. r.resize(sz, 0);
  108. char* pBuf = NULL;
  109. pBuf = new char[sz];
  110. VS_FIXEDFILEINFO* pVsInfo;
  111. if(GetFileVersionInfo(getExePath().c_str(), 0, sz, pBuf))
  112. {
  113. if(VerQueryValue(pBuf, L"\\", (void**)&pVsInfo, &sz))
  114. {
  115. sprintf(pBuf, "%d.%d.%d.%d", HIWORD(pVsInfo->dwFileVersionMS), LOWORD(pVsInfo->dwFileVersionMS), HIWORD(pVsInfo->dwFileVersionLS), LOWORD(pVsInfo->dwFileVersionLS));
  116. r = pBuf;
  117. }
  118. }
  119. delete pBuf;
  120. }
  121. return r;
  122. }
  123. static BOOL IsRunasAdmin();
  124. };