|
Delphi 로 작성된 DLL 을 C++ Builder 에서 동적으로 불러와 사용하려 합니다.
Delphi 에서 export; stdcall; 로 export를 했고 Builder 에서 LoadLibraryEx 로
DLL 로딩까지는 되었는데 함수를 호출하기위해 GetProcAddress 를 수행하면 NULL이 넘어오더군요.
그래서 implib 프로그램으로 .lib 파일을 만들어 정적으로 DLL 을 올리고 함수를 호출 하였는데
함수 호출이 정상적으로 호출이 되고 동적로딩을 시도했던 코드도 동작을 하는데 결과가 이상하게 나오더군요.
뭐가 문제인 거죠?
어떻게 하면 델파이 에서 만든 Dll 을 Builder 에서 동적으로 사용할 수 있나요?
다음은 제가 테스트한 소스 중 일부 입니다.
---------------------------------------------------------------------------------------
Delphi DLL
---------------------------------------------------------------------------------------
:
uses SysUtils, Classes, uDelphiDLL in 'uDelphiDLL.pas';
{$R *.res}
exports
DelphiDLLFunc_01 name 'DelphiDLLFunc_01'
, DelphiDLLFunc_Sum name 'DelphiDLLFunc_Sum'
;
:
---------------------------------------------------------------------------------------
uDelphiDLL.pas
---------------------------------------------------------------------------------------
unit uDelphiDLL;
interface
uses
windows, dialogs;
procedure DelphiDLLFunc_01(); export; stdcall;
function DelphiDLLFunc_Sum(a:integer; b:integer; var c:integer):integer; export; stdcall;
implementation
procedure DelphiDLLFunc_01();
begin
ShowMessage('Delhpi DLL');
end;
function DelphiDLLFunc_Sum(a:integer; b:integer; var c:integer):integer;
begin
c := a + b;
Result:= a + b;
end;
end.
---------------------------------------------------------------------------------------
BuilderCaller
---------------------------------------------------------------------------------------
typedef void (*TDelphiDLLFunc_01)();
typedef int (*TDelphiDLLFunc_Sum)(int, int, int&);
void __fastcall TForm1::btnDynamicCallClick(TObject *Sender)
{
// Dynamic call
HINSTANCE hDLL = NULL;
hDLL = LoadLibraryEx("DelphiDLL.dll", NULL, LOAD_LIBRARY_AS_DATAFILE);
if (hDLL)
{
TDelphiDLLFunc_01 DLLFunc_01;
DLLFunc_01 = (TDelphiDLLFunc_01)GetProcAddress(hDLL, "DelphiDLLFunc_01");
if (DLLFunc_01)
{
DLLFunc_01();
}
else
ShowMessage("Can't find function 1");
TDelphiDLLFunc_Sum DLLFunc_Sum;
DLLFunc_Sum = (TDelphiDLLFunc_Sum)GetProcAddress(hDLL, "DelphiDLLFunc_Sum");
if (DLLFunc_Sum)
{
int sum1, sum2;
sum2 = DLLFunc_Sum(10, 20, sum1);
ShowMessage("Sum = " + IntToStr(sum1) + ", " + IntToStr(sum2));
}
else
ShowMessage("Can't find function 2");
}
else
ShowMessage("Can't load library");
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllimport) void __stdcall DelphiDLLFunc_01();
extern "C" __declspec(dllimport) int __stdcall DelphiDLLFunc_Sum(int, int, int&);
void __fastcall TForm1::btnStaticCallClick(TObject *Sender)
{
// Static call
DelphiDLLFunc_01();
int sum1, sum2;
sum2 = DelphiDLLFunc_Sum(10, 20, sum1);
ShowMessage("Sum = " + IntToStr(sum1) + ", " + IntToStr(sum2));
// 위 두 함수(DelphiDLLFunc_01, DelphiDLLFunc_Sum)를 활성화 하면
// 위 함수의 동적 호출이 이루어 지지만 계산 결과가 이상하게 나오고
// 두 함수를 주석처리하여 수행하지 않으면
// 위 함수의 동적 호출시 함수를 찾을 수 없다는 메세지가 나옵니다.
}
//---------------------------------------------------------------------------
(추가내용)
값이 이상하게 나오는 문제는 동적 호출의 함수포인터 선언에 __stdcall 을 지정해 주니 해결되네요..
typedef void (__stdcall *TDelphiDLLFunc_01)();
typedef int (__stdcall *TDelphiDLLFunc_Sum)(int, int, int&);
|