CSystem.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #ifdef _WIN32
  12. // 程序开机自动启动
  13. static void autostart()
  14. {
  15. HKEY hKey;
  16. wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  17. //1、找到系统的启动项
  18. if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) ///打开启动项
  19. {
  20. //2、得到本程序自身的全路径
  21. TCHAR strExeFullDir[MAX_PATH];
  22. GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
  23. //3、添加一个子Key,并设置值,"GISRestart"是应用程序名字(不加后缀.exe)
  24. RegSetValueEx(hKey, L"乐外卖接单软件", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1) * sizeof(TCHAR));
  25. //4、关闭注册表
  26. RegCloseKey(hKey);
  27. }
  28. else
  29. {
  30. cout << "系统参数错误, 不能随系统启动";
  31. }
  32. }
  33. // 取消开机自动启动
  34. static void cancelAutoStart()
  35. {
  36. HKEY hKey;
  37. wstring strRegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
  38. //1、找到系统的启动项
  39. if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
  40. {
  41. //2、删除值
  42. RegDeleteValue(hKey, L"乐外卖接单软件");
  43. //3、关闭注册表
  44. RegCloseKey(hKey);
  45. }
  46. }
  47. static std::wstring GetProgramDir()
  48. {
  49. wchar_t exeFullPath[MAX_PATH]; // Full path
  50. std::wstring strPath = L"";
  51. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  52. strPath = (wstring)exeFullPath; // Get full path of the file
  53. int pos = strPath.find_last_of('\\', strPath.length());
  54. return strPath.substr(0, pos); // Return the directory without the file name
  55. }
  56. // 判断文件是否存在
  57. static BOOL IsFileExist(const wstring& csFile)
  58. {
  59. DWORD dwAttrib = GetFileAttributes(csFile.c_str());
  60. return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  61. }
  62. // 判断文件夹是否存在
  63. static BOOL IsDirExist(const wstring& csDir)
  64. {
  65. DWORD dwAttrib = GetFileAttributes(csDir.c_str());
  66. return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  67. }
  68. // 判断文件或文件夹是否存在
  69. static BOOL IsPathExist(const wstring& csPath)
  70. {
  71. DWORD dwAttrib = GetFileAttributes(csPath.c_str());
  72. return INVALID_FILE_ATTRIBUTES != dwAttrib;
  73. }
  74. #else
  75. static std::string getAbsopath()
  76. {
  77. char buf[1024];
  78. size_t size = 1024;
  79. size_t i;
  80. size_t len = readlink("/proc/self/exe", buf, size - 1);
  81. if (len < 0 || (len >= size - 1))
  82. {
  83. return NULL;
  84. }
  85. buf[len] = '\0';
  86. for (i = len; i >= 0; i--)
  87. {
  88. if (buf[i] == '/')
  89. {
  90. buf[i + 1] = '\0';
  91. break;
  92. }
  93. }
  94. return buf;
  95. }
  96. #endif
  97. };