-2

When im try to write a RegEdit Key, he only writes only the first 4 Charaters of the file path.

HKEY hKey;
RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_SET_VALUE, &hKey);
RegSetValueEx(hKey, "Test", 0, REG_SZ, (BYTE*)file, sizeof(file));
RegCloseKey(hKey);
zemicolon
  • 152
  • 1
  • 13

3 Answers3

2

sizeof(pointer) gives 4 on 32 bit systems. Try string::size (online documentation) instead.

std::string filename ("file.exe");
RegSetValueEx( ..., filename.size());
IInspectable
  • 46,945
  • 8
  • 85
  • 181
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
1

Apparently your file is a pointer. sizeof(file) is size of pointer in bytes, which is 4 on your platform.

What is file? How is it declared? What does it point to? Why are you using sizeof on it in order to determine how many bytes to write? If file is a pointer to a C-style string, then sizeof will not help you to determine the length of that string. You need strlen for that.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

If file is a char array, then...

RegSetValueEx(hKey, "Test", 0, REG_SZ, (BYTE*)file, strlen(file) + 1); // API says to include terminating NULL.

"The size of the information pointed to by the lpData parameter, in bytes. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character or characters."

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724923(v=vs.85).aspx

Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29