Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DLL Talk / How to explicitly load a Dll in C++?

Author
Message
Freedom Fighters
14
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 21st May 2010 04:38 Edited at: 8th Jun 2010 10:45
ok, i have written two dlls for GDK but i would like to be able to load then from sub folders not in the general area of the exe.
Eg: Load dll from "Data\\dlls\\<DllName>.dll"
not
"<DllName>.dll" <- general area of the exe.


Quote: "well you use LoadLibrary, GetProcAddrrees..etc""


i already know these functions. but how do i use them.

see i making a class that you can use to call the functions which call the functions from the dll.

but say i have like 20 functions. how would i call each and load them all?
so each time i call the function-> then call the function from the dll-> Dll funtcion

i have seen examples on how to load one function but not multiple.

so how can i do this?

please explain it to me, still learning c++.

Cheers.



EDIT: PROBLEM SOLVED, SEE LAST POST FOR SOURCE

Problem Solution That Never Fails: "Build A Bridge And Get Over It"
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 22nd May 2010 01:10
I might not be the best C++ teacher out here, but here goes:

To load the library you indeed use LoadLibrary(). It returns a handle to that library.

HMODULE Library = LoadLibrary("MyDLL.dll");

Then you first define the function pointer using the following syntax:
ReturnType (*FunctionName)(parameters);
eg.
int (*GetSum)(int,int);

Then you need to get the decorated names of the functions you want to load. I usually open the DLL file in notepad and CTRL+F the function to find the decorated name (they are separated by a space). The decorated names are dependent on how they are exported, but you'll notice once you search in the file.
eg. "?GetExist@@YA_NH@Z" or "_CleanD3D@0"

Then you use GetProcAdress() to get the function pointer as followed:
GetSum = (int(*)(int,int)) GetProcAddress(Library, "_DecoratedFunction@1");
(GetProcAddress returns a FARPROC type, so you have to cast it to your function prototype).

After you loaded it, you can call the function using GetSum(a, b); like any other function.

I hope this helps.

Cheers!
Sven B

Freedom Fighters
14
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 25th May 2010 10:51 Edited at: 25th May 2010 10:56
Ok, well so far i have written the code but i an having an issue loading the 'DLL' for what reason ever it wont load.

To load the dll


i checked the file name and tried it with '.dll' and with out '.dll', either way it wont load. it just returns '0' or 'NULL'. i have no clue what the problem is. 'UdpDll' is defined as a 'HMODULE', i even tried it with 'HINSTANCE'

im using a class to load it. this is the class to load the DLL

'FF_UdpDll.h"


FF_UdpDll.cpp



Problem Solution That Never Fails: "Build A Bridge And Get Over It"
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 25th May 2010 18:12
And you're sure it's in the right directory? Have you tried the full path name? Is the DLL not corrupt or did you get it to work somewhere else? I have always added .dll so it should if you add .dll.

Sorry, it's just guessing for me now too.
If you tried all these things, perhaps you can give some info on what you're using to create it. I assumed it was VC++ express.

Sven B

IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 26th May 2010 00:00
Your mistake is here:

You are creating an 8 bit character array, then forcing the compiler to believe it's a 16 bit character pointer - that doesn't convert the characters themselves.

2 options:
1. Prefix your string literal with an uppercase L:


That switched it to a 16 bit literal string.

2. Switch your DLL to 8 bit characters, not unicode. This is 'better' for DBPro plug-ins as DBPro uses an 8 bit character system only.

Freedom Fighters
14
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 26th May 2010 14:16
Quote: "Your mistake is here:
+ Code Snippet

(LPCWSTR)"FreedomFighters - UDP_Network_dll"


You are creating an 8 bit character array, then forcing the compiler to believe it's a 16 bit character pointer - that doesn't convert the characters themselves.

2 options:
1. Prefix your string literal with an uppercase L:
+ Code Snippet

L"FreedomFighters - UDP_Network_dll"



That switched it to a 16 bit literal string.

2. Switch your DLL to 8 bit characters, not unicode. This is 'better' for DBPro plug-ins as DBPro uses an 8 bit character system only."


Yep, this fixed the problem. but now when i got to call one of the functions it comes up with a error: 'access violation' im trying to work out what could be causing the isssue.

i am using visual c++ 2010

Problem Solution That Never Fails: "Build A Bridge And Get Over It"
Freedom Fighters
14
Years of Service
User Offline
Joined: 2nd Nov 2009
Location:
Posted: 8th Jun 2010 11:04
[PROBLEM SOLVED! ]

ok, i solved the problem and now it all works. ;P

to help others i will release my source for loading the dll and calling it.

*Note: this will not include the source for the dll.

Enjoy.

Note*: The Project is in Visual Studio 2010 C++ Sorry.

Problem Solution That Never Fails: "Build A Bridge And Get Over It"

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-04-19 08:00:40
Your offset time is: 2024-04-19 08:00:40