본문으로 이동

동적 링크 라이브러리: 두 판 사이의 차이

위키백과, 우리 모두의 백과사전.
내용 삭제됨 내용 추가됨
잔글 봇: 같이 보기 문단 추가
 
(사용자 9명의 중간 판 11개는 보이지 않습니다)
1번째 줄: 1번째 줄:
{{위키데이터 속성 추적}}
{{다른 뜻 넘어온다|DLL|조사=은}}
{{다른 뜻 넘어옴|DLL|조사=은}}
'''동적 링크 라이브러리''', 줄여서 '''DLL'''({{lang|en|dynamic-link library}}) [[마이크로소프트 윈도]]에서 구현된 [[동적 라이브러리]]이다. 내부에는 다른 프로그램이 불러서 쓸 수 있는 다양한 [[서브루틴|함수]]들을 가지고 있는데, 확장DLL인 경우는 [[클래스 (컴퓨터 과학)|클래스]]를 가지고 있기도 한다. DLL은 [[컴포넌트 오브젝트 모델|COM]]을 담는 그릇의 역할도 한다.
{{Infobox file format
| name = 동적 링크 라이브러리<br />Dynamic link library
| icon = File:Dll icon.png
| extension = .dll
| mime = application/vnd.microsoft.portable-executable
| type code =
| uniform type = com.microsoft.windows-dynamic-link-library
| magic = MZ
| owner = [[마이크로소프트]]
| genre =
| container for = [[공유 라이브러리]]
| contained by =
| extended from =
| extended to =
| standard =
| url =
}}
'''동적 링크 라이브러리'''({{llang|en|dynamic-link library}}, {{lang|en|DLL}}) [[마이크로소프트 윈도우]]에서 구현된 [[동적 라이브러리]]이다. 내부에는 다른 프로그램이 불러서 쓸 수 있는 다양한 [[서브루틴|함수]]들을 가지고 있는데, 확장DLL인 경우는 [[클래스 (컴퓨터 과학)|클래스]]를 가지고 있기도 한다. DLL은 [[컴포넌트 오브젝트 모델|COM]]을 담는 그릇의 역할도 한다.


사용하는 방법에는 두 가지가 있는데,
사용하는 방법에는 두 가지가 있는데,
13번째 줄: 31번째 줄:


'''델파이'''
'''델파이'''
<source lang="delphi">
<syntaxhighlight lang="delphi">
library Example;
library Example;

// 두 개의 수를 추가하는 함수
// 두 개의 수를 추가하는 함수
function AddNumbers(a, b: Double): Double; cdecl;
function AddNumbers(a, b: Double): Double; cdecl;
21번째 줄: 39번째 줄:
Result := a + b;
Result := a + b;
end;
end;

// 이 함수를 내보낸다
// 이 함수를 내보낸다
exports
exports
AddNumbers;
AddNumbers;

// DLL 초기화 코드. 특별한 핸들링이 필요하지 않다.
// DLL 초기화 코드. 특별한 핸들링이 필요하지 않다.
begin
begin
end.
end.
</syntaxhighlight>
</source>
'''C'''
'''C'''
<source lang="c">
<syntaxhighlight lang="c">
#include <windows.h>
#include <windows.h>

// 이 함수 내보내기
// 이 함수 내보내기
extern "C" __declspec(dllexport) double AddNumbers(double a, double b);
extern "C" __declspec(dllexport) double AddNumbers(double a, double b);

// DLL 초기화 함수
// DLL 초기화 함수
BOOL APIENTRY
BOOL APIENTRY
43번째 줄: 61번째 줄:
return TRUE;
return TRUE;
}
}


// 두 개의 수를 추가하는 함수
// 두 개의 수를 더하는 함수
double AddNumbers(double a, double b)
double AddNumbers(double a, double b)
{
{
return a + b;
return a + b;
}
}
</syntaxhighlight>
</source>


==== DLL 가져오기(임포트) 사용하기 ====
==== DLL 가져오기(임포트) 사용하기 ====
56번째 줄: 74번째 줄:


'''델파이'''
'''델파이'''
<source lang="delphi">
<syntaxhighlight lang="delphi">
program Example;
program Example;
{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}

