ss

void CMy20180203_MFC_獲取系統所有賬戶名稱Dlg::OnBnClickedGetalluser()
{
    // TODO: 在此添加控件通知處理程序代碼
    UpdateData(TRUE);

    LPUSER_INFO_0 pBuf = NULL;
    LPUSER_INFO_0 pTmpBuf;
    DWORD dwLevel = 0;
    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
    DWORD dwEntriesRead = 0;
    DWORD dwTotalEntries = 0;
    DWORD dwResumeHandle = 0;
    DWORD i;
    DWORD dwTotalCount = 0;
    NET_API_STATUS nStatus;
    LPTSTR pszServerName = NULL;

    do // begin do
    {
        nStatus = NetUserEnum(NULL,
            dwLevel,
            FILTER_NORMAL_ACCOUNT, // global users
            (LPBYTE*)&pBuf,
            dwPrefMaxLen,
            &dwEntriesRead,
            &dwTotalEntries,
            &dwResumeHandle);
        //
        // If the call succeeds,
        //
        if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
        {
            if ((pTmpBuf = pBuf) != NULL)
            {
                //
                // Loop through the entries.
                //
                for (i = 0; (i < dwEntriesRead); i++)
                {
                    assert(pTmpBuf != NULL);

                    if (pTmpBuf == NULL)
                    {
                        m_username.Format(_T("%s"), "An access violation has occurred\n");
                        break;
                    }
                    //
                    //  Print the name of the user account.
                    //

                    TCHAR tcBuffer[1024] = { 0 };
                    wsprintf(tcBuffer, L"\t-- %s\r\n", pTmpBuf->usri0_name);
                    m_username += tcBuffer;

                    pTmpBuf++;
                    dwTotalCount++;
                }
            }
        }
        //
        // Otherwise, print the system error.
        //
        else
            fprintf(stderr, "A system error has occurred: %d\n", nStatus);
        //
        // Free the allocated buffer.
        //
        if (pBuf != NULL)
        {
            NetApiBufferFree(pBuf);
            pBuf = NULL;
        }
    }
    // Continue to call NetUserEnum while 
    //  there are more entries. 
    // 
    while (nStatus == ERROR_MORE_DATA); // end do
                                        //
                                        // Check again for allocated memory.
                                        //
    if (pBuf != NULL)
        NetApiBufferFree(pBuf);
    //
    // Print the final count of users enumerated.
    //
    m_CountNum.Format(L"總計有%d個賬戶被枚舉", dwTotalCount);
    
    UpdateData(FALSE);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章