How to join a string to a variable of type BYTE path[]

Asked

Viewed 45 times

-1

Guys, my question is how to put string1 from a function strcat(string1,string3); within a string of grabbing windows code user name:system("reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v NOME_DO_SEU_PROGRAMA /d $string1");

#include <iostream>
#include <cstdio>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int main()
{

//pegando usuario do windows

//WINBASEAPI BOOL WINAPI GetUserNameA (LPSTR,PDWORD);

//WINBASEAPI BOOL WINAPI GetUserNameW(LPWSTR,PDWORD);

char acUserName[100];
char destino11[100];
DWORD nUserName = sizeof(acUserName);
char destino[200];
if (GetUserName(acUserName, &nUserName)) {


}   



//movendo 


    int ret;
    char oldname[] = "config/config.exe";
    char string1[] = "C:\\Users\\";
    strcat(string1,acUserName);
    char string3[] = "\\Searches\\config.exe";
    strcat(string1,string3);


  ret = rename(oldname, string1);

   if(ret == 0) {
      printf("File renamed successfully");
   } else {
      printf("Error: unable to rename the file");
   }




//ADICIONAR REGISTRO AQUI AQUI COMO COLOCAR A VARIAVEL STRING1 AI DENTRO DO SYSTEM???

system("reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v NOME_DO_SEU_PROGRAMA /d $string1");






    return 0;
    system("pause");     

}
  • people? Someone can help me?

  • if you already have both strings ready, how difficult it is to use strcpy to copy and strcat to concatenate?

1 answer

0

In C you will need to create a char array that is the size of the two strings in order to concatenate.
Take a look at this example.

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

int main(){
    char string1[] = "reg add HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /t REG_SZ /v NOME_DO_SEU_PROGRAMA /d ";
    char string2[] = "Qualquer coisa que você quiser aqui";
    //Cria a stringTemp que tem que ser do tamanho da string1 + string2;
    char stringTemp[strlen(string1)+strlen(string2)];
    //Joga o conteúdo da string1 em stringTemp
    strcpy(stringTemp,string1);
    //Joga o conteudo da string2 no FINAL da stringTemp, concatenando as duas
    strcat(stringTemp,string2);

    printf("%s",stringTemp);
    //Aqui você pode chamar assim:
   //system(stringTemp);
 return 0;
}

You can test this code above here.

  • Now how to play stringTemp within the system("reg add HKEY_LOCAL_MACHINE Software Microsoft Windows Currentversion Run /t REG_SZ /v NOME_DO_SEU_PROGRAMA /d $stringTempNAOFUNCIONAAQUI

  • What you did with the concatenation part I did up there using strcat, I’m asking how to put the char type variable inside the system function.

  • You will spend the string time in there, you concatenate everything you need and just pass it in the function, so system(stringTempo);

  • If you test this will see that it will not work, I’m talking about putting inside it:system("reg add HKEY_LOCAL_MACHINE Software Microsoft Windows Currentversion Run /t REG_SZ /v NOME_DO_SEU_PROGRAMA /d STRINGTEMPO");

  • @Mayararabelo , give a look, I edited to get closer to your reality. Of course in the part where I put the system() commented you uncomment in your code.

  • At the time of running the system gave invalid syntax

  • @Mayararabelo gives a printf on how was his command and post, probably this with some wrong parameter. Make sure you give space also at the end of the string for when concatenate do not end up "pasting" the 2

  • like this: char string11[] = "reg add HKEY_LOCAL_MACHINE Software Microsoft Windows Currentversion Run /t REG_SZ /v NOME_DO_SEU_PROGRAMA /d "; char string22[] = string1;

  • Why doesn’t it work? char string22[] = string1;

  • in php of the right

  • @Mayararable In C it does not accept you to initialize a string vector with another vector, so there are strcat and strcpy functions to concatenate and copy the values. Always remembering that the vector that will receive the data of the other vector must be large enough for this, so the question of using a third vector that is created with the size of the first + the size of the second, and then concatenate everything in it.

Show 6 more comments

Browser other questions tagged

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