SystemCmds.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "StdAfx.h"
  2. #include "AutoFunc.h"
  3. #include <Psapi.h>
  4. #include <shellapi.h>
  5. #pragma comment(lib, "shell32.lib")
  6. #pragma comment(lib, "Psapi.lib")
  7. SystemCmds::SystemCmds(void)
  8. {
  9. ::CoInitialize(NULL);
  10. }
  11. SystemCmds::~SystemCmds(void)
  12. {
  13. CoUninitialize();
  14. }
  15. SystemCmds g_SystemCmds_Init;
  16. BOOL SystemCmds::CreateTheProcess_Console(const TCHAR *pfullFilePath, PROCESS_INFORMATION &pinfo, bool HideWindow)
  17. {
  18. string fullFilePath = pfullFilePath;
  19. size_t index = fullFilePath.rfind('\\');
  20. std::string strPathOnly = fullFilePath.substr(0, index);
  21. char szCommand[MAX_PATH];
  22. memset(szCommand, 0, MAX_PATH);
  23. strcpy_s(szCommand, MAX_PATH, pfullFilePath);
  24. STARTUPINFO si;
  25. ZeroMemory(&si, sizeof(si));
  26. si.cb = sizeof(si);
  27. ZeroMemory(&pinfo, sizeof(pinfo));
  28. if (HideWindow)
  29. {
  30. si.dwFlags = STARTF_USESHOWWINDOW;
  31. si.wShowWindow = SW_HIDE;
  32. }
  33. if (!CreateProcess(NULL, // No module name (use command line)
  34. szCommand, // Command line
  35. NULL, // Process handle not inheritable
  36. NULL, // Thread handle not inheritable
  37. FALSE, // Set handle inheritance to FALSE
  38. CREATE_NEW_CONSOLE, // No creation flags
  39. NULL, // Use parent's environment block
  40. //NULL,
  41. strPathOnly.c_str(), // Use parent's starting directory
  42. &si, // Pointer to STARTUPINFO structure
  43. &pinfo) // Pointer to PROCESS_INFORMATION structure
  44. )
  45. {
  46. printf("CreateProcess failed (%d).\n", GetLastError());
  47. return FALSE;
  48. }
  49. return TRUE;
  50. }
  51. BOOL SystemCmds::RunTheProcess(const TCHAR *fullFilePath,bool HideWindow)
  52. {
  53. INT ShowFlag = SW_SHOWNORMAL;
  54. if (HideWindow)
  55. {
  56. ShowFlag = SW_HIDE;
  57. }
  58. string fullFilePathL = fullFilePath;
  59. size_t index = fullFilePathL.rfind('\\');
  60. std::string strPathOnly = fullFilePathL.substr(0, index);
  61. std::string fileName = fullFilePathL.substr(index + 1,fullFilePathL.size() - (index + 1));
  62. if ((INT_PTR)ShellExecute(NULL, NULL, fileName.c_str(), NULL, strPathOnly.c_str(), ShowFlag) > 32)
  63. {
  64. return TRUE;
  65. }
  66. return FALSE;
  67. }
  68. DWORD SystemCmds::FindProcess(const TCHAR *strProcessName)
  69. {
  70. DWORD aProcesses[4096], cbNeeded;
  71. HANDLE hProcess;
  72. TCHAR szProcessName[MAX_PATH];
  73. if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
  74. {
  75. return 0;
  76. }
  77. for(int i=0; i< (int) (cbNeeded / sizeof(DWORD)); i++)
  78. {
  79. hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
  80. if(hProcess)
  81. {
  82. memset(szProcessName,0,sizeof(szProcessName));
  83. GetModuleFileNameEx(hProcess,NULL,szProcessName,MAX_PATH);
  84. CloseHandle(hProcess);
  85. string prc = szProcessName;
  86. if(prc.size() == 0)
  87. {
  88. continue;
  89. }
  90. std::size_t found = prc.find_last_of("\\");
  91. string filename = prc.substr(found+1);
  92. //_tprintf(_T("filename:%s\n"),filename.c_str());
  93. if (_stricmp(filename.c_str(), strProcessName) == 0)
  94. {
  95. return(aProcesses[i]);
  96. }
  97. }
  98. }
  99. return 0;
  100. }
  101. DWORD SystemCmds::KillProcess(const TCHAR *strProcessName)
  102. {
  103. HANDLE TargetProcess = OpenProcess(PROCESS_TERMINATE, FALSE, SystemCmds::FindProcess(strProcessName));
  104. if(TargetProcess == NULL)
  105. {
  106. return 0;
  107. }
  108. TerminateProcess(TargetProcess, 0);
  109. CloseHandle(TargetProcess);
  110. return 1;
  111. }
  112. BOOL SystemCmds::EnableComputerCtrl()
  113. {
  114. HANDLE tokenHandle;
  115. LUID privilegeLUID;
  116. PTOKEN_PRIVILEGES tokenPrivileges = new TOKEN_PRIVILEGES();
  117. PTOKEN_PRIVILEGES olePrivileges = new TOKEN_PRIVILEGES();
  118. try
  119. {
  120. if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &tokenHandle))
  121. {
  122. return FALSE;
  123. }
  124. if (!LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &privilegeLUID))
  125. {
  126. return FALSE;
  127. }
  128. tokenPrivileges->PrivilegeCount = 1;
  129. tokenPrivileges->Privileges->Attributes = SE_PRIVILEGE_ENABLED;
  130. tokenPrivileges->Privileges->Luid = privilegeLUID;
  131. DWORD dsize;
  132. if(!AdjustTokenPrivileges(tokenHandle, 0, tokenPrivileges, 4 + (12 * tokenPrivileges->PrivilegeCount), olePrivileges, &dsize))
  133. {
  134. return FALSE;
  135. }
  136. }
  137. catch(...)
  138. {
  139. return FALSE;
  140. }
  141. return TRUE;
  142. }
  143. BOOL SystemCmds::ShutDownComputer(BOOL Shutdown)
  144. {
  145. BOOL ret = SystemCmds::EnableComputerCtrl();
  146. if (Shutdown == FALSE)
  147. {
  148. ret &= ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0);
  149. }
  150. else
  151. {
  152. ret &= ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0);
  153. }
  154. return ret;
  155. }