Reputation: 10290
I am new here. As well, I need some help with converting some C# code to Java using JNA to call native Windows functions.
First and foremost, this is the code I am trying to convert: http://pastebin.com/0b5zMvBg
So far, I have written the JNA interfaces to represent the Kernel32 and NtDll library. Within the Kernel32 library, I have also defined the structures that I need to use.
Kernel32.java http://pastebin.com/93xTvD0T
NtDll.java http://pastebin.com/giup88Tk
After I have defined the library and structures I moved on to writing the non-native functions. The problem is, I don't understand all of the C# syntax. I do, however, understand all of the Java features that are in C#.
Here is what I have written so far; note that I have commented what I don't understand as well as if I'm questionable about my conversion. Most of the problems repeat, meaning that if you supply me with even one conversion I will probably be able to figure out how to do the rest. Thanks in prior for any help! :-)
Upvotes: 0
Views: 677
Reputation: 10069
In general, you're not even doing anything specific to C#, but basic C pointer arithmetic.
The easiest way to walk around a block of data and perform conversions between data types is to use com.sun.jna.Memory
rather than byte[]
. You can use Pointer.share(long offset)
to perform pointer arithmetic (the result is the original pointer offset by the requested amount). You can read and write the native memory using various Pointer.write()
or Pointer.readXXX()
variants.
You can initialize Java-side Structure
objects using a Pointer
as the base address, just remember to explicitly call Structure.read()
to sync native memory to Java.
A few examples, from your pasty:
Memory data = ...;
// mzH = *(MZHeader*) dPtr;
MZHeader mzH = new MZHeader(data);
mzH.read(); // or put this in the MZHeader(Pointer) ctor
// peH = *(PE_Header*)&dPtr[mzH.offsetToPE];
PE_Header peH = new PE_Header(data.share(mzH.offsetToPE));
peH.read(); // or put this in the PE_Header(Pointer) ctor
// peXH = *(PE_ExtHeader*)&dPtr[mzH.offsetToPE + sizeof(PE_Header)];
PE_ExtHeader peXH = new PE_ExtHeader(data.share(mzH.offsetToPE + peH.size()));
peXH.read(); // or put this in the PE_ExtHeader(Pointer) actor
I think you can get the idea from there...
EDIT
Pointer p = new Memory();
byte[] data = ...;
// Copy into Memory from data
p.write(0, data, 0, data.length);
// Copy out of Memory into data
p.read(0, data, 0, data.length);
Upvotes: 2
Reputation: 2020
So your Kine.Run() looks like the main entry point of this code. And what it does is take the given byte array, fork the current process, then overwrite the current process memory with the byte array and start executing it. If you implemented this, you would run the byte array as a child process.
Now, assuming that the byte array comes from some file in the file system (or can be written there), you can replace ALL this code with the following line:
Runtime.getRuntime().exec(filename);
Assuming that the filename is the file that contains the byte array. No knowledge of the PE header or windows internals is necessary. If the byte array comes from some where you cannot store locally, then you won't be able to run it anyway.
Upvotes: 1
Reputation: 2020
Not to be pedantic, but don't do that. You are trying to run a C# program in a JVM? It would be simpler and faster to write a layer that abstracts out the OS specific parts of the application. There are several things you can do in C# that will not work in a JVM. Such as load DLLs and then call methods in them. Or get the messages from the windows message loop.
Now, I will agree that there are some things you may want to do that can only be done from a native library. Like interact with other windows. But you are re-implementing the CLR in Java. That's a HUGE amount of work, and some parts will NEVER WORK.
Just don't do it. (Insert nike swoosh here)
Upvotes: -1