Sometimes you need to execute code in the context of another process. The usual way to do this is to put your code into a little DLL and somehow load your DLL into the target process. The function "RemoteExecute" however gives you a more direct way to solve the problem. You can directly execute a function in the context of another process.

In order to make this work, RemoteExecute internally copies your function to the target process by using CopyFunction. Then it copies the parameters to the target process, as well, by using AllocMemEx and "WriteProcessMemory". Finally it executes the function in the context of the target process by using CreateRemoteThreadEx.

The function which you want to have executed in the other process needs to follow some rules. Please read the documentation of CopyFunction to learn more about those rules.

If you enter the "size" of the parameter block, "RemoteExecute" copies the parameter block to the other process before executing the function there. And after the function was fully executed, the parameter block is read back, so you can get detailed results from the function execution.

If you don't specify the "size" of the parameter block, the "params" 32bit pointer value is directly passed to the function, when it is executed in the other process.

// this is how your remote function must look like
type TRemoteExecuteFunction = function (params: pointer) : dword; stdcall;

function RemoteExecute (processHandle  : dword;
                        func           : TRemoteExecuteFunction;
                        var funcResult : dword;
                        params         : pointer = nil;
                        size           : dword   = 0           ) : bool; stdcall;

Please also have a look at the "RemoteCmdLine" Example.