-
"DLL Failed To Load!"
Hi.
I am trying to create a .dll for a C++ project using Visual Studio 2005.
I successfully created it. Now when I am calling this .dll from another console application, I am not able to get HINSTANCE handle.
I am attaching code below....
Project DLLTutorial creates a dll. And I am using it in Test1 project(console project).
DLLTutorial has two files DLLTutorial.h and .cpp. And Test1 has one file Test1.cpp which loads DLL.
Project DLLTutorial builds successfully and creates .dll and .lib which I have put in Test1 project.
WHen I run Test1 project, it prints "DLL Failed To Load!" .
Below are code for three files.
DLLTutorial.h
Code:
#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
extern "C"
{
__declspec(dllexport) int Add( int a, int b );
__declspec(dllexport) void Function( void );
}
#endif
DLLTutorial.cpp
Code:
#include <iostream>
#include "DllTutorial.h"
#define DLL_EXPORT
extern "C"
{
__declspec(dllexport) int Add( int a, int b )
{
return( a + b );
}
__declspec(dllexport) void Function( void )
{
std::cout << "DLL Called!" << std::endl;
}
}
Test1.cpp
Code:
#include <iostream>
#include <windows.h>
typedef int (*AddFunc)(int,int);
typedef void (*FunctionFunc)();
int main()
{
AddFunc _AddFunc;
FunctionFunc _FunctionFunc;
LPCTSTR path= (LPCTSTR)"DLLTutorial.dll";
HINSTANCE hInstLibrary = LoadLibrary(path);
if (hInstLibrary)
{
_AddFunc = (AddFunc)GetProcAddress(hInstLibrary, "Add");
_FunctionFunc = (FunctionFunc)GetProcAddress(hInstLibrary,
"Function");
if (_AddFunc)
{
std::cout << "23 = 43 = " << _AddFunc(23, 43) << std::endl;
}
if (_FunctionFunc)
{
_FunctionFunc();
}
FreeLibrary(hInstLibrary);
}
else
{
std::cout << "DLL Failed To Load!" << std::endl;
}
std::cin.get();
return 0;
}
Thanks !!
-
Is the .dll in the same directory as the .exe from the test-harness?
--
Mats
-
Yes...both .dll and .lib are in same directory as .exe.
-
Try printing the return value from GetLastError() in the error case.
--
Mats
-
They sure make it more difficult than needs be. Does the tutorial specifically mention dynamic dll loading?
-
Hi.
I searched for GetLastError() in MSDN. But could not find way to use it.
Can you tell.
Thanks
-
What did you not understand? GetLastError() returns an error.
DWORD dwErr = GetLastError();
-
-
so print out the error and it might give you an idea of whats going on. There are about 74 pages of error codes listed on msdn, so without knowing the specific error code returned, you have no way of knowing whats wrong.
-
Change this
Code:
LPCTSTR path= (LPCTSTR)"DLLTutorial.dll";
HINSTANCE hInstLibrary = LoadLibrary(path);
...
std::cout << "DLL Failed To Load!" << std::endl;
to this
Code:
const char *path= "DLLTutorial.dll";
HINSTANCE hInstLibrary = LoadLibraryA(path);
...
std::cout << "DLL Failed To Load! Error = " << GetLastError() << std::endl;
You should never have to force a string literal into something (it may not be) with a cast.
gg
-
Hi.
I tried it....now I am getting error....
error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
-
the sample calls LoadLibraryA not LoadLibraryW
if you want to have Unicode version - you could use
Code:
LPCTSTR path= TEXT("DLLTutorial.dll");
HINSTANCE hInstLibrary = LoadLibrary(path);
-
It worked this time....
Thanks a lot...
I didn't get the difference between LoadLibrary(path) and LoadLibraryA(path) functions.
Kindly reply.
Thanks again for your help.
-
Text in Windows is either 8-bit character (char), aka ANSI or 16-bit characters (wchar_t), aka UNICODE.
Almost every Windows function has two variants, one that takes "ANSI", called XxxxxA, and one that takes "UNICODE", called XxxxxxW.
If you want to write code that works for either, then you should use TEXT("xxxx"), which expands to either ANSI or UNICODE strings.
The compiler is complaining in your code that you are passing a regular char string to a wide char expecting function.
--
Mats
-
In case you want to switch between ansi and unicode (unicode is recommended), you can do so in project settings, under Character Set (General).