🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #5120

runas replacement

#5120
From: Bart
Date: Mon, 10 Oct 2005 20:37
67 lines
3116 bytes
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



    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);
Technical Details
Message-ID: <434ad08c$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,d180810b743175b0,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Bart" <Nomail@Spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: runas replacement
Date: Mon, 10 Oct 2005 22:37:49 +0200
Lines: 70
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 194.171.5.81
Message-ID: <434ad08c$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1128976524 194.171.5.81 (10 Oct 2005 13:35:24 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail