Set Environment variable for another process

delphi package - getting into other processes

Set Environment variable for another process

Postby Mazinger » Sat Nov 05, 2005 1:04 pm

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

Postby madshi » Sat Nov 05, 2005 8:26 pm

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

OK, but I have another problem ...

Postby Mazinger » Mon Nov 07, 2005 9:31 am

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

Postby Arksole Hoax » Sat Jun 24, 2006 10:50 am

what would a change of an environment variable actually bring?
Arksole Hoax
 
Posts: 211
Joined: Sat May 08, 2004 11:41 am

Re: Set Environment variable for another process

Postby bkdroid13 » Fri Jul 05, 2019 8:23 am

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

Postby bkdroid13 » Fri Jul 05, 2019 8:26 am

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

Re: Set Environment variable for another process

Postby iconic » Fri Jul 05, 2019 1:28 pm

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
iconic
Site Admin
 
Posts: 894
Joined: Wed Jun 08, 2005 5:08 am


Return to madRemote

Who is online

Users browsing this forum: No registered users and 1 guest