-
Notifications
You must be signed in to change notification settings - Fork 3
Getting Handles to Remote Object
Shai S edited this page Aug 25, 2023
·
2 revisions
There are 2 kind of object you can get in the remote app:
- Existing objects - Which you'll have to search for.
- New objects - Which you can create by calling remote constructors or functions that return them.
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());
Note:
- In .NET targets, all objects are found in the heap. The stack only ever holds pointers to them (or primitives).
-
In MSVC targets this is not the case. An objects might be allocated on the stack.
TheQueryInstances
function also searches all threads' stacks but the chances of "catching" those short-lived objects are much smaller.
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 RemoteObject
s:
// 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);