RegSetValueEx I can not create a rule in registry using QT

0

Hi, I am using RegSetValueEx to create my rule in register using Qt when I use codeblocks, but when I create it with Qt it gives me a conversion problem, the code is as follows:

string subclave="Hello";
string valor="Anyone";
LPCTSTR _subclave = TEXT("");
LPCTSTR _valor = TEXT("");
long crear = RegSetValueEx(key, _subclave, 0, REG_SZ, (LPBYTE) _valor, strlen(_valor) * sizeof(char));

Errors I get:

I tried to do the conversion but when I do, he writes me in Chinese. The code when I make the changes you ask me:

string hola="hola";
std::wstring stemp = std::wstring(hola.begin(), hola.end());
LPCWSTR subclave = stemp.c_str();
const char * valor = "hola";
long crear = RegSetValueEx(key, subclave, 0, REG_SZ, (LPBYTE) valor,     strlen(valor) * sizeof(char));

Capture:

    
asked by Perl 04.02.2017 в 12:35
source

1 answer

1

There you are not using Qt but, in that case, QtCreator that is not more than an IDE for specialized C ++ to develop with Qt . Qt is not more than a C ++ framework and you are not making use of any characteristic of Qt , at least not in the code that you show. In fact if it works in codeblocks is, as you can see below, pure chance (you have a 50% chance that it works).

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

strlen(_valor) : _valor is not type const char* but LPCTSTR . LPCTSTR is an alias whose specific type is variable:

  • const char* if _UNICODE is not declared.
  • const wchar_t* if declared _UNICODE .

Since the implementation of strlen is always fixed size_t strlen(const char*) it is clear that calling this function directly can be a source of conflicts.

There is a function, called _tcslen that should be used for this case ... and forget about multiplying by sizeof(char) ... it will not solve the problem:

long crear = RegSetValueEx(key, _subclave, 0, REG_SZ, (LPBYTE) _valor, _tcslen(_valor));
    
answered by 04.02.2017 / 17:06
source