简单例子如下:<br>[blue]DLL中:[/blue]<br>type<br> TTestRec = record<br> A, B: Word;<br> end;<br><br>function TestProc(AArgType, AArg: Longint): Longint; stdcall;<br><br>implementation<br>{$R *.DFM}<br><br>function TestProc(AArgType, AArg: Longint): Longint;<br>var<br> S: string;<br> F: Single;<br>begin<br> Result := 0;<br> case AArgType of<br> 0:<br> Result := TTestRec(AArg).A * TTestRec(AArg).B;<br> 1:<br> begin<br> S := 'OH! my god! ' + PChar(AArg);<br> Result := Longint(@S[1]);<br> end;<br> 2:<br> begin<br> F := Single(Pointer(AArg)^) * 0.5;<br> Result := LongInt(@F);<br> end;<br> end;<br>end;<br>导出:<br>exports<br> TestProc;<br><br>[blue]调用程序:[/blue]<br> TTestRec = record<br> A, B: Word;<br> end;<br> TTestProc = function(AArgType, AArg: Longint): Longint; stdcall;<br><br>var<br> LibHandle: THandle;<br> Proc: TTestProc;<br> A: TTestRec;<br> S: string;<br> F: Single;<br> R: LongInt;<br> ArgType, FuncName: string;<br>begin<br> LibHandle := LoadLibrary('TESTDLL.DLL');<br> if LibHandle = 0 then<br> raise Exception.Create('Unable to Load DLL');<br> try<br> ArgType := Edit1.Text; //输入参数类型,调用者与DLL编写者约定<br> FuncName := 'TestProc';<br> @Proc := GetProcAddress(LibHandle, PChar(FuncName));<br> if not (@Proc = nil) then<br> begin<br> if ArgType = '0' then //0时传入的是TTestRec的记录类型<br> begin<br> A.A := 10;<br> A.B := 20;<br> Edit2.Text := IntToStr(Proc(0, Longint(A)));<br> end;<br> if ArgType = '1' then //1为字符串类型<br> begin<br> S := 'This is Test string';<br> R := Proc(1, LongInt(@S[1]));<br> Edit2.Text := PChar(R);<br> end;<br> if ArgType = '2' then //2为Single<br> begin<br> F := 3.14;<br> R := Proc(2, LongInt(@F));<br> Edit2.Text := FloatToStr(Single(Pointer(R)^));<br> end;<br> end;<br> finally<br> FreeLibrary(LibHandle);<br> end;<br><br>D6 + Win2K 测试通过。