S
snopy
Unregistered / Unconfirmed
GUEST, unregistred user!
为什么我用静态调用 DLL 里面的函数就没有问题, 如改用动态调用就出现如下问题<br>(存取地址00403E46违例发生在模块 'Project1.exe'中, 读在地址00D71E68里.)<br>源码如下:<br><br>主程序文件:<br>type<br> TLanCardID = function:string;stdcall;<br> TGetNetBIOSAddress = function:string;stdcall;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> proc:TLanCardID;<br> aptr:TFarproc;<br> Moudle:THandle;<br> s:string;<br>begin<br> Moudle:= Loadlibrary('IntDLL.dll');<br> if Moudle <> 0 then<br> try<br> begin<br> aptr:= GetProcAddress(Moudle,'GetIP');<br> if aptr <> nil then<br> begin<br> proc:=TLanCardID(aptr);<br> s := proc;<br> Edit1.Text:= '本机IP地址: '+ s;<br> end;<br> end;<br> finally<br> Freelibrary(Moudle);<br> end;<br>end;<br><br>procedure TForm1.Button5Click(Sender: TObject);<br>var<br> proc1:TGetNetBIOSAddress;<br> aptr1:TFarproc;<br> Moudle1:Integer;<br> s1:string;<br>begin<br> Moudle1:= Loadlibrary('IntDLL.dll');<br> if Moudle1 <> 0 then<br> try<br> begin<br> aptr1:= GetProcAddress(Moudle1,'GetNetBIOSAddress');<br> if aptr1 <> nil then<br> begin<br> proc1:=TGetNetBIOSAddress(aptr1);<br> s1 := proc1;<br> Edit3.Text:= 'NetBios地址: '+ s1;<br> end;<br> end;<br> finally<br> Freelibrary(Moudle1);<br> end;<br>end;<br><br><br>DLL文件:<br>library IntDLL;<br><br>uses<br> SysUtils,<br> Classes,<br> netfun in 'netfun.pas';<br><br>{$R *.res}<br><br>exports<br> GetNetBIOSAddress, <br> GetIP;<br><br>begin<br>end.<br><br>////////////////////////////////////<br>netfun文件:<br>unit netfun;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics,Controls, <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> ncb : TNCB;<br> status : TAdapterStatus;<br> lanenum : TLanaEnum;<br><br> procedure ResetAdapter (num : char);<br> begin<br> fillchar(ncb,sizeof(ncb),0);<br> ncb.ncb_command:=char(NCBRESET);<br> ncb.ncb_lana_num:=num;<br> Netbios(@ncb);<br> end;<br><br>var<br> lanNum : char;<br> address : record<br> part1 : Longint;<br> part2 : Word;//Smallint;<br> end absolute status;<br>begin<br> Result:='';<br><br> fillchar(ncb,sizeof(ncb),0);<br> ncb.ncb_command:=char(NCBENUM);<br> ncb.ncb_buffer:=@lanenum;<br> ncb.ncb_length:=sizeof(lanenum);<br> Netbios(@ncb);<br><br> if lanenum.length=#0 then exit;<br> lanNum:=lanenum.lana[0];<br><br> ResetAdapter(lanNum);<br><br> fillchar(ncb,sizeof(ncb),0);<br> ncb.ncb_command:=char(NCBASTAT);<br> ncb.ncb_lana_num:=lanNum;<br> ncb.ncb_callname[0]:='*';<br> ncb.ncb_buffer:=@status;<br> ncb.ncb_length:=sizeof(status);<br> Netbios(@ncb);<br> ResetAdapter(lanNum);<br><br> Result:=Format('%x%x',[address.part1,address.part2]);<br>end;<br><br>function GetIP:string;stdcall;<br>var<br> WSAData:TWSAData;<br> HostName:array[0..MAX_COMPUTERNAME_LENGTH] of Char;<br> HostEntHostEnt;<br> LastIPInAddr; <br> IPList:^PInAddr; <br>begin <br> result:=''; <br> if 0=WSAStartup(MAKEWORD(1,1), WSAData) then <br> try<br> if 0=gethostname(HostName, MAX_COMPUTERNAME_LENGTH+1) then<br> begin<br> HostEnt:=gethostbyname(HostName);<br> if HostEnt<>nil then<br> begin<br> IPList:=Pointer(HostEnt^.h_addr_list);<br> repeat<br> LastIP:=IPList^;<br> INC(IPList);<br> until<br> IPList^=nil;<br> if LastIP<>nil then<br> result:=inet_ntoa(LastIP^);<br> end;<br> end;<br> finally<br> WSACleanup;<br> end;<br>end;<br><br>end.