If you guys had followed the msdn tutorial for dll you'd probably notice that whilst it allows you to link a dll embedded in the same solution as your project, it doesn't actually teach you how to link a dll defined somewhere else on your computer.
So how could I link a dll file without sticking it in the same folder as my project?
The trouble here is that this only loads the file which is a little different from linking to a pre-compiled library. In order to use the functions in the DLL you need to use another function GetProcAdderss(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms683212(v=vs.85).aspx passing the EXPORTED FUNCTION NAME as the second argument. You then save this address to a function pointer and use it from there. The exported function name may not always be what you expect it to be so I use DLL Export Viewer From Nirsoft to get the names of the functions in the file: http://www.nirsoft.net/utils/dll_export_viewer.html
This can get very complicated very fast so feel free to ask any follow up questions.
I've tried to run this bit of code and it doesn't work, I had added the directory where my dll was, but it still doesn't recognises it. What am I doing wrong?
//This is windows
#define WINDOWS
#include <iostream>
#ifdef WINDOWS
#include <windows.h>
#endif
//Dynamic link libraries
#ifdef WINDOWS
typedefint (__cdecl *MYPROC)(LPWSTR);
#endif
int main( void )
{
//Dynamic link libraries
#ifdef WINDOWS
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("BlessfuncDLL.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "BlessfuncDLL");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd) (L"Message sent to the DLL function\n");
}
}
//////////////////////////////////////////////////////////////////////
//Using code from DLL
usingnamespace std;
double a = 7.4;
int b = 99;
cout << "a + b = " <<
MathFuncs::MyMathFuncs::Add(a, b) << endl;
cout << "a - b = " <<
MathFuncs::MyMathFuncs::Subtract(a, b) << endl;
cout << "a * b = " <<
MathFuncs::MyMathFuncs::Multiply(a, b) << endl;
cout << "a / b = " <<
MathFuncs::MyMathFuncs::Divide(a, b) << endl;
//////////////////////////////////////////////////////////////////////
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess)
printf("Message printed from executable\n");
#endif
while(1){}
return 0;
}
[output]1>------ Build started: Project: King_DLLL, Configuration: Debug Win32 ------
1>Build started 20/06/2012 22:57:31.
1>InitializeBuildStatus:
1> Creating "Debug\King_DLLL.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> main.cpp
1>c:\users\portsmouthuni\documents\visual studio 2010\projects\king_dlll\king_dlll\main.cpp(48): error C2653: 'MathFuncs' : is not a class or namespace name
1>c:\users\portsmouthuni\documents\visual studio 2010\projects\king_dlll\king_dlll\main.cpp(48): error C3861: 'Add': identifier not found
1>c:\users\portsmouthuni\documents\visual studio 2010\projects\king_dlll\king_dlll\main.cpp(50): error C2653: 'MathFuncs' : is not a class or namespace name
1>c:\users\portsmouthuni\documents\visual studio 2010\projects\king_dlll\king_dlll\main.cpp(50): error C3861: 'Subtract': identifier not found
1>c:\users\portsmouthuni\documents\visual studio 2010\projects\king_dlll\king_dlll\main.cpp(52): error C2653: 'MathFuncs' : is not a class or namespace name
1>c:\users\portsmouthuni\documents\visual studio 2010\projects\king_dlll\king_dlll\main.cpp(52): error C3861: 'Multiply': identifier not found
1>c:\users\portsmouthuni\documents\visual studio 2010\projects\king_dlll\king_dlll\main.cpp(54): error C2653: 'MathFuncs' : is not a class or namespace name
1>c:\users\portsmouthuni\documents\visual studio 2010\projects\king_dlll\king_dlll\main.cpp(54): error C3861: 'Divide': identifier not found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.76
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[/output]
The problem isn't with your DLL loading, it's with how you're trying to use the custom classes that I assume are declared in your DLL. Normally Class definitions are done in header files, I don't think you can declare them in DLL's like that. Even if you can though you're calling your functions wrong, it would be namespace colon colon datatype period function name not the way you have it here.
A .lib file would allow you to link to the DLL directly instead of having to use the LoadLibrary() function. But normally I put things like that in my header files.
I've included my interface header file on its own but the issue I now get is the missing "implementation" were VS2010 says that it can't find my implementations. what am I suppose to do? this is now getting more irritating than it hopefully mustn't be.
I really don't think you can, at least I've never seen it done before. You can have custom classes for use within your DLL and obviously you can have classes call functions exported from a DLL. But I don't think a DLL can export a custom data type.