为什么我列举不出共享文件夹。(20分)

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

forza

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么列举不出来尼?
procedure TForm1.Button4Click(Sender: TObject);
var
str : string;
NetApiStatus : integer;
servername,qualifier : pwidechar;
shareinfo : PShare_info_0;
lpBuffer : pointer;
entriesread, totalentries, resume_handle : integer;
counter : integer;
begin
str := '//'+edit2.Text;
getmem(servername,sizeof(widechar)*(length(str)+1));
stringtowidechar(str,servername,sizeof(char)*(length(str)+1));
repeat
NetApiStatus := NetShareEnum(servername, 2, lpBuffer, -1,entriesread, totalentries, resume_handle);
shareInfo := pShare_info_0(lpbuffer);
for Counter := 0 to EntriesRead - 1 do
begin
showmessage(widechartostring(shareInfo^.shi0_netname));
netapibufferfree(lpbuffer);
Inc(shareInfo);
end;
until (NetApiStatus <> ERROR_MORE_DATA);
end;

另外,我用下面的程序(截取了一段)
res := WnetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY,
RESOURCEUSAGE_CONNECTABLE, @NetResource,lphEnum);

count :=$ffffffff;
buffersize := 8192;
getmem(buf,buffersize);
res := WnetEnumResource(lphEnum,count,buf,buffersize);

     Temp := TNetResourceArray(Buf);
For Ind := 0 to Count - 1 do
begin
showmessage(Temp^.lpRemoteName + 2);
inc(temp);
end;

这样获取的只有自己设定的共享文件夹,对默认共享如(IPC$,C$,ADMIN$)列举不出来,是不是我的参数设定有问题,请指教。
 
有没有建立空连接?
 
建立空连接了,不行哦。为什么?有什么错误吗?

我查过MSDN了,如果要把默认共享列出来,需要要netresourceEnum,另外一个不行。
The WNetEnumResource function does not enumerate users connected to a share; use the NetConnectionEnum function. Also, to enumerate hidden shares, use the NetShareEnum function.


上面有一个地方写错了
NetApiStatus := NetShareEnum(servername, 2, lpBuffer, -1,entriesread, totalentries, resume_handle);
应该是:
NetApiStatus := NetShareEnum(servername, 0, lpBuffer, -1,entriesread, totalentries, resume_handle);

 
tt.bat:
net share >c:/1.txt

 
to :hfghfghfg
我是想获取对方机器的共享,不是本地的。编程实现,不是用net的命令。

我把建立空连接后的再贴一个
 
 procedure TForm1.Button1Click(Sender: TObject);
var str : string;
NetApiStatus : integer;
servername : pwidechar;
shareinfo : PShareinfo0;
lpBuffer,h: pointer;
entriesread, totalentries, resume_handle : integer;
counter : integer;
// 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:='//10.65.60.78/ipc$';
aNetRes.lpRemoteName :=pchar(str);
error := WNetAddConnection2(anetres,'','',0);
TimeOut.tv_sec:=2;
TimeOut.tv_usec:=0;
if error = NO_ERROR then
begin
str := '//10.65.60.78';
getmem(servername,sizeof(widechar)*(length(str)+1));
stringtowidechar(str,servername,sizeof(char)*(length(str)+1));
repeat
NetApiStatus := NetShareEnum(servername, 0, lpBuffer, -1, entriesread, totalentries, resume_handle);
shareInfo := pShareinfo0(lpbuffer);
for Counter := 0 to EntriesRead - 1 do
begin
showmessage(widechartostring(shareInfo^.shi0_netname));
Inc(shareInfo);
end;
netapibufferfree(lpbuffer);
until (NetApiStatus <> ERROR_MORE_DATA);
showmessage(inttostr(netapistatus));
end;
end;

在另外一个单元文件中定义了:

function NetShareEnum(servername: LPWSTR; level: integer; var butptr: Pointer;prefmaxlen: integer; var entriesread: integer; var totalentries: integer; resume_handle: integer):NET_API_STATUS; stdcall;

implementation

function NetShareEnum; external netapi32lib name 'NetShareEnum';

先谢谢大家伙儿了
 
谢谢大家,我已经找到错在哪里了,很低级。

我要把此帖删除了。
 
后退
顶部