PDA

View Full Version : Loading a DLL using Qt



srohit24
24th February 2009, 09:15
Hi

I have created a DLL named firstdll.dll and its written in C. It has only 1 function, named Display(), which i am exporting. When i call that function, it returns a string.

Now my question is how can I load that dll into my c++ application, so that i can get the string and display it?

I am trying using LoadLibrary() and other such functions to load the dll but i am not able to make it work. (In C# and VB its not that tough, but since i want to use c++ and Qt, i am finding it very hard to load the dll.)

I am using this code


HMODULE hDll = LoadLibrary("firstdll.dll");
FARPROC fpDisplay = GetProcAddress(hDll, "Display");
typedef char* (__stdcall *fpDisplayT)();
fpDisplayT Display = (fpDisplayT)fpDisplay;

but i get the following error


Error 1 error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR'
How can i solve this error?

It would be very helpful if you can give me the complete code to load the dll and then how to retrieve a string that dll returns when called.

thanks in advance.

talk2amulya
24th February 2009, 09:40
try L"firstdll.dll" instead of "firstdll.dll" in LoadLibrary

srohit24
24th February 2009, 12:15
thanks mate, it worked.

what was the problem??

talk2amulya
24th February 2009, 12:18
did u try it? if i remember correctly, it would convert parameter 1 from 'const char [12]' to 'LPCWSTR'

srohit24
24th February 2009, 12:34
yup. I tried it and it worked.

Now i have a working program in VC++, but i need a Qt version of it, how can I do that?

aamer4yu
24th February 2009, 17:34
Have a look at QLibrary

stevey
25th February 2009, 00:27
I just thought I'd mention that you're using Win32 API calls which are windows specific.
The L prefix has something to do with treating the string as Unicode or something, again windows specific. You've probably got Unicode specified in your project settings so the IDE has picked up the unicode version of it's C++ libraries. You're quoted error states 'LoadLibraryW' as having a problem. LoadLibrary is the standard one that takes the standard char set. LoadLibraryW takes a Unicode string, so the L I believe tells the pre-processor to convert this to a unicode string. Another way too fix this error would have been to turn off Unicode under the Project properties dialog, with no need to prefix with the letter L.

I would say that if you're on QtCentre you're intention is to produce cross platform code, so use QLibrary as suggested by aamer4yu.
You can still to Qt libraries in VC++, so there's not really any need to prototype a full blown Win32 version prior to going for Qt, unless of course you're testing migration strategies of legacy windows code.

I hope this provides a little more insight into the monster what is Visual Studio.