在 Windows 7 操作系统上创建/删除新用户帐户

发布于 2024-12-13 08:50:30 字数 747 浏览 1 评论 0原文

我想在我的代码上创建和删除新用户。我设法使用“NetUser”类的“createUser()”函数进行创建,但是当我按照参考中的方式设置选项来创建标准用户时,它仍然创建为来宾帐户。有谁知道我的问题的原因是什么?

我正在使用此代码:

//userinfo structure:

 ui.usri1_name = username;
 ui.usri1_password = password;
 ui.usri1_priv = USER_PRIV_USER;
 ui.usri1_home_dir = NULL;
 ui.usri1_comment = NULL;
 ui.usri1_flags = UF_SCRIPT;
 ui.usri1_script_path = NULL;

//Creating user:

DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
nStatus = NetUserAdd(L"localhost",dwLevel,(LPBYTE)&ui,&dwError); 

这应该创建一个新的用户帐户作为标准用户帐户,但它始终创建一个任务用户帐户。该函数在凭据提供程序中运行,因此我认为问题不在于权限。有关 NetUserAdd 函数的详细信息 单击

I want to create and delete new users on my code. I managed to create with "NetUser" class's "createUser()" functions but when I set the options as in the references to create a standard user, it still creating as a guest account. Is there anyone knows what is the reason of my problem?

I am using this code:

//userinfo structure:

 ui.usri1_name = username;
 ui.usri1_password = password;
 ui.usri1_priv = USER_PRIV_USER;
 ui.usri1_home_dir = NULL;
 ui.usri1_comment = NULL;
 ui.usri1_flags = UF_SCRIPT;
 ui.usri1_script_path = NULL;

//Creating user:

DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
nStatus = NetUserAdd(L"localhost",dwLevel,(LPBYTE)&ui,&dwError); 

This should create a new user account to be a standard user account but it is always creating a quest user account. This function is running in a credential provider hence I don't think the problem is about the permissions. For more information about the NetUserAdd function click.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-12-20 08:50:30

您必须将用户添加到名为“Users”的本地组中。因此,您可以使用 NetLocalGroupAddMembers< /a> 函数。

本地组 Users 的名称取决于安装的 Windows 版本的语言。为了获取本地组“Users”的名称,您可以使用 CreateWellKnownSid(WinBuiltinUsersSid 为 WellKnownSidType)和 LookupAccountSid

You have to add the user to the local group called "Users". Therefore you can use the NetLocalGroupAddMembers function.

The name of the local group Users depends on the language of the installed Windows version. In order to get the name of the local group "Users" you can use CreateWellKnownSid (with WinBuiltinUsersSid as WellKnownSidType) and LookupAccountSid.

獨角戲 2024-12-20 08:50:30

这是我在 Windows 中创建帐户的示例。如果您想将此帐户添加到本地用户以便您可以登录当前计算机,则需要调用 NetLocalGroupAddMembers 函数。作为本地组名称,您应该只键入“Users”,但它会因 Windows 的语言版本而异。在波兰语版本中,它将是“Użytkownicy”等。
如果您想在远程服务器上添加帐户,则需要调用 NetGetDCName 并提供第一个参数给它服务器地址。然后在本例的其他函数中使用PDC。
请记住以管理员身份运行此代码(即使是 IDE,如果处于调试状态),否则您将出现访问被拒绝的错误。

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <lmcons.h>
#include <lmaccess.h>
#include <lmerr.h>
#include <lmapibuf.h>
#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib, "netapi32.lib")

