I would like to do DllInject using Java Jna.I know I'm new to Jna and C++, but I can't seem to get the code to mesh with the inject part.I would like to know if you can help me.
Things were going well up to the point of obtaining ProcessId.
public static WinDef.DWORD getProcId(String process) {
WinDef.DWORD procId = new WinDef.DWORD(0);
Tlhelp32.PROCESSENTRY32.ByReference procEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
WinNT.HANDLE hSnap = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
if (hSnap != WinNT.INVALID_HANDLE_VALUE) {
if (Kernel32.INSTANCE.Process32First(hSnap, procEntry)) {
do {
if (Native.toString(procEntry.szExeFile).equalsIgnoreCase(process)) {
procId = procEntry.th32ProcessID;
break;
}
} while (Kernel32.INSTANCE.Process32Next(hSnap, procEntry));
}
Kernel32.INSTANCE.CloseHandle(hSnap);
}
return procId;
}
However, the code at the point of Inject does not mesh well.
public static int Injection(DWORD procId, String dllPath) {
HANDLE hProc = Kernel32.INSTANCE.OpenProcess(Kernel32.PROCESS_ALL_ACCESS, false, procId.intValue());
if (hProc != null && !hProc.equals(Kernel32.INVALID_HANDLE_VALUE)) {
Memory pathMem = new Memory((dllPath.length() + 1) * 2);
pathMem.setWideString(0, dllPath);
Pointer loc = Kernel32.INSTANCE.VirtualAllocEx(hProc, null, new SIZE_T(pathMem.size()), Kernel32.MEM_COMMIT | Kernel32.MEM_RESERVE, Kernel32.PAGE_READWRITE);
Kernel32.INSTANCE.WriteProcessMemory(hProc, loc, pathMem, new SIZE_T(pathMem.size()), null);
Pointer loadLibraryAddr = Kernel32.INSTANCE.GetProcAddress(Kernel32.INSTANCE.GetModuleHandle(null), "LoadLibraryW");
HANDLE hThread = Kernel32.INSTANCE.CreateRemoteThread(hProc, null, 0, loadLibraryAddr, loc, 0, null);
if (hThread != null) {
Kernel32.INSTANCE.CloseHandle(hThread);
}
}
if (hProc != null) {
Kernel32.INSTANCE.CloseHandle(hProc);
}
return 0;
}
Error
Exception in thread "main" java.lang.
Method WriteProcessMemory(WinNT.HANDLE, Pointer, Pointer, int, IntByReference) of type Kernel32 is not applicable on argument (WinNT.HANDLE, Pointer, Memory, BaseTSD.SIZE_T, null) null) cannot be applied to the argument
Method GetProcAddress(WinDef.HMODULE, int) with type Kernel32 cannot be applied to argument (WinDef.HMODULE, String)
Injection(Injector.java:72)
Injector.main(Injector.java:33)
could anyone lend me some wisdom?