DLL问题!(50分)

  • 主题发起人 主题发起人 6713827
  • 开始时间 开始时间
6

6713827

Unregistered / Unconfirmed
GUEST, unregistred user!
公共部分<br>[red]TWMSOCK = procedure(ToAdd:TSockAddrIn;var ABuffer;Count:Integer) of [/red]<br>dll部分!<br>[red]procedure TUDP.WMSOCKET(var Message: TMessage);<br>var<br>&nbsp; addr: TSockAddrIn;<br>&nbsp; addrlen: Integer;<br>&nbsp; buffer: array [0..20480] of byte;<br>begin<br>&nbsp; case WSAGetSelectEvent(Message.LParam) of<br>&nbsp; &nbsp; FD_READ:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; addrlen:=sizeof(addr);<br>&nbsp; &nbsp; &nbsp; addrlen:=recvfrom(sock,buffer,20480,0,addr,addrlen);<br>&nbsp; &nbsp; &nbsp; WMSOCK(Addr,Buffer,Addrlen);<br>&nbsp; &nbsp; &nbsp; buffer:=buffer;<br>&nbsp; &nbsp; End;<br>&nbsp; End;<br>End;[/red]<br>exe部分<br>[red]Function UDPSend(ToAdd:TSockAddrIn;Var Buffer;Count:Integer):Integer;<br>&nbsp; stdcall; external 'WinSocketUDP.dll';<br>procedure TForm1.WMSOCK(ToAdd:TSockAddrIn;var ABuffer;Count:Integer);<br>Var A: PChar;<br>&nbsp; &nbsp; B: String;<br>begin<br>&nbsp; Try<br>&nbsp; getmem(A,Count);<br>&nbsp; //move(ABuffer,a,Count);<br>&nbsp; //Showmessage(Inttostr(sizeof(toadd)));<br>&nbsp; Move(ABuffer,b,Count-1);<br>&nbsp; Memo1.Lines.Add('包长度:'+Inttostr(Count)+':'+b);<br>&nbsp; Form1.Caption:=Inttostr(Strtoint(Form1.Caption)+1);<br>&nbsp; except<br>&nbsp; End;<br>end;[/red]<br>当调用过程时,提示【存取地址00403E46为例发生在模块'exe.exe'种.读地址00000000里.】<br>有那位大侠知道怎么处理啊!
 
//delphi 的 move函数要好好理解了<br>确认你ABuffer有地址
 
肯定不是空地址,否则不能正常运行。提示的是入口违规。
 
move(ABuffer,a[1],Count);
 
更加不行了。
 
是更加不行了! 是的因为a没有分配任何空间!<br>你都知道不行了,为何不分配内寸再做处理啊! <br>比如说! A: PChar<br>&nbsp; getmem(A, Size),<br>&nbsp;move(ABuffer[0],a[0],Count);
 
没看出你的程序有什么错误,不会是ABuffer数据超过255的大小溢出?
 
//测试了一下,你的代码改成如下就没问题。<br>procedure Mess(Const ABuffer;Count:Integer); stdcall;<br>var a:String;<br>begin<br>&nbsp; SetLength(a,Count); // 为a分配足够的内存<br>&nbsp; move(ABuffer,Pointer(a)^,Count); //因a是一个String指针变量,而move移动数据的变量却是内存缓冲变量,所以得将a指向的字符缓冲地址传给move才对,而不是将a的地址传给move。<br>end;<br>//在exe中则应该如下调用。<br>var a:array[0..256] of char;<br>begin<br>&nbsp; FillChar(a,sizeof(a),'a');<br>&nbsp; Mess(a,sizeof(a)); // 这里的a也应该是一个缓冲指针,而不能是指针变量。<br>end;
 
还是不行啊。不知道是为什么!!!
 
公共部分<br>TWMSOCK = procedure(ToAdd:TSockAddrIn;var ABuffer;Count:Integer) of <br>dll部分!<br>procedure TUDP.WMSOCKET(var Message: TMessage);<br>var<br>&nbsp; addr: TSockAddrIn;<br>&nbsp; addrlen: Integer;<br>&nbsp; buffer: array [0..20480] of byte;<br>begin<br>&nbsp; case WSAGetSelectEvent(Message.LParam) of<br>&nbsp; &nbsp; FD_READ:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; addrlen:=sizeof(addr);<br>&nbsp; &nbsp; &nbsp; addrlen:=recvfrom(sock,buffer,20480,0,addr,addrlen);<br>&nbsp; &nbsp; &nbsp; WMSOCK(Addr,Buffer,Addrlen);<br>&nbsp; &nbsp; &nbsp; buffer:=buffer;<br>&nbsp; &nbsp; End;<br>&nbsp; End;<br>End;<br>exe部分<br>Function UDPSend(ToAdd:TSockAddrIn;Var Buffer;Count:Integer):Integer;<br>&nbsp; stdcall; external 'WinSocketUDP.dll';<br>procedure TForm1.WMSOCK(ToAdd:TSockAddrIn;var ABuffer;Count:Integer);<br>Var A: PChar;<br>&nbsp; &nbsp; B: String;<br>begin<br>&nbsp; Try<br>// &nbsp;getmem(A,Count);<br>&nbsp; //move(ABuffer,a,Count);<br>&nbsp; //Showmessage(Inttostr(sizeof(toadd)));<br>[red] &nbsp;SetLength(b,Count);<br>&nbsp; Move(ABuffer,Pointer(b)^,Count);[/red]<br>&nbsp; Memo1.Lines.Add('包长度:'+Inttostr(Count)+':'+b);<br>&nbsp; Form1.Caption:=Inttostr(Strtoint(Form1.Caption)+1);<br>&nbsp; except<br>&nbsp; End;<br>end;
 
后退
顶部