PDA

View Full Version : Creating DLL file in Qt and using it in Java



sattu
6th May 2011, 12:34
Hi everyone,

I am creating a simple shared library which contains the following files:
mylib.h and mylib.cpp


the code for mylib.pro is:


QT -= gui

TARGET = myLIB
TEMPLATE = lib
SOURCES += mylib.cpp

HEADERS += mylib.h



the code for mylib.h is:

#ifndef MYLIB_H
#define MYLIB_H

extern "C"{
__declspec(dllexport) int __stdcall hello();
}

#endif // MYLIB_H



the code for mylib.cpp is:



#include "mylib.h"

__declspec(dllexport) int __stdcall hello()
{
return 56;
hello() is a simple function which returns 56
}


After building the project, i am getting a myLIB.dll file in debug directory. I am able to use this DLL in a visual C# project by referencing to the DLL in C# code.
But i am not able to use the hello() function of the DLL in java.


So, can anyone please tell me the correct way of creating a DLL in Qt, which i can further use in Java? :confused:


Thanks in advance,

With regards,
Satya Prakash.

ChrisW67
7th May 2011, 01:53
This question has nothing to do with Qt.

You want to read about Java Native Interface (JNI) (http://en.wikipedia.org/wiki/Java_Native_Interface). That should enable your Java code to talk to suitable code you write in C or C++.

sattu
7th May 2011, 05:31
Hi Chris,

Thanks for replying. Actually now the problem is solved. As you told, I can do it by either using JNI or JNA package. Now, I am looking forward to using a DLL and it's methods in Qt. I hope, examples are available to guide regarding the use of DLLs in Qt. :)

ChrisW67
7th May 2011, 06:40
Your question was about using C/C++ libraries from Java. Now you are saying you want it the other way around.
In either case, this has little to do with Qt.

sattu
7th May 2011, 07:35
Your question was about using C/C++ libraries from Java. Now you are saying you want it the other way around.
In either case, this has little to do with Qt.

Sorry for being in such a rush. Actually the project is such that it has to be both way. Creating a DLL in qt and using it in Java is fine.

Now, the concern is that I have got a C++ DLL containing some functions. I have to use that DLL(and it's methods) in my Qt program. So, currently my problem is this. So, can you provide me some links or examples which can guide regarding the usage of DLL in Qt (both by static and dynamic linking).

Thanking in advance, :confused:

stampede
7th May 2011, 08:37
Qt is C++, so you need to search for info on how to link to external libraries using your C++ compiler, for example if its gcc, then look here: Linking with external libraries (http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html)

sattu
7th May 2011, 09:24
Ok stampede, I will see to it and reply if I have got any doubt.