CSystem.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. class CSystem
  3. {
  4. public:
  5. CSystem();
  6. ~CSystem();
  7. static int get_CPU_core_num();
  8. //程序休眠X秒
  9. static void my_sleep(int second);
  10. static std::wstring GetProgramDir()
  11. {
  12. wchar_t exeFullPath[MAX_PATH]; // Full path
  13. std::wstring strPath = L"";
  14. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  15. strPath = (wstring)exeFullPath; // Get full path of the file
  16. int pos = strPath.find_last_of('\\', strPath.length());
  17. return strPath.substr(0, pos); // Return the directory without the file name
  18. }
  19. // 程序开机自动启动
  20. static void autostart()
  21. {
  22. HKEY hKey;
  23. wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  24. //1、找到系统的启动项
  25. if(RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) ///打开启动项
  26. {
  27. //2、得到本程序自身的全路径
  28. TCHAR strExeFullDir[MAX_PATH];
  29. GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
  30. //3、判断注册表项是否已经存在
  31. TCHAR strDir[MAX_PATH] = {};
  32. DWORD nLength = MAX_PATH;
  33. long result = RegGetValue(hKey, nullptr, L"智铺子收银软件", RRF_RT_REG_SZ, 0, strDir, &nLength);
  34. //4、已经存在
  35. if(result != ERROR_SUCCESS || _tcscmp(strExeFullDir, strDir) != 0)
  36. {
  37. //5、添加一个子Key,并设置值,"GISRestart"是应用程序名字(不加后缀.exe)
  38. RegSetValueEx(hKey, L"智铺子收银软件", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1) * sizeof(TCHAR));
  39. //6、关闭注册表
  40. RegCloseKey(hKey);
  41. }
  42. }
  43. else
  44. {
  45. cout << "系统参数错误, 不能随系统启动";
  46. }
  47. }
  48. // 取消开机自动启动
  49. static void cancelAutoStart()
  50. {
  51. HKEY hKey;
  52. wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  53. //1、找到系统的启动项
  54. if(RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
  55. {
  56. //2、删除值
  57. RegDeleteValue(hKey, L"智铺子收银软件");
  58. //3、关闭注册表
  59. RegCloseKey(hKey);
  60. }
  61. }
  62. };