Set Environment variable for another process

delphi package - getting into other processes
Post Reply
Mazinger
Posts: 33
Joined: Wed Jan 26, 2005 6:26 am

Set Environment variable for another process

Post 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.
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post 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;
Mazinger
Posts: 33
Joined: Wed Jan 26, 2005 6:26 am

OK, but I have another problem ...

Post 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.
Arksole Hoax
Posts: 211
Joined: Sat May 08, 2004 11:41 am

Post by Arksole Hoax »

what would a change of an environment variable actually bring?
bkdroid13
Posts: 4
Joined: Fri Jun 07, 2019 8:43 am

Re: Set Environment variable for another process

Post 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.
bkdroid13
Posts: 4
Joined: Fri Jun 07, 2019 8:43 am

Re: Set Environment variable for another process

Post 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-se ... ws-10.html

Is there any more simpler way.
iconic
Site Admin
Posts: 1064
Joined: Wed Jun 08, 2005 5:08 am

Re: Set Environment variable for another process

Post 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
Post Reply