Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++

C++
I want to know the Guest account is active or not in C++.Here is my code ,but I do not figure out the problem 


#ifndef UNICODE
#define UNICODE
#endif
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "netapi32.lib")

#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <assert.h>
#include <lm.h>
#include <sddl.h>  
//#define BUFFER 8192
int wmain(int argc, wchar_t * argv[])
{
	bool ace = true;	
	LPUSER_INFO_1 puiVal = NULL;
	
		if (NERR_Success == NetUserGetInfo(NULL, L"Guest", 1, (LPBYTE*)&puiVal))
		{
			if (!(puiVal->usri1_flags &UF_ACCOUNTDISABLE))
			if (!(puiVal->usri1_priv &USER_PRIV_GUEST))
			{
				ace = false;
				wprintf(L"\tGuest is disabled\n");
				//cout<< "Guest is disabled" << endl;
			}
		}
		else
		if (ace = true)
			wprintf(L"\tGuest is enable\n");

	//	if (puiVal)
		//	NetApiBufferFree(puiVal);
	
	getchar();
		//cout << "Guest is disabled" << endl;
	

	return 0;
}</sddl.h></lm.h></assert.h></stdio.h></iostream></windows.h>
Posted

1 solution

You should specify where the code fails. So assign the return value of NetUserGetInfo() to a variable and print that upon failure like in the example code from the MSDN page for the NetUserGetInfo function (Windows)[^] which seems to be the source of your code.

If you got an error code of 0x8AD / 2221: That is NERR_UserNotFound.

I have looked up your profile and saw that you are from China. When not using an English Windows version, you must pass the guest user name for your localised Windows version.

There seems also to be a wrong condition checking in your code. You are printing "Guest is disabled" when the flag UF_ACCOUNTDISABLE is not set.

So you might try something like this to check it (note that the user name in the below code is "gast" which is the German guest account name and therefore works on my system):
LPUSER_INFO_1 puiVal = NULL;
	
NET_API_STATUS status = NetUserGetInfo(NULL, L"gast", 1, (LPBYTE*)&puiVal);
if (NERR_Success == status)
{
    wprintf(L"Disabled: %d\n", (puiVal->usri1_flags & UF_ACCOUNTDISABLE) ? 1 : 0);
    wprintf(L"Priv Guest: %d\n", (puiVal->usri1_flags & USER_PRIV_GUEST) ? 1 : 0);
}
else
{
    wprintf(L"Failed with code %#X / %d\n", status, status);
}
if (puiVal)
    NetApiBufferFree(puiVal);
 
Share this answer
 
v2
Comments
_Asif_ 14-Jan-16 6:35am    
+5ed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900