Avatar of ALNMOO
ALNMOOFlag for Saudi Arabia

asked on 

SYSTEM function problem

hi

why is only half this command executed !!! I mean it create the user but didn't create the logs?


  system("NET USER NewUSER XXXX /add /domain >>c:\new.log");




C++

Avatar of undefined
Last Comment
jkr
ASKER CERTIFIED SOLUTION
Avatar of rajeev_devin
rajeev_devin

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of rajeev_devin
rajeev_devin

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of ALNMOO
ALNMOO
Flag of Saudi Arabia image

ASKER

still it didn't work.

I think system funaction cann't except parameters like this
Strange - I've tested (Windows XP) it and it works - log file is created.
Avatar of rajeev_devin
rajeev_devin

Are you looking for the logfile in same directory or in C: ?
Avatar of jkr
jkr
Flag of Germany image

The file redirection is done by the shell, not, so that should be

system("cmd.exe /c NET USER NewUSER XXXX /add /domain >> c:\\new.log");

But, why using 'system()' if there's an API?

Avatar of jkr
jkr
Flag of Germany image

Speaking of that API - use

#include <tchar.h>
#include <lmcons.h>
#include <lmerr.h>
#include <lmaccess.h>
#include <lmapibuf.h>

NET_API_STATUS  AddNewUser  (   LPWSTR  pwstrName, LPWSTR   pwstrPwd)
{
    NET_API_STATUS              rc;
    USER_INFO_1                 ui1;

    wchar_t                     awcDomUsrName       [   LM20_DNLEN  +   LM20_UNLEN  +   1];
    wchar_t                     awcWkSta            [   MAX_COMPUTERNAME_LENGTH + 2];
    DWORD                       dwBufSiz            =   MAX_COMPUTERNAME_LENGTH + 1;

    ZeroMemory  (   &ui1,   sizeof (    USER_INFO_1));

    ui1.usri1_name          =   pwstrName;
    ui1.usri1_password      =   pwstrPwd;
    ui1.usri1_password_age  =   0;
    ui1.usri1_priv          =   USER_PRIV_USER;
    ui1.usri1_flags         =   UF_DONT_EXPIRE_PASSWD | UF_NORMAL_ACCOUNT | UF_SCRIPT;
    ui1.usri1_home_dir      =   L"";
    ui1.usri1_comment       =   L"";
    ui1.usri1_script_path   =   L"";


    GetComputerName (   awcWkSta,   &dwBufSiz);

    wsprintf    (   awcDomUsrName,  
                    L"%s\\%s",  
                    awcWkSta,
                    pwstrName
                );  

    rc  =   NetUserAdd  (   NULL,   1,  ( LPBYTE) &ui1, &dwParmErr);

    return rc;
}

NOTE that both the user name and the password need to be UNICODE.
Avatar of jkr
jkr
Flag of Germany image

May I point out that the answer you accepted is partially incorrect in that context?
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo