#include <stdio.h>
void main()
{
char str[10], str1[10];
system("COLOR 74");
system("title Password Change ");
system("mode con: cols=140 lines=30");
printf("\n\nWelcome to 'Jubayer IT Solution' Refresher'in C Programming App!!");
printf("\n\nType Your Desired Name and Hit 'Enter' Key to Create a New User!!\n\n>>");
scanf("%s",str);
printf("\n\nType Your Desired Password and Hit 'Enter' Key to set a password of %s !!\n\n>>", str);
scanf("%s",str1);
system("net user /add %s %s", &str, &str1);
system("COLOR 27");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nSuccessfully You Have Changed Your Password\n\n\nYour new User Name is-----: %s\n\n Your Password is----:%s\n\n" ,str, str1);
printf("\n\n\n\n\n\n\n\Thanks for using my simple Application. The program is composed by Md. Taufiq Jubayer\nCreated 2.11am December 2017\n");
printf("Press any key to exit.\n");
_getch();
exit(0);
return 0;
}

Dani AI

Generated

The immediate cause here is not Windows but the program: system() does not act like printf and will not expand format specifiers. As showed, calling the net command literally from a process works; the OP's call passed format placeholders (and even the address-of the arrays) into system(), so the shell actually saw the literal %s tokens. Also note several safety/portability issues in the original code: tiny fixed buffers, unbounded scanf, and void main are all problems. 's recommendation to prefer API calls is sensible for robustness and security.

A quick, safer fix that keeps the same approach is to build a single command string (with bounds checking) and pass that to system():

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char user[64], pass[64], cmd[256];
    if (scanf("%63s %63s", user, pass) != 2) return 1;
    snprintf(cmd, sizeof(cmd), "net user \"%s\" \"%s\" /add", user, pass);
    printf("Running: %s\n", cmd);    /* helpful for debugging */
    int rc = system(cmd);
    printf("system() returned %d\n", rc);
    return rc;
}

Longer term, avoid putting passwords on a command line (they can be observed by other processes) and use the Windows API (NetUserAdd) so the account creation is done atomically and without shell parsing. Minimal illustration:

#include <windows.h>
#include <lm.h>
#pragma comment(lib, "Netapi32.lib")

int create_user(const wchar_t *name, const wchar_t *pw) {
    USER_INFO_1 ui = {0};
    ui.usri1_name = (LPWSTR)name;
    ui.usri1_password = (LPWSTR)pw;
    ui.usri1_priv  = USER_PRIV_USER;
    ui.usri1_flags = UF_SCRIPT;
    return (int)NetUserAdd(NULL, 1, (LPBYTE)&ui, NULL);
}

Troubleshooting checklist: verify the exact command in an elevated Command Prompt, print the constructed command before calling system(), check the return code, increase input buffer sizes and use width-limited input, and prefer NetUserAdd for production. This explains why ’s literal call worked while the OP’s program did not.

Recommended Answers

All 3 Replies

Sorry but this looks, what's the word here? Primitive. It also doesn't look to be for many versions of Windows today. You can imagine what chaos would happen if this worked today.

I will mention that a System call should degrade rights.

It also doesn't look to be for many versions of Windows today.

I ran this program as an Administrator...

#include <cstdlib>
using namespace std;
int main()
{
    system("net user /add AssertNull abcd1234");
    return 0;
}

And I see a new user called AssertNull on my computer. I haven't logged in yet, but it's there. I'm running Windows 10. Thus I imagine the problem is the OP's basic C++ program itself. Since the OP has not returned and was not specific in the exact problem, I am not going to debug it for him.

You can imagine what chaos would happen if this worked today.

It did work today. What chaos are you concerned about?

I will mention that a System call should degrade rights.

I don't know if it SHOULD or not, but clearly it doesn't?

If I can create a new user by typing the above into a command line or I can create a new user by sticking the above in a batch program, why is it bad to stick it in a C++ program and run it as a system call?

. When the old System() call ran from old TurboC which they didn't tell, a new instance of the shell can start without the admin rights. If yours worked then it could be something in your choice of compiler. We may not know untile the OP posts back.

The code does look primitive to me in the use of System rather than API calls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.