ALNMOO
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");
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");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Strange - I've tested (Windows XP) it and it works - log file is created.
Are you looking for the logfile in same directory or in C: ?
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?
system("cmd.exe /c NET USER NewUSER XXXX /add /domain >> c:\\new.log");
But, why using 'system()' if there's an API?
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.
#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.
May I point out that the answer you accepted is partially incorrect in that context?
ASKER
I think system funaction cann't except parameters like this