Page 1 of 1

Set Environment variable for another process

PostPosted: Sat Nov 05, 2005 1:04 pm
by Mazinger
Hi all,

I'm trying to use madRemote to change a environment variable of another process, but ...

The code seems very simple:

...
// The function to be executed on remote process context:

Function SetEnv(params: Pointer):dword; stdcall;
Begin
If SetEnvironmentVariable('COLORS','OFF') then
Result:=0
Else
Result:=1;
End;

...
//The calling procedure:

GetWindowThreadProcessID(NHWindow,@ePID);
PH:=OpenProcess(PROCESS_ALL_ACCESS,false,ePID);
Try
Try
If not RemoteExecute(PH,@SetEnv,aresult,@Params,21) then
Begin
Dialogs.MessageDlg('RemoteExecute Error',mtError,[mbOk],0);
End
Else
Dialogs.MessageDlg(Format('Result=%d',[aresult]),mtInformation,[mbOk],0);
Finally
End;
Finally
CloseHandle(PH);
End;


Allways return Result=1

Thanks in advance.

PostPosted: Sat Nov 05, 2005 8:26 pm
by madshi
The problem is that the 2 strings you're using are in reality pointers to a series of chars. But pointers are only valid in their own process. So in the other process you're not setting "COLORS", but you're calling SetEnvironmentVariable with a random pointer which has no specific meaning in the target process.

How to solve the problem? Something like the following code should work. Please note that single chars are not handled by Delphi as pointers, but as static ordinal values. So using the following code should be fine:

Code: Select all
Function SetEnv(params: Pointer):dword; stdcall;
var str1, str2 : array [0..10] of char;
Begin
  str1[0] := 'C';
  str1[1] := 'O';
  str1[2] := 'L';
  str1[3] := 'O';
  str1[4] := 'R';
  str1[5] := 'S';
  str1[6] := chr(0);
  str2[0] := 'O';
  str2[1] := 'F';
  str2[2] := 'F';
  str2[3] := chr(0);
  If SetEnvironmentVariable(str1, str2) then
    Result:=0
  Else
    Result:=1;
End;

OK, but I have another problem ...

PostPosted: Mon Nov 07, 2005 9:31 am
by Mazinger
Works fine! Thanks.

But after the remote function is executed successfully if I try to terminate the process by issuing a

Process(ProcessID).Terminate(900);

Delphi generates a EExternalException on this line.

I the remote funcion is not executed, the terminate procedure works fine.

Any idea?

Thanks.

PostPosted: Sat Jun 24, 2006 10:50 am
by Arksole Hoax
what would a change of an environment variable actually bring?

Re: Set Environment variable for another process

PostPosted: Fri Jul 05, 2019 8:23 am
by bkdroid13
I was trying to do exactly the same.
I am on Windows 10, I am searching if there is any windows command to do so.

Re: Set Environment variable for another process

PostPosted: Fri Jul 05, 2019 8:26 am
by bkdroid13
I set the environment variable via AutoIt.
I hope I can exactly follow these steps through the commands.
I am following this guide: https://www.hows.tech/2019/03/how-to-set-environment-variables-in-windows-10.html

Is there any more simpler way.

Re: Set Environment variable for another process

PostPosted: Fri Jul 05, 2019 1:28 pm
by iconic
This is a very old thread (nearly 14 years old) however the method of calling SetEnvironmentVariable() in the target process can be done remotely, as the original code in this thread is doing. If you're not using madRemote (due to AutoIt scripting) then you'll need to call the Win32 APIs directly to achieve this. OpenProcess/VirtualAllocEx/WriteProcessMemory/CreateRemoteThread(@SetEnvironmentVariable)

--Iconic