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

내용 삭제됨 내용 추가됨
Chobot (토론 | 기여)
잔글 robot Modifying: es:Dynamic Linking Library
편집 요약 없음
2번째 줄:
 
사용하는 방법에는 두 가지가 있는데,
*'''묵시적 링킹'''(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, 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('The result was: ', result)
end.
 
'''C, C++'''
 
정적 링크를 하기 앞서 Example.lib 파일(Exazmple.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("The result was: %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 "The result was: " & 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('The result was: ', result);
end
else
begin
Writeln('ERROR: unable to find DLL function');
ExitCode := 1;
end;
FreeLibrary(hInstLib);
end
else
begin
Writeln('ERROR: Unable to load library');
ExitCode := 1;
end;
ExitCode := 0;
end.
 
'''C, 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("ERROR: unable to load DLL\n");
return 1;
}
// 함수 포인터 얻기
addNumbers = (importFunction)GetProcAddress(hinstLib, "AddNumbers");
if (addNumbers == NULL) {
printf("ERROR: unable to find DLL function\n");
FreeLibrary(hinstLib);
return 1;
}
// 함수 요청하기.
result = addNumbers(1, 2);
// DLL 파일의 로드를 해제한다
FreeLibrary(hinstLib);
// 결과를 보여 준다
printf("The result was: %f\n", result);
return 0;
}
 
 
{{토막글|소프트웨어}}