Programm per Klick als Administrator ausführen
Um ein Windows-Programm z.B. per Klick auf einen Button als Administrator auszuführen, kann folgender Code verwendet werden:
1// Elevate the process if it is not run as administrator.
2if (!fIsRunAsAdmin)
3{
4 wchar_t szPath[MAX_PATH];
5 if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)))
6 {
7 // Launch itself as administrator.
8 SHELLEXECUTEINFO sei = { sizeof(sei) };
9 sei.lpVerb = L"runas";
10 sei.lpFile = szPath;
11 sei.hwnd = hWnd;
12 sei.nShow = SW_NORMAL;
13
14 if (!ShellExecuteEx(&sei))
15 {
16 DWORD dwError = GetLastError();
17 if (dwError == ERROR_CANCELLED)
18 {
19 // The user refused the elevation.
20 // Do nothing ...
21 }
22 }
23 else
24 {
25 EndDialog(hWnd, TRUE); // Quit itself
26 }
27 }
28}Die Benutzung des Verbs "runas" ist in der aktuellen MSDN weder unter ShellExecuteEx noch unter SHELLEXECUTEINFO dokumentiert.