동적 링크 라이브러리

(DLL에서 넘어옴)

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

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

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

  • 묵시적 링킹(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;
 }

외부 링크 편집