Skip to content

Getting Handles to Remote Object

Shai S edited this page Jan 10, 2022 · 2 revisions

There are 2 kind of object you can get in the remote app:

  1. Existing objects - Which you'll have to search
  2. New object - Which you can create by calling remote constructors or functions that return them

✳️ Getting Existing Remote Objects

RemoteNET allows you to find existing objects in the remote app.
To do so you'll need to search the remote heap for the right object.

Use RemoteApp.QueryInstances() to find possible candidates for the desired object (by it's type)
and then use RemoteApp.GetRemoteObject() to get handles of the returned candidates.

IEnumerable<CandidateObject> candidates = remoteApp.QueryInstances("MyApp.PasswordContainer");
RemoteObject passwordContainer = remoteApp.GetRemoteObject(candidates.Single());

✳️ Creating New Objects in the Remote App

Sometimes the existing objects in the remote app are not enough to do what you want.
For this purpose you can also create new objects remotely.
Use the Activator-lookalike property of RemoteApp for that cause:

// Creating a remote StringBuilder with default constructor
RemoteObject remoteSb1 = remoteApp.Activator.CreateInstance(typeof(StringBuilder));

// Creating a remote StringBuilder with the "StringBuilder(string, int)" ctor
RemoteObject remoteSb2 = remoteApp.Activator.CreateInstance(typeof(StringBuilder), "Hello", 100);

Note how we used constructor arguments (string, int) in the second CreateInstance call.
Those could also be other RemoteObjects:

// Constructing a new StringBuilder
RemoteObject remoteStringBuilder = remoteApp.Activator.CreateInstance(typeof(StringBuilder));
// Constructing a new StringWriter using the "StringWriter(StringBuilder sb)" ctor
RemoteObject remoteStringWriter = remoteApp.Activator.CreateInstance(typeof(StringWriter), remoteStringBuilder);
Clone this wiki locally