请教一个编译错误:常量对象不能作为变量参数传递

qiushuifuye 2010-01-27 10:45:55
最近看到一段源码,用于局域网内部,IP段搜索对应的MAC地址,可不解的是同样的程序,将其单元文件加载到其他程序里便出现编译错误:常量对象不能作为变量参数传递。(文中红字标识部门报错)
部分代码如下:(具体程序源码我已经上传了,供大家参考)

……
……
{****************** SOCKET通讯 *****************}
{始化SOCKET}
Procedure WinSockInital(Handle: HWnd);
begin
{ 1 初始化SOCKET}
TF_stop:=false;
if WSAStartup(2, TempWSAData)=1 then
begin
showmessage('Socket初始化出错');
exit
end;
{若是用UDP通信,则用}
sockfd:=Socket(AF_INET,SOCK_DGRAM,0);

FSockAddrIn.SIn_Family := AF_INET;
FSockAddrIn.SIn_Port := htons(NBTPORT); {137端口}

{Socket的信息传递--》From.Handle 窗体, FD_READ 在读就绪的时候, WM_SOCK 自定义消息号}
WSAAsyncSelect(sockfd, Handle , WM_SOCK, FD_READ);
{创建事件}
WAIT_ACK_EVENT:=CreateEvent(nil,true,false,pchar('WAIT_ACK') );
ResetEvent(WAIT_ACK_EVENT);
end;
{关闭SOCKET}
Procedure WinSockClose();
begin
TF_stop:=True;
CloseSocket(sockfd); {closesocket函数用来关闭一个描述符为sockfd套接字}
WSACleanup;
sleep(2);
end;
{****************** SOCKET通讯 *****************}
{扫描开始}
procedure TSearchNetIp_frm.SBrnScanNetIPClick(Sender: TObject);
var
DWx1,Dwx:Dword;
IP,Str_Ip,begin_ip,end_ip:string;
Tcomr:TComread;
begin
//RxRichEdit1.text:='';
Setlength(TGroupNet,0);
{始化SOCKET}
WinSockInital(SearchNetIp_frm.Handle);
{清除Grid}
ClearGrid;
TF_stop:=false;
begin_ip:=edit1.Text;
IP:=begin_ip;
//RxRedDispinf.Text :='';
stabar.Panels[0].Text:='';
stabar.Panels[1].Text:='';

stabar.Panels[1].Text:='扫描用时:';
if Longword(inet_addr(pchar(IP)))=INADDR_NONE then
begin
Messagebox(Application.handle,pchar(IP + ' TP 出错'),'警告',MB_OK+MB_ICONERROR);
exit;
end;
end_ip:=edit2.Text;
IP:=end_ip;
if Longword(inet_addr(pchar(IP)))=INADDR_NONE then
begin
Messagebox(Application.handle,pchar(IP + ' TP 出错'),'警告',MB_OK+MB_ICONERROR);
exit;
end;
IP:=begin_ip;
ComputerIP:=inet_addr(PChar(IP)) ; // 192.168.0.1 ===>01 00 A8 C0
str_ip:=inttohex(ComputerIP,8); //01 00 A8 C0
str_ip:=copy(str_ip,7,2)+copy(str_ip,5,2)+copy(str_ip,3,2)+copy(str_ip,1,2); //==> C0 A8 00 01
DWx:=strtoint('$'+str_ip); {转换成为一个长整形数}

str_ip:=inttohex(ComputerIP,8);
str_ip:=inttostr(strtoint('$' + copy(str_ip,7,2)))
+'.' + inttostr(strtoint('$' + copy(str_ip,5,2)))
+'.' + inttostr(strtoint('$' + copy(str_ip,3,2)))
+'.' + inttostr(strtoint('$' + copy(str_ip,1,2)));
//EdNetIP1.text:=str_ip; {允许输入 一个数值表示IP}
IP:=end_ip;
ComputerIP:=inet_addr(PChar(IP)) ; // 192.168.0.1 ===>01 00 A8 C0
str_ip:=inttohex(ComputerIP,8); //01 00 A8 C0
str_ip:=copy(str_ip,7,2)+copy(str_ip,5,2)+copy(str_ip,3,2)+copy(str_ip,1,2); //==> C0 A8 00 01
DWx1:=strtoint('$'+str_ip); {转换成为一个长整形数}

