MSDN dll tutorial problem

hi

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?
I think the only way I've done it (WinXP, VS 2010) is by putting the dll in the system32 folder.

As an end user, the dlls a program needs are installed to the exe folder anyway.
Last edited on
Yes, you do this with the LoadLibrary() function: http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175(v=vs.85).aspx and use the full path to the target file.

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?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//This is windows
#define WINDOWS

#include <iostream>

#ifdef WINDOWS
#include <windows.h>
#endif


//Dynamic link libraries
#ifdef WINDOWS
typedef int (__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
	using namespace 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]
_bump any help?
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.
@computergeek01

Do you stick the interface in a header file only or could a ".lib" file also count?
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.
@computergeek01

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.
Do you have the implementations in the header file? Or did you forget to link the .cpp file that has your function implementations?
No, I thought it would include my class' implementation from the .dll, the whole point is to find out if I can load a class from a .dll file.
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.
Ok then, but how do you reference a custom class in a .dll file?
It looks like I am wrong yet again: http://www.daniweb.com/software-development/cpp/threads/208629/export-class-from-dll-with-dev-c-or-codeblocks it appears that the key to exporting a class from a DLL is the "DLL_EXPORT" tag. I've never experimented with this myself but it looks interesting.
Topic archived. No new replies allowed.