Calling dll and c++ header file in a java program - java

I know many people have asked similar question but i want to specifically ask how do we call both a .dll and a header file in one java program. In this program i must use the dll file and include the header file if written in c++. But i am not so good in using c++, i want to use java, so how to i achieve this? I know about jna which is good when calling dll files in java and jni, used when calling c/c++ files in java. But in this case, i need to use the dll file in a java program and the c++ header file must be included in order for the program to perform very well.

Related

creating a .h file for a DLL file, if you do know the method signatures

I am trying to access the methods of a DLL file in Java.
I wanted to use ComfyJ to create a wrapper for the DLL file.
ComfyJ has a wizard for this, however when I select the DLL file, it says that it cannot use this kind of DLL file.
So, next I tried to use the JNIWrapper wizard, but it requires a .h file. The problem is, that I don't have a .h file for this DLL.
(I have a commercial license for both these tools).
I am a bit surprised that there is no way to export or create a .h file for any given DLL file. I searched the internet but couldn't find any tool capable of doing this. Which gives me the feeling that I'm still missing some piece of information, which is not in the DLL file, but will be necessary to create this .h file.
Secondly, I do actually know all method names, parameters and return types. That is because the WinApiOverride tool can list them for me. I can even see which parameters are passed to the functions/methods of this DLL file, while applications are running.
Foo.dll|bool __cdecl ?FooAddBar##YA_NHHH#Z(int,int,int)
So, I have the impression that I have all required information. Before I go in too deep ... My question: do you think it's possible that I create this .h file manually using Notepad ?
Legal Note: this DLL file is not a pre-installed DLL. It's part of a commercial application for which I own a valid commercial license. The company says that I am free to use their DLL file for whatever I want, as long as I use it for personal use only.
EDIT:
I also tried to write the jniwrapper code myself.
Result looks something like this:
Library lib = new Library("foo", Function.CDECL_CALLING_CONVENTION);
lib.load();
// works fine.
Function f = lib.getFunction("?FooAddBar##YA_NHHH#Z");
// doesn't work.
f.invoke(null, new Int(), new Int(), new Int());
The result is a fancy exception:
Exception c0000005, at 157694FA
Access violation: attempting to read memory at address 00000078
Native function stack data: 0,0,0,0,b0dcbfe6,800fdc5,65637845,6f697470,3063206e,30303030,202c3530,31207461,39363735,a414634,65636341,76207373
Exception in thread "main" com.jniwrapper.FunctionExecutionException: c0000005
at com.jniwrapper.Function.invokeCFunc(Native Method)
at com.jniwrapper.FunctionCall.a(SourceFile:127)
at com.jniwrapper.FunctionCall.call(SourceFile:35)
at com.jniwrapper.Function.invoke(SourceFile:188)
at com.jniwrapper.Function.invoke(SourceFile:239)
That strange method name seems to be the correct form. The method name does look strange. I found out that this is because C++ "encodes" the file names inside dll files. ?FooAddBar##YA_NHHH#Z actually is bool FooAddBar (int, int, int).
Nevertheless, it looks like JNIWrapper prefers the encoded name. Because when I try with the short name "FooAddBar" or antything else, I already have an exception because it cannot find the method. On the other hand, when I use the encoded method name (i.e. "?FooAddBar##YA_NHHH#Z"), it does find the method, proving that it is the correct one.
However, during the invocaton, things do go wrong. So, I'm guessing that I use the wrong parameters or something. Clearly, I'm doing something wrong, and hoping that JniWrapper wizard can create a correct wrapper which fixes this. (However, in that case, I do need a .h file).

How to call jar file from c# [duplicate]

This question already has answers here:
Execute Jar file from C sharp code and get return value
(2 answers)
Closed 7 years ago.
I have a c# application and I have a .jar file that I have created. I want to call from c# application, my jar file, I want to call a method in the jar file. I try to use JNI but I can't call jar file.
There is a mode to do this?
Your main problem is the lanuage incomaptabilities
As far as i understand there is no direct way that Java (jar) and c# assembles can talk to each other.
Question is what does the jar file do ? and can the same be done using c#
If not then you are going to have to create a java application using somethign like Netbeans to act as a kind of middle man
Then when the c# application does a function call the call gets serialised into a file
The java app should then look for the file (in a thread or something) pulling the data and writing the response data to the file
While asp.net looks for changes in the file and pulls the data back from the file once java has written
There are other options such as web service and TCP IP socket server to communicate
But Long story short there is no clear cut way as far as i am aware of that there is not clear cut way of making java and .net talk to each other .
A quick google search did bring up http://www.ikvm.net/userguide/tutorial.html[^] which seems to be promising.
Hope this helps

