如何查询远程计算机用户,如何获取远程计算机的用户列表?

本文介绍了一个用于获取主机IP地址的函数gethostip,并实现了一个枚举网络资源的函数EnumNetResource,该函数可以列出指定类型的网络资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值