//本段程序是几年前写,现在由于为写C#相同的功能,所以特别贴出来
//相同的C#程序,http://blog.csdn.net/zerodj/services/trackbacks/475349.aspx
#include <winsock2.h>
#include <winnetwk.h>
TTreeNode *Node1;
TTreeNode *Node2;
TTreeNode *Node3;
TTreeNode *gNode=NULL;
//---------------------------------------------------------------------------
void Tfrm_pc::getNetHood(char* domainname,char* msdomain){
NETRESOURCE parent;
LPNETRESOURCE pnr;
HANDLE hEnum;
DWORD BufSize;
DWORD ResNum;
ZeroMemory(&parent, sizeof(parent));
pnr = (LPNETRESOURCE)GlobalAlloc(GMEM_FIXED, 1000);
parent.dwScope = RESOURCE_GLOBALNET;
parent.dwType = RESOURCETYPE_ANY;
parent.dwDisplayType = RESOURCEDISPLAYTYPE_DOMAIN;
parent.dwUsage = RESOURCEUSAGE_CONTAINER;
if(AnsiString(domainname)=="");
else parent.lpRemoteName = domainname;
if(AnsiString(msdomain)=="");
else parent.lpProvider = msdomain;
WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, &parent, &hEnum);
while(BufSize = 1000, ResNum = 1, WNetEnumResource(hEnum, &ResNum, pnr, &BufSize) == NO_ERROR) {
Application->ProcessMessages();
AnsiString pcname=AnsiString(pnr->lpRemoteName);
if(pcname.Trim().SubString(0,2)=="\\\\"){
pcname=pcname.SubString(3,pcname.Length());
tv->Items->AddChild(Node2,pcname);
Node3=tv->Items->Item[tv->Items->Count-1];
Node3->ImageIndex=2;
Node3->SelectedIndex=2;
}
else if(pcname.LowerCase().Trim().Pos("microsoft windows ")>0){
tv->Items->Add(NULL,pcname);
Node1 = tv->Items->Item[tv->Items->Count-1];
Node1->ImageIndex=0;
Node1->SelectedIndex=0;
getNetHood(NULL,pcname.c_str());
}
else{
tv->Items->AddChild(Node1,pcname);
Node2 = tv->Items->Item[tv->Items->Count-1];
Node2->ImageIndex=1;
Node2->SelectedIndex=1;
getNetHood(pcname.c_str(),NULL);
}
}
WNetCloseEnum(hEnum);
GlobalFree(pnr);
}
//---------------------------------------------------------------------------
void __fastcall Tfrm_pc::FormCreate(TObject *Sender)
{
TCursor oldcur=Screen->Cursor;
Screen->Cursor = crHourGlass;
tv->Items->Clear(); //TreeView的VCL组件
getNetHood(NULL,NULL); //显示网络资源
tv->FullExpand();
Screen->Cursor=oldcur;
}
//---------------------------------------------------------------------------
我的主页:itbaby.jss.cn