Uruchomić program jako inny użytkownik

0

Witam,

Za pomocą ShellExecute można urochomić dowolny program/otworzyć dokument. Ja natomiast potrzebuję uruchomić aplikację zewnętrzną jako inny użytkownik. Czyli podaję w kodzie nazwę używkotnika, jego hasło i uruchamiam aplikację.

Da się to zrealizować? Szukalem na MSDN i w necie ale jakos nie moge trafic na nic konkretnego.

0

Zainteresuj się raczej CreateProcess, niż ShellExecute. W tej funkcji możesz wyspecyfikować również przywileje dla nowego procesu. Niestety nie próbowałem odpalać jako inny użytkownik, więc szczegółów nie podam.

0

Ok poprobuje. To jest jakis pomysl.

Pomyslalem ze moze daloby sie jakos wysledzic jaka funkcja WinApi jest wywolywana po odpaleniu Uruchom jako... ?

[edit]
Niestety nie widze jednak sposobu uzycia CreateProcess.

pSecurityDescriptor
A pointer to a security descriptor for the object that controls the sharing of it. If NULL is specified for this member, the object is assigned the default security descriptor of the calling process. This is not the same as granting access to everyone by assigning a NULL discretionary access control list (DACL). The default security descriptor is based on the default DACL of the access token belonging to the calling process. By default, the default DACL in the access token of a process allows access only to the user represented by the access token. If other users must access the object, you can either create a security descriptor with the appropriate access, or add ACEs to the DACL that grants access to a group of users.

Czyli chodzi tu raczej o przyznawanie dostepu użytkownikom... ale jak uruchomic jako inny użytkownik?

0

Używasz runas? Bo ja bym coś z tym kombinował.
Pozdrawiam

0

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:

Skompiluj i uruchom, aby zarejestrował się w powłoce dla .exe

#define UNICODE
#include "stdafx.h"
#include <windows.h>
#include <shlwapi.h>
#include <Userenv.h>
#include <shellapi.h>
#pragma comment(lib, "shlwapi.lib")

// target user account informations
#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);

		// extract executable directory from path
		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;

	// query registry path for .exe
	if (!SHGetValue(HKEY_CLASSES_ROOT, L".exe", NULL, &type, wszRegExe, &cb))
	{
		// append path for verbs
		wcscat(wszRegExe, L"\\shell\\Run as ");
		wcscat(wszRegExe, LOGIN);
		wcscat(wszRegExe, L"\\command");

		// query path to this programm
		WCHAR wszMainPath[MAX_PATH + 10];
		GetModuleFileName(0, wszMainPath, MAX_PATH);
		PathQuoteSpaces(wszMainPath);

		wcscat(wszMainPath, L" \"%1\" %*");

		// add new verb - Run as *username*
		if (!SHSetValue(HKEY_CLASSES_ROOT, wszRegExe, NULL, REG_SZ, wszMainPath, (DWORD)wcslen(wszMainPath)<<1))
			MessageBox(0, L"Instalation completed", L"", MB_ICONINFORMATION);
	}
}
0

Skompiluj i uruchom, aby zarejestrował się w powłoce dla .exe

Tego akurat nie potrzebuje... ale wiem juz jakie funkcje sluza do uruchomienia jako inny user - ta, ktora ty uzyles - CreateProcessWithLogonW albo CreateProcessAsUser (o ktorych do tej pory nie mialem pojecia ze istnieja).

To bedzie to, czego potrzebuje, dzieki za naprowadzenie

Teraz potrzebowalbym jeszcze sposobu na wylistowanie listy użytkowników systemu. Zamierzam napisac nakladke na okno "Uruchom" w Windows, ktora umozliwi wpisywanie polecen jako inny użytkownik (tak jest w KDE na przyklad), bo mi tego bardzo brakuje w Windows :/

1 użytkowników online, w tym zalogowanych: 0, gości: 1