S
Ich benutze dieses Programm in XP:
// RunAsAdmin.cpp
// running processes in other user account
// usage: RunAsAdmin.exe "full path\your program.exe" {parameters}
#include <shlwapi.h>
#include <Userenv.h>
#pragma comment(lib,"Shell32.lib")
// target user account informations
#define LOGIN L"Administrator"
//#define PASSWORD L"" // PW ist optional
#define DOMAIN L"."
void RegisterSelf()
{
WCHAR wszRegExe[MAX_PATH];
DWORD type, cb=64;
// query registry path for .exe
if (!SHGetValueW(HKEY_CLASSES_ROOT, L".exe", NULL, &type, wszRegExe, &cb))
{
// append path for verbs
// SHSetValue: HKCR\exefile\shell\run as administrator\command
// oder besser HKCU\software\classes\exefile\shell\run as administrator\command
lstrcatW(wszRegExe, L"\\shell\\Run as ");
lstrcatW(wszRegExe, LOGIN);
lstrcatW(wszRegExe, L"\\command");
// query path to this programm
WCHAR wszMainPath[MAX_PATH + 10];
GetModuleFileNameW(0, wszMainPath, MAX_PATH);
// add quotes the path if contain spaces
PathQuoteSpacesW(wszMainPath);
// append "%1" %*
lstrcatW(wszMainPath, L" \"%1\" %%*");
// add new verb - Run as System
if (!SHSetValueW(HKEY_CLASSES_ROOT, wszRegExe, NULL, REG_SZ, wszMainPath, (lstrlenW(wszMainPath)+1)<<1))
MessageBoxW(0, L"Instalation completed", L"", MB_ICONINFORMATION);
}
}
//int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
int main()
{
STARTUPINFOW si;
PROCESS_INFORMATION pi;
WCHAR *lpCmd = PathGetArgsW(GetCommandLineW());
if (*lpCmd == 32) lpCmd = &lpCmd[1];
if (!*lpCmd) // no parameters given
{
RegisterSelf();
}
else
{
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
int argc;
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
// extract executable directory from path
WCHAR wszCurrentDirectory[MAX_PATH];
lstrcpynW(wszCurrentDirectory, argv[1], MAX_PATH);
PathRemoveFileSpecW(wszCurrentDirectory);
#ifdef PASSWORD
BOOL created = CreateProcessWithLogonW(LOGIN, DOMAIN, PASSWORD, LOGON_WITH_PROFILE,
NULL, lpCmd, 0,0, wszCurrentDirectory, &si, &pi);
#else
// enable blank passwords (XP)
BOOL limit = 0;
SHSetValueW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Lsa",
L"limitblankpassworduse", REG_DWORD, &limit, 4);
// do not crash until limitblankpassworduse is set back to 1
BOOL created = CreateProcessWithLogonW(LOGIN, DOMAIN, NULL, LOGON_WITH_PROFILE,
NULL, lpCmd, 0,0, wszCurrentDirectory, &si, &pi);
// disable blank passwords (default)
limit = 1;
SHSetValueW(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Control\\Lsa",
L"limitblankpassworduse", REG_DWORD, &limit, 4);
#endif
if (created)
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL, GetLastError(), 0, wszCurrentDirectory, MAX_PATH, NULL);
MessageBoxW(0, wszCurrentDirectory, L"CreateProcessWithLogon error", MB_TOPMOST | MB_ICONSTOP);
MessageBoxW(0, lpCmd, L"CreateProcessWithLogon error", MB_TOPMOST | MB_ICONSTOP);
}
LocalFree(argv);
}
}