TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
AnsiString gethostip(AnsiString &host)
{
WSADATA wsaData;
AnsiString IP;
char temp[255];
strcpy(temp,host.c_str());
int pos=0;
while(temp[pos++]=='//');
pos--;
// temp+=i-1;
WSAStartup(MAKEWORD(2,0),&wsaData);
if(host.IsEmpty())
{
char hostname[128];
if(gethostname(hostname,128)!=0)
return AnsiString("");
host=hostname;
}
try
{
struct hostent *hp=gethostbyname(temp+pos);//host.c_str());
if(hp == NULL)
{
IP="";
}
else
{
IP=inet_ntoa(*(struct in_addr*)hp-> h_addr_list[0]);
}
}
catch(...)
{
IP="";
}
WSACleanup();
return IP;
}
//---------------------------------------------------------------------------
bool EnumNetResource(LPNETRESOURCE lpNR,DWORD dwScope,DWORD dwType)
{
HANDLE hEnum = 0;
DWORD dwResult = WNetOpenEnum(
dwScope, // scope of enumeration
dwType, // resource types to list
0, // enumerate all resources
lpNR, // pointer to resource structure (NULL at first time)
&hEnum // handle to resource
);
if(dwResult != NO_ERROR) return false;
bool bRet=true;
DWORD dwEntries = 0xFFFFFFFF; // enumerate all possible entries
NETRESOURCE NR[1024];
DWORD dwBuffer=1024*sizeof(NETRESOURCE);
while(1)
{
dwResult = WNetEnumResource(hEnum, // resource-handle
&dwEntries,
(LPVOID)NR,
&dwBuffer
);
if(dwResult == ERROR_NO_MORE_ITEMS)
{
break;
}
else
{
if(dwResult != NO_ERROR)
{
bRet=false;
break;
}
}
for(DWORD i=0;i
{
//ShowMessage("OK");
if(NR[i].dwDisplayType==RESOURCEDISPLAYTYPE_SERVER)
{
char *p=NR[i].lpRemoteName;
while(*p=='//') p++;
if(*p)
{
AnsiString IP = gethostip(p);
// TListItem *Item1;
// Item1 = Form1->ListView1->Items->Add();
// Item1->Caption = p;
// Item1->SubItems->Add(IP);
Form1->ListBox1->Items->Add(IP+"==="+p);
}
}
else
{
if((NR[i].dwUsage& RESOURCEUSAGE_CONTAINER)==RESOURCEUSAGE_CONTAINER)
{
bRet=EnumNetResource(&NR[i],dwScope,dwType);
if(bRet==false) break;
}
}
}
if(bRet==false) break;
}
WNetCloseEnum(hEnum) ;
return bRet;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListView1->Clear();
Screen->Cursor=crHourGlass;
EnumNetResource(NULL,RESOURCE_GLOBALNET,RESOURCETYPE_ANY);
Screen->Cursor=crDefault;
}
//---------------------------------------------------------------------------