๋ฐ˜์‘ํ˜•

๋ณธ๋ฌธ์˜ ๋ช…์‹œ์  ๋งํฌ๋ฅผ ์ฝ๊ธฐ ์ „์—, ์•„๋ž˜ ๋งํฌ๋ฅผ ๋จผ์ € ์ฝ์–ด์ฃผ์„ธ์š”.

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

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

๋ช…์‹œ์  ๋งํ‚น(Explicit Linking)

๋ช…์‹œ์  ๋งํ‚น์€ ๋Ÿฐํƒ€์ž„ ์ค‘ ํŠน์ • DLL์„ ๋กœ๋“œํ•˜์—ฌ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ํฌ์ธํ„ฐ๋ฅผ ์–ป์–ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋งํฌ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋ง๋กœ๋งŒ ๋ด์„œ๋Š” ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ ๋  ๊ฒƒ ๊ฐ™์€๋ฐ์š”. ๋ฐ”๋กœ ์ƒ˜ํ”Œ ์ฝ”๋“œ๋กœ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (์ƒ˜ํ”Œ ์ฝ”๋“œ๋Š” ๋ณธ๋ฌธ์˜ ํ•˜๋‹จ์— ์ฒจ๋ถ€๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.)

CreateDLL ํ”„๋กœ์ ํŠธ๋Š” ์‚ฌ์น™์—ฐ์‚ฐ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” DLL์„ ์ƒ์„ฑํ•˜๊ณ , MainDLL ํ”„๋กœ์ ํŠธ๋Š” ํ•ด๋‹น DLL์„ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

MyMath.h์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

#pragma once

#ifdef CREATEDLL_EXPORTS
#define MYMATH_DECLSPEC __declspec(dllexport)
#else
#define MYMATH_DECLSPEC __declspec(dllimport)
#endif

extern "C" MYMATH_DECLSPEC double Sum(double a, double b);
extern "C" MYMATH_DECLSPEC double Sub(double a, double b);
extern "C" MYMATH_DECLSPEC double Mul(double a, double b);
extern "C" MYMATH_DECLSPEC double Div(double a, double b);

MainDLL.cpp์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

#include <Windows.h>
#include <iostream>

//#include "../CreateDLL/MyMath.h"

typedef double (*DLL_SUM)(double a, double b);

int main()
{
	std::string path = "..//x64//Debug//CreateDLL.dll";
	HMODULE hDll = ::LoadLibraryA(path.c_str());
	if (hDll != NULL)
	{
		DLL_SUM pFunc = (DLL_SUM)::GetProcAddress(hDll, "Sum");
		double ret = pFunc(5.0, 7.0);
		std::cout << "Result: " << ret << std::endl;

		::FreeLibrary(hDll);
	}
}

์—ฌ๊ธฐ์— ์ฃผ์˜๊นŠ๊ฒŒ ๋ด์•ผ ํ•  ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ๋กœ, 4๋ฒˆ์งธ ์ค„์˜ MyMath.h ํŒŒ์ผ์„ include ํ•˜๋Š” ๋ถ€๋ถ„์ด ์ฃผ์„์ฒ˜๋ฆฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. include ํ•ด๋„ ๋˜๊ณ , ์•ˆ ํ•ด๋„ ๋œ๋‹ค๋Š” ์˜๋ฏธ์ธ๋ฐ์š”. ๋งŒ์•ฝ, ๊ฐœ๋ฐœ์ž๊ฐ€ DLL ํŒŒ์ผ๋งŒ ๊ฐ–๊ณ  ์žˆ๋‹ค๋ฉด DLL ํŒŒ์ผ ๋‚ด์— ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”์ง€ ๋˜๋Š” ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์„ ์–ธ๋˜์–ด ์žˆ๋Š”์ง€ ์–ด๋–ป๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์„๊นŒ์š”? Dependency Walker ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ์„ ์‚ฌ์šฉํ•˜์—ฌ ์•Œ ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์ฒ˜์Œ๋ถ€ํ„ฐ ํ—ค๋” ํŒŒ์ผ์„ ์ฐธ๊ณ ํ•˜๋ฉด ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์„ ์–ธ๋˜์–ด ์žˆ๋Š”์ง€ ๋˜๋Š” ํ•จ์ˆ˜์˜ ์ด๋ฆ„๊ณผ ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฐ˜ํ™˜ํ˜•์„ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ๋กœ, 6๋ฒˆ์งธ ์ค„์˜ typedef ์„ ์–ธ๋ถ€๋ถ„ ์ธ๋ฐ์š”. ๋ช…์‹œ์  ๋งํฌ๋Š” ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ›์•„ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด๊ธฐ ๋•Œ๋ฌธ์—, ํ˜ธ์ถœํ•˜๋ ค๋Š” ํ•จ์ˆ˜์˜ ์›ํ˜•์„ ํ•จ์ˆ˜ ํฌ์ธํ„ฐ ํ˜•ํƒœ๋กœ ์„ ์–ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Sum ํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ double a, double b๋ฅผ ๋ฐ›๊ณ , double์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋งž๊ฒŒ *DLL_SUM ํ•จ์ˆ˜ ํฌ์ธํ„ฐ ์ด๋ฆ„์„ typedef ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ main() ํ•จ์ˆ˜ ๊ตฌํ˜„ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. LoadLibrary() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํŠน์ • ๊ฒฝ๋กœ์˜ DLL์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ , ๋ถˆ๋Ÿฌ์™€์ง„ DLL ํ•ธ๋“ค์€ HMODULE๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ •์ƒ์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์™€์ง€์ง€ ์•Š์•˜๋‹ค๋ฉด NULL์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. NULL ์ฒดํฌ ํ›„ GetProcAddress() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ, DLL ํ•ธ๋“ค์—์„œ ํŠน์ • ํ•จ์ˆ˜ ์ด๋ฆ„์˜ ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ถˆ๋Ÿฌ์™€์ง„ ํ•จ์ˆ˜ ํฌ์ธํ„ฐ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด DLL์˜ ํŠน์ • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ œ์ผ ์ค‘์š”ํ•œ ์ ์€ FreeLibrary()์ž…๋‹ˆ๋‹ค. ๋” ์ด์ƒ DLL์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋ฐ˜๋“œ์‹œ DLL ํ•ธ๋“ค์„ ํ•ด์ œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๋ก 

์—ฌ๊ธฐ๊นŒ์ง€ ์•”์‹œ์  ๋งํฌ์™€ ๋ช…์‹œ์  ๋งํฌ์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ์–ด๋–ค ๋ฐฉ์‹์ด ๋” ํŽธํ•œ์ง€, ํšจ์œจ์ ์ธ์ง€๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์„ ํƒํ•˜๊ธฐ ๋‚˜๋ฆ„์ด๋ฉฐ, ์ด๋Ÿฌํ•œ ๋ฐฉ์‹๋„ ๋ฐฐ์›Œ๋‘๋ฉด ์‹ค๋ฌด์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์จ๋จน์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ExamDLL.zip
0.01MB

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” Class ๊ฐ์ฒด๋ฅผ DLL๋กœ Exportํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•