Windows FileChooser Or Save File Dialog Handling Using JACOB Or JNA

I need to access the windows native file upload and save window using java.
Though it is not possible only using java so I try this using jacob and JNA library.
Actually I also need to handle the any type of pop up message from OS.
There are lots of any other approach using java robot,sikuli,autoit but i want to stick(learn)
on jacob or jna.
Can any body help me regarding this problem.Any code snippet on this, helpful for me not only that if some body told me how to get hwnd ID dword ID which is need to pass into the different methods that also be useful for me.
Im guessing you were referring to CFileDialog or OpenFileDialog, well that can be accomplished via JNI and a native DLL which you need to compile yourself, which is somewhat easy:
create a C++ DLL - project in visual studio, i'd recommend using MFC - methods which are called via C-functions, that'll make everything MUCH easier since you wont need to re-invent the wheel and create WindowProcs yourself and whatnot - MFC does all of that transparently. You could also create a CLR-DLL which uses WinForms but thats a bit more complex. Do yourself a favor and disregard anything about MinGW, GCC or even any other OSS-solution if you want to create native windows ... that'll give you headaches, trust me. Within windows, you will need to use microsoft-libraries if you want to create Microsoft-compatible user interfaces, everything else is very error-prone.
include jni.h which is found in your JDK-library
write a JNI-compatible method signature
compile
declare compatible, native methods in java
compile
try to run your java project
report back, i shall help you to some extent if you run into any problems
JNI howto

How to store a file in Java, which is generated on execution of a .exe file?

I have a .exe file, which produces certain files when made to run :
The files produced are WatchDataTest, ngrtgs.test, shubhangi, slatey (as they appear in the image)
I want to run the .exe file through a separate Java Program and obtain reference to the above files. How can this be done?
My point of view: I think, obtaining an OutputStream(wrapped by ObjectOutputStream) on the Process object of this executable can be used to read the objects (files, in this case). However, I am not sure in what way does this executable provides reference to the files produced. Other than that, I have a confusion whether the GUI display is part of the output. I mean does the OutputStream of this executable include the GUI object, which displays on the screen? If not, what all is the output of this .exe?(Pretty confusion here)
The .exe file calls your OS native functions to create those files. You cannot catch that from Java.
If you want to read the content of those files from Java, find them in the directory structure, and open them for reading with the normal Java File I/O API.
I think you want to access those certificates, right? Most likely they're not stored in separate files, but in one file called keystore. In this case I recommend to use Java PKI API or tools to manage your keystore.

Write to hidden files in Java [duplicate]

This question already has answers here:
Modify a hidden file in Java
(2 answers)
Closed 10 years ago.
I have a problem with my hidden files in Java. I would like to write to a hidden txt file but it always says "access denied".
My suggestion would be to make the file visible, write into it and then make it hidden again.
But how can i make a hidden file visible?
I previously said: The problem is not that the file is hidden. The problem is that your program doesn't have write access for the file. Making it "unhidden" is unlikely to help.
It seems that it is more complicated than that for Windows. Certainly it is worth trying to "unhide" the file before modifying it.
Anyway, if you are using Java 7, the way to read and write Windows-specific file attributes is to use the java.nio.file.Files API, and specifically the getFileAttributeView(...) method. For older versions you will either need to use an external utility (see #Achintya Jha's Answer) or use a JNI / JNA wrapper to call a Windows native API.
If you are using Linux, change Windows-specific to POSIX and/or Linux specific. (Note that a lot of this stuff to do with file attributes and permissions is intrinsically OS specific.)
Finally, if the problem is that the program doesn't have write access to the file, there is a fair chance that there is nothing that it can do to get write access.

Categories

Resources