您的位置:首页 > 其它

090826项目进展:添加smb资源的网段搜索二

2009-08-26 19:48 302 查看
1,添加变量

// Dialog Data
//{{AFX_DATA(CDlgSmbList)
enum { IDD = IDD_SMBLIST };
CTreeCtrl m_smbTree;
CIPAddressCtrl m_addrTo;
CIPAddressCtrl m_addrFrom;
//}}AFX_DATA

2,函数OnButtonSearch

void CDlgSmbList::OnButtonSearch()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);

tListSmb.pm_addrFrom = &m_addrFrom;
tListSmb.pm_addrTo = &m_addrTo;
tListSmb.pm_smbTree = &m_smbTree;

m_smbTree.DeleteAllItems();
AfxBeginThread(ThreadFuncListSmbRecource, &tListSmb);
}

3,使用线程函数

a,先判断某一ip是否在线

b,如果在线采取获取共享资源

UINT ThreadFuncListSmbRecource(LPVOID lpParam)
{
tCheckStatus* pInfo=(tCheckStatus*)lpParam;

BYTE field0,field1,field2,field3;
BYTE field4,field5,field6,field7;

pInfo->pm_addrFrom->GetAddress(field0,field1,field2,field3);
pInfo->pm_addrTo->GetAddress(field4,field5,field6,field7);

CString strIPAddr;
for(int i =field3; i <= field7; i++)
{
strIPAddr.Format("%d.%d.%d.%d",field0,field1,field2,i);
pInfo->pm_wndMessage->SetWindowText(""+strIPAddr);

if(CheckHostStatus(strIPAddr))
{
ListOnlineRecourse(pInfo->pm_smbTree, strIPAddr);
}
}

return 0;
}


void ListOnlineRecourse(CTreeCtrl* pTree, const CString &strIP)
{
/* 1,入参初始化,NETRESOURCE初始化 */
DWORD dwResult, dwResultEnum;
HANDLE hEnum;
DWORD cbBuffer = 16384;      // 16K is a good size
DWORD cEntries = -1;         // enumerate all possible entries
LPNETRESOURCE lpnrLocal;     // pointer to enumerated structures
DWORD i;

HTREEITEM hItemParent = pTree->InsertItem(strIP, 0, 1);
CString cstrTemp = "////" + strIP;

NETRESOURCE tNetRs;
memset(&tNetRs, 0, sizeof(NETRESOURCE));
tNetRs.dwScope = RESOURCE_GLOBALNET;
tNetRs.dwType = RESOURCETYPE_ANY;
tNetRs.dwDisplayType = RESOURCEDISPLAYTYPE_SERVER;
tNetRs.dwUsage = RESOURCEUSAGE_CONTAINER;
tNetRs.lpProvider = "Microsoft Windows Network";

char destip[64] ;
sprintf(destip, "%s", cstrTemp.GetBuffer(cstrTemp.GetLength()));
int cntDelete = strlen(destip) + 1;

tNetRs.lpRemoteName = destip;

NETRESOURCE *lpnr = &tNetRs;

// 2,检测主机是否在线
// Call the WNetOpenEnum function to begin the enumeration.
dwResult = WNetOpenEnum(RESOURCE_GLOBALNET, // all network resources
RESOURCETYPE_ANY,   // all resources
0,        // enumerate all resources
lpnr,     // NULL first time the function is called
&hEnum);  // handle to the resource

if (dwResult != NO_ERROR)
{
return;
}
//
// Call the GlobalAlloc function to allocate resources.
lpnrLocal = (LPNETRESOURCE)GlobalAlloc(GPTR, cbBuffer);
if (lpnrLocal == NULL)
{
return;
}

do
{
//
// Initialize the buffer.
//
ZeroMemory(lpnrLocal, cbBuffer);

// 3,遍历资源,并挂载树结构
// Call the WNetEnumResource function to continue
//  the enumeration.
dwResultEnum = WNetEnumResource(hEnum,      // resource handle
&cEntries,  // defined locally as -1
lpnrLocal,  // LPNETRESOURCE
&cbBuffer); // buffer size
//
// If the call succeeds, loop through the structures.
if (dwResultEnum == NO_ERROR)
{
for(i = 0; i < cEntries; i++)
{
// Call an application-defined function to
//  display the contents of the NETRESOURCE structures.
// DisplayStruct(&lpnrLocal[i]);
// 将以前的打印 改进为 挂载树
CString strSmbFile(lpnrLocal[i].lpRemoteName);
strSmbFile.Delete(0, cntDelete);
pTree->InsertItem(strSmbFile,0,1, hItemParent, TVI_LAST);
}
}
// Process errors.
else if (dwResultEnum != ERROR_NO_MORE_ITEMS)
{
break;
}
}
//
// End do.
//
while(dwResultEnum != ERROR_NO_MORE_ITEMS);

// 4,释放资源
// Call the GlobalFree function to free the memory.
//
GlobalFree((HGLOBAL)lpnrLocal);
//
// Call WNetCloseEnum to end the enumeration.
//
dwResult = WNetCloseEnum(hEnum);

if(dwResult != NO_ERROR)
{
//
// Process errors.
//
return;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: