请大家帮我看看这调用dll文件为什么老出错?(25分)

  • 主题发起人 主题发起人 11830
  • 开始时间 开始时间
1

11830

Unregistered / Unconfirmed
GUEST, unregistred user!
-------------------------------dll文件开始-----------------------------<br>library testdll;<br><br><br>{ Important note about DLL memory management: ShareMem must be the<br>&nbsp; first unit in your library's USES clause AND your project's (select<br>&nbsp; Project-View Source) USES clause if your DLL exports any procedures or<br>&nbsp; functions that pass strings as parameters or function results. This<br>&nbsp; applies to all strings passed to and from your DLL--even those that<br>&nbsp; are nested in records and classes. ShareMem is the interface unit to<br>&nbsp; the BORLNDMM.DLL shared memory manager, which must be deployed along<br>&nbsp; with your DLL. To avoid using BORLNDMM.DLL, pass string information<br>&nbsp; using PChar or ShortString parameters. }<br><br>uses<br>&nbsp; SysUtils,Classes,Dialogs;<br><br>{$R *.RES}<br>//功能是返回某字符在字符串中的位置<br>function InStr(SourceStr:PChar;Ch:Char):Integer;stdcall;<br>var Len,I:Integer;<br>begin<br>&nbsp; Len:=StrLen(SourceStr);<br>&nbsp; for i:=0 to Len-1 do<br>&nbsp; &nbsp; &nbsp;if SourceStr=Ch then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:=i;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;Result:=-1;<br>end;<br><br>exports<br>&nbsp; instr index 1 name 'MyInStr' resident;<br>begin<br>end.<br>-------------------------------dll文件结束-----------------------------<br><br>-------------------------------exe文件开始-----------------------------<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; procedure Edit2KeyPress(Sender: TObject; var Key: Char);<br><br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br><br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br><br><br>implementation<br><br>{$R *.DFM}<br><br>type<br>&nbsp; &nbsp;TinStr=function(source:pchar;check:char):integer; &nbsp; <br>procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);<br>var<br>Order :integer;<br>txt:PChar;<br>PFunc:TFarProc;<br>Moudle:thandle;<br>begin<br>Moudle:=loadlibrary('D:/dll/testdll.dll');<br>if Moudle&gt;32 then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;Edit2.Text:='';<br>&nbsp; &nbsp; &nbsp;PFunc:=GetProcAddress(Moudle,'instr');<br>&nbsp; &nbsp; &nbsp;txt:=StrAlloc(80);<br>&nbsp; &nbsp; &nbsp;txt:=strpcopy(txt,Edit1.Text);<br>ShowMessage('e');<br>&nbsp; &nbsp; &nbsp;Order:=tInStr(PFunc)(txt,key);<br>ShowMessage('f');<br>&nbsp; &nbsp; &nbsp;if Order=-1 then Label1.Caption:='没有找到这个字符' else Label1.Caption:='位于第'+inttostr(Order+1)+'位';<br>&nbsp; end;<br>FreeLibrary(Moudle);<br>end;<br><br>end.<br>-------------------------------exe文件结束-----------------------------
 
你的dll中的函数InStr的引出名为MyInStr,而不是InStr。<br>所以你的获得函数地址的地方应当写为<br>GetProcAddress(Module, 'MyInStr')<br>这样就OK了。
 
接受答案了.
 

Similar threads

后退
顶部