我要CB的查找网络邻居的源代码!谁解决了,我拜谁为师。

wyb_star 2001-05-27 06:35:00
...全文
204 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
CKER的例程:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FormMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FtpSrv"
#pragma link "Ftpcli"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
// Rebrowse:
void __fastcall TForm1::ButtonOKClick(TObject *Sender)
{
DoEnumeration();
}
//---------------------------------------------------------------------------
// Allow loading of tree view from a file:
void __fastcall TForm1::mniLoadFromFileClick(TObject *Sender)
{
if ( OpenDialog->Execute())
tvResources->LoadFromFile(OpenDialog->FileName);
}
//---------------------------------------------------------------------------
// Allow saving of tree view to a file:
void __fastcall TForm1::mniSaveToFileClick(TObject *Sender)
{
if ( SaveDialog->Execute())
tvResources->SaveToFile(SaveDialog->FileName);
}
//---------------------------------------------------------------------------
// Collapse all containers in the tree view:
void __fastcall TForm1::mniCollapseAllClick(TObject *Sender)
{
tvResources->FullCollapse();
}
//---------------------------------------------------------------------------
// Expand all containers in the tree view:
void __fastcall TForm1::mniExpandAllClick(TObject *Sender)
{
tvResources->FullExpand();
}
//---------------------------------------------------------------------------
// Add a connection to the tree view.
// Mostly used for persistent and currently connected
// resources to be displayed.
void __fastcall TForm1::AddConnection(NETRESOURCE* NetRes)
{
AnsiString ItemName;
ItemName=Trim(AnsiString(NetRes->lpLocalName));
if ( Trim(AnsiString(NetRes->lpRemoteName))!= "" )
{
if ( ItemName != "" ) ItemName += " ";
ItemName += "-> "+Trim(AnsiString(NetRes->lpRemoteName));
}

tvResources->Items->Add(tvResources->Selected,ItemName);
}
//---------------------------------------------------------------------------
// Add child item to container denoted as current top;
// this just adds a string for purposes such as being unable
// to enumerate a container. That is, the container's shares
// are not accessible to us.
void __fastcall TForm1::AddShareString(int TopContainerIndex, AnsiString ItemName)
{

tvResources->Items->AddChild(tvResources->Items->Item[TopContainerIndex],ItemName);
}
//---------------------------------------------------------------------------
// Add child item to container denoted as current top:
void __fastcall TForm1::AddShare(int TopContainerIndex, NETRESOURCE* NetRes)
{
AnsiString ItemName;
ItemName = Trim(AnsiString(NetRes->lpRemoteName));
if ( Trim(AnsiString(NetRes->lpComment)) != "")
{
if (ItemName!="") ItemName += " ";
ItemName += "("+AnsiString(NetRes->lpComment)+")";
}

tvResources->Items->AddChild(tvResources->Items->Item[TopContainerIndex],ItemName);
}
//---------------------------------------------------------------------------
// Add item to tree view; indicate that it is a container:
void __fastcall TForm1::AddContainer(NETRESOURCE* NetRes)
{

AnsiString ItemName;
ItemName = Trim(AnsiString(NetRes->lpRemoteName));
if ( Trim(AnsiString(NetRes->lpComment)) != "" )
{
if ( ItemName != "" ) ItemName += " ";
ItemName += "("+AnsiString(NetRes->lpComment)+")";
}

tvResources->Items->Add(tvResources->Selected,ItemName);
}
void __fastcall TForm1::FormShow(TObject *Sender)
{
DoEnumeration();

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
Application->OnHint = ShowHint;
BarBottom->Panels->Items[0]->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ShowHint(TObject* Sender)
{
BarBottom->Panels->Items[0]->Text = Application->Hint;
}
void __fastcall TForm1::ButtonCloseClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
// Enumerate through all network resources
void __fastcall TForm1::DoEnumeration()
{
NETRESOURCE* NetRes[3];
int Loop;
DWORD r, EntryCount, NetResLen;
HANDLE hEnum;

switch (grpListType->ItemIndex)
{
// Connected resources
case 0: ListType = RESOURCE_CONNECTED; break;
// Persistent resources
case 1: ListType = RESOURCE_REMEMBERED; break;
// Global
case 2: ListType = RESOURCE_GLOBALNET; break;
}

switch (grpResourceType->ItemIndex)
{
// Disk resources
case 0: ResourceType = RESOURCETYPE_DISK; break;
// Print resources
case 1: ResourceType = RESOURCETYPE_PRINT; break;
// All
case 2: ResourceType = RESOURCETYPE_ANY; break;
}

Screen->Cursor = crHourGlass;

try
{
// Delete any old items in the tree view
for (Loop = tvResources->Items->Count-1;Loop > 0;Loop-- )
tvResources->Items->Item[Loop]->Delete();
}
catch(...)
{ return; }

// Start enumeration:
r = WNetOpenEnum(ListType,ResourceType,0,NULL,&hEnum);
if ( r != NO_ERROR)
{
if ( r == ERROR_EXTENDED_ERROR )
MessageDlg("Unable to Enumerate the Network.\nA network-specific error occurred.",
mtError,TMsgDlgButtons() << mbOK,0);
else
MessageDlg("Unable to Enumerate the Network.",
mtError,TMsgDlgButtons() << mbOK,0);
return;
};

try
{
// We got a valid enumeration handle; walk the resources:
for(;;)
{
EntryCount = 1;
NetResLen = sizeof(NetRes);
r = WNetEnumResource(hEnum,&EntryCount,NetRes,&NetResLen);
switch (r)
{
case 0:
// It's a container, iterate it:
if ( NetRes[0]->dwUsage == RESOURCEUSAGE_CONTAINER )
DoEnumerationContainer(NetRes[0]);
// Persistent and connected resources show up here:
else if ( ListType >= RESOURCE_REMEMBERED && ListType <= RESOURCE_CONNECTED )
AddConnection(NetRes[0]);
break;
// Done
case ERROR_NO_MORE_ITEMS:
return;
//Other error
default :
MessageDlg("Error Walking Resources.",mtError,TMsgDlgButtons() << mbOK,0);
return;
}
}
}
__finally
{
Screen->Cursor=crDefault;
// Close enumeration handle:
WNetCloseEnum(hEnum);
}
}
//---------------------------------------------------------------------------
// Enumerate through the specified container:
// This function is usually recursively called.
void __fastcall TForm1::DoEnumerationContainer(NETRESOURCE* NetResContainer)
{
NETRESOURCE* NetRes[11] ;
int TopContainerIndex ;
DWORD r, EntryCount, NetResLen;
HANDLE hEnum;
// Add container name to tree view:
AddContainer(NetResContainer);
// Keep track of this item as current root:
TopContainerIndex = tvResources->Items->Count-1;
// Start enumeration:
if ( ListType == RESOURCE_GLOBALNET )
// Enumerating global net:
r = WNetOpenEnum(ListType,ResourceType,RESOURCEUSAGE_CONTAINER,
NetResContainer,&hEnum);
else
// Enumerating connections or persistent (won't normally get here):
r = WNetOpenEnum(ListType,ResourceType,RESOURCEUSAGE_CONTAINER,
NULL,&hEnum);
// Couldn't enumerate through this container; just make a note
// of it and continue on:
if ( r != NO_ERROR )
{
AddShareString(TopContainerIndex,"<Couldn''t Enumerate Resources(Error #"+
IntToStr(r)+">");
WNetCloseEnum(hEnum);
return;
}

try
{
// We got a valid enumeration handle; walk the resources:
for (;;)
{
EntryCount = 1;
NetResLen = sizeof(NetRes);
r = WNetEnumResource(hEnum,&EntryCount,NetRes,&NetResLen);
switch (r)
{
case 0:
// Yet another container to enumerate; call this function
// recursively to handle it:
if ((NetRes[0]->dwUsage == RESOURCEUSAGE_CONTAINER) ||
(NetRes[0]->dwUsage == 10))
DoEnumerationContainer(NetRes[0]);
else
switch (NetRes[0]->dwDisplayType)
{
// Top level type:
case RESOURCEDISPLAYTYPE_GENERIC:
case RESOURCEDISPLAYTYPE_DOMAIN:
case RESOURCEDISPLAYTYPE_SERVER:
AddContainer(NetRes[0]);
break;

// Share:
case RESOURCEDISPLAYTYPE_SHARE:
AddShare(TopContainerIndex,NetRes[0]);
break;
}
break;

case ERROR_NO_MORE_ITEMS:
return;

default :
MessageDlg("Error #"+IntToStr(r)+" WalkingResources.",
mtError,TMsgDlgButtons() << mbOK,0);
return;

}
}
}
__finally
{
// Close enumeration handle:
WNetCloseEnum(hEnum);
}
}
wowboy 2001-05-28
  • 打赏
  • 举报
回复
to cker(有空就玩票...甭活得忒累...) 
太复杂了,能发了源程序过来吗?
wowboy@21cn.com
谢谢
wowboy 2001-05-27
  • 打赏
  • 举报
回复
太慢了,有什么办法让程序Application->HandleMessage();要不我一按就像死机一样.
程序半个钟才有反应。
有什么办法让这句每加一个Item就停一下,我这里上千台机,不好玩。
EnumNetResource(ListBox1->Items,NULL,RESOURCE_GLOBALNET,RESOURCETYPE_ANY);
wowboy 2001-05-27
  • 打赏
  • 举报
回复
忘了,谢谢czylj(夭夭)提供这么好的源代码。^_^
wowboy 2001-05-27
  • 打赏
  • 举报
回复
哈哈,行了,wyb_star(孤星),快拜师吧,我也想,不过不知收不收^_^
不过我这里有1000台机左经常开的也有四五百台,所以我等了20分钟,程序才搜完但只有几十台。^_^不过好像都是同一个IP段的.但我们不同IP段的机也能在网上邻居见到的啊。
夭夭 2001-05-27
  • 打赏
  • 举报
回复
我抄了别人的,你试试
夭夭 2001-05-27
  • 打赏
  • 举报
回复
include <System.hpp>
#include <classes.hpp>
#include <winnetwk.h>
//获取网上邻居的列表,存储在串表中
bool EnumNetResource(TStrings *rcList,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 < dwEntries ; i++ ) {
if(NR[i].dwDisplayType==RESOURCEDISPLAYTYPE_SERVER) {
char *p=NR[i].lpRemoteName;
while(*p=='\\') p++;
if(*p) rcList->Add(p);
} else if((NR[i].dwUsage&RESOURCEUSAGE_CONTAINER)==
RESOURCEUSAGE_CONTAINER) {
bRet=EnumNetResource(rcList,&NR[i],dwScope,dwType);
if(bRet==false) break;
}
}
if(bRet==false) break;
}

