我有BCB的.
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("无法与主机联系!");
}
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("无法与主机联系!");
}
function GetIpAddress(host:String):String;
var
ch:array[1..32] of Char;
I : Integer;
WSData:TWSAData;
MyHost:PHostEnt;
S:String;
begin
if WSAstartup(2,wsdata)<>0 then S:='Winsock Error'
else
if GetHostName(@ch[1],32)<>0 then S:='HostName Error'
else begin
StrPCopy(@ch[1],host);
MyHost:=GetHostByName(@ch[1]);
if MyHost=nil then S:='IP Error'
else begin
S:='';
for i:=1 to 4 do begin
S:=S+IntToStr(Ord(MyHost.h_addr^[i-1]));
if i<4 then S:=S+'.';
end;
end;
end;
Result:=S;
end;function BrowseForComputer(const winhandle : THANDLE; const title : string) : string;
var
BrowseInfo: TBrowseInfo;
IDRoot: PItemIDList;
Path: array [1..MAX_PATH] of char;
begin
SHGetSpecialFolderLocation(winHandle, CSIDL_NETWORK, IDRoot);
ZeroMemory(@BrowseInfo, SizeOf(TBrowseInfo));
ZeroMemory(@path, MAX_PATH);
BrowseInfo.hwndOwner := winhandle;
BrowseInfo.pidlRoot := IDRoot;
BrowseInfo.lpszTitle := PChar(title);
BrowseInfo.pszDisplayName := @path[1];
BrowseInfo.ulFlags := BIF_BROWSEFORCOMPUTER;// or BIF_RETURNONLYFSDIRS ;
SHBrowseForFolder(BrowseInfo);
Result:=Copy(Path,1,StrLen(@Path[1]));
end;调用方法
S:String; S:=BrowseForComputer(handle,'选择计算机名');
S:=GetIpAddress(S);
ShowMessage(S);