Visual Studio 2010 - Visual C++
Linking Explicitly
With explicit linking, applications must make a function call to explicitly load the DLL at run time. To explicitly link to a DLL, an application must:
Call LoadLibrary (or a similar function) to load the DLL and obtain a module handle.
Call GetProcAddress to obtain a function pointer to each exported function that the application wants to call. Because applications are calling the DLL's functions through a pointer, the compiler does not generate external references, so there is no need to link with an import library.
Call FreeLibrary when done with the DLL.
For example:
typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);
...
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
DWORD dwParam1;
UINT uParam2, uReturnVal;
hDLL = LoadLibrary("MyDLL");
if (hDLL != NULL)
{
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,
"DLLFunc1");
if (!lpfnDllFunc1)
{
// handle the error
FreeLibrary(hDLL);
return SOME_ERROR_CODE;
}
else
{
// call the function
uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
}
}
What do you want to do?
What do you want to know more about?
See Also
Concepts
Community Content
Hyryel
here is a little contribution to you, newby coder...
//this is a friendly type definition to a fonction that return an UINT and takes a DWORD and an UINT as parameters
// it will be used to declare a function pointer more easely
typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);
...
HINSTANCE hDLL; // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1; // Function pointer
DWORD dwParam1;
UINT uParam2, uReturnVal;
//the function LoadLibrary take the name of the library as parameter, in this example,
// MyDLL reffers to the name of the file MyDLL.dll, and this file reside in the same
//directory of the executable running this code
hDLL = LoadLibrary("MyDLL");
//here, we test if the library has loaded
if (hDLL != NULL)
{
//if so, we want to get the proc address, so we use GetProcAddress wich take two arguments :
// a handle to the library to search in, and the name of the function
lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL, "DLLFunc1");
//we test if we found the proc address
if (!lpfnDllFunc1)
{
// if not, we
// handle the error
FreeLibrary(hDLL);
return SOME_ERROR_CODE;
}
else
{
// call the function
uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
}
//I add just a remark, think to unload the library when done
FreeLibrary(hDll);
}
NewbieCoder0123
You must work more on your documentation
I'm not a native English speaker and some words are not easy to understand but I'm pretty sure that there is a paradox between your Linking Explicitly page and your GetProcAddress page: on Linking Explicitly page you explain that GetProcAddress returns "A function pointer to EACH exported function..." whereas on GetProcAddress page it is wrote: "call GetProcAddress to obtain THE address of AN exported function in the DLL."
If I'm wrong, at least it shows that your explanations are not easy for anybody to understand.
But that is not the main problem:
you give us a sample code going out of nowhere without any kind of commentary! I'm unable to know what "MyDLL" is referring to and when I looked at the LoadLibrary page I found out that there were no definition of the type of the entry value neither an example of en entry value and so it is quite hard for me to understand. And the rest of the code is not much more understandable, it took me a long and hard time looking at your others pages to understand what "dllFunc1" was referring to for example.
These are not the only problems I encountered reading your documentation, as a fresh coder I found it really hard to understand, incomplete and done with a lot of links necessary to understand a unique page. (I ever found a link leading to an error page saying the page I tried to reach does not exists!)
So if you can take my comment in account in order to improve your documentation (especially if you could make it more "newbie-proof") it will be very kind of you.
A limited coder, beginning by a hard way.
If I'm wrong, at least it shows that your explanations are not easy for anybody to understand.
But that is not the main problem:
you give us a sample code going out of nowhere without any kind of commentary! I'm unable to know what "MyDLL" is referring to and when I looked at the LoadLibrary page I found out that there were no definition of the type of the entry value neither an example of en entry value and so it is quite hard for me to understand. And the rest of the code is not much more understandable, it took me a long and hard time looking at your others pages to understand what "dllFunc1" was referring to for example.
These are not the only problems I encountered reading your documentation, as a fresh coder I found it really hard to understand, incomplete and done with a lot of links necessary to understand a unique page. (I ever found a link leading to an error page saying the page I tried to reach does not exists!)
So if you can take my comment in account in order to improve your documentation (especially if you could make it more "newbie-proof") it will be very kind of you.
A limited coder, beginning by a hard way.