关于DLL问题(50分)

  • 主题发起人 主题发起人 yytxt
  • 开始时间 开始时间
Y

yytxt

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位, DLL中的函数难道不能返回一些值吗?<br>比如我写了如下一个DLL:<br>project1文件:<br>library TestDLL;<br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; TestDLLF in '../TestDLLF.pas';<br>{$R *.res}<br>exports<br>&nbsp; MyTestDLL;<br>begin<br>end.<br><br>TestDLLF文件:<br>unit TestDLLF;<br><br>interface<br><br>uses<br>&nbsp; ShareMem, Classes, StrUtils, SysUtils, IdGlobal, Dialogs;<br><br>&nbsp; function MyTestDLL: string; stdcall;<br><br>implementation<br><br>function MyTestDLL: string;<br>begin<br>&nbsp; Result := 'True';<br>end;<br><br>end.<br><br>测试程序:<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; end;<br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; function MyTestDLL: string; stdcall; external 'TestDLL.DLL';<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; OneHandle: THandle;<br>&nbsp; PFunc: TFarProc;<br>begin<br>&nbsp; OneHandle := LoadLibrary('TestDLL.dll'); <br>&nbsp; try<br>&nbsp; &nbsp; PFunc := nil;<br>&nbsp; &nbsp; if OneHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; PFunc := GetProcAddress(OneHandle, 'MyTestDLL');<br>&nbsp; &nbsp; if not (PFunc = nil) then<br>&nbsp; &nbsp; &nbsp; ShowMessage(MyTestDLL)<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; RaiseLastWin32Error;<br>&nbsp; finally<br>&nbsp; &nbsp; FreeLibrary(OneHandle);<br>&nbsp; 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>&nbsp; ShareMem, Classes, StrUtils, SysUtils, IdGlobal, Dialogs;<br><br>&nbsp; function MyTestDLL: string; stdcall;<br><br>implementation<br><br>function MyTestDLL: string;<br>begin<br>&nbsp; ShowMessage('True');<br>end;<br><br>end.<br><br>相应的测试程序为:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; OneHandle: THandle;<br>&nbsp; PFunc: TFarProc;<br>begin<br>&nbsp; OneHandle := LoadLibrary('TestDLL.dll'); <br>&nbsp; try<br>&nbsp; &nbsp; PFunc := nil;<br>&nbsp; &nbsp; if OneHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; PFunc := GetProcAddress(OneHandle, 'MyTestDLL');<br>&nbsp; &nbsp; if not (PFunc = nil) then<br>&nbsp; &nbsp; &nbsp; MyTestDLL<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; RaiseLastWin32Error;<br>&nbsp; finally<br>&nbsp; &nbsp; FreeLibrary(OneHandle);<br>&nbsp; end;<br>end;<br>这样运行测试程序,则不会出错。<br><br>请问各位高手,究竟问题出在什么地方?
 
function MyTestDLL: pchar;试试
 
同意楼上!
 
代码够乱的<br>首先在dll中<br>function MyTestDLL: string; stdcall;<br>implementation<br>function MyTestDLL: string;<br>你在声明中用了stdcall,在实现中就应该加上<br>第二<br>在测试程序TestDLLF中<br>你同时用到了静态载入 function MyTestDLL: string; stdcall;external 'TestDLL.DLL';<br>和动态载入<br>&nbsp;OneHandle := LoadLibrary('TestDLL.dll'); <br>&nbsp;try<br>&nbsp; &nbsp;PFunc := nil;<br>&nbsp; &nbsp;if OneHandle &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp;PFunc := GetProcAddress(OneHandle, 'MyTestDLL');<br>&nbsp; &nbsp;if not (PFunc = nil) then<br>&nbsp; &nbsp; &nbsp;MyTestDLL<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp;RaiseLastWin32Error;<br>&nbsp;finally<br>&nbsp; &nbsp;FreeLibrary(OneHandle);<br>&nbsp;end;<br>在动态载入的语句中<br>&nbsp;if not (PFunc = nil) then<br>&nbsp; &nbsp; &nbsp;MyTestDLL<br>&nbsp; &nbsp;else<br>成功的话就调用静态载入的函数,动态载入等于没起作用<br>应该改为<br>&nbsp;if not (PFunc = nil) then<br>&nbsp; &nbsp; PFunc <br>&nbsp; &nbsp;else<br>在声明部分应该该为<br>uses<br>&nbsp;ShareMem, Classes, StrUtils, SysUtils, IdGlobal, Dialogs;<br>implementation<br>var <br>&nbsp; &nbsp;PFunc:function:string;stdcall;<br>第三<br>如果在dll中要用到字符串变量最好定义成pchar类型<br><br><br><br>
 
如果加了ShareMem单元,请同时发布Borlndmm.dll,这样试试??
 
我按各位所给出的提示都试了一遍,可是还是存在地址访问错误或非法指针操作等错误。<br>:(<br>苦恼!<br>不知还有哪位可以提供一些思路。问题究竟出在哪儿呢?
 
后退
顶部