This is a tool which can be used for injecting managed .NET DLL libraries into native process (or, with some limitations, into non-native process). Supports both x86 and x64 architectures. It was tested on v2.0.50727 and v4.0.30319 .NET runtimes. Please note that interface used for runtime loading is marked as obsolete and therefore it may stop working some day.
This project is based on original NDLLInjector by fday.
You can either use dependency manager like Maven, Grendle or Ivy or download precompiled JARs and include them in your project's classpath.
The JAR is being releases as OSGi bundle and therefore it can be used in such OSGi frameworks like Equinox, Apache Felix or FUSE ESB.
Maven dependency to be added into the POM:
<dependency>
<groupId>com.github.sarxos</groupId>
<artifactId>dotnet-dll-injector</artifactId>
<version>0.2.1</version>
</dependency>
If you are not using Maven (nor any other dependency manager), then you have to download precompiled binaries available here along with all required dependencies (~1MB zip file).
Use this code to inject DLL into any process (this is Java code, not C#):
public class Main {
public static void main(String[] args) {
String procName = "someprocess.exe"; // process name
File dll = new File("path/to/some.dll"); // injectee DLL path
// signature of method to be run after DLL is injected
Signature signature = new Signature("TestNamespace", "Program", "Main");
int pid = InjectorUtils.getProcessID(procName); // get process ID
Injector.getInstance().inject(pid, dll, signature); // inject DLL into process
}
}
The DLL file which you want to inject has to define method with the following signature (this is C# code, not Java):
public static int MethodNameHere(string arg) {
// code
}
For example (this is C# code, not Java):
namespace TestNamespace {
class Program {
public static int Main(string arg) {
MessageBox.Show("Hello World from Injectee!");
return 0;
}
}
}
It's important to note that if you want to read / write to the process memory classes / objects, the DLL file should be build with the same framework version as the process into which you want to inject it.
If you do not want to mess with process runtime, then you can use any framework you need.
If everything is configured, then it's enough to run:
mvn clean install
Follow the next points to understand how to configure build if your environment has not been configured.
Sonar maven-dotnet-plugin is not able to take parameters for MSBuild, so you have to change all *.csproj
files from the project top reflect correct path to .NET framework home directory. I've already created ticket
for this problem in Sonar JIRA - SONARPLUGINS-2133.
Because of that you have to edit each *.csproj
file and align this path to point existing file:
<Import Project="C:\WINNT\Microsoft.NET\Framework\v2.0.50727\Microsoft.CSharp.targets" />