RegCreateKey

GoodMorning, I've a question about the RegCreateKey in Visual studio in windows 10. I try to create a new key registry. The code is compiled successfully, but no new key is generated. I follow some forum online without any success. Can I ask some help to solve the problems? I write here the code:


int main(){
...
HKEY hKey;
TCHAR RegValue[]=TEXT("myprogram");
BYTE ProgramPath[]="C:\\User\\Desktop\\file.bat"
RegCreateKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),&hKey);
RegCloseKey(hKey);


...}


or maybe do someone has already a c++ code to suggest me?

thank really much
Best Regards
When working with the Win API it's always a good idea to check the return values.
1
2
3
4
5
if(RegCreateKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 
&hKey) != ERROR_SUCCESS)
    cout << "Win error: " << ::GetLastError();
  else
    cout << "Key creation succesfull)";
closed account (E0p9LyTq)
RegCreateKey function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724842(v=vs.85).aspx

Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the RegCreateKeyEx function. However, applications that back up or restore system state including system files and registry hives should use the Volume Shadow Copy Service instead of the registry functions.

RegCreateKeyEx function
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724844(v=vs.85).aspx
Thank you very very much for the answer. I tried to use that function but I have some problems to understand with parameter use. Maybe, if I'm not impolite, can I ask you if you have an example where you use that function?

thank you really much
Best regards
thank you really much for the answer and analyzing the code I reached the following one, which create correctly the registry key
1
2
3
4
5
6
7
	HKEY hkey;
	long regOpenResult;
	const char path[] = "C:\\Users\\Desktop\\program.bat";
	regOpenResult = RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run",0,KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hkey);
	LPCSTR program = "program";
	RegSetValueEx(hkey, (LPCWSTR) program, 0, REG_SZ, (BYTE*) path, strlen(path));
	RegCloseKey(hkey);


but the results is very strange:
name value=灣⵰畯灴瑵
data value=㩃啜敳獲䑜湥獩䑜獥瑫灯捜灰漭瑵異⹴慢

when I want:
data value= C:\\Users\\Desktop\\program.bat

thank you really much
Hi
It seems to me you're mixing unicode and multibyte strings.
Please tell us if you compile for unicode or multibyte
thank you really much...I solved the problem imposing multibyte :)


thank you really much
Best regards
Topic archived. No new replies allowed.