I have a dll, which I want to use in my java code.
I declare a function as native and the return type is bool (c).
As I saw in JNI documentation, java 'boolean' should be mapped as 'jboolean' in c code. But the problem is that i don't have the c code, only dll.
My actually problem is UnsatisfiedLinkError, but I have no idea what else can be wrong.
If it is the problem, should i write another c-wrapper?
(I know that this exception was already discussed a lot of times, but I didn't find some useful information according my case)
UPD:
Maybe there are anothere way to use dll?
You can't use an arbitrary DLL directly from Java. You have to declare a native Java method, write the JNI for it, and call the DLL method(s) from the JNI code.
Related
I'm trying to import some functions created in a C++ library, to be used in delphi. Here is the library i'm trying to use: https://github.com/NGSolve/netgen/blob/master/nglib/nglib.h
I've looked around on google, and found that i've to create a .dll file out of this header file and then, somehow, import hese functions in delphi. The problem is i don't know how i can do any of this!
Can you guys help me out? Thanks in advance!!
There are two main ways to do this. Both require you to generate a DLL out of the code. I can't give a detailed explanation on how to do this. But on my website I do give you the details and some sample code. See links below.
The two main ways are
Flatten the object. Wrap each method of the object into a plain function and export that from the DLL. The object is simply passed along as some kind of handle or untyped pointer. You can't use the object directly, so no need to type the pointer or handle. This is the easiest solution, but not as convenient for the user of the DLL as the next one:
Turn your object into a COM interface and write a function to instantiate that. This is far from easy, but makes the DLL much easier to use.
Both ways are described in my article Using C++ objects in Delphi.
You can find some more info in other articles of mine:
Pitfalls of converting
DLL dos and don'ts
Using C object files in Delphi.
If these are not compiled with C++Builder, you may be lucky that you can do without the DLL and link to object files directly, using the flattening method. But I never tried that.
You can only use plain C API interfaces in Delphi so far. So maybe try to wrap the C++ API with plain C function calls first.
For some test needs I need some third-party dll to be replaced with own stubbed version: the actual methods should return some hard-coded data with some specific delays and delays' effect on the system that uses the original dll are actually the under the test.
So, for this purpose I need some how to create a *.cpp file with the same structs and signatures from the dll. What is the best approach to do it programmatically?
1) decompilers? but actually I don't need the code itself, just class definitions and signatures only.
2) do it programmatically with c++ code
3) do it programmatically with java code through JNI, for example.
Any ideas and directions are welcome. Unfortunately, googling doesn't give the straighforward answer, at least for c++ newbies as me.
If this DLL is loaded from Java and called via JNI, then you can launch javah to produce the relevant headers; use -stubs option to prepare the C files with dummy implementations of all native methods.
Update: -stubs does not work correctly, see Unable to generate JNI .c file using javah -stubs
Is it possible to load a native (C++) DLL as an executable?
preferablly straight from the memory without creating EXE on the hard-drive or something similar?
Microsoft provides Rundll32.exe which can be used to execute DLL functions that have been explicitly coded to support this usage.
What, specifically, would this mean? For example, what entry point would it use in the DLL?
The only way this would actually work would be if the DLL was specifically written to allow it. And if that were the case, then it's not exactly clear why you would not just create an executable file instead of a DLL in the first place.
Case in point is the RunDLL32.exe stub. It's designed to execute a function from a DLL with a specific signature as the entry point. If the DLL wasn't specifically designed to comply with this signature, then things don't go well. If you need functionality like this, you might want to consider matching the function signature required by RunDLL32.exe and using it to "execute" your DLL.
Look up rundll32.exe. But you'd better know exactly what you're doing. I'm not sure, honestly.
You can use LoadLibrary WinAPI call to load a DLL.
Is there a way to write a qt library such that I can then use it (statically linked is fine) in a C application?
My C code is huge, old and will not convert to C++ without an inordinate amount of work. I say this as other similar questions seem to answer "just make your C code a Qt app". That's not an option.
I hope I can write a qt library, and build it in a way that lets it be called from C (something alluded to in QLibrary documentation).
The symbol must be exported as a C
function from the library for
resolve() to work. This means that the
function must be wrapped in an extern
"C" block if the library is compiled
with a C++ compiler. On Windows, this
also requires the use of a dllexport
macro; see resolve() for the details
of how this is done.
Can someone confirm/deny that I can do this, and let me know how much "qt" I can put in the library?
I don't need a GUI but would like to use some of the SQL handling.
Cheers
Mike
You can put as much Qt in a library as you wish, including full UI capability. The rub is that since you want to access it from C code, you must provide your own access functions and your C functionality will be constrained to whatever level of access you provide.
You can even pass Qt object pointers between C and C++ but you'll need to cast them into something that C can compile -- either void * or preferably your own new type definition (such as C_QString *). To C code these pointers will be opaque, but they'll still be valid.
I have a library that I am compiling and creating a fully standalone C++ program. There are two cpp files, one that has the main, the other with all the functionality.
Currently, this program is implemented with a Java ProcessBuilder with args to call the C++ program and the results of that C++ program just simply go out to a file.
Now, I am wanting to get the results of that C++ function that does that work back to my java program. (The results in the C++ function is a double unsigned char array)
So my question is - is there a way to map those existing library functions so that I can call them from my java program directly, AND still keep using that library in the stand-alone way that I currently am, which is through that driver C++ program main()?
I am basically trying to avoid having to compile the same library twice - once for JNI functionality, and once as a standalone C++ program
Thanks
Java Native Access (JNA) will do what you want.
Java Native Interface (JNI) requires an additional layer of C glue between Java and C++. But JNA can access some C functions directly from Java.
You'll probably need to declare your function extern "C". You won't have to recompile the library, but you will have to link it with your C++ main function.
If you had a large C++ class library to expose, then you'd might be interested in SWIG. But for single C function, JNA is probably sufficient.
I think an exe can export functions similar to a dll (using __declspec(dllexport) on windows), which means you might be able to load it similarly to a jni-dll. (You might need to rename it .dll or .so to have java load it though.)
If loading doesn't work, another way would be to start your program in a separate process as you do now, but give it an command-line option which establishes some kind of shared-memory-area with the Java program.
That would avoid copying the daya back and forth, but might require a small utility DLL loaded by both the exe and the java program, i.e. not simpler. (Not sure if Java can setup shared-memory easily w/o native c calls ..)
(keeping answers separate for voting/comments/accept granularity)
Create a JNI wrapper DLL that runs the executable and returns the chars. Alternatively, compile the relevant code into a static library and link it either to a main() function for a standalone program, or to a JNI stub for calling from Java. In-process always better.
I believe a proper way to do it would be first to compile your functions into an independent dynamic library (.dll / .so / .dylib).
Then you can :
write a C++ executable that links against your shared library
write a Java program that binds to your C++ library thanks to BridJ, for instance (or JNA, if you stick to plain old C)