regedit rule c++

Hi i am trying to create a regedit rule with the follow code. But i always get one error message of regedit saying me the regedit.exe has stopped working.
The code is the follow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{

 HKEY * key;
 LPCTSTR ruta = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\");
 long status = RegOpenKey(HKEY_LOCAL_MACHINE, ruta, key);
 if (status != 0)
 {
  cout << "No se puede abrir la clave" << endl;
 }
 else
 {
  cout << "Nombre de la subclave: ";
  string subclave;
  getline(cin, subclave);
  cout << "Valor de la subclave: ";
  string valor;
  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 << "Ha ocurrido un error al crear la subclave" << endl;
  }
  else
  {
    cout << "Subclave creada correctamente" << endl;
  }
 }

 RegCloseKey(*key);

 system("PAUSE");
 return 0;
}

Thanks in advance.
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main(int argc, char *argv [])
{

  HKEY key = 0;
  LPCTSTR ruta = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\");
  long status = RegOpenKey(HKEY_LOCAL_MACHINE, ruta, &key);
  if (status != 0)
  {
    cout << "Win error: " << GetLastError() <<  endl;
  }
  else
  {
    cout << "Nombre de la subclave: ";
    string subclave;
    getline(cin, subclave);
    cout << "Valor de la subclave: ";
    string valor;
    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);

  system("PAUSE");
  return 0;
}
thxx!
Topic archived. No new replies allowed.