Hola a todos,
estoy tratando de crear una clave en el registro de windows, pero me lanza un error al momento de crearla, agrego que la compilación fue sin error, estuve viendo otros hilos abiertos en la página y logro solucionarme una parte pero lo principal no puedo, a ver si me dan una manito, les dejo los códigos que están en mi problema:
Primeramente empezare con "Leer un valor de una clave":
con este código puedo leer sin problemas:
Código:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TRegistry *Registry = new TRegistry(KEY_ALL_ACCESS);
Registry->RootKey = HKEY_CURRENT_USER;
// Poner true si queremos que cree la clave si ésta no existe
if(Registry->KeyExists("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
Registry->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",false);
ShowMessage(Registry->ReadString("miprograma"));
Registry->CloseKey();
}
}
Pero cuando quiero leer "HKEY_LOCAL_MACHINE", no me muestra nada, pero se compila bien, a pesar que lo ejecute como administrador y tambien existe la clave, el código es este (solo modifique "HKEY_CURRENT_USER" por "HKEY_LOCAL_MACHINE"):
Código:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TRegistry *Registry = new TRegistry(KEY_ALL_ACCESS);
Registry->RootKey = HKEY_LOCAL_MACHINE;
if(Registry->KeyExists("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
Registry->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",false);
ShowMessage(Registry->ReadString("miprograma"));
Registry->CloseKey();
}
}
buscando por el foro pude encontrar solución, para leer "HKEY_LOCAL_MACHINE", agregando unas lineas y cambiando otra, el código es este:
Código:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// esta linea hace que me muestre la clave, diferenciando mi SO actual (WIndows 7 64 bits)
std::auto_ptr<TRegistry> rRegistro(new TRegistry(KEY_READ | KEY_WOW64_64KEY));
rRegistro->RootKey = HKEY_LOCAL_MACHINE;
if(rRegistro->KeyExists("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
rRegistro->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",false);
ShowMessage(rRegistro->ReadString("miprograma"));
rRegistro->CloseKey();
}
}
mas detallado con las librerias necesarias esta acá:
clubdelphi.com/foros/showpost.php?p=468929&postcount=9
amolde el código para crear llaves, pero aún asi no puedo crearlas en "HKEY_LOCAL_MACHINE", solo puedo en "HKEY_CURRENT_USER", este es el código:
Código:
std::auto_ptr<TRegistry> Registry(new TRegistry(KEY_READ | KEY_WOW64_64KEY));
Registry->RootKey = HKEY_LOCAL_MACHINE;
if(Registry->KeyExists("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
Registry->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",false);
Registry->WriteString("miprograma2","C:\\Video tutoriales\\registro\\pregistro.exe");
Registry->CloseKey();
}
ShowMessage("Listo");
ahora mi pregunta es, si ya puedo leer claves desde "HKEY_LOCAL_MACHINE" agregando esas librerias que faltaban, porque no puedo crearlas?; pero puedo crear claves normalmente siempre en cuando este en "HKEY_CURRENT_USER", quedo atento a sus comentarios.
Saludos.