Skip to main content
GameDev.net gamedev.net
🔒 Locked

Using a dynamic link library

Started by BlessmanTsanga Jun 21, 2012 at 4:48 PM 0 replies 2k views
Original Post
BlessmanTsanga
BlessmanTsanga
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? What am I doing wrong with my dll linking in visual studio 2010 (full edition).




//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;

}





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 ===[/sub]=======
[/quote]
Amr0
Amr0
You need to #include the header file which contains the prototypes for the code in the dll. Or you could load the addresses of the functions dynamically like you are doing with the "BlessfuncDLL" function, but you'll have to do that with every function in the dll that you want to use in the application. In the code you posted above, the application has no way of knowing anything about MathFuncs, MyMathFuncs, or the Add function inside. See this for example for an introductory article/tutorial for using dlls.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.