Search code examples
javajnadll-injection

Can Java inject DLL in another process?


I've actually already asked questions regarding my problem. To prevent people asking for irrelevant details over again, I'll post this background info you can skip:

I'm making a automation application. This application does some tasks for the user over time for specific window. When these taks are required to be made, window flashes in taskbar and sometimes even steals focus to get the user to do the task. Once the automation is here, this is no longer wanted - the user will only focus the window when he wants to check how well is the automation doing it's job.

I discovered this focus and flash disable dll project thanks to this superuser post about applications stealing focus.

My automation application is in Java. So while I can open a DLL injection application and disable flashing manually, I'd like to integrate it in the java applicaton - for example as a setting option. When user selects to disable flashing and stealing focus, the dll will be injected.

Of course, this requires Java to be able to inject my .dll file. I've found this project: dotnet-dll-injector but it only deals with .NET dll files.

Q: Is there a way, in Java, to pick a .dll file and inject it into process? Which libraries would lead to this if the solution isn't straightforward?

I've noticed CreateRemoteThread is somehow related to the DLL injection. Maybe JNA library supports that?


Solution

  • I don't know of a way to do it natively in Java.

    Since you've found a working solution in .NET and your task is Windows-specific, I suggest the following:

    Build dotnet-dll-injector as a DLL and call it from your Java app. How to call into .NET dll from Java

    Edit: easier to build it as a console app and use Runtime.getRuntime().exec("...");

    Or if you're still early in the development process and you have no immediate plan to go cross-platform, just do everything in C# and save some headache.