๋ฐ˜์‘ํ˜•

๋ณธ๋ฌธ์„ ์ฝ๊ธฐ ์ „์— ์•„๋ž˜์˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ์ดํ•ดํ•˜๋Š”๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

[C++ DLL] C++ ํด๋ž˜์Šค๋ฅผ DLL๋กœ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

[C++ DLL] Visual Studio C++ ๋ช…์‹œ์  ๋งํฌํ•˜๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

[C++ DLL] Visual Studio C++ ์•”์‹œ์  ๋งํฌํ•˜๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

[C++ DLL] Visual Studio C++ DLL ์ƒ์„ฑํ•˜๊ธฐ (์˜ˆ์ œ ํฌํ•จ)

MFC ํ™•์žฅ DLL

MFC ํ™•์žฅ DLL์€ MFC ํ”„๋กœ์ ํŠธ์™€ MFC ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์—ฐ๊ฒฐ๋œ ์ผ๋ฐ˜ DLL์—์„œ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. MFC ํ™•์žฅ DLL์„ ์‚ฌ์šฉํ•˜๋ฉด ํด๋ž˜์Šค๋ฅผ ์‰ฝ๊ฒŒ DLL๋กœ Export ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ํƒ€๊ฒŸ ํ”„๋กœ์ ํŠธ๊ฐ€ MFC๋ผ๋ฉด MFC ํ™•์žฅ DLL๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ๋„ ์‰ฌ์šด ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

MFC ํ™•์žฅ DLL ๋งŒ๋“ค๊ธฐ

ํ•œ ๋ฒˆ ๋”ฐ๋ผํ•ด ๋ณผ๊นŒ์š”? MFC ๋™์  ์—ฐ๊ฒฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์€ CreateDLL๋กœ ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

๊ทธ๋ฆฌ๊ณ  MFC ํ™•์žฅ DLL์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

MFC ํ™•์žฅ DLL ์„ ํƒ

ํ”„๋กœ์ ํŠธ์— ์ƒˆ ํ•ญ๋ชฉ์„ ์„ ํƒํ•˜์—ฌ MyDLL.h์™€ MyDLL.cpp๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

h ๋ฐ cpp ํŒŒ์ผ ์ถ”๊ฐ€

๊ทธ๋ฆฌ๊ณ  MyDLL.h์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

#pragma once

#include <string>
#include <iostream>

class AFX_EXT_CLASS Person {
public:
	Person(std::string name, int age, std::string address);
	void ShowInfo();

private:
	int age;
	std::string name;
	std::string address;
};

์—ฌ๊ธฐ์„œ ๊ธฐ์กด C++ DLL ์ƒ์„ฑ๊ณผ ๋‹ค๋ฅธ ์ ์€ AFX_EXT_CLASS ํ‚ค์›Œ๋“œ์ž…๋‹ˆ๋‹ค. ์ด ํ‚ค์›Œ๋“œ๋Š” afxv_dll.h์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

#ifndef AFX_EXT_DATA
	#ifdef _AFXEXT
		#define AFX_EXT_CLASS       AFX_CLASS_EXPORT
		#define AFX_EXT_API         AFX_API_EXPORT
		#define AFX_EXT_DATA        AFX_DATA_EXPORT
		#define AFX_EXT_DATADEF
	#else
		#define AFX_EXT_CLASS       AFX_CLASS_IMPORT
		#define AFX_EXT_API         AFX_API_IMPORT
		#define AFX_EXT_DATA        AFX_DATA_IMPORT
		#define AFX_EXT_DATADEF
	#endif
#endif

ํ•œ๋ฒˆ ๋” ๋“ค์–ด๊ฐ€๋ฉด, AFX_CLASS_EXPORT๋Š” afxver_h์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

// for classes
#ifndef AFX_CLASS_EXPORT
	#define AFX_CLASS_EXPORT __declspec(dllexport)
#endif
#ifndef AFX_CLASS_IMPORT
	#define AFX_CLASS_IMPORT __declspec(dllimport)
