大侠们,如何取得所在局域网的所有电脑的名字? WNetX函数族,能实现这一功能。详细参考MSDN 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 试一试WNetEnumResource函数,msdn上有详细介绍。 用DsGetDcName函数:DWORD DsGetDcName( LPCTSTR ComputerName, LPCTSTR DomainName, GUID *DomainGuid, LPCTSTR SiteName, ULONG Flags, PDOMAIN_CONTROLLER_INFO ); DsGetDcName()是NT5专用函数,9X不支持。 #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("无法与主机联系!");} MFC 在一个Dlg中创建了另一个Dlg,请问怎样才能使两个窗口可以自由切换 为何我的串行化没有执行? 请问可以修改ms agent在系统托盘上的图标和它的提示信息吗?急求助 求CPropertyPage Class E 文翻译(1) 如何截获WWW服务器返回给浏览器的信息? 求~MPEG1得文件格式~啊 11:30结贴,关于一vc下的开发工具的菜鸟问题 高分求一源码,如果哪位能提供,必重分感谢. 哪有反弹端口木马的源码示例,高分求购 哪儿有《深入浅出MFC》的电子版?? 如何按扇区访问CD-ROM 怎样用VC打开一个可运行的程序。
DWORD DsGetDcName(
LPCTSTR ComputerName,
LPCTSTR DomainName,
GUID *DomainGuid,
LPCTSTR SiteName,
ULONG Flags,
PDOMAIN_CONTROLLER_INFO
);
//获取网上邻居的列表,存储在串表中
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("无法与主机联系!");
}