NET_API_STATUS NetSample( LPWSTR lpszDomain,
                          LPWSTR lpszUser,
                          LPWSTR lpszPassword,
                          LPWSTR lpszLocalGroup )
{

    USER_INFO_1               user_info;
    LOCALGROUP_INFO_1         localgroup_info;
    LOCALGROUP_MEMBERS_INFO_3 localgroup_members;
    LPWSTR                    lpszPrimaryDC = NULL;
    NET_API_STATUS            err = 0;
    DWORD                     parm_err = 0;

// Get the name of the primary domain controller.
// Be sure to free the returned buffer.
// On local machine we don't need to do this

    //err = NetGetDCName( NULL,                    // local computer
    //               NULL,                   // domain name
    //               (LPBYTE *) &lpszPrimaryDC );  // returned PDC

    //if ( err != 0 )
    //{
    //    printf( "Error getting DC name: %d\n", err );
    //    return( err );
    //}

// Set up the USER_INFO_1 structure.

    user_info.usri1_name = lpszUser;
    user_info.usri1_password = lpszPassword;
    user_info.usri1_priv = USER_PRIV_USER;
    user_info.usri1_home_dir = TEXT("");
    user_info.usri1_comment = TEXT("");
    user_info.usri1_flags = UF_SCRIPT;
    user_info.usri1_script_path = TEXT("");

        //err = NetUserDel(NULL, L"NowyUse2r");  this will delete user

    err = NetUserAdd( NULL/*lpszPrimaryDC*/,        // PDC name
                      1,                    // level
                      (LPBYTE) &user_info,  // input buffer
                      &parm_err );          // parameter in error

    switch ( err )
    {
    case 0:
        printf("User successfully created.\n");
        break;
    case NERR_UserExists:
        printf("User already exists.\n");
        err = 0;
        break;
    case ERROR_INVALID_PARAMETER:
        printf("Invalid parameter error adding user; parameter index = %d\n",
                parm_err);
        NetApiBufferFree( lpszPrimaryDC );
   return( err );
    case ERROR_ACCESS_DENIED:
    printf("ERROR_ACCESS_DENIED; parameter index = %d\n",
                parm_err);
        NetApiBufferFree( lpszPrimaryDC );
        return( err );
    default:
        printf("Error adding user: %d\n", err);
        NetApiBufferFree( lpszPrimaryDC );
        return( err );
    }

// Set up the LOCALGROUP_INFO_1 structure.

    localgroup_info.lgrpi1_name = lpszLocalGroup;
    localgroup_info.lgrpi1_comment = TEXT("Sample local group.");

    err = NetLocalGroupAdd( NULL,    // PDC name
                  1,                          // level
                  (LPBYTE) &localgroup_info,  // input buffer
                  &parm_err );                // parameter in error

    switch ( err )
    {
    case 0:
        printf("Local group successfully created.\n");
        break;
    case ERROR_ALIAS_EXISTS:
        printf("Local group already exists.\n");
        err = 0;
        break;
    case ERROR_INVALID_PARAMETER:
        printf("Invalid parameter error adding local group; parameter index = %d\n",
                err, parm_err);
        NetApiBufferFree( lpszPrimaryDC );
        //return( err );
    default:
        printf("Error adding local group: %d\n", err);
        NetApiBufferFree( lpszPrimaryDC );
        //return( err );
    }

// Now add the user to the local group.

    localgroup_members.lgrmi3_domainandname = lpszUser;

    err = NetLocalGroupAddMembers( NULL,        // PDC name
                           lpszLocalGroup,               // group name
                           3,                            // name
                           (LPBYTE) &localgroup_members, // buffer
                           1 );                          // count

    switch ( err )
    {
    case 0:
        printf("User successfully added to local group.\n");
        break;
    case ERROR_MEMBER_IN_ALIAS:
        printf("User already in local group.\n");
        err = 0;
        break;
    default:
        printf("Error adding user to local group: %d\n", err);
        break;
    }

    NetApiBufferFree( lpszPrimaryDC );
    return( err );
}

int main()
{
    NET_API_STATUS err = 0;

    printf( "Calling NetSample.\n" );
    err = NetSample( L"",
                     L"NewUser",
                     L"SamplePswd",
                     L"Users" );
    printf( "NetSample returned %d\n", err );
    return( 0 );
}

Here is my example creating an account in Windows. If You want to add this account to local users so You could log on current machine it is necessary to call NetLocalGroupAddMembers function. As a local group name You should type just "Users", but it varies on the language version of Windows. On polish version it would be "Użytkownicy" and etc.
If You want to add an account on a remote server then it needed to call NetGetDCName and ass first parameter give it server address. Then use PDC in other functions in this example.
REMEMBER to run this code as Administrator(even IDE if in debug) or You will have access denied error.

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <lmcons.h>
#include <lmaccess.h>
#include <lmerr.h>
#include <lmapibuf.h>
#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib, "netapi32.lib")