str_ip:=inttohex(ComputerIP,8);
str_ip:=inttostr(strtoint('$' + copy(str_ip,7,2)))
+'.' + inttostr(strtoint('$' + copy(str_ip,5,2)))
+'.' + inttostr(strtoint('$' + copy(str_ip,3,2)))
+'.' + inttostr(strtoint('$' + copy(str_ip,1,2)));
//EdNetIP2.text:=str_ip; {允许输入 一个数值表示IP}
if DWx1>Dwx then
begin
IP_count:=DWx1-Dwx;
ComputerIP:=inet_addr(PChar(begin_ip));
IP_end:=DWx1;
end
else begin
IP_count:=DWx-Dwx1;
ComputerIP:=inet_addr(PChar(end_ip));
IP_end:=DWx;
end;
SBrnScanNetIP.Enabled :=false; {屏蔽按键}
Tcomr:=TComread.Create(True);
StartTickCount:=GetTickCount;
Tcomr.Resume ; {开始线程}
end;
{ TComread }
{通讯线程}
procedure TComread.Execute;
var
str_ip:string;
IP_now:Dword;
Procedure MakeEnd();
begin
SearchNetIp_frm.SBrnScanNetIP.Enabled :=True;
StopTickCount:=GetTickCount;
SearchNetIp_frm.stabar.Panels[1].Text:='扫描用时:' +FloatToStr(StopTickCount-startTickCount)+' mS';
SearchNetIp_frm.stabar.Panels[0].Text:='IP个数:'+inttostr(SearchNetIp_frm.SgDispNetIP.Items.Count);
{排序
'TP地址'0 '计算机名称'1 '计算机描述' 2
'网络组'3 '网卡MAC码'4
MakeGrid(const IntIndex: integer=3);}
end;
begin
with SearchNetIp_frm do
begin
str_ip:=inttohex(ComputerIP,8); //01 00 A8 C0
str_ip:=copy(str_ip,7,2)+copy(str_ip,5,2)+copy(str_ip,3,2)+copy(str_ip,1,2); //==> C0 A8 00 01
IP_now:=strtoint('$'+str_ip);
while IP_end>=IP_now do ////IP_end==> C0 A8 00 01
begin
if TF_stop=True then
begin
MakeEnd();
{关闭SOCKET}
WinSockClose();
exit;
end;
str_ip:=inttohex(ComputerIP,8);
str_ip:=inttostr(strtoint('$' + copy(str_ip,7,2)))
+'.' + inttostr(strtoint('$' + copy(str_ip,5,2)))
+'.' + inttostr(strtoint('$' + copy(str_ip,3,2)))
+'.' + inttostr(strtoint('$' + copy(str_ip,1,2)));
stabar.Panels[0].Text:=str_ip; {目前扫描的IP}
FSockAddrIn.SIn_Addr.S_addr := ComputerIP;//inet_addr(pchar(IP));
str_ip:=inttohex(ComputerIP,8); //01 00 A8 C0
str_ip:=copy(str_ip,7,2)+copy(str_ip,5,2)+copy(str_ip,3,2)+copy(str_ip,1,2); //==> C0 A8 00 01
str_ip:=inttohex( strtoint(('$' + str_ip))+1,8); //==> 加一C0 A8 00 02
IP_now:=strtoint('$'+str_ip);
str_ip:=copy(str_ip,7,2)+copy(str_ip,5,2)+copy(str_ip,3,2)+copy(str_ip,1,2); //==>02 00 A8 C0
ComputerIP:=strtoint('$'+str_ip);
sendto(sockfd, NbtstatPacket,50, 0, FSockAddrIn, sizeof(FSockAddrIn));
//发送数据后
waitforsingleobject(WAIT_ACK_EVENT,10); // 等待事件变成有信号
ResetEvent(WAIT_ACK_EVENT); // 设为没有信号
end;
MakeEnd();
end;
{关闭SOCKET}
WinSockClose();
end;
……
……
...全文
156 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
柯本 2010-01-27
  • 打赏
  • 举报
回复
你的NbtstatPacket是如何定义的,是const的吗?如果是,肯定要出错的。
sendto的定义为:
function sendto(s: TSocket; var Buf; len, flags: Integer; var addrto: TSockAddr;
tolen: Integer): Integer; stdcall;
其中Buf,addrto都必须是变量
dd_zhouqian 2010-01-27
  • 打赏
  • 举报
回复
winsock
和winsock2吧

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