Error writing registry key c++
Hello, i have this code
this code read exe's path and create a registry key with the path as value
but he give me an error! why?
error: invalid cast from type 'std::string {aka std::basic_string<char>}' to type 'LPBYTE {aka unsigned char*}
error: cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'size_t strlen(const char*)
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
|
#include <iostream>
#include <windows.h>
using namespace std;
string exePath(){
string path;
char buffer[MAX_PATH];
cout<<"reading path\n";
GetModuleFileName( NULL, buffer, MAX_PATH );
string::size_type pos = string( buffer ).find_last_of( "\\/" );
path=string( buffer ).substr( 0, pos)+"\\chiavi registro.exe";
return path;
}
int main()
{
cout<<exePath();
HKEY key;
if(RegOpenKey(HKEY_CURRENT_USER,TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"),&key)!=ERROR_SUCCESS){
cout<<"Errore 1\n";
}
if(RegSetValueEx(key,TEXT("Startup"),0,REG_SZ,(LPBYTE)exePath(),strlen(exePath())*sizeof(char))!=ERROR_SUCCESS){
cout<<"Errore 2\n";
RegCloseKey(key);
}else{
cout<<"Success\n";
}
return 0;
}
|
Last edited on
Line 24 must be:
if(RegSetValueEx(key,TEXT("Startup"),0,REG_SZ,(LPBYTE)exePath().c_str(),strlen(exePath().c_str())*sizeof(char))!=ERROR_SUCCESS)
Yeah Thanks a lot!!!
For people who need it:
this is the working code
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
|
#include <iostream>
#include <windows.h>
using namespace std;
string exePath(){
string path;
char buffer[MAX_PATH];
cout<<"reading path\n";
GetModuleFileName( NULL, buffer, MAX_PATH );
string::size_type pos = string( buffer ).find_last_of( "\\/" );
path=string( buffer ).substr( 0, pos)+"\\chiavi registro.exe";
return path;
}
int main()
{
cout<<exePath();
HKEY key;
if(RegOpenKey(HKEY_CURRENT_USER,TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"),&key)!=ERROR_SUCCESS){
cout<<"Errore 1\n";
}
if(RegSetValueEx(key,TEXT("Startup"),0,REG_SZ,(LPBYTE)exePath().c_str(),strlen(exePath().c_str())*sizeof(char))!=ERROR_SUCCESS){
cout<<"Errore 2\n";
RegCloseKey(key);
}else{
cout<<"Success\n";
}
return 0;
}
|
Topic archived. No new replies allowed.