|
From: Dmitry S. <si...@si...> - 2004-09-13 04:35:13
|
Hello, dev-cpp-users.
I must import some class from my dll into main program using explicit
linking. DLL sources consists of 2 files
// ========= number.h ===============
#ifndef _NUMBER_H_
#define _NUMBER_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
class DLLIMPORT Number
{
public:
Number();
virtual ~Number();
virtual int Get();
virtual void Set( const int );
private:
int number;
};
extern "C" DLLIMPORT Number* CreateInstance_Number();
#endif // _NUMBER_H_
and
// ========= dllmain.cpp ===============
#include "number.h"
#include <windows.h>
Number::Number():
number( 7 )
{
}
Number::~Number ()
{
}
int Number::Get()
{
return number;
}
void Number::Set( const int n )
{
number = n;
}
Number* CreateInstance_Number()
{
return new Number();
}
int WINAPI DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved )
{
switch (reason){
case DLL_PROCESS_ATTACH:
case DLL_PROCESS_DETACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
Main program consists of the only main.cpp
// ========= dllmain.cpp ===============
#include <iostream>
#include <stdlib.h>
#include "number.h"
using namespace std;
int main(int argc, char *argv[])
{
HMODULE hMod;
typedef Number* (*pCreateNumber)();
hMod = LoadLibrary("mydll.dll");
if( !hMod )
cerr << "Failed to Load the dll: " << GetLastError() << endl;
pCreateNumber create = (pCreateNumber)GetProcAddress( hMod, "CreateInstance_Number" );
Number *num = create();
num->Set(172);
cout << "Value = " << num->Get() << endl;
system("PAUSE");
return 0;
}
Both DLL and main program are compiled and linked without errors
But when I`m trying to run a program, it is crushing in a line
Number *num = create();
What do I wrong?
--
kind regards
Dmitry
|