Why does not this code work in Qt? creating rule in registry

0

Hi, I'm creating a rule in the registry using c ++ with QT and the windows function for it incorporates the following libraries:

#include <iostream>
#include <windows.h>
#include <conio.h>

Then I create the rule using the following example:

//asignamos key el valor nulo
      HKEY key = 0;
      //elegimos la ruta donde se creara
      LPCTSTR ruta = TEXT("SOFTWARE\Microsoft\Windows\CurrentVersion\Run\");
      //con status podemos saber si se creo correctamente
      long status = RegOpenKey(HKEY_CURRENT_USER, ruta, &key);
      if (status != 0)
      {
        cout << "Win error: " << GetLastError() <<  endl;
      }
      else
      {
        //cout << "Nombre de la subclave: ";
        string subclave="System";
        //getline(cin, subclave);
        cout << "Valor de la subclave: ";
        string valor="C:\miprograma.exe";
        //getline(cin, valor);
        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));
        if (crear != 0)
        {
          cout << "Win error: " << GetLastError() << endl;
        }
        else
        {
          cout << "Subclave creada correctamente" << endl;
        }
      }

      RegCloseKey(key);

But that gives me the following errors:

My question is in which you can not use iostream or use the following code? It should work, I do not know why I get these errors since I included the libraries. Finally, I need another way to create qsettings for the application I'm creating that will come with this option in my desktop application.

errors when using .c_str:

    
asked by jeronimo urtado 04.02.2017 в 00:26
source

1 answer

2
  

Hi, I'm creating a rule in the registry using c ++ with QT

I correct you. You will be using QtCreator which is the IDE that brings Qt, but you are not using any feature or characteristic of Qt, then all uses would give you the same compile from codeblocks or any other IDE.

  

My question is in which you can not use iostream or use the following code?

Qt is nothing more than a framework for C ++, any C ++ feature will be available also if you work with Qt. And this includes the standard C ++ library.

That said, let's analyze your problems:

string subclave="System";

In C ++ the functions and classes of the standard library are grouped by namespaces. To be able to call a functionality of the standard library it is necessary to indicate the corresponding namespace and this can be done in three different ways:

  • Indicate the namespace in each use. The main advantage of this system is that always we can be sure of what functionality we are resorting to.

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      std::string a;
      std::cout << "Introduce un nombre ";
      std::cin >> a;
      std::cout << a;
    }
    
  • Import the entire namespace. Although it is the first thing they usually teach you, it is not usually the most recommended.

    #include <iostream>
    #include <string>
    
    // Puede ir antes o después de los includes.
    // Por motivos históricos se suelen poner los includes primero
    using namespace std; 
    
    int main()
    {
      string a;
      cout << "Introduce un nombre ";
      cin >> a;
      cout << a;
    }
    
  • Import only those functions or classes that interest us. It is a cleaner mechanism since the import is more selective, which helps reduce collisions

    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::string;
    
    int main()
    {
      string a;
      cout << "Introduce un nombre ";
      std::cin >> a; // no se ha importado std::cin
      cout << a;
    }
    
  • On the other hand, the creation of the registry has problems too:

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

    The RegSetValueEx function expects (_value) to be LPCTSTR type and this type is an alias whose specific type may vary:

    • const char* if _UNICODE has not been defined
    • const wchar* if _UNICODE has been defined

    So it does not seem like a good idea to call strlen by passing it a variable of type LPCTSTR since there will be cases where it does not compile. To solve this problem there is the macro _tcslen :

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