WNetCloseEnum(hEnum) ;
return bRet;
}


//------获取某主机的IP地址,如果主机名为空,则返回本机的名和IP地址---------

AnsiString gethostip(AnsiString &host)
{
WSADATA wsaData;
AnsiString IP;

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(host.c_str());
IP=inet_ntoa(*(struct in_addr*)hp->h_addr_list[0]);
}
catch(...){
IP="";
}
WSACleanup();
return IP;
}

//----------------------Example-------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListBox1->Clear();
Screen->Cursor=crHourGlass;
EnumNetResource(ListBox1->Items,NULL,RESOURCE_GLOBALNET,RESOURCETYPE_ANY);
Screen->Cursor=crDefault;
}


void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(ListBox1->ItemIndex==-1) return;
Screen->Cursor=crHourGlass;
AnsiString IP=gethostip(ListBox1->Items->Strings[ListBox1->ItemIndex]);
Screen->Cursor=crDefault;
Edit1->Text=IP;
if(IP=="") ShowMessage("无法与主机联系!");
}
tgshsl 2001-05-27
  • 打赏
  • 举报
回复
多看看ShellAPI就会了,我看就是目录列表
wyb_star 2001-05-27
  • 打赏
  • 举报
回复
难道没人会吗
rh 2001-05-27
  • 打赏
  • 举报
回复
难道斑竹就不可以了么
xycleo 2001-05-27
  • 打赏
  • 举报
回复
我想有两个人可以!!!老火和ALNG!!!!!!!

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