Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I constructed a java program using netbeans IDE that has ability to connect with a respective DLL; I want to connect and interchange data between my java program(.jar) and VSC++ program(.exe) through DLLs.JNI uses single Dll to invoke C/C++ function in native manner,In order to increase the efficiency I tried to connect and interchange primitive data types between Java program and C++ program using that DLL(JNI implemented).

Unfortunately C++ program cannot obtain data values that has been changed by Java Program.For example - If I declare a global int variable in DLL,java program can catch that int variable and can update it but If I run my C++ program(exe) loading same DLL simultaneously it cannot receive the updated value of that int variable declared in the DLL.

Therefore I need a solution to share/Interchange at least primitive data and their respective values between a JAVA and C++ Program using JNI (in spite of date transferring through Sockets).JAVA TO C/C++ & C/C++ to JAVA using DLLs.

PLEASE HELP ME!! THANK YOU
Posted
Comments
Richard MacCutchan 21-Aug-13 2:58am    
The JNI interface allows you to share all types between a Java main program and a C or C++ dll, and vice versa. However what you describe sounds more like you need to use sockets or a message queue.
Buddhi Chaturanga 21-Aug-13 5:46am    
In spite of SOCKET PROGRAMMING .. Couldn't be achieve it by using only JNI and DLLs ? Build a primitive link between Java Program <=> C/C++ program (Those are running same instance)
Richard MacCutchan 21-Aug-13 5:57am    
In theory it may be possible, but it is really not the way to uses DLLs. Remember that even though the code in the DLL is shared, each application runs in its own address space, independent of the other. So, even if you have a global variable it still belongs only to one or other application. I am not sure that shared memory would work in this case, but you may like to research it.
Buddhi Chaturanga 21-Aug-13 10:04am    
@Richard => I have already researched some variations.. One single DLL duplicate it's own memory spaces for different processes. Even if we load Same single DLL within two different programs we can obtain two different memory allocation.My point is HOW WE CAN MAP THESE TWO MEMORY ADDRESSES FOR DECLARED GLOBAL VARIABLES IN DLL then we can direct the data flow.
Richard MacCutchan 21-Aug-13 11:03am    
I think you probably need to use shared memory to achieve this.

What is "loading same DLL simultaneously"? First: If you start a VC++ program that loads a DLL and you start a Java program that loads the same DLL then you have created two processes that have nothing to do with each other so your assumption that you require some kind of IPC to communicate between these processes is correct. Sockets is a very good communication, I'm using that most of the time. Depending on the current scenario you may or may not require jni and native code in your java program but we can not help you in that based on the information you provided. Also the cut in your architecture where you put the IPC (socket) communication could be a nice base for debate but again, we don't have the info for this.

EDIT: Thinking it over again: If you want to share just a small piece of data (like int or a few bytes/ints) then you can also do that by using shared memory + an interprocess mutext and maybe one or two interprocess events if you want to signal about data change to one or both directions. In this case however you will need a small jni dll in your java program. Another thing: A jni call can be quite expensive. For this reason I usually don't make a jni call in either direction (java->c or c->java) just to read/set variables if I don't have to. If setting/reading the variable isn't so performance critical and a socket communication is satisfactory then I would go with sockets. This solution has 2 advantages: You don't need jni in the java program (that has another indirect benefit: it remains platform independent), and you can run the programs on separate machines easily that sometimes has some benefit!

If you use shared memory then you have two choices:

1. You write just a single jni dll for the java program. You don't need a dll in the C++ exe, especially not the same dll. Of course you can still reuse the same C/C++ code in the jni dll and the c++ program. The shared memory must be created programmatically along with the mutext for synchronization and optionally one or two inter-process events.

2. Once I've created shared memory by marking a segment (.bss) in my (mouse hook) dll as shared. This way if you load this dll into separate processes all of these loaded dll instances see that segment as shared. Marking the segment as shared requires only some linker magic and knowing the name of the segment maybe explicitly putting some variables to explicitly defined segments (with some platform specific C++ compiler pragmas). I don't recommend this approach if you don't understand anything in what I've described previously.
 
