CSystem.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 GetProgramDir()
  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. int pos = strPath.find_last_of('\\', strPath.length());
  18. return strPath.substr(0, pos); // Return the directory without the file name
  19. }
  20. // 程序开机自动启动
  21. static void autostart()
  22. {
  23. HKEY hKey;
  24. wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  25. //1、找到系统的启动项
  26. if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) ///打开启动项
  27. {
  28. //2、得到本程序自身的全路径
  29. TCHAR strExeFullDir[MAX_PATH];
  30. GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
  31. //3、添加一个子Key,并设置值,"GISRestart"是应用程序名字(不加后缀.exe)
  32. RegSetValueEx(hKey, L"智铺子收银软件", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1) * sizeof(TCHAR));
  33. //4、关闭注册表
  34. RegCloseKey(hKey);
  35. }
  36. else
  37. {
  38. cout << "系统参数错误, 不能随系统启动";
  39. }
  40. }
  41. // 取消开机自动启动
  42. static void cancelAutoStart()
  43. {
  44. HKEY hKey;
  45. wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  46. //1、找到系统的启动项
  47. if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
  48. {
  49. //2、删除值
  50. RegDeleteValue(hKey, L"智铺子收银软件");
  51. //3、关闭注册表
  52. RegCloseKey(hKey);
  53. }
  54. }
  55. // 判断文件是否存在
  56. static BOOL IsFileExist(const wstring& csFile)
  57. {
  58. DWORD dwAttrib = GetFileAttributes(csFile.c_str());
  59. return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  60. }
  61. // 判断文件夹是否存在
  62. static BOOL IsDirExist(const wstring& csDir)
  63. {
  64. DWORD dwAttrib = GetFileAttributes(csDir.c_str());
  65. return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  66. }
  67. // 判断文件或文件夹是否存在
  68. static BOOL IsPathExist(const wstring& csPath)
  69. {
  70. DWORD dwAttrib = GetFileAttributes(csPath.c_str());
  71. return INVALID_FILE_ATTRIBUTES != dwAttrib;
  72. }
  73. };