CSystem.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // 判断文件是否存在
  64. static BOOL IsFileExist(const wstring& csFile)
  65. {
  66. DWORD dwAttrib = GetFileAttributes(csFile.c_str());
  67. return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  68. }
  69. // 判断文件夹是否存在
  70. static BOOL IsDirExist(const wstring& csDir)
  71. {
  72. DWORD dwAttrib = GetFileAttributes(csDir.c_str());
  73. return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  74. }
  75. // 判断文件或文件夹是否存在
  76. static BOOL IsPathExist(const wstring& csPath)
  77. {
  78. DWORD dwAttrib = GetFileAttributes(csPath.c_str());
  79. return INVALID_FILE_ATTRIBUTES != dwAttrib;
  80. }
  81. static std::string GetVersion()
  82. {
  83. std::string r = "";
  84. UINT sz = GetFileVersionInfoSize(getExePath().c_str(), 0);
  85. if(sz != 0)
  86. {
  87. r.resize(sz, 0);
  88. char* pBuf = NULL;
  89. pBuf = new char[sz];
  90. VS_FIXEDFILEINFO* pVsInfo;
  91. if(GetFileVersionInfo(getExePath().c_str(), 0, sz, pBuf))
  92. {
  93. if(VerQueryValue(pBuf, L"\\", (void**)&pVsInfo, &sz))
  94. {
  95. sprintf(pBuf, "%d.%d.%d.%d", HIWORD(pVsInfo->dwFileVersionMS), LOWORD(pVsInfo->dwFileVersionMS), HIWORD(pVsInfo->dwFileVersionLS), LOWORD(pVsInfo->dwFileVersionLS));
  96. r = pBuf;
  97. }
  98. }
  99. delete pBuf;
  100. }
  101. return r;
  102. }
  103. };