0

I have the problem that I use REG ADD to call a path that contains a space. When I then run my code, I get the following error:

Syntax Error

Here is my code:

system(("REG ADD HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion /v ProductId /t REG_SZ /d " + uuid + " /f").c_str());
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    The code does not compile. You can't use `+` with the first operand being a char array in C++. – kotatsuyaki Jun 11 '22 at 07:55
  • As you're clearly targeting Windows, then you probably should be using `CreateProcessW()` instead of `system()`. – Dai Jun 11 '22 at 07:57
  • @kotatsuyaki *"You can't use `+` with the first operand being a char array in C++."* -- you can sometimes, but it depends on the type of `uuid`, which is not in the question. – JaMiT Jun 11 '22 at 07:59
  • 3
    @Dai no, in that case you should use [registry functions](https://learn.microsoft.com/en-us/windows/win32/sysinfo/registry-functions) in Win32 API directly. Never spawn a new process just to do this – phuclv Jun 11 '22 at 08:02
  • Does this answer your question? [How do I use spaces in the Command Prompt?](https://stackoverflow.com/questions/6376113/how-do-i-use-spaces-in-the-command-prompt) – JaMiT Jun 11 '22 at 08:04
  • @phuclv You can't do it in-proc if you want to write to HKLM but your process isn't elevated, but if you shell-out to `reg.exe` with `CreateProcessW` then Windows will prompt for elevation. This is how I implement simple service control buttons in UIs that cannot require elevation to run. – Dai Jun 11 '22 at 08:07
  • The + is not the problem its the path – Jens Schulze Jun 11 '22 at 11:43
  • 1
    @JensSchulze What is the full and exact "syntax error" you're getting? I note that a syntax error is a compile-time error, not a runtime error, so your code isn't actually running - and I agree with @kotatsuyaki that it's because you're using the `+` operator with string-literals, which C++ doesn't support. – Dai Jun 11 '22 at 12:12

1 Answers1

0

How do I use REG ADD when there is a space in the path?

You need to wrap the key path in quotes, eg:

string uuid = ...;
system(("REG ADD \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\" /v ProductId /t REG_SZ /d " + uuid + " /f").c_str());
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770