建立空连接和列举用户是不是不能写在一个procedure里,我很郁闷(100分)

  • 主题发起人 主题发起人 forza
  • 开始时间 开始时间
F

forza

Unregistered / Unconfirmed
GUEST, unregistred user!
以下两个过程分开做是没有问题的,但是如果写在一个procedure里就不可以了。
其中 NetApiStatus := NetUserEnum(serverName, 0, 0, lpBuffer, -1, EntiesRead, TotalEntries, ResumeHandle);
UserInfo := PUSER_INFO_0(lpBuffer);
UserInfo总是nil,何故呀?


procedure TForm1.Button1Click(Sender: TObject);
var
str : string;
aNetRes:TNETRESOURCE;
Error: DWord;
TimeOut:TimeVal;
begin

fillchar(aNetRes,sizeof(TNetResource),0);
anetRes.dwScope := RESOURCE_CONNECTED;
anetRes.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;
aNetRes.dwType := RESOURCETYPE_ANY;
aNetRes.lpLocalName := '';
aNetRes.lpProvider := '';
str:='//'+edit2.text+'/ipc$';
aNetRes.lpRemoteName :=pchar(str);
error := WNetAddConnection2(anetres,'','',0);
TimeOut.tv_sec:=2;
TimeOut.tv_usec:=0;
end;


procedure TForm1.Button3Click(Sender: TObject);
var str : string;
Userinfo : PUser_info_0;
servername : pwidechar;
netapiStatus, nStatus : longword;
lpbuffer : pointer;
counter : integer;
s:string;

begin
str := '//'+edit2.Text;
getmem(servername,sizeof(widechar)*(length(str)+1));
stringtowidechar(str,servername,sizeof(char)*(length(str)+1));

repeat

NetApiStatus := NetUserEnum(serverName, 0, 0, lpBuffer, -1, EntiesRead, TotalEntries, ResumeHandle);
UserInfo := PUSER_INFO_0(lpBuffer);

for Counter := 0 to EntiesRead - 1 do
begin
//do something
Inc(UserInfo);

end;
freemem(servername);
NetApiBufferFree(lpBuffer);
until (NetApiStatus <> ERROR_MORE_DATA);


end;


如果合在一起写的话就不行了:
procedure TForm1.Button1Click(Sender: TObject);
var
str,str1 : string;
aNetRes:TNETRESOURCE;
Error: DWord;
TimeOut:TimeVal;
Userinfo : PUser_info_0;
servername : pwidechar;
netapiStatus, nStatus : longword;
lpbuffer : pointer;
Counter : integer;
begin

fillchar(aNetRes,sizeof(TNetResource),0);
anetRes.dwScope := RESOURCE_CONNECTED;
anetRes.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;
aNetRes.dwType := RESOURCETYPE_ANY;
aNetRes.lpLocalName := '';
aNetRes.lpProvider := '';
str:='//'+edit2.text+'/ipc$';
aNetRes.lpRemoteName :=pchar(str);
error := WNetAddConnection2(anetres,'','',0);
TimeOut.tv_sec:=2;
TimeOut.tv_usec:=0;

if error = NO_ERROR then
begin
str1 := '//'+edit2.Text;
getmem(servername,sizeof(widechar)*(length(str1)+1));
stringtowidechar(str1,servername,sizeof(char)*(length(str1)+1));

repeat

NetApiStatus := NetUserEnum(serverName, 0, 0, lpBuffer, -1, EntiesRead, TotalEntries, ResumeHandle);
UserInfo := PUSER_INFO_0(lpBuffer);

for Counter := 0 to EntiesRead - 1 do
begin
//do something as NetUserGetInfo
Inc(UserInfo);
end;
freemem(servername);
NetApiBufferFree(lpBuffer);
until (NetApiStatus <> ERROR_MORE_DATA);
end;
end;
 
可能他需要枚举我没看代码,
 
这个好象和什么写不写在一个过程里没关系,不管你有多少个过程,代码段都是从
头到尾的
 
我找到问题了,:)

这里提的问题是不是自己不能删除呀,浪费空间哦,还有大家的时间
 
后退
顶部