Click here to Skip to main content
15,561,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to access my c code from java and get data from C struct using JNI. I want to achieve this using hashtable or hashmap. Here is my C struct:
struct process
{
char str[];
char strr[];
int i;
}; 
struct process *ptr;

Now, I want to pass the data from the struct array(*ptr) to the java code. Can I pass the data using Hashtable or Hashmap? Or can I pass the struct array directly to java? Is there any decent way for doing this?

What I have tried:

I stored all data in a string array and passed it. But, it seems so cheap. So, suggest some ideas to pass it.
Posted
Updated 6-May-21 22:27pm
v2

Be happy that it works and move on. Transfering complex objects between different runtimes isnt possible because of memory layout.
You may transfer some byte array in which the data is an reinterpret it. Like
C++
struct Data {
 int i;
 int lenstr;
 int lenstrr;
 char* data; // allocate with  lenstr + lenstr and copy the bits one and the other string
};
In Java you can fizzle the bytes out.
 
Share this answer
 
Quote:
Can I pass the data using Hashtable or Hashmap? Or can I pass the struct array directly to java? Is there any decent way for doing this?

Sadly, no. The Java Native Interface is designed to allow C programmers to write support libraries for Java programs. You cannot call a C library direct from Java unless it has been coded to the standard. So you would need to create an interface library that is callable from Java before you can access any of the C library functions.
 
Share this answer
 
Comments
[no name] 7-May-21 4:05am    
Is there any way that Java can communicate with C and fetch the data?
Richard MacCutchan 7-May-21 5:40am    
No, only by doing what I just explained.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900