Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2005 Apr : getting environment variable to use with RunAsUser

www.cryer.info
Managed Newsgroup Archive

getting environment variable to use with RunAsUser

Subject:getting environment variable to use with RunAsUser
Posted by:"Colin Briede" (crbrie..@iinet.net.au)
Date:Tue, 5 Apr 2005 08:14:37

I want to get a user token to use when creating an EnvironmentBlock. The
lpEnvironment variable is then used with RunAsUser. RunAsUser will be used
to install programs. It uses adminName, adminPassword and domain, but needs
the users environment variable to ensure the installation is correct for the
user.
I have put a test to see if I get an environment variable,   if
lpEnvironment<>nil then showmessage('got environment'). I always get nil.
Is this the correct way to get a token and an environment variable?

The following is my code.

regards

Colin


var
domainName,adminPWord,adminName:string;
lpEnvironment:pointer;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  hToken: THandle;
begin
  //get a user token
  OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY,hToken);
  //use the token to get a pointer to lpEnvironment
  CreateEnvironmentBlock(
  lpEnvironment,
  hToken,
  true);
  //test if the pointer is nil???
  if lpEnvironment<>nil then showmessage('got environment')
  else showmessage('Did not get environment');
  //use run user to launch program using administartor password etc and the
lpEnvironment variable for
  //current user
  runasuser('notepad.exe',domainName,adminName,adminPWord);
  //destroy environment block
  DestroyEnvironmentBlock(lpEnvironment);
  //close the handle
  closehandle(hToken);
end;

Replies:

www.cryer.info
Managed Newsgroup Archive