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 !!