C++ : Class로 만든 DLL Export 및 Import
프로젝트 만들 때 솔루션 디렉토리를 생성하고
Dll 프로젝트와 Test Dll 프로젝트를 생성한다
Test Dll 프로젝트는 MFC 응용 프로그램에서 다이얼로그 기반만 설정하고 마침한다
Dll 프로젝트는 MFC 확장 DLL만 설정하고 마침한다
Dll 프로젝트에서 Dll과 App에서 사용할 공용 헤더를 추가한다
Dll.h
class A
{
public:
virtual int AddInteger(int A, int B) = 0;
virtual void destroy() = 0;
};
Dll.cpp
#include "stdafx.h"
#include "Dll.h"
#include <iostream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace std;
class B : public A
{
public:
B()
{
cerr << "B constructor\n";
}
~B()
{
cerr << "B destructor\n";
}
int AddInteger(int C, int D)
{
return C+D;
}
void destroy()
{
delete this;
}
};
extern "C" __declspec ( dllexport ) A* create_b ()
{
return new B;
}
Test_App의 Dlg.cpp 에서 Dll.h 파일을 include 하고
테스트용으로 만든 버튼에
HINSTANCE hDll = NULL;
hDll = LoadLibrary(L"Dll.dll");
if(hDll == NULL)
{
OutputDebugString(L"dll load fail");
return;
}
typedef A* (*a_factory) ();
a_factory a_factory_func = NULL;
a_factory_func = (a_factory) GetProcAddress(hDll, "create_b");
if(a_factory_func == NULL)
{
OutputDebugString(L"getprocaddress fail");
return;
}
A* instance = NULL;
instance = a_factory_func();
if(instance == NULL)
{
OutputDebugString(L"class instance fail");
return;
}
int result = 0;
TCHAR reBuf[128] = {0, };
result = instance->AddInteger(3, 4);
wsprintf(reBuf, L"AddInteger 결과 값 : %d", result);
MessageBox(reBuf);
instance->destroy();
FreeLibrary(hDll);
작성하면 3+4를 한 결과 7을 반환한다
Dll 프로젝트와 Test Dll 프로젝트를 생성한다
Test Dll 프로젝트는 MFC 응용 프로그램에서 다이얼로그 기반만 설정하고 마침한다
Dll 프로젝트는 MFC 확장 DLL만 설정하고 마침한다
Dll 프로젝트에서 Dll과 App에서 사용할 공용 헤더를 추가한다
Dll.h
class A
{
public:
virtual int AddInteger(int A, int B) = 0;
virtual void destroy() = 0;
};
Dll.cpp
#include "stdafx.h"
#include "Dll.h"
#include <iostream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace std;
class B : public A
{
public:
B()
{
cerr << "B constructor\n";
}
~B()
{
cerr << "B destructor\n";
}
int AddInteger(int C, int D)
{
return C+D;
}
void destroy()
{
delete this;
}
};
extern "C" __declspec ( dllexport ) A* create_b ()
{
return new B;
}
Test_App의 Dlg.cpp 에서 Dll.h 파일을 include 하고
테스트용으로 만든 버튼에
HINSTANCE hDll = NULL;
hDll = LoadLibrary(L"Dll.dll");
if(hDll == NULL)
{
OutputDebugString(L"dll load fail");
return;
}
typedef A* (*a_factory) ();
a_factory a_factory_func = NULL;
a_factory_func = (a_factory) GetProcAddress(hDll, "create_b");
if(a_factory_func == NULL)
{
OutputDebugString(L"getprocaddress fail");
return;
}
A* instance = NULL;
instance = a_factory_func();
if(instance == NULL)
{
OutputDebugString(L"class instance fail");
return;
}
int result = 0;
TCHAR reBuf[128] = {0, };
result = instance->AddInteger(3, 4);
wsprintf(reBuf, L"AddInteger 결과 값 : %d", result);
MessageBox(reBuf);
instance->destroy();
FreeLibrary(hDll);
작성하면 3+4를 한 결과 7을 반환한다
댓글
댓글 쓰기