Create a rule in regedit with C ++

1

Hi, I want my application to be started from the startup, that is, when I start up my windows. It is an application that I use very often and it is annoying to start it over and over again. Well what I want my subkey value is the path of the program I am using. What I've been up to now is:

#include "stdafx.h"
#include <iostream>
#include <windows.h>

int main()
{
    HKEY * key;
    LPCTSTR ruta = TEXT("SOFTWARE\Microsoft\Windows\CurrentVersion\Run\");
    long status = RegOpenKey(HKEY_LOCAL_MACHINE, ruta, key);
    string valor;
    string subclave;
    LPCTSTR _subclave = TEXT(subclave.c_str());
    LPCTSTR _valor = TEXT(valor.c_str());
    long crear = RegSetValueEx(*key, _subclave, 0, REG_SZ, (LPBYTE)_valor, strlen(_valor) * sizeof(char));
    return 0;
}

But it shows me several errors like the subkey does not recognize and such:

And I wanted the value of my subkey to be the path of my program started. Any solution or do you know the reason for these errors?

    
asked by jeronimo urtado 10.12.2016 в 14:31
source

1 answer

1

The macro TEXT expects to receive a variable of type LPTSTR , the type LPTSTR being an alias of char* :

void TEXT(
   LPTSTR string
);

And yet your code tries to do the following:

string subclave;
LPCTSTR _subclave = TEXT(subclave.c_str());

If you look at the documentation on c_str() , you will see that it returns a pointer of type const char and that type will not be directly compatible with the types expected by the macro TEXT .

On the other hand, the type LPCSTR is, directly, an alias of const char* . That is, the conversion you make should look like this:

LPCTSTR _subclave = subclave.c_str();

On the other hand, notice that you are not giving values neither to subclave nor to valor , then the call:

long crear = RegSetValueEx(*key, _subclave, 0, REG_SZ, (LPBYTE)_valor, strlen(_valor) * sizeof(char));

You will receive two blank parameters.

    
answered by 12.12.2016 / 10:00
source