// 두 개의 수를 추가하는 함수 가져오기
// 두 개의 수를 추가하는 함수 가져오기
function AddNumbers(a, b: Double): Double; cdecl; external 'Example.dll';
function AddNumbers(a, b: Double): Double; cdecl; external 'Example.dll';

var result: Double;
var result: Double;
begin
begin
68번째 줄: 86번째 줄:
Writeln('결과는: ', result)
Writeln('결과는: ', result)
end.
end.
</syntaxhighlight>
</source>
'''C, C++'''
'''C, C++'''


정적 링크를 하기 앞서 Example.lib 파일(Example.dll이 만들어졌다고 하면)이 프로젝트 안에 반드시 있어야 한다. 또, DLL Example.dll을 다음의 코드로 만들어진 .exe 파일이 있는 곳에 복사해야 한다.
정적 링크를 하기 앞서 Example.lib 파일(Example.dll이 만들어졌다고 하면)이 프로젝트 안에 반드시 있어야 한다. 또, DLL Example.dll을 다음의 코드로 만들어진 .exe 파일이 있는 곳에 복사해야 한다.
<source lang="c">
<syntaxhighlight lang="c">
#include <windows.h>
#include <windows.h>
#include <stdio.h>
#include <stdio.h>

// 두 개의 수를 추가하는 함수 가져오기
// 두 개의 수를 추가하는 함수 가져오기
extern "C" __declspec(dllimport) double AddNumbers(double a, double b);
extern "C" __declspec(dllimport) double AddNumbers(double a, double b);

int main(int argc, char **argv) {
int main(int argc, char **argv) {
double result = AddNumbers(1, 2);
double result = AddNumbers(1, 2);
84번째 줄: 102번째 줄:
return 0;
return 0;
}
}
</syntaxhighlight>
</source>


==== 분명한 런타임 링크 사용하기 ====
==== 분명한 런타임 링크 사용하기 ====
90번째 줄: 108번째 줄:


; 마이크로소프트 비주얼 베이직
; 마이크로소프트 비주얼 베이직
<source lang="vb">
<syntaxhighlight lang="vb">
Option Explicit
Option Explicit
Declare Function AddNumbers Lib "Example.dll" _
Declare Function AddNumbers Lib "Example.dll" _
(ByVal a As Double, ByVal b As Double) As Double
(ByVal a As Double, ByVal b As Double) As Double

Sub Main()
Sub Main()
Dim Result As Double
Dim Result As Double
100번째 줄: 118번째 줄:
Debug.Print "결과는: " & Result
Debug.Print "결과는: " & Result
End Sub
End Sub
</syntaxhighlight>
</source>
;델파이
;델파이
<source lang="delphi">
<syntaxhighlight lang="delphi">
program Example;
program Example;
{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}

uses
uses
Windows;
Windows;

Type
Type
AddNumbersProc = function (a, b: Double): Double; cdecl;
AddNumbersProc = function (a, b: Double): Double; cdecl;

var
var
result: Double;
result: Double;
121번째 줄: 139번째 줄:
begin
begin
AddNumbers := GetProcAddress(hInstLib, 'AddNumbers');
AddNumbers := GetProcAddress(hInstLib, 'AddNumbers');

if Assigned(AddNumbers) then
if Assigned(AddNumbers) then
begin
begin
132번째 줄: 150번째 줄:
ExitCode := 1;
ExitCode := 1;
end;
end;

FreeLibrary(hInstLib);
FreeLibrary(hInstLib);
end
end
140번째 줄: 158번째 줄:
ExitCode := 1;
ExitCode := 1;
end;
end;

ExitCode := 0;
ExitCode := 0;
end.
end.
</syntaxhighlight>
</source>
'''C'''
'''C'''
<source lang="c">
<syntaxhighlight lang="c">
#include <windows.h>
#include <windows.h>
#include <stdio.h>
#include <stdio.h>

// DLL 함수 서명
// DLL 함수 서명
typedef double (*importFunction)(double, double);
typedef double (*importFunction)(double, double);

