English 中文(简体)
在Windows 7 OS上创建新的用户账户
原标题:Creating-deleting new user accounts on Windows 7 OS

我希望在我的法典中创造和删除新的用户。 我设法以“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); 

这应当建立一个新的用户账户,使之成为一个标准用户账户,但总是建立一个用户账户。 这一职能由具有挑战性的提供者承担,因此我不认为问题在于许可。 http://msdn.microsoft.com/en-us/library/windows/desktop/a37064928vs. 8529.aspx”rel=“nofollow”>。

最佳回答
问题回答

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
", 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.
");
        break;
    case NERR_UserExists:
        printf("User already exists.
");
        err = 0;
        break;
    case ERROR_INVALID_PARAMETER:
        printf("Invalid parameter error adding user; parameter index = %d
",
                parm_err);
        NetApiBufferFree( lpszPrimaryDC );
   return( err );
    case ERROR_ACCESS_DENIED:
    printf("ERROR_ACCESS_DENIED; parameter index = %d
",
                parm_err);
        NetApiBufferFree( lpszPrimaryDC );
        return( err );
    default:
        printf("Error adding user: %d
", 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.
");
        break;
    case ERROR_ALIAS_EXISTS:
        printf("Local group already exists.
");
        err = 0;
        break;
    case ERROR_INVALID_PARAMETER:
        printf("Invalid parameter error adding local group; parameter index = %d
",
                err, parm_err);
        NetApiBufferFree( lpszPrimaryDC );
        //return( err );
    default:
        printf("Error adding local group: %d
", 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.
");
        break;
    case ERROR_MEMBER_IN_ALIAS:
        printf("User already in local group.
");
        err = 0;
        break;
    default:
        printf("Error adding user to local group: %d
", err);
        break;
    }

    NetApiBufferFree( lpszPrimaryDC );
    return( err );
}

int main()
{
    NET_API_STATUS err = 0;

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签