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: 9432
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


Return to madRemote

Who is online

Users browsing this forum: No registered users and 1 guest

cron