int main(int argc, char **argv)
int main(int argc, char **argv)
{
{
importFunction addNumbers;
importFunction addNumbers;
double result;
double result;

// DLL 파일 불러오기
// DLL 파일 불러오기
HINSTANCE hinstLib = LoadLibrary("Example.dll");
HINSTANCE hinstLib = LoadLibrary("Example.dll");
163번째 줄: 181번째 줄:
return 1;
return 1;
}
}

// 함수 포인터 얻기
// 함수 포인터 얻기
addNumbers = (importFunction)GetProcAddress(hinstLib, "AddNumbers");
addNumbers = (importFunction)GetProcAddress(hinstLib, "AddNumbers");
171번째 줄: 189번째 줄:
return 1;
return 1;
}
}

// 함수 요청하기.
// 함수 요청하기.
result = addNumbers(1, 2);
result = addNumbers(1, 2);

// DLL 파일의 로드를 해제한다
// DLL 파일의 로드를 해제한다
FreeLibrary(hinstLib);
FreeLibrary(hinstLib);

// 결과를 보여 준다
// 결과를 보여 준다
printf("결과는: %f\n", result);
printf("결과는: %f\n", result);

return 0;
return 0;
}
}
</syntaxhighlight>
</source>


== 바깥 고리 ==
== 같이 보기 ==
* [[라이브러리 (컴퓨팅)]]
* [http://msdn2.microsoft.com/en-us/library/ms682589.aspx Dynamic-Link Libraries] - MSDN
* [[링커 (컴퓨팅)]]
* [[로더 (컴퓨팅)]]
* [[정적 라이브러리]]
* [[DLL 지옥]]
== 외부 링크 ==
* [https://web.archive.org/web/20080415234337/http://msdn2.microsoft.com/en-us/library/ms682589.aspx Dynamic-Link Libraries] - MSDN
* [http://support.microsoft.com/kb/815065 What is a DLL?] - 마이크로소프트 지원
* [http://support.microsoft.com/kb/815065 What is a DLL?] - 마이크로소프트 지원


{{윈도 구성 요소}}
{{윈도우 구성 요소}}


[[분류:라이브러리]]
[[분류:라이브러리]]

2024년 6월 2일 (일) 17:13 기준 최신판

동적 링크 라이브러리
Dynamic link library
파일 확장자.dll
인터넷 미디어 타입
application/vnd.microsoft.portable-executable
UTIcom.microsoft.windows-dynamic-link-library
매직 넘버MZ
개발마이크로소프트
다음의 컨테이너공유 라이브러리

동적 링크 라이브러리(영어: dynamic-link library, DLL)는 마이크로소프트 윈도우에서 구현된 동적 라이브러리이다. 내부에는 다른 프로그램이 불러서 쓸 수 있는 다양한 함수들을 가지고 있는데, 확장DLL인 경우는 클래스를 가지고 있기도 한다. DLL은 COM을 담는 그릇의 역할도 한다.

사용하는 방법에는 두 가지가 있는데,

  • 묵시적 링킹(Implicit linking) : 실행 파일 자체에 어떤 DLL의 어떤 함수를 사용하겠다는 정보를 포함시키고 운영체제가 프로그램 실행 시 해당 함수들을 초기화한 후 그것을 이용하는 방법과,
  • 명시적 링킹(Explicit linking) : 프로그램이 실행 중일 때 API를 이용하여 DLL 파일이 있는지 검사하고 동적으로 원하는 함수만 불러와서 쓰는 방법이 있다.

전자의 경우는 컴파일러가 자동으로 해주는 경우가 많으며, 후자의 경우는 사용하고자 하는 DLL이나 함수가 실행 환경에 있을지 없을지 잘 모르는 경우에 사용된다. (때때로 메모리 절약을 위해 쓰이기도 한다.)

프로그램의 예

[편집]

DLL 내보내기(엑스포트) 만들기

[편집]

다음의 예는 DLL로부터 심볼(symbol)을 내보내기 위한 언어의 특정한 묶음을 보여 준다.

델파이

 library Example;

 // 두 개의 수를 추가하는 함수
 function AddNumbers(a, b: Double): Double; cdecl;
 begin
   Result := a + b;
 end;

 // 이 함수를 내보낸다
 exports
 AddNumbers;

 // DLL 초기화 코드. 특별한 핸들링이 필요하지 않다.
 begin
 end.

C

 #include <windows.h>

 // 이 함수 내보내기
 extern "C" __declspec(dllexport) double AddNumbers(double a, double b);

 // DLL 초기화 함수
 BOOL APIENTRY
 DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)
 {
 	return TRUE;
 }


 // 두 개의 수를 더하는 함수
 double AddNumbers(double a, double b)
 {
 	return a + b;
 }

DLL 가져오기(임포트) 사용하기

[편집]

다음의 예는 컴파일할 때 DLL에 맞춰 링크하기 위한 심볼을 불러오기 위해 어떻게 언어의 특정한 묶음을 사용하는지를 보여 준다.

델파이

 program Example;
 {$APPTYPE CONSOLE}

 // 두 개의 수를 추가하는 함수 가져오기
 function AddNumbers(a, b: Double): Double; cdecl; external 'Example.dll';

 var result: Double;
 begin
   result := AddNumbers(1, 2);
   Writeln('결과는: ', result)
 end.

C, C++

정적 링크를 하기 앞서 Example.lib 파일(Example.dll이 만들어졌다고 하면)이 프로젝트 안에 반드시 있어야 한다. 또, DLL Example.dll을 다음의 코드로 만들어진 .exe 파일이 있는 곳에 복사해야 한다.

 #include <windows.h>
 #include <stdio.h>

 // 두 개의 수를 추가하는 함수 가져오기
 extern "C" __declspec(dllimport) double AddNumbers(double a, double b);

 int main(int argc, char **argv) {
 	double result = AddNumbers(1, 2);
 	printf("결과는: %f\n", result);
 	return 0;
 }

분명한 런타임 링크 사용하기

[편집]

다음의 예는 언어의 특정한 WIN32 API 묶음을 사용하여 런타임 로딩/링크 체계를 사용하는 방법을 보여 준다.

마이크로소프트 비주얼 베이직
 Option Explicit
 Declare Function AddNumbers Lib "Example.dll" _
 (ByVal a As Double, ByVal b As Double) As Double

 Sub Main()
 Dim Result As Double
 Result = AddNumbers(1, 2)
 Debug.Print "결과는: " & Result
 End Sub
델파이
 program Example;
 {$APPTYPE CONSOLE}

 uses
   Windows;

 Type
   AddNumbersProc = function (a, b: Double): Double; cdecl;

 var
   result: Double;
   hInstLib: HMODULE;
   AddNumbers: AddNumbersProc;
 begin
   hInstLib := LoadLibrary('example.dll');
   if hInstLib <> 0 then
   begin
     AddNumbers := GetProcAddress(hInstLib, 'AddNumbers');

     if Assigned(AddNumbers) then
     begin
       result := AddNumbers(1, 2);
       Writeln('결과는: ', result);
     end
     else
     begin
       Writeln('오류: DLL 함수를 찾을 수 없습니다');
       ExitCode := 1;
     end;

     FreeLibrary(hInstLib);
   end
   else
   begin
     Writeln('오류: 라이브러리를 불러올 수 없습니다');
     ExitCode := 1;
   end;

   ExitCode := 0;
 end.

C

 #include <windows.h>
 #include <stdio.h>

 // DLL 함수 서명
 typedef double (*importFunction)(double, double);

 int main(int argc, char **argv)
 {
 	importFunction addNumbers;
 	double result;

 	// DLL 파일 불러오기
 	HINSTANCE hinstLib = LoadLibrary("Example.dll");
 	if (hinstLib == NULL) {
 		printf("오류: DLL을 불러올 수 없습니다\n");
 		return 1;
 	}

 	// 함수 포인터 얻기
 	addNumbers = (importFunction)GetProcAddress(hinstLib, "AddNumbers");
 	if (addNumbers == NULL) {
 		printf("오류: DLL 함수를 찾을 수 없습니다\n");
                FreeLibrary(hinstLib);
 		return 1;
 	}

 	// 함수 요청하기.
 	result = addNumbers(1, 2);

 	// DLL 파일의 로드를 해제한다
 	FreeLibrary(hinstLib);

 	// 결과를 보여 준다
 	printf("결과는: %f\n", result);

 	return 0;
 }

같이 보기

[편집]

외부 링크

[편집]