我怎么知道Guest帐户是否有效,这是我的代码,但我不知道
问题描述:
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>
答
您应该指定代码失败的位置。因此,将NetUserGetInfo()
的返回值分配给变量,并在失败时打印,如同来自MSDN页面的示例代码中的NetUserGetInfo function(Windows) [ ^ ]这似乎是来源您的代码。
如果您的错误代码为0x8AD / 2221:那是NERR_UserNotFound
。
我查了一下你的个人资料,发现你来自中国。如果不使用英文版Windows,则必须传递本地化Windows版本的访客用户名。
检查代码中的条件似乎也有错误。当没有设置标志UF_ACCOUNTDISABLE
时,您正在打印Guest is disabled。
所以你可能会尝试一些东西像这样来检查它(注意下面代码中的用户名是gast,这是德国客人帐户名,因此适用于我的系统):
You should specify where the code fails. So assign the return value ofNetUserGetInfo()
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 isNERR_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 flagUF_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);