Y
yytxt
Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位, DLL中的函数难道不能返回一些值吗?<br>比如我写了如下一个DLL:<br>project1文件:<br>library TestDLL;<br>uses<br> SysUtils,<br> Classes,<br> TestDLLF in '../TestDLLF.pas';<br>{$R *.res}<br>exports<br> MyTestDLL;<br>begin<br>end.<br><br>TestDLLF文件:<br>unit TestDLLF;<br><br>interface<br><br>uses<br> ShareMem, Classes, StrUtils, SysUtils, IdGlobal, Dialogs;<br><br> function MyTestDLL: string; stdcall;<br><br>implementation<br><br>function MyTestDLL: string;<br>begin<br> Result := 'True';<br>end;<br><br>end.<br><br>测试程序:<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> end;<br>var<br> Form1: TForm1;<br> function MyTestDLL: string; stdcall; external 'TestDLL.DLL';<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> OneHandle: THandle;<br> PFunc: TFarProc;<br>begin<br> OneHandle := LoadLibrary('TestDLL.dll'); <br> try<br> PFunc := nil;<br> if OneHandle <> 0 then<br> PFunc := GetProcAddress(OneHandle, 'MyTestDLL');<br> if not (PFunc = nil) then<br> ShowMessage(MyTestDLL)<br> else<br> RaiseLastWin32Error;<br> finally<br> FreeLibrary(OneHandle);<br> end;<br>end;<br><br>end.<br><br>运行测试程序时,TestDLL的内容执行了,但在执行结束后出现了“Invalid Pointer Operation”的错误。<br><br>如果将TestDLLF文件改为:<br>unit TestDLLF;<br><br>interface<br><br>uses<br> ShareMem, Classes, StrUtils, SysUtils, IdGlobal, Dialogs;<br><br> function MyTestDLL: string; stdcall;<br><br>implementation<br><br>function MyTestDLL: string;<br>begin<br> ShowMessage('True');<br>end;<br><br>end.<br><br>相应的测试程序为:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> OneHandle: THandle;<br> PFunc: TFarProc;<br>begin<br> OneHandle := LoadLibrary('TestDLL.dll'); <br> try<br> PFunc := nil;<br> if OneHandle <> 0 then<br> PFunc := GetProcAddress(OneHandle, 'MyTestDLL');<br> if not (PFunc = nil) then<br> MyTestDLL<br> else<br> RaiseLastWin32Error;<br> finally<br> FreeLibrary(OneHandle);<br> end;<br>end;<br>这样运行测试程序,则不会出错。<br><br>请问各位高手,究竟问题出在什么地方?