1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| BOOL EnablePrivilege(LPCTSTR lpszPrivilegeName, BOOL bEnable) { int nResult = FALSE; int nRetCode = FALSE; HANDLE hToken = NULL; TOKEN_PRIVILEGES tkp = { 0 };
do { nRetCode = ::OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken); if (!nRetCode) break;
nRetCode = ::LookupPrivilegeValue(NULL, lpszPrivilegeName, &tkp.Privileges[0].Luid); if (!nRetCode) break;
tkp.PrivilegeCount = 1; tkp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0; nRetCode = ::AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL); if (!nRetCode) break;
nResult = TRUE; } while (FALSE);
if (hToken != NULL) { CloseHandle(hToken); }
return nResult; }
HANDLE GetExplorerToken() { EnablePrivilege(SE_DEBUG_NAME, TRUE);
HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapshot == INVALID_HANDLE_VALUE) { return NULL; }
HANDLE hExplorerToken = NULL; PROCESSENTRY32 pe = { 0 }; pe.dwSize = sizeof(pe);
BOOL bMore = ::Process32First(hSnapshot, &pe); while (bMore) { if (_tcsicmp("explorer.exe", pe.szExeFile) == 0) { HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pe.th32ProcessID); if (hProcess == NULL) { continue; } if (OpenProcessToken(hProcess, TOKEN_QUERY, &hExplorerToken)) { CloseHandle(hProcess); break; }
CloseHandle(hProcess); } bMore = ::Process32Next(hSnapshot, &pe); } CloseHandle(hSnapshot);
return hExplorerToken; }
|