RemoteHookingInject Method (Int32, InjectionOptions, String, String, Object) |
Namespace: EasyHook
public static void Inject( int InTargetPID, InjectionOptions InOptions, string InLibraryPath_x86, string InLibraryPath_x64, params Object[] InPassThruArgs )
Exception | Condition |
---|---|
InvalidOperationException | It is unstable to inject libraries into the same process. This exception is disabled in DEBUG mode. |
AccessViolationException | Access to target process denied or the current user is not an administrator. |
ArgumentException | The given process does not exist or unable to serialize/deserialize one or more pass thru arguments. |
FileNotFoundException | The given user library could not be found. |
OutOfMemoryException | Unable to allocate unmanaged memory in current or target process. |
NotSupportedException | It is not supported to inject into the target process. This is common on Windows Vista and Server 2008. |
TimeoutException | Unable to wait for user library to be initialized. Check your library's IEntryPoint constructor. |
EntryPointNotFoundException | The given user library does not export a class implementing the IEntryPoint interface. |
There are two possible user library paths. The first one should map to a 32-bit library, and the second one should map to 64-bit library. If your code has been compiled for "AnyCPU", like it's the default for C#, you may even specify one library path for both parameters. Please note that your library including all of it's dependencies must be registered in the Global Assembly Cache (GAC). Refer to Register(String, String) for more information about how to get them there.
If you inject a library into any target process please keep in mind that your working directory will be switched. EasyHook will automatically add the directory of the injecting application as first directory of the target's PATH environment variable. So make sure that all required dependencies are either located within the injecting application's directory, a system directory or any directory already contained in the PATH variable.
EasyHook provides extensive error information during injection. Any kind of failure is being caught and thrown as an exception by this method. If for example your library does not expose a class implementing IEntryPoint, an exception will be raised in the target process during injection. The exception will be redirected to this method and you can catch it in a try-catch statement around Inject(Int32, InjectionOptions, String, String, Object).
You will often have to pass parameters to your injected library. IpcChannel names are common, but also any other kind of data can be passed. You may add a custom list of objects marked with the SerializableAttribute. All common NET classes will be serializable by default, but if you are using your own classes you might have to provide serialization by yourself. The custom parameter list will be passed unchanged to your injected library entry points Run and construcotr. Verify that all required type libraries to deserialize your parameter list are either registered in the GAC or otherwise accessible to your library by being in the same path.
It is supported to inject code into 64-bit processes from within 32-bit processes and vice versa. It is also supported to inject code into other terminal sessions. Of course this will require additional processes and services to be created, but as they are managed internally, you won't notice them! There will be some delay when injecting the first library.
Even if it would technically be possible to inject a library for debugging purposes into the current process, it will throw an exception. This is because it heavily depends on your injected library whether the current process will be damaged. Any kind of communication may lead into deadlocks if you hook the wrong APIs. Just use the capability of Visual Studio to debug more than one process simultanously which will allow you to debug your library as if it would be injected into the current process without running into any side-effects.
The given exceptions are those which are thrown by EasyHook code. The NET framework might throw any other exception not listed here. Don't rely on the exception type. If you passed valid parameters, the only exceptions you should explicitly check for are NotSupportedException and AccessViolationException. All others shall be caught and treated as bad environment or invalid parameter error.