SYSTEM function problem

Posted on 2006-04-30
Last Modified: 2010-04-01

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");

Question by:ALNMOO
    LVL 12

    Accepted Solution

    >> system("NET USER NewUSER XXXX /add /domain >>c:\new.log");
    This should be modified to
     system("NET USER NewUSER XXXX /add /domain >>c:\\new.log");
    LVL 12

    Assisted Solution

    >> "NET USER NewUSER XXXX /add /domain >>c:\new.log"
    In you string \n will  be taken as escape char new-line.
    Since you want \ in your string make it \\
    LVL 7

    Author Comment

    still it didn't work.

    I think system funaction cann't except parameters like this
    LVL 8

    Expert Comment

    Strange - I've tested (Windows XP) it and it works - log file is created.
    LVL 12

    Expert Comment

    Are you looking for the logfile in same directory or in C: ?
    LVL 86

    Expert Comment

    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?

    LVL 86

    Expert Comment

    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,  

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

        return rc;

    NOTE that both the user name and the password need to be UNICODE.
    LVL 86

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now