BOOL Dlg_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam) { chSETDLGICONS(hwnd, IDI_PROCESSINFO); /* Is set to TRUE if the Administrator privileges * are available; either because running elevated * or simply because UAC has been disabled. */ BOOL bCanReadSystemProcesses = FALSE; /* Show if we are running with filtered token or not */ if (GetProcessElevation(&s_elevationType, &s_bIsAdmin)) { /* prefix title with elevation */ TCHAR szTitle[64]; switch(s_elevationType) { /* Default user or UAC is disabled */ case TokenElevationTypeDefault: if (IsUserAnAdmin()) { _tcscpy_s( szTitle, _countof(szTitle), TEXT("Default Administrator: ")); bCanReadSystemProcesses = true; } else { _tcscpy_s( szTitle, _countof(szTitle), TEXT("Default: ")); } break; /* Process has been successfully elevated */ case TokenElevationTypeFull: if (IsUserAnAdmin()) { _tcscpy_s( szTitle, _countof(szTitle), TEXT("Elevated Administrator: ")); bCanReadSystemProcesses = true; } else { _tcscpy_s( szTitle, _countof(szTitle), TEXT("Elevated: ")); } break; /* Process is running with limited privileges */ case TokenElevationTypeLimited: if (s_bIsAdmin) { _tcscpy_s( szTitle, _countof(szTitle), TEXT("Filtered Administrator: ")); } else { _tcscpy_s( szTitle, _countof(szTitle), TEXT("Filtered: ")); } break; } /* Update the dialog title based on the elevation level */ GetWindowText( hwnd, _tcschr(szTitle, TEXT('\0')), _countof(szTitle) - _tcslen(szTitle)); SetWindowText(hwnd, szTitle); /* Add the "shield" icon if needed to allow the user * to restart the application with elevated privileges */ if (!bCanReadSystemProcesses) { Button_SetElevationRequiredState( GetDlgItem(hwnd, IDC_BTN_SYSTEM_PROCESSES), !bCanReadSystemProcesses); } else { /* No need to show the button... */ ShowWindow(GetDlgItem(hwnd, IDC_BTN_SYSTEM_PROCESSES), SW_HIDE); /* ... and the combo-box can take the whole width of the dialog box */ MoveWindow(GetDlgItem(hwnd, IDC_BTN_SYSTEM_PROCESSES), 0, 0, 0, 0, FALSE); } } /* Hide the module-helper listbox. */ ShowWindow(GetDlgItem(hwnd, IDC_MODULEHELP), SW_HIDE); /* Have the results window use a fixed-pitch font */ SetWindowFont( GetDlgItem(hwnd, IDC_RESULTS), GetStockFont(ANSI_FIXED_FONT), FALSE); /* By default, show the running processes */ Dlg_PopulateProcessList(hwnd); return(TRUE); }
void Dlg_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { static BOOL s_fProcesses = TRUE; switch (id) { case IDCANCEL: EndDialog(hwnd, id); break; /* Restart the application when we are not running * as Elevated Administrator. */ case IDC_BTN_SYSTEM_PROCESSES: { /* Hide ourself before trying to start the same application * but with elevated privileges. */ ShowWindow(hwnd, SW_HIDE); TCHAR szApplication[MAX_PATH]; DWORD cchLength = _countof(szApplication); /* Retrieves the full name of the executable * image for the specified process. * hProcess [in] * A handle to the process. * This handle must be created with the PROCESS_QUERY_INFORMATION * or PROCESS_QUERY_LIMITED_INFORMATION access right. * For more information, see Process Security and Access Rights. * dwFlags [in] * This parameter can be one of the following values. * 0 The name should use the Win32 path format. * The name should use the native system path format. * lpExeName [out] * The path to the executable image. * If the function succeeds, this string is null-terminated. * lpdwSize [in, out] * On input, specifies the size of the lpExeName buffer, in characters. * On success, receives the number of characters written to the buffer, * not including the null-terminating character. */ QueryFullProcessImageName( GetCurrentProcess(), 0, szApplication, &cchLength); DWORD dwStatus = StartElevatedProcess(szApplication, NULL); if (dwStatus == S_OK) { /* not need to keep on working under lower privileges. */ ExitProcess(0); } /* In case of error, show up again. */ ShowWindow(hwnd, SW_SHOWNORMAL); } break; case ID_PROCESSES: s_fProcesses = TRUE; EnableMenuItem(GetMenu(hwnd), ID_VMMAP, MF_BYCOMMAND | MF_ENABLED); DrawMenuBar(hwnd); Dlg_PopulateProcessList(hwnd); break; case ID_MODULES: EnableMenuItem(GetMenu(hwnd), ID_VMMAP, MF_BYCOMMAND | MF_GRAYED); DrawMenuBar(hwnd); s_fProcesses = FALSE; Dlg_PopulateModuleList(hwnd); break; case IDC_PROCESSMODULELIST: if (codeNotify == CBN_SELCHANGE) { DWORD dw = ComboBox_GetCurSel(hwndCtl); if (s_fProcesses) { dw = (DWORD) ComboBox_GetItemData(hwndCtl, dw); // Process ID ShowProcessInfo(GetDlgItem(hwnd, IDC_RESULTS), dw); } else { // Index in helper listbox of full path dw = (DWORD) ComboBox_GetItemData(hwndCtl, dw); TCHAR szModulePath[1024]; ListBox_GetText(GetDlgItem(hwnd, IDC_MODULEHELP), dw, szModulePath); ShowModuleInfo(GetDlgItem(hwnd, IDC_RESULTS), szModulePath); } } break; case ID_VMMAP: { TCHAR szCmdLine[32]; HWND hwndCB = GetDlgItem(hwnd, IDC_PROCESSMODULELIST); DWORD dwProcessId = (DWORD) ComboBox_GetItemData(hwndCB, ComboBox_GetCurSel(hwndCB)); StringCchPrintf(szCmdLine, _countof(szCmdLine), TEXT("%d"), dwProcessId); DWORD dwStatus = StartElevatedProcess(TEXT("\"14-VMMap.exe\""), szCmdLine); if (dwStatus == ERROR_CANCELLED) { chMB("Failed to run 14-VMMap.exe: you refused access."); } } break; } }
void Dlg_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify) { static BOOL s_fProcesses = TRUE; switch (id) { case IDCANCEL: EndDialog(hwnd, id); break; // Restart the application when we are not running // as Elevated Administrator. case IDC_BTN_SYSTEM_PROCESSES: { // Hide ourself before trying to start the same application // but with elevated privileges. ShowWindow(hwnd, SW_HIDE); TCHAR szApplication[MAX_PATH]; DWORD cchLength = _countof(szApplication); QueryFullProcessImageName( GetCurrentProcess(), 0, szApplication, &cchLength); DWORD dwStatus = StartElevatedProcess(szApplication, NULL); if (dwStatus == S_OK) { // not need to keep on working under lower privileges. ExitProcess(0); } // In case of error, show up again. ShowWindow(hwnd, SW_SHOWNORMAL); } break; case ID_PROCESSES: s_fProcesses = TRUE; EnableMenuItem(GetMenu(hwnd), ID_VMMAP, MF_BYCOMMAND | MF_ENABLED); DrawMenuBar(hwnd); Dlg_PopulateProcessList(hwnd); break; case ID_MODULES: EnableMenuItem(GetMenu(hwnd), ID_VMMAP, MF_BYCOMMAND | MF_GRAYED); DrawMenuBar(hwnd); s_fProcesses = FALSE; Dlg_PopulateModuleList(hwnd); break; case IDC_PROCESSMODULELIST: if (codeNotify == CBN_SELCHANGE) { DWORD dw = ComboBox_GetCurSel(hwndCtl); if (s_fProcesses) { dw = (DWORD) ComboBox_GetItemData(hwndCtl, dw); // Process ID ShowProcessInfo(GetDlgItem(hwnd, IDC_RESULTS), dw); } else { // Index in helper listbox of full path dw = (DWORD) ComboBox_GetItemData(hwndCtl, dw); TCHAR szModulePath[1024]; ListBox_GetText(GetDlgItem(hwnd, IDC_MODULEHELP), dw, szModulePath); ShowModuleInfo(GetDlgItem(hwnd, IDC_RESULTS), szModulePath); } } break; case ID_VMMAP: { TCHAR szCmdLine[32]; HWND hwndCB = GetDlgItem(hwnd, IDC_PROCESSMODULELIST); DWORD dwProcessId = (DWORD) ComboBox_GetItemData(hwndCB, ComboBox_GetCurSel(hwndCB)); StringCchPrintf(szCmdLine, _countof(szCmdLine), TEXT("%d"), dwProcessId); DWORD dwStatus = StartElevatedProcess(TEXT("\"14-VMMap.exe\""), szCmdLine); if (dwStatus == ERROR_CANCELLED) { chMB("Failed to run 14-VMMap.exe: you refused access."); } } break; } }