Solved Setting registry key value

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

user123

Newbie
Full Member
Jul 8, 2014
17
142
0
Im trying to set a regristry key to a value, but im having problems.
const char PATH[] = "registry value here";
regModifyResult = RegSetValueEx(hkey,
"newregkey",
0,
REG_SZ,
(BYTE*)PATH,
strlen(PATH));
Goal: I want to input a string, which is way easier to handle, instead of a const char array.
i.e.
string value = "put regristry value here";
Also, what does const mean specificly?
I only know it makes the variable unchangable...

I tried converting the two, does not give errors, only when running the code, it gives https://i.imgur.com/zpYP6DD.png .

void CrashFunction()
{
string str = "asdasdsa";
char PATH[] = "";
strcpy_s(PATH, str.c_str());
}
What do I have to do?
Besides solution, I also need explanaition, im new to C++.
Its late so I probably forgot some info, will add tomorrow if that is the case.
 

Rake

Cesspool Admin
Administrator
Jan 21, 2014
12,118
78,998
2,392
Here is an example on how to write to the registry. I would've thrown something together myself but I lack the time to do so. I hope this solves your problem. :)

C++:
#include <windows.h>
#include <string.h>

int WINAPI WinMain(HINSTANCE hinst, HINSTANCE previnst,
                   LPSTR lpszArgs, int Mode){

    HKEY hMykey; //Handle to your key
    DWORD pDWDisp; // Ignore for this
    LONG lRes; // Test Success
    char prog[] = "C:\\WINNT\\system32\\sol.exe"; //Key to launch

lRes = RegCreateKeyEx(HKEY_CURRENT_USER,
               "Software\\Microsoft\\Windows\\CurrentVersion\\run",
               0,"Whatever",REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,
               NULL,&hMykey,&pDWDisp); // Open a key for edit

if(lRes != ERROR_SUCCESS){
    MessageBox(0,"Error opening key","",MB_OK);
    exit(0);// Shutdown on fail
}

lRes = RegSetValueEx(hMykey,"Sol",0,REG_SZ,
                   (LPBYTE)prog,strlen(prog)+1);// Add your key value

if(lRes != ERROR_SUCCESS){
    MessageBox(0,"Error saving record","",MB_OK);
    RegCloseKey(hMykey);
    exit(0);// Shutdown on fail
}

MessageBox(0,"Success!! Registry value recorded","",MB_OK);
RegCloseKey(hMykey);
return 0;
}

// Source: https://cboard.cprogramming.com/c-programming/8583-editing-windows-registry-c.html
Im afraid not, since it still uses character arrays.
std::string::c_str() gives you a char array ptr
 
Last edited:

user123

Newbie
Full Member
Jul 8, 2014
17
142
0
Im afraid not, since it still uses character arrays.
I solved it by this:
C++:
string filepath = ExePath(true);
char PATH[50] = "";
for (int i = 0; i < filepath.length(); i++)
{
	PATH[i] = (filepath.at(i));
}
Its clumsy but it works
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods