关于 DLL 的问题!!!(20分)

  • 主题发起人 主题发起人 snopy
  • 开始时间 开始时间
S

snopy

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么我用静态调用 DLL 里面的函数就没有问题, 如改用动态调用就出现如下问题<br>(存取地址00403E46违例发生在模块 'Project1.exe'中, 读在地址00D71E68里.)<br>源码如下:<br><br>主程序文件:<br>type<br>&nbsp; TLanCardID = function:string;stdcall;<br>&nbsp; TGetNetBIOSAddress = function:string;stdcall;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; proc:TLanCardID;<br>&nbsp; aptr:TFarproc;<br>&nbsp; Moudle:THandle;<br>&nbsp; s:string;<br>begin<br>&nbsp; Moudle:= Loadlibrary('IntDLL.dll');<br>&nbsp; if Moudle &lt;&gt; 0 then<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; aptr:= GetProcAddress(Moudle,'GetIP');<br>&nbsp; &nbsp; &nbsp; &nbsp; if aptr &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc:=TLanCardID(aptr);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s := proc;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Edit1.Text:= '本机IP地址: '+ s;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; Freelibrary(Moudle);<br>&nbsp; &nbsp; end;<br>end;<br><br>procedure TForm1.Button5Click(Sender: TObject);<br>var<br>&nbsp; proc1:TGetNetBIOSAddress;<br>&nbsp; aptr1:TFarproc;<br>&nbsp; Moudle1:Integer;<br>&nbsp; s1:string;<br>begin<br>&nbsp; Moudle1:= Loadlibrary('IntDLL.dll');<br>&nbsp; if Moudle1 &lt;&gt; 0 then<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; aptr1:= GetProcAddress(Moudle1,'GetNetBIOSAddress');<br>&nbsp; &nbsp; &nbsp; &nbsp; if aptr1 &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proc1:=TGetNetBIOSAddress(aptr1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s1 := proc1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Edit3.Text:= 'NetBios地址: '+ s1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; Freelibrary(Moudle1);<br>&nbsp; &nbsp; end;<br>end;<br><br><br>DLL文件:<br>library IntDLL;<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; netfun in 'netfun.pas';<br><br>{$R *.res}<br><br>exports<br>&nbsp; GetNetBIOSAddress, <br>&nbsp; GetIP;<br><br>begin<br>end.<br><br>////////////////////////////////////<br>netfun文件:<br>unit netfun;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics,Controls, &nbsp; &nbsp; &nbsp;<br>Forms, Dialogs, wininet, Winsock, NB30, Registry, ActiveX;<br><br>function GetNetBIOSAddress : string;stdcall;<br><br>function GetIP:string;stdcall;<br><br>implementation<br><br>function GetNetBIOSAddress : string;stdcall;<br>var<br>&nbsp; ncb &nbsp;: TNCB;<br>&nbsp; status &nbsp;: TAdapterStatus;<br>&nbsp; lanenum : TLanaEnum;<br><br>&nbsp; procedure ResetAdapter (num : char);<br>&nbsp; begin<br>&nbsp; &nbsp; fillchar(ncb,sizeof(ncb),0);<br>&nbsp; &nbsp; ncb.ncb_command:=char(NCBRESET);<br>&nbsp; &nbsp; ncb.ncb_lana_num:=num;<br>&nbsp; &nbsp; Netbios(@ncb);<br>&nbsp; end;<br><br>var<br>&nbsp; lanNum &nbsp;: char;<br>&nbsp; address : record<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;part1 : Longint;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;part2 : Word;//Smallint;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end absolute status;<br>begin<br>&nbsp; Result:='';<br><br>&nbsp; fillchar(ncb,sizeof(ncb),0);<br>&nbsp; &nbsp; ncb.ncb_command:=char(NCBENUM);<br>&nbsp; &nbsp; ncb.ncb_buffer:=@lanenum;<br>&nbsp; &nbsp; ncb.ncb_length:=sizeof(lanenum);<br>&nbsp; Netbios(@ncb);<br><br>&nbsp; if lanenum.length=#0 then exit;<br>&nbsp; lanNum:=lanenum.lana[0];<br><br>&nbsp; ResetAdapter(lanNum);<br><br>&nbsp; fillchar(ncb,sizeof(ncb),0);<br>&nbsp; &nbsp; ncb.ncb_command:=char(NCBASTAT);<br>&nbsp; &nbsp; ncb.ncb_lana_num:=lanNum;<br>&nbsp; &nbsp; ncb.ncb_callname[0]:='*';<br>&nbsp; &nbsp; ncb.ncb_buffer:=@status;<br>&nbsp; &nbsp; ncb.ncb_length:=sizeof(status);<br>&nbsp; Netbios(@ncb);<br>&nbsp; ResetAdapter(lanNum);<br><br>&nbsp; Result:=Format('%x%x',[address.part1,address.part2]);<br>end;<br><br>function GetIP:string;stdcall;<br>var<br>&nbsp; WSAData:TWSAData;<br>&nbsp; HostName:array[0..MAX_COMPUTERNAME_LENGTH] of Char;<br>&nbsp; HostEnt:PHostEnt;<br>&nbsp; LastIP:PInAddr; <br>&nbsp; IPList:^PInAddr; <br>begin <br>&nbsp; result:=''; <br>&nbsp; if 0=WSAStartup(MAKEWORD(1,1), WSAData) then <br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; if 0=gethostname(HostName, MAX_COMPUTERNAME_LENGTH+1) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HostEnt:=gethostbyname(HostName);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if HostEnt&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IPList:=Pointer(HostEnt^.h_addr_list);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LastIP:=IPList^;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INC(IPList);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; until<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IPList^=nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if LastIP&lt;&gt;nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=inet_ntoa(LastIP^);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; WSACleanup;<br>&nbsp; &nbsp; &nbsp; end;<br>end;<br><br>end.
 
不要返回String<br>Dll里最好用PChar代替String
 
TO:pihome <br><br>怎样改,能说一说吗??<br>
 
既然是静态调用,你调用了lib文件了吗?我好像没有看到啊。<br><br>不还是动态调用?动态调用就不一样了啦。
 
TLanCardID = function:pchar;stdcall;<br>Edit1.Text:= '本机IP地址: '+ strpas(proc);<br><br>function GetIP:string;stdcall;<br>result:=PChar(inet_ntoa(LastIP^));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
 
TO:reallike<br>我的这些源码是动态调用的, 静态调用的源码没有问题我就不放出来, 你当然看不到.
 
TO:pihome <br>strpas 是什么, 有什么作用???<br>
 
接受答案了.
 

Similar threads

后退
顶部