NET_API_STATUS NetSample( LPWSTR lpszDomain,
                          LPWSTR lpszUser,
                          LPWSTR lpszPassword,
                          LPWSTR lpszLocalGroup )
{

    USER_INFO_1               user_info;
    LOCALGROUP_INFO_1         localgroup_info;
    LOCALGROUP_MEMBERS_INFO_3 localgroup_members;
    LPWSTR                    lpszPrimaryDC = NULL;
    NET_API_STATUS            err = 0;
    DWORD                     parm_err = 0;

// Get the name of the primary domain controller.
// Be sure to free the returned buffer.
// On local machine we don't need to do this

    //err = NetGetDCName( NULL,                    // local computer
    //               NULL,                   // domain name
    //               (LPBYTE *) &lpszPrimaryDC );  // returned PDC

    //if ( err != 0 )
    //{
    //    printf( "Error getting DC name: %d\n", err );
    //    return( err );
    //}

// Set up the USER_INFO_1 structure.

    user_info.usri1_name = lpszUser;
    user_info.usri1_password = lpszPassword;
    user_info.usri1_priv = USER_PRIV_USER;
    user_info.usri1_home_dir = TEXT("");
    user_info.usri1_comment = TEXT("");
    user_info.usri1_flags = UF_SCRIPT;
    user_info.usri1_script_path = TEXT("");

        //err = NetUserDel(NULL, L"NowyUse2r");  this will delete user

    err = NetUserAdd( NULL/*lpszPrimaryDC*/,        // PDC name
                      1,                    // level
                      (LPBYTE) &user_info,  // input buffer
                      &parm_err );          // parameter in error

    switch ( err )
    {
    case 0:
        printf("User successfully created.\n");
        break;
    case NERR_UserExists:
        printf("User already exists.\n");
        err = 0;
        break;
    case ERROR_INVALID_PARAMETER:
        printf("Invalid parameter error adding user; parameter index = %d\n",
                parm_err);
        NetApiBufferFree( lpszPrimaryDC );
   return( err );
    case ERROR_ACCESS_DENIED:
    printf("ERROR_ACCESS_DENIED; parameter index = %d\n",
                parm_err);
        NetApiBufferFree( lpszPrimaryDC );
        return( err );
    default:
        printf("Error adding user: %d\n", err);
        NetApiBufferFree( lpszPrimaryDC );
        return( err );
    }

// Set up the LOCALGROUP_INFO_1 structure.

    localgroup_info.lgrpi1_name = lpszLocalGroup;
    localgroup_info.lgrpi1_comment = TEXT("Sample local group.");

    err = NetLocalGroupAdd( NULL,    // PDC name
                  1,                          // level
                  (LPBYTE) &localgroup_info,  // input buffer
                  &parm_err );                // parameter in error

    switch ( err )
    {
    case 0:
        printf("Local group successfully created.\n");
        break;
    case ERROR_ALIAS_EXISTS:
        printf("Local group already exists.\n");
        err = 0;
        break;
    case ERROR_INVALID_PARAMETER:
        printf("Invalid parameter error adding local group; parameter index = %d\n",
                err, parm_err);
        NetApiBufferFree( lpszPrimaryDC );
        //return( err );
    default:
        printf("Error adding local group: %d\n", err);
        NetApiBufferFree( lpszPrimaryDC );
        //return( err );
    }

// Now add the user to the local group.

    localgroup_members.lgrmi3_domainandname = lpszUser;

    err = NetLocalGroupAddMembers( NULL,        // PDC name
                           lpszLocalGroup,               // group name
                           3,                            // name
                           (LPBYTE) &localgroup_members, // buffer
                           1 );                          // count

    switch ( err )
    {
    case 0:
        printf("User successfully added to local group.\n");
        break;
    case ERROR_MEMBER_IN_ALIAS:
        printf("User already in local group.\n");
        err = 0;
        break;
    default:
        printf("Error adding user to local group: %d\n", err);
        break;
    }

    NetApiBufferFree( lpszPrimaryDC );
    return( err );
}

int main()
{
    NET_API_STATUS err = 0;

    printf( "Calling NetSample.\n" );
    err = NetSample( L"",
                     L"NewUser",
                     L"SamplePswd",
                     L"Users" );
    printf( "NetSample returned %d\n", err );
    return( 0 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文