#endif

์ฆ‰, AFX_EXT_CLASS ํ‚ค์›Œ๋“œ๋Š” __declspec(dllexport)์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์„ค๋ช…ํ•˜๋ฉด, Person์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ํ†ต์งธ๋กœ Export ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

class AFX_EXT_CLASS Person {

์ด์ œ MyDLL.cpp ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„์ฃผ ๊ฐ„๋‹จํ•˜์ฃ ?

#include "pch.h"
#include "MyDLL.h"

Person::Person(std::string name, int age, std::string address) {
	this->age = age;
	this->name = name;
	this->address = address;
}

void Person::ShowInfo() {
	OutputDebugStringA(("์ด๋ฆ„: " + name + "\n").c_str());
	OutputDebugStringA(("๋‚˜์ด: " + std::to_string(age) + "\n").c_str());
	OutputDebugStringA(("์ฃผ์†Œ: " + address + "\n").c_str());
}

๋นŒ๋“œ ํ›„ ์ƒ์„ฑ๋œ ํŒŒ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋นŒ๋“œ ํ›„ ์ƒ์„ฑ๋œ ํŒŒ์ผ

MFC ํ™•์žฅ DLL ์‚ฌ์šฉํ•˜๊ธฐ

์ด์ œ ์ƒ์„ฑ๋œ DLL์„ ์‚ฌ์šฉํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์†”๋ฃจ์…˜์— ์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์ƒˆ ํ”„๋กœ์ ํŠธ ์ถ”๊ฐ€

MFC ์•ฑ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ ์ด๋ฆ„์€ MainMFC๋ผ๊ณ  ์ง€์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

MFC ์•ฑ ์ƒ์„ฑ

๊ทธ๋ฆฌ๊ณ  ๋Œ€ํ™” ์ƒ์ž ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋Œ€ํ™” ์ƒ์ž ๊ธฐ๋ฐ˜์œผ๋กœ ์ƒ์„ฑ

MainMFCDlg.h์— ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

// MainMFCDlg.h: ํ—ค๋” ํŒŒ์ผ
//

#pragma once

#include "../CreateDLL/MyDLL.h" //dll header include
#pragma comment (lib, "../Debug/CreateDLL.lib") //dll lib ์ž…๋ ฅ

// CMainMFCDlg ๋Œ€ํ™” ์ƒ์ž
class CMainMFCDlg : public CDialogEx

๊ทธ๋ฆฌ๊ณ  MainMFCDlg.cpp์˜ OnInitDialog()์— 7, 8๋ฒˆ์งธ ์ค„์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ถ”๊ฐ€ํ•˜๋Š” ์œ„์น˜๋Š” ์–ด๋””๋“  ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค. ํŽธ์˜์ƒ OnInitDialog()์—์„œ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

BOOL CMainMFCDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	//...
    
	Person person1("ํ™๊ธธ๋™", 35, "์„œ์šธ์‹œ");
	person1.ShowInfo();

	return TRUE;  // ํฌ์ปค์Šค๋ฅผ ์ปจํŠธ๋กค์— ์„ค์ •ํ•˜์ง€ ์•Š์œผ๋ฉด TRUE๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
}

์ด์ œ ๋นŒ๋“œํ•ด ๋ด…๋‹ˆ๋‹ค. ์•„๋ฌด ์—๋Ÿฌ ์—†์ด ๋นŒ๋“œ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰ ํ›„ ์ถœ๋ ฅ ์ฐฝ์„ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

์‹คํ–‰ ๊ฒฐ๊ณผ

์•„์ฃผ ๊ฐ„๋‹จํ•˜์ฃ ? MFC ํ™•์žฅ DLL์„ ์‚ฌ์šฉํ•˜๋ฉด ์•„์ฃผ ์‰ฝ๊ฒŒ ํด๋ž˜์Šค๋ฅผ DLL๋กœ Export ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ExamDLL.zip
0.08MB

๋ฐ˜์‘ํ˜•