Hi, takiego czegoś często używam do testowania dziwacznych programów - klikam prawym na jego ikonce i z menu wybieram usera z docelowymi uprawnieniami:
Kopiuj
#define UNICODE
#include "stdafx.h"
#include <windows.h>
#include <shlwapi.h>
#include <Userenv.h>
#include <shellapi.h>
#pragma comment(lib, "shlwapi.lib")
#define LOGIN L"admin"
#define DOMAIN L"."
#define PASSWORD L"kutas"
void RegisterSelf();
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,int nCmdShow)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
WCHAR *lpCmd = PathGetArgs(GetCommandLine());
if (lpCmd[0] == 32) lpCmd++;
if (!lpCmd[0])
{
RegisterSelf();
}
else
{
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
int argc;
LPWSTR *argv = CommandLineToArgvW(GetCommandLine(), &argc);
WCHAR wszCurrentDirectory[MAX_PATH];
wcsncpy(wszCurrentDirectory, argv[1], MAX_PATH);
PathRemoveFileSpec(wszCurrentDirectory);
if (CreateProcessWithLogonW(LOGIN, DOMAIN, PASSWORD, LOGON_WITH_PROFILE, NULL, lpCmd, 0,0, wszCurrentDirectory, &si, &pi))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL, GetLastError(), 0, wszCurrentDirectory, MAX_PATH, NULL);
MessageBox(0, wszCurrentDirectory, L"CreateProcessWithLogon", MB_TOPMOST | MB_ICONSTOP);
}
LocalFree(argv);
}
return 0;
}
void RegisterSelf()
{
WCHAR wszRegExe[MAX_PATH];
DWORD type, cb=64;
if (!SHGetValue(HKEY_CLASSES_ROOT, L".exe", NULL, &type, wszRegExe, &cb))
{
wcscat(wszRegExe, L"\\shell\\Run as ");
wcscat(wszRegExe, LOGIN);
wcscat(wszRegExe, L"\\command");
WCHAR wszMainPath[MAX_PATH + 10];
GetModuleFileName(0, wszMainPath, MAX_PATH);
PathQuoteSpaces(wszMainPath);
wcscat(wszMainPath, L" \"%1\" %*");
if (!SHSetValue(HKEY_CLASSES_ROOT, wszRegExe, NULL, REG_SZ, wszMainPath, (DWORD)wcslen(wszMainPath)<<1))
MessageBox(0, L"Instalation completed", L"", MB_ICONINFORMATION);
}
}