Share this answer
 
v2
Comments
Buddhi Chaturanga 21-Aug-13 6:16am    
http://stackoverflow.com/questions/18349760/how-to-interchange-data-between-java-program-and-c-program
pasztorpisti 21-Aug-13 8:59am    
How is this related?
Buddhi Chaturanga 21-Aug-13 6:25am    
@pasztorpisti I got your point. I am not advanced Win32 Programmer.But I wanted to do here is pass data between running Java Program and C/C++ program.I think JNI has kind of efficient approach than USING SOCKET PROGRAMMING OR
IPC http://msdn.microsoft.com/en-us/library/aa365574(v=vs.85).aspx
I wanted to know with or without using separate DLL for C++ program how we can pass values from C++ program to Java side JNI DLL..?
pasztorpisti 21-Aug-13 8:30am    
Sorry, in my previous post I misunderstood (reading too quickly) and I thought you are advanced in Win32. If sockets isn't good for you (for example because it isn't fast enough) then you have to write a DLL that you load from java. In the C++ program you don't need any DLLs, especially not the same DLL... But you can use the same classes/header files/libs in the C++ program and your java/jni DLL if you want to!

First create shared memory with the same name in both the DLL and the C++ program: link
Warning!! shared memory isn't necessarily mapped to the same address in different processes!!


For synchronization you have to use named interprocess mutex: link
If you want to signal uni- or bidirectional signaling then you need 1 or 2 event objects: link

All of these objects can be created with a specified name. If you create two objects with the same name (optionally in different processes) then the returned handles will refer to the same object!

From this point this is exactly like multithreaded programming, the only difference is that the threads reside in different processes. Multithreading in very difficult topic and we cant teach you multithreading in a few paragraphs and answers. Period. Since we don't know what you wanna do we cant help you with codesnippets.
pasztorpisti 21-Aug-13 9:33am    
I would still consider using sockets. Depending on your problem (that you keep as a secret for some reason...) sockets can be a good solution with the advantages I previously mentioned. It can easily happen that your solution can be replaced with another solution that doesn't require frequent (and/or low latency) value update of the shared data and then socket usage is okay. I think you should describe the usage of this shared data from both the C++ program and java side. Why do you need it?
The JNI interface works fine if you handle it very carefully.

Everything should be transfered by value and then work on a local copy. Do dont work with pointer from C++ in Java and vice versa or some strange exceptions will hurt you :mad:

That looks interesting for you:

Sample JNI Application[^]

Debugging a JNI Application using Netbeans and Visual Studio[^]
 
Share this answer
 
Comments
Buddhi Chaturanga 21-Aug-13 5:42am    
Thank you for links.But I want to construct two way data communication between two separate Java and C/C++ programs running simultaneously using DLLs.Example If I declared int x variable in DLL so problem is we must able to change it's value by each program and the value should be visible to each program instantaneously.(Similar to static variable)
Consider using a notification model. The callee changes the value and the called is performing some actions. like notifying other modules.

That event driven model has a good performance and stability.
 
Share this answer
 
v2
Comments
Buddhi Chaturanga 21-Aug-13 5:50am    
@KarstenK Could you send me some relevant example link or resources web sites for further info?
pasztorpisti 21-Aug-13 6:14am    
Just a side note: By using sockets your solution becomes automatically event driven. If sockets is a viable solution for you then definitely go with that without using any DLLs anywhere and without jni.
KarstenK 21-Aug-13 9:44am    
Look in the sample jni app.

I mean that you have some "Set" function which will call other functions.
Buddhi Chaturanga 22-Aug-13 0:26am    
JAVA <=> JNI DLL fine.. collaboration fine.. but passing/recieving values of any data type to JNI DLL <=> C/C++ (EXE) using SETTERS/GETTERS FUNCTION not the option
KarstenK 22-Aug-13 2:22am    
I had broken my objects into int and strings. So instead of
from
SetObject( CInfo* object );
to
SetObject( int state, char *info, char *detail );

Only transfer native type, because memory layout is different.

PS: the same memory layout is a big advantage of the net-languages from M$

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