1206 ListOnlineRecourse函数的主干

来源:互联网 发布:惠普打印机驱动for mac 编辑:程序博客网 时间:2024/04/19 11:53

 

线程函数中的主体:

              if(CheckHostStatus(strIPAddr))

        {

            // 获取共享资源的功能

            ListOnlineRecourse(pInfo->pm_smbTree, strIPAddr, pInfo->bLevel);

              }

 

void ListOnlineRecourse(CTreeCtrl* pTree, const CString &strIP, BYTE bLevel)

{

    /* 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;

 

    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)

        {

            HTREEITEM hItemParent;

            if (cEntries > 0)

            {

                hItemParent = pTree->InsertItem(strIP, 0, 1);

            }

           

            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);

                HTREEITEM hItemChild = pTree->InsertItem(strSmbFile,0,1, hItemParent, TVI_LAST);

 

                if (RESOURCEUSAGE_CONNECTABLE == lpnrLocal[i].dwUsage

                    && RESOURCETYPE_DISK == lpnrLocal[i].dwType)

                {

                    AddConnectableRcs(&lpnrLocal[i], pTree, hItemChild, bLevel);

                }

            }

        }

        // 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;

    }

}

原创粉丝点击