获取局域网在线用户名和IP(VC)

#pragma once

#include <winnetwk.h>
#include <afxtempl.h>

#pragma comment(lib, “mpr.lib”)

typedef struct _WorkStation
{
TCHAR szName[128];
TCHAR szIP[128];
}WorkStation, *LPWORKSTATION;

typedef CList<WorkStation, WorkStation&> CWorkStationList;

class CGTNetwork
{
public:
CGTNetwork(void);
~CGTNetwork(void);

const bool IsServer(const LPNETRESOURCE lpNetRS);
const bool IsDomain(const LPNETRESOURCE lpNetRS);
const bool IsGeneric(const LPNETRESOURCE lpNetRS);

const bool EnumerateNetRS(const LPNETRESOURCE lpNetRS, DWORD dwFlags = RESOURCE_GLOBALNET);

bool GetWorkStationInfo(const LPNETRESOURCE lpNetRS, LPWORKSTATION ws);
const CString GetRemoteIP(const LPNETRESOURCE lpNetRS);
const CString GetRemoteName(const LPNETRESOURCE lpNetRS);

public:
CWorkStationList m_WorkStationList;

};

#include “StdAfx.h”
#include “gtnetwork.h”

CGTNetwork::CGTNetwork(void)
{
WSAData wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}

CGTNetwork::~CGTNetwork(void)
{
m_WorkStationList.RemoveAll();
WSACleanup();
}

const CString CGTNetwork::GetRemoteName(const LPNETRESOURCE lpNetRS)
{
CString str;

if ((lpNetRS != NULL) && IsServer(lpNetRS) && (lpNetRS->lpRemoteName != NULL))
{
str = lpNetRS->lpRemoteName;

if (0 == str.Left(2).Compare(“////”))
str.Delete(0, 2);
}

return str;
}

const CString CGTNetwork::GetRemoteIP(const LPNETRESOURCE lpNetRS)
{
CString strName, strIP;
LPHOSTENT host = NULL;
in_addr * ptr = NULL;

strName = lpNetRS->lpRemoteName;

if (0 == strName.Left(2).Compare(“////”))
strName.Delete(0, 2);

host = gethostbyname(strName);

if (host == NULL)
{
TRACE0(“/n/r gethostbyname func fail”);
return strIP;
}

ptr = (in_addr *)host->h_addr_list[0];

if (ptr == NULL)
return strIP;

int a = ptr->S_un.S_un_b.s_b1;
int b = ptr->S_un.S_un_b.s_b2;
int c = ptr->S_un.S_un_b.s_b3;
int d = ptr->S_un.S_un_b.s_b4;

strIP.Format(“%d.%d.%d.%d”, a, b, c, d);

return strIP;
}

const bool CGTNetwork::IsServer(const LPNETRESOURCE lpNetRS)
{
return lpNetRS->dwDisplayType == RESOURCEDISPLAYTYPE_SERVER;
}

const bool CGTNetwork::IsDomain(const LPNETRESOURCE lpNetRS)
{
return lpNetRS->dwDisplayType == RESOURCEDISPLAYTYPE_DOMAIN;
}

const bool CGTNetwork::IsGeneric(const LPNETRESOURCE lpNetRS)
{
return lpNetRS->dwDisplayType == RESOURCEDISPLAYTYPE_GENERIC;
}

const bool CGTNetwork::EnumerateNetRS(const LPNETRESOURCE lpNetRS, DWORD dwFlags)
{
DWORD         dwScope;
LPNETRESOURCE lpnrLocal = NULL;
DWORD         dwResult;
HANDLE        hEnum = 0;

dwScope = dwFlags;

dwResult = WNetOpenEnum(dwScope, RESOURCETYPE_ANY, RESOURCEUSAGE_ALL, lpNetRS, &hEnum);

if (dwResult != NO_ERROR)
{
TRACE0(“Open Enum Fail”);
return false;
}

DWORD dwBuf = 16384;
DWORD dwEnties = 0xFFFFFFFF;

lpnrLocal = (LPNETRESOURCE)GlobalAlloc(GPTR, dwBuf);

if (lpnrLocal == NULL)
{
TRACE0(“GlobalAlloc fail”);
return false;
}

do
{
dwResult = WNetEnumResource(hEnum, &dwEnties, lpnrLocal, &dwBuf);

if (dwResult != NO_ERROR)
{
TRACE0(“WNetEnumResource Fail”);
continue;
}

for (DWORD i=0; i<dwEnties; i++)
{
if (RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER) &&
lpnrLocal[i].dwDisplayType != RESOURCEDISPLAYTYPE_SERVER)
{
if (!EnumerateNetRS(&lpnrLocal[i]))
{
TRACE1(“Enum lprsLocal[%d] &Ecirc;§°&Uuml;”,i);
}
}

WorkStation ws;

if (GetWorkStationInfo(&lpnrLocal[i], &ws))
m_WorkStationList.AddTail(ws);

}

}while(dwResult != ERROR_NO_MORE_ITEMS);

if (lpnrLocal)
GlobalFree((HGLOBAL)lpnrLocal);

WNetCloseEnum(hEnum);

return true;
}

bool CGTNetwork::GetWorkStationInfo(const LPNETRESOURCE lpNetRS, LPWORKSTATION ws)
{
CString strName, strIP;

strName = GetRemoteName(lpNetRS);
strIP   = GetRemoteIP(lpNetRS);

if ((!strName.IsEmpty()) && (!strIP.IsEmpty()))
{
strcpy(ws->szName, strName);
strcpy(ws->szIP, strIP);
return true;
}

return false;
}

 

本文固定链接: http://www.ccsbbs.com.cn/archives/5981.html | 极限手指

该日志由 极限手指 于2013年03月10日发表在 C++ 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 获取局域网在线用户名和IP(VC) | 极限手指

获取局域网在线用户名和IP(VC):等您坐沙发呢!

发表评论

您必须 [ 登录 ] 才能发表留言!