这程序是网上来的,你看看吧
procedure oncreate;
begin GetServerList(Listbox1.Items);end;
procedure Listbox1.Onclick
begin
GetUserList(Listbox1.Items[Listbox1.ItemIndex],Listbox2.Items);
end;
**************************************
获得网上邻居列表的程序
程序如下:
var
i : Integer;
ErrCode : Integer;
NetRes : Array[0..1023] of TNetResource;
EnumHandle : Thandle;
EnumEntries : Dword;
BufferSize : Dword;
ComputerIP,ComputerName:String;
begin
//try
With NetRes[0] do begin
dwScope :=RESOURCE_GLOBALNET;
dwType :=RESOURCETYPE_ANY;
dwDisplayType :=RESOURCEDISPLAYTYPE_DOMAIN;
dwUsage :=RESOURCEUSAGE_CONNECTABLE;
lpLocalName :=NIL;
lpRemoteName :=NIL;
lpComment :=NIL;
lpProvider :=NIL;
end;
{ get net root }
ErrCode:=WNetOpenEnum(
RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
RESOURCEUSAGE_CONTAINER,
@NetRes[0],
EnumHandle
);
If ErrCode=NO_ERROR then begin
EnumEntries:=1;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes[0],BufferSize);
WNetCloseEnum(EnumHandle);
ErrCode:=WNetOpenEnum(
RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
RESOURCEUSAGE_CONNECTABLE,
@NetRes[0],
EnumHandle
);
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(
EnumHandle,
EnumEntries,
@NetRes,
BufferSize
);
for i:=0 to 1023 do
if NetRes
.lpRemoteName='' then Exit
else begin
ComputerName:=Copy(NetRes.lpRemoteName,3,Length(NetRes.lpRemoteName)-2);
ComputerIP:=GetIP(ComputerName);
ShowMessage('计算机名' + NetRes.lpRemoteName + chr(13)
+ '计算机说明:' + NetRes.lpComment + chr(13)
+ 'IP地址:' + ComputerIP);
end;
end;
*************
请问怎样获取所有能访问的网上邻居中的计算机名称列表?
通过Windows Shell API
用SHGetSpecialFolderLocation获得Neighborhood的PIDL,然后将一个IShellFolder接
口绑定到这个PIDL,再然后就可以用这个接口的EnumObjects函数枚举其中的对象了。
因我不会Delphi,所以不知道用Delphi应该具体如何做,下面一段C++代码列出了"我
的电脑"下的所有项目(我的计算机没有安装网络邻居,所以用这个代替,方法应该是
一样的)
#include <stdio.h>
#include <iostream.h>
#include <comdef.h>
#include <windows.h>
#include <shellapi.h>
#include <shlobj.h>
#include <shlguid.h>
#include <shlwapi.h>
void _inline error(){ cerr<<"ERROR!"<<endl; _exit(1); }
void main(int argc, char* argv[])
{
IMallocPtr pMalloc;
IShellFolderPtr pShellFolder,pFolder;
LPITEMIDLIST pidl;
IEnumIDListPtr pEnum;
HRESULT hr;
STRRET Name;
ULONG celtFetched;
SHGetMalloc(&pMalloc);
if (!SUCCEEDED(SHGetDesktopFolder(&pShellFolder)))
error();
if (!SUCCEEDED(SHGetSpecialFolderLocation(NULL,CSIDL_DRIVES,&pidl)))
error();
pShellFolder->BindToObject(pidl,NULL,IID_IShellFolder,(LPVOID*)&pFolder);
hr=pFolder->EnumObjects(NULL,SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|
SHCONTF_INCLUDEHIDDEN,&pEnum);
while ((hr = pEnum->Next (1, &pidl, &celtFetched)) != S_FALSE &&
celtFetched == 1)
{
hr = pFolder->GetDisplayNameOf (pidl, SHGDN_NORMAL , &Name);
if (FAILED (hr))
{
error();
break;
}
printf("%s/n",Name.cStr);
}
if (!pidl)
pMalloc->Free(pidl);
}
**************
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ImgList, ExtCtrls, ToolWin;
type
TForm1 = class(TForm)
ImageList1: TImageList;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
Panel1: TPanel;
ResourceTree: TTreeView;
Splitter1: TSplitter;
ResourceList: TListView;
Panel2: TPanel;
lbScope: TLabel;
lbType: TLabel;
lbDisplayType: TLabel;
lbUsage: TLabel;
lbLocalName: TLabel;
lbRemoteName: TLabel;
lbComment: TLabel;
lbProvider: TLabel;
ToolButton2: TToolButton;
procedure Button1Click(Sender: TObject);
procedure ResourceTreeChange(Sender: TObject; Node: TTreeNode);
procedure ToolButton2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function AddTreeItem(nr:NETRESOURCE;node:TTreeNode):TTreeNode;
function EnumNetWorkResource(lpnrNetResource;node:TTreeNode):Boolean;
procedure ConnectResource(pnrNetResource);
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
{ TForm1 }
function GetLastErrorText:string;
var
dwSizeWORD;
lpszTemp:LPSTR;
begin
dwSize:=512;
lpszTemp:=nil;
try
GetMem(lpszTemp,dwSize);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_ARGUMENT_ARRAY,
nil,GetLastError,LANG_NEUTRAL,lpszTemp,dwSize,nil);
finally
Result:=StrPas(lpszTemp);
FreeMem(lpszTemp);
end;
end;
function TForm1.EnumNetWorkResource(lpnrNetResource;node:TTreeNode):Boolean;
var
nr:NetResource;
hEnum:THandle;
lpnrLocal: PNETRESOURCE;
cbBufferWORD;
cEntries,dwEnumResultWORD;
i:Integer;
LocalNode:TTreeNode;
begin
Result:=False;
cbBuffer:=163840;
cEntries:=$ffffffff;
lpnrLocal:=nil;
if(WNetOpenEnum(RESOURCE_GLOBALNET,
RESOURCETYPE_ANY,
0,
lpnr,
hEnum)<>NO_ERROR)then
begin
ShowMessage('Open enum error,node='+lpnr^.lpRemoteName);
ShowMessage(GetLastErrorText);
Result:=False;
Exit;
end;
try
repeat
lpnrLocal:=PNetResource(GlobalAlloc(GPTR,cbBuffer));
dwEnumResult:=WNetEnumResource(hEnum,
cEntries,
lpnrLocal,
cbBuffer);
if(dwEnumResult=NO_ERROR)then
begin
for i:=0 to cEntries-1 do
begin
nr:=(PNetResource(PChar(lpnrLocal)+sizeOf(NetResource)*i))^;
LocalNode:=AddTreeItem(nr,node);
if(RESOURCEUSAGE_CONTAINER=
nr.dwUsage and RESOURCEUSAGE_CONTAINER)then
begin
if(not EnumNetWorkResource(@nr,LocalNode))then
begin
ShowMessage('Error enum,Parent='+lpnr^.lpRemoteName);
ShowMessage(GetLastErrorText);
end;
end;
end;
end;
until (dwEnumResult=ERROR_NO_MORE_ITEMS);
if(dwEnumResult=ERROR_NO_MORE_ITEMS)then
Result:=True;
Except
GlobalFree(Cardinal(lpnrLocal));
WNetCloseEnum(hEnum);
Raise;
end;
GlobalFree(Cardinal(lpnrLocal));
WNetCloseEnum(hEnum);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
EnumNetWorkResource(nil,ResourceTree.Items[0]);
end;
function TForm1.AddTreeItem(nr: NETRESOURCE; node: TTreeNode): TTreeNode;
var
pnrNetResource;
begin
GetMem(pnr,SizeOf(NetResource));
pnr^:=nr;
Result:=ResourceTree.Items.AddChildObject(node,nr.lpRemoteName,pnr);
if(nr.dwUsage=RESOURCEUSAGE_CONTAINER)then
Result.ImageIndex:=0
else Result.ImageIndex:=1;
end;
procedure TForm1.ResourceTreeChange(Sender: TObject; Node: TTreeNode);
var
pnrNetResource;
begin
pnr:=PNetResource(Node.Data);
if(pnr=nil)then Exit;
case pnr^.dwScope of
RESOURCE_CONNECTED:
lbScope.Caption:='Connetted';
RESOURCE_GLOBALNET:
lbScope.Caption:='GlobalNet';
RESOURCE_REMEMBERED:
lbScope.Caption:='Remembered';
else
lbScope.Caption:='else scope:'+IntToStr(pnr^.dwScope);
end;
case pnr^.dwType of
RESOURCETYPE_DISK:
lbType.Caption:='Disk';
RESOURCETYPE_PRINT:
lbType.Caption:='Printer';
else
lbType.Caption:=IntToStr(pnr^.dwType);
end;
case pnr^.dwDisplayType of
RESOURCEDISPLAYTYPE_DOMAIN:
lbDisplayType.Caption:='Domain';
RESOURCEDISPLAYTYPE_GENERIC:
lbDisplayType.Caption:='Generic';
RESOURCEDISPLAYTYPE_SERVER:
lbDisplayType.Caption:='Server';
RESOURCEDISPLAYTYPE_SHARE:
lbDisplayType.Caption:='Shared';
else
lbDisplayType.Caption:='else type:'+IntToStr(pnr^.dwDisplayType);
end;
case pnr^.dwUsage of
RESOURCEUSAGE_CONTAINER:
lbUsage.Caption:='Container';
RESOURCEUSAGE_CONNECTABLE:
lbUsage.Caption:='Connectable';
else
lbUsage.Caption:='else Usage:'+IntToStr(pnr^.dwUsage);
end;
lbLocalName.Caption:=pnr^.lpLocalName;
lbRemoteName.Caption:=pnr^.lpRemoteName;
lbComment.Caption:=pnr^.lpComment;
lbProvider.Caption:=pnr^.lpProvider;
//ConnectResource(pnr);
end;
procedure TForm1.ConnectResource(pnr: PNetResource);
begin
if((pnr^.dwUsage and RESOURCEUSAGE_CONNECTABLE)
=RESOURCEUSAGE_CONNECTABLE)then
begin
{ if(WNetAddConnection2(pnr^,PChar(''),'wuwei',0)<>NO_ERROR)then
ShowMessage(GetLastErrorText);}
if(WNetAddConnection(pnr^.lpRemoteName,'','h:')<>NO_ERROR)then
ShowMessage(GetLastErrorText);
end;
end;
procedure TForm1.ToolButton2Click(Sender: TObject);
var
pNetResource;
begin
p:=PNetResource(ResourceTree.Selected.Data);
ConnectResource(p);
end;
end.
*******
查询局域网中NT服务器的计算机名
You should use API function
NET_API_STATUS NetServerEnum(
LPWSTR servername,
DWORD level,
LPBYTE *bufptr,
DWORD prefmaxlen,
LPDWORD entriesread,
LPDWORD totalentries,
DWORD servertype,
LPWSTR domain,
LPDWORD resume_handle
);