Insert value from GUI CString into Registry
-
Hi, So I have this code to open a handle hKey which works fine. RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyProgram", 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); Then I try to set a value from a GUI CString member variable m_license_key into registry. RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_DWORD,(LPBYTE)&m_license_key, sizeof(DWORD)); The set function also works fine other than the fact that "rubbish" gets inserted as value for my registry key. In other words, HKEY_LOCAL_MACHINE\\SOFTWARE\\MyProgram\\PROGRAM_KEY is added, but the value is not what I keyed into m_license_key (it is just some strange text or some 0xHex numbers) To be honest, I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx. Any help would be greatly appreciated. Thanks.
-
Hi, So I have this code to open a handle hKey which works fine. RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyProgram", 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); Then I try to set a value from a GUI CString member variable m_license_key into registry. RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_DWORD,(LPBYTE)&m_license_key, sizeof(DWORD)); The set function also works fine other than the fact that "rubbish" gets inserted as value for my registry key. In other words, HKEY_LOCAL_MACHINE\\SOFTWARE\\MyProgram\\PROGRAM_KEY is added, but the value is not what I keyed into m_license_key (it is just some strange text or some 0xHex numbers) To be honest, I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx. Any help would be greatly appreciated. Thanks.
I use this code...
bool CRegistry::SetKey(HKEY hKey, CString csSubkey, CString csValName, CString csVal)
{
if(hKey != HKEY_CURRENT_USER)
return false;if(csSubkey.IsEmpty() || csValName.IsEmpty() || csVal.IsEmpty()) return false; HKEY hWkKey; LONG lRetorn = RegOpenKeyEx(hKey,csSubkey,0,KEY\_ALL\_ACCESS,&hWkKey); if(lRetorn == ERROR\_SUCCESS) { int iLength = csVal.GetLength(); lRetorn = RegSetValueEx(hWkKey, csValName, 0, REG\_SZ, (BYTE \*)csVal.GetBuffer(0), iLength); RegFlushKey(hWkKey); RegCloseKey(hWkKey); if( lRetorn == ERROR\_SUCCESS) return true; } return false;
}
Hope this helps.
-
Hi, So I have this code to open a handle hKey which works fine. RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyProgram", 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); Then I try to set a value from a GUI CString member variable m_license_key into registry. RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_DWORD,(LPBYTE)&m_license_key, sizeof(DWORD)); The set function also works fine other than the fact that "rubbish" gets inserted as value for my registry key. In other words, HKEY_LOCAL_MACHINE\\SOFTWARE\\MyProgram\\PROGRAM_KEY is added, but the value is not what I keyed into m_license_key (it is just some strange text or some 0xHex numbers) To be honest, I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx. Any help would be greatly appreciated. Thanks.
-
Hi, So I have this code to open a handle hKey which works fine. RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\MyProgram", 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL); Then I try to set a value from a GUI CString member variable m_license_key into registry. RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_DWORD,(LPBYTE)&m_license_key, sizeof(DWORD)); The set function also works fine other than the fact that "rubbish" gets inserted as value for my registry key. In other words, HKEY_LOCAL_MACHINE\\SOFTWARE\\MyProgram\\PROGRAM_KEY is added, but the value is not what I keyed into m_license_key (it is just some strange text or some 0xHex numbers) To be honest, I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx. Any help would be greatly appreciated. Thanks.
You can't cast a CString to a LPBYTE. If you want the text in the CString saved then use the REG_SZ type... RegSetValueEx(hKey, "PROGRAM_KEY", 0L, REG_SZ,(LPBYTE)(LPCTSTR)m_license_key, m_license_key.GetLength() + 1);
-
trumper wrote:
I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx.
They hide that information in the documentation[^] :rolleyes:
led mike
led mike wrote:
They hide that information
bastards
-
led mike wrote:
They hide that information
bastards
-
trumper wrote:
I do not know what to specify for parameters 4, 5, 6 of RegSetValueEx.
They hide that information in the documentation[^] :rolleyes:
led mike
How mean they do it to us :-D
Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா