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;
}
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...