WSADATA wsd;
WSAStartup(MAKEWORD(2,2),&wsd);
m_grid.SetExtendedStyle(LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_ONECLICKACTIVATE|LVS_EX_GRIDLINES);
m_grid.InsertColumn(0,L"计算机名",LVCFMT_LEFT,200,0);
m_grid.InsertColumn(1,L"IP地址",LVCFMT_LEFT,200,0);
DWORD Count=0xFFFFFFFF,Bufsize=4096,Res;
NETRESOURCE* nRes;
NETRESOURCE* nRes1;
NETRESOURCE* nRes2;
HANDLE lphEnum;
LPVOID Buf=new char[4096];
LPVOID Buf1=new char[4096];
LPVOID Buf2=new char[4096];
Res=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,NULL,&lphEnum);
Res=WNetEnumResource(lphEnum,&Count,Buf,&Bufsize);
nRes=(NETRESOURCE*)Buf;
for(DWORD n=0;n<Count;nRes++)
{
DWORD Count1=0xFFFFFFFF;
Res=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,nRes,&lphEnum);
Res=WNetEnumResource(lphEnum,&Count1,Buf1,&Bufsize);
nRes1=(NETRESOURCE*)Buf1;
for(DWORD i=0;i<Count1;i++)
{
DWORD Count2=0xFFFFFFFF;
Res=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,nRes1,&lphEnum);
Res=WNetEnumResource(lphEnum,&Count2,Buf2,&Bufsize);
nRes2=(NETRESOURCE*)Buf2;
for(DWORD j=0;j<Count2;j++,nRes2++)
{
m_grid.InsertItem(j,0);
CString sName=nRes->lpRemoteName;
sName=sName.Right(sName.GetLength()-2);
m_grid.SetItemText(j,0,sName);
CString str=_T("");
struct hostent * pHost;
pHost=gethostbyname((char*)(LPCTSTR)sName);
if(pHost==NULL)
m_grid.SetItemText(j,1,L"无法获得IP地址");
else
{
for(int n=0;n<4;n++)
{
CString addr;
if(n>0)
str+=".";
addr.Format(L"%u",(unsigned int)((unsigned char*)pHost->h_addr_list[0])[n]);
str+=addr; }
m_grid.SetItemText(j,1,str);
}
}
}
}
delete Buf;
delete Buf1;
delete Buf2;
WNetCloseEnum(lphEnum);
Res=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,nRes,&lphEnum);
调试就是这一句不对,但是我不知道怎么修改,在VS2008中提示
未处理的异常0xC0000005读取位置0xcdcdcdcd时发生访问冲突

解决方案 »

  1.   

    LPVOID Buf=new char[4096];
    LPVOID Buf1=new char[4096];
    LPVOID Buf2=new char[4096];把你的几个数组初始化一下
      

  2.   

    传去进的nRes没有申请空间啊
    lpNetResource 
    Pointer to a NETRESOURCE structure that specifies the container to enumerate. If the dwScope parameter is not RESOURCE_GLOBALNET, this parameter must be NULL. If this parameter is NULL, the root of the network is assumed. (The system organizes a network as a hierarchy; the root is the topmost container in the network.)If this parameter is not NULL, it must point to a NETRESOURCE structure. This structure can be filled in by the application or it can be returned by a call to the WNetEnumResource function. The NETRESOURCE structure must specify a container resource; that is, the RESOURCEUSAGE_CONTAINER value must be specified in the dwUsage parameter.To enumerate all network resources, an application can begin the enumeration by calling WNetOpenEnum with the lpNetResource parameter set to NULL, and then use the returned handle to call WNetEnumResource to enumerate resources. If one of the resources in the NETRESOURCE array returned by the WNetEnumResource function is a container resource, you can call WNetOpenEnum to open the resource for further enumeration.