Set Environment variable for another process
Set Environment variable for another process
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.
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.
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:
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 ...
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.
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.
-
- Posts: 211
- Joined: Sat May 08, 2004 11:41 am
Re: Set Environment variable for another process
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.
I am on Windows 10, I am searching if there is any windows command to do so.
Re: Set Environment variable for another process
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.
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.
Re: Set Environment variable for another process
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