枚举当前系统中的所有用户的名称

First, you should let the application support the UNICODE 。 Next , I will tell you what  to do  in your application:

Create a project named EmuteUser of the MFC AppWizard(exe) based dialog.

Next, you should click sections of VC, Project->Settings,select C/C++ section, and in Preporcessors definitions, add UNICODE,_UNICODE, expression ,this shows  your application support UNICODE.

Next , we should  select Link section, and in it's Projects options, add  /entry:"wWinMainCRTStartup", this tells  your application's entry. In order to get more information ,you should look up MSDN.

 

Now , we have set the environment that support the UNICODE of the application.

Let's go to subject of the application.

 

In the application , I had added a List Box and a Button Control.In List Box,show the user's name of the OS, and Button was to realize the enumeration function.

 

Next, we should add some settings about the application:

#include
#include
#pragma comment(lib,"Netapi32.lib")

If application can use NetUserEnum() etc, we should add these settings.

And we add function named EmuteUser(),this function can enumerate all the users in our OS.The main code here:

void CEmuteUserDlg::EmuteUser()
{
 LPUSER_INFO_0 pBuf = NULL;
 LPUSER_INFO_0 pTmpBuf;
 DWORD dwLevel = 0;
 DWORD dwPrefMaxLen = -1;
 DWORD dwEntriesRead = 0;
 DWORD dwTotalEntries = 0;
 DWORD dwResumeHandle = 0;
 DWORD i;
 DWORD dwTotalCount = 0;
 NET_API_STATUS nStatus;
 LPTSTR? pszServerName = NULL;
 
 // The server is not the default local computer.
 //
 //
 // Call the NetUserEnum function, specifying level 0;
 // enumerate global user account types only.
 //
 do // begin do
 {
  nStatus = NetUserEnum(pszServerName,
  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)
 {
   AfxMessageBox(_T("An access violation has occurred"));
  break;
 }
  //
  //  Print the name of the user account.
 //
  CString strUser;
 strUser.Format(_T("%s"), pTmpBuf->usri0_name);
 m_strList.AddString(strUser);
 
 pTmpBuf++;
 dwTotalCount++;
 }
}
}
 //
 // Otherwise, print the system error.
 //
 else
 {
  CString strError;
  strError.Format(_T( "A system error has occurred: %d"), nStatus);
  AfxMessageBox(strError);
 }

 if (pBuf != NULL)
 {
 NetApiBufferFree(pBuf);
 pBuf = NULL;
 }
 }

while (nStatus == ERROR_MORE_DATA); // end do

if (pBuf != NULL)
  NetApiBufferFree(pBuf);

}

But how can we call this function? Now ,it's code :

m_strList.ResetContent();
EmuteUser();

 

In settings , you can fall across this question :

When you  compile the application , you can receive the error report:

LINK : fatal error LNK1104: cannot open file "mfc42ud.lib"

How can we do?

Ok! You should select Project->Settings, in General section,in Microsoft Foundation Classes:,

you can select Use MFC in a Static Library.

VC+ WINXP+VSP5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值