You can import a routine under a different name from the one it has in the DLL. If you do this, specify the original name in the external directive:external stringConstant1 name stringConstant2;where the first stringConstant gives the name of the .DLL file and the second stringConstant is the routine抯 original name. For example, the following declaration imports a function from USER32.DLL (part of the Windows API).function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer; stdcall; external 'user32.dll' name 'MessageBoxA';The function抯 original name is MessageBoxA, but it is imported as MessageBox. Instead of a name, you can use a number to identify the routine you want to import:external stringConstant index integerConstant;where integerConstant is the routine抯 index in the DLL export table. In your importing declaration, be sure to match the exact spelling and case of the routine抯 name. Later, when you call the imported routine, the name is case-insensitive.
我不是说了吗,Windows中有的是例子,下面就是一个典型,我稍微修改了一下。你可以慢慢研究。 interface function DrawText(hDC: HDC; lpString: PChar; nCount: Integer; var lpRect: TRect; uFormat: UINT): Integer; stdcall;implementation function DrawText; external 'user32.dll' name 'DrawTextA';
Unit CallDll;//假定一个TESTDLL.DLL输出了一个TestFunc,其声明如下: //function TestFunc(S: string): string; stdcall;uses ...;type TTestFunc = function(S: string): string; stdcall; function TestFunc(S: string): string;implementationvar hTestDll: THandle; pTestFunc: TTestFunc;procedure LoadDll; begin if hTestDll = 0 then hTestDll := LoadLibrary('TestDll.DLL'); end;procedure UnloadDll; begin if hTestDll <> 0 then begin FreeLibrary(hTestDll); hTestDll := 0; pTestFunc := nil; end; end;function TestFunc(S: string): string; begin LoadDll; if @pTestFunc = nil then @pTestFunc := GetProcAddress(hTestDll,'TestFunc'); if @pTestFunc = nil then Result := '' else Result := pTestFunc(S); end;initialization //LoadDll;finalization UnloadDll;end;在你的主调工程里引入该文件,就可以调用该Dll了。
我学delphi不久,我看了还是不是很明白,希望你能举个例子!谢谢!!
我是菜鸟,我还是不明白,能兴个例子吗?
stdcall; external 'user32.dll' name 'MessageBoxA';The function抯 original name is MessageBoxA, but it is imported as MessageBox.
Instead of a name, you can use a number to identify the routine you want to import:external stringConstant index integerConstant;where integerConstant is the routine抯 index in the DLL export table.
In your importing declaration, be sure to match the exact spelling and case of the routine抯 name. Later, when you call the imported routine, the name is case-insensitive.
interface
function DrawText(hDC: HDC; lpString: PChar; nCount: Integer;
var lpRect: TRect; uFormat: UINT): Integer; stdcall;implementation
function DrawText; external 'user32.dll' name 'DrawTextA';
//function TestFunc(S: string): string; stdcall;uses
...;type
TTestFunc = function(S: string): string; stdcall; function TestFunc(S: string): string;implementationvar
hTestDll: THandle;
pTestFunc: TTestFunc;procedure LoadDll;
begin
if hTestDll = 0 then
hTestDll := LoadLibrary('TestDll.DLL');
end;procedure UnloadDll;
begin
if hTestDll <> 0 then
begin
FreeLibrary(hTestDll);
hTestDll := 0;
pTestFunc := nil;
end;
end;function TestFunc(S: string): string;
begin
LoadDll;
if @pTestFunc = nil then
@pTestFunc := GetProcAddress(hTestDll,'TestFunc');
if @pTestFunc = nil then
Result := ''
else
Result := pTestFunc(S);
end;initialization
//LoadDll;finalization
UnloadDll;end;在你的主调工程里引入该文件,就可以调用该Dll了。