Tech Support > Microsoft Windows > Development Resources > runas replacement
runas replacement
Posted by Bart Huls on October 14th, 2005


Hi,

I'm trying to implement a runas replacement within my software.
For this far I get something to work. The only thing is that a process
(running with the new user) is not shut down correctly the next time the
program starts it comes with an Error in CreateEnvironmentBlock Error: 203.
When I kill this process (as Admin) the program wil start and start the
ppfgui.exe.
When I'm trying to do this with runas it gives me no problems. So I must be
doing someting wrong I tried 2 implementations, both from the platform sdk.
Below you will find what i'm currently using.
ALso it looks like that the profile isn't loaded correctly (the new user is
a domain user), but i'm not sure about that

Regards,

Bart Huls



int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DWORD dwSize;
HANDLE hToken;
LPVOID lpvEnv;
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
WCHAR szUserProfile[256] = L"";
WCHAR szCmd [1024] = L"";
WCHAR szDrive[256] = L"";
WCHAR szDir [256] = L"";
WCHAR szFname[256] = L"";
WCHAR szExt [256] = L"";



si.cb = sizeof(STARTUPINFO);

GetModuleFileName(hInstance, szCmd,1024/sizeof(WCHAR));

_wsplitpath(szCmd,szDrive,szDir,szFname,szExt);
swprintf(szCmd,L"%s%sppfgui.exe",szDrive,szDir);

if (!LogonUser(szUserName, szDomain, szPassWord,
LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, &hToken))
DisplayError(L"LogonUser");

if (!CreateEnvironmentBlock(&lpvEnv, hToken, TRUE))
DisplayError(L"CreateEnvironmentBlock");

dwSize = sizeof(szUserProfile)/sizeof(WCHAR);

if (!GetUserProfileDirectory(hToken, szUserProfile, &dwSize))
DisplayError(L"GetUserProfileDirectory");


if (!CreateProcessWithLogonW(szUserName, szDomain, szPassWord,
LOGON_WITH_PROFILE, NULL, szCmd,
CREATE_UNICODE_ENVIRONMENT, lpvEnv, szUserProfile,
&si, &pi))
DisplayError(L"CreateProcessWithLogonW");

if (!DestroyEnvironmentBlock(lpvEnv))
DisplayError(L"DestroyEnvironmentBlock");

CloseHandle(hToken);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

return 0;
}


Posted by Boris on October 15th, 2005


"Bart Huls" <Nomail@Spam.com> wrote in message
news:434fbee3$1@news.wau.nl...
Depending which user logged in, CURRENT_USER contain info for that user. For
example, if user A isn't logged on but your application calls LogonUser() or
CreateProcessAsUser(), CURRENT_USER for user A will not be loaded for you
automatically. Your code will have to do it.
Here's an excerpt from MSDN:
HKEY_USERS registry key. Therefore, to access the information in the
HKEY_CURRENT_USER registry key, you must load the user's profile information
into HKEY_USERS with the LoadUserProfile function before calling
CreateProcessAsUser.

<<<<<<<<<PASTE

-Boris




Posted by Richard Ward on October 16th, 2005



Why are you logging the user on twice? Once in your code,
and once in the CreateProcessWithLogon() call? Just call that
function, and pass in the LOGON_WITH_PROFILE flag, and
then you're done.

"Bart Huls" <Nomail@Spam.com> wrote in message
news:434fbee3$1@news.wau.nl...


Posted by Bart Huls on October 18th, 2005


Hi Richard,

How do I get the ProfileDirectory for the user (or must I leave it blank).
This I'm doing by LogonUser.
Also What must I pass to lpvEnv.

Regards,

Bart


Posted by Richard Ward on October 28th, 2005



Either call CreateProcessWithLogonW(), or don't. You can't mix and match.
If you want to load the profile yourself, and create the environment block,
etc., etc.,
then you call CreateProcessAsUser. If you want to avoid that hassle, which
is
highly recommended, then just call CPWL(). Much, much simpler.

"Bart Huls" <Nomail@Spam.com> wrote in message
news:435522f7$1@news.wau.nl...



Similar Posts