get directory

Asked

Viewed 41 times

1

I would like to know how to add the name of the user where it is in quotes written "I must put the name of the user here"

#include "stdafx.h"
#include<iostream>
#include<Windows.h>
#include<lmcons.h>

using namespace std;

int main() {
//User Name
TCHAR username[UNLEN + 1];
DWORD username_len = UNLEN + 1;

GetUserName((TCHAR*)username, &username_len);

wcout << username << endl;

//Computer name
TCHAR compname[UNCLEN + 1];
DWORD compname_len = UNCLEN + 1;

GetComputerName((TCHAR*)compname, &compname_len);

wcout << compname << endl;

cin.get();

system("reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v formula /d C://users//"DEVO COLOCAR O NOME DO USUARIO AQUI"//downloads//formula.exe");

return EXIT_SUCCESS;

}

1 answer

1


There are some ways to do what you want. Among the various show two.

sprintf

With sprintf can interpolate a string with whatever values you want. You first need to build the string in the form of char[] which will take the final text and then call sprintf about her:

char username[] = "carlos";
char cmd[200];
sprintf(cmd, "reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v formula /d C://users//%s//downloads//formula.exe", username);

system(cmd);

Look closely at the text used in sprintf that has %s at the place where the name is placed.

See this example in Ideone

Concatenation of string

Using the operator + about a string c++ can concatenate a char[] directly, making the whole process easy too:

char username[] = "carlos";
string cmd = "reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v formula /d C://users//";
cmd += username;
cmd += "//downloads//formula.exe";

system(cmd.c_str());

See also this example in Ideone

This version has some differences to point out. The command is built as string and not char[]. For this reason the call to system that is supposed to pass the char* has to be with cmd.c_str() to obtain the string format char[].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.