3

After running following codes, in 32bit process, the data %ProgramFiles% converted automatically.

HKEY hSubKey;
DWORD dwState;
RegCreateKeyEx(HKEY_CURRENT_USER, L"TestKey", NULL, NULL,
   REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hSubKey, &dwState);
std::wstring data = L"%ProgramFiles%";
RegSetValueEx(hSubKey, L"TestValue", NULL, REG_EXPAND_SZ,
  (BYTE*)data.c_str(), (DWORD)(data.size() * sizeof(WCHAR)) + sizeof(WCHAR));

The Result
enter image description here <- What the hell. Who expands it?

I don't want to convert the data. -But I have to use type REG_EXPAND_SZ.
How do I do?

Benjamin
  • 10,085
  • 19
  • 80
  • 130

1 Answers1

3

This is called Registry value redirection, and it's a part of WOW64's Registry Virtualization. You can't disable this on Server 2003 or Vista, but you can in Win7 (can't remember the flag off the top of my head though). Why do you want to write keys pointing to the 64-bit Program Files from a 32-bit application? (there are legit answers to this question, I'm trying to figure out the scenario more)

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • I think it's little different registry redirection. Any key didn't redirect. Just environment value has expanded. I don't want to point to the 64bit program files. I want to point 32 and 64 bit program files depends on application reading the registry. That's why I used environment value. – Benjamin Jan 25 '11 at 02:48
  • 1
    No, it has changed the literal string "%ProgramFiles%" to "%ProgramFiles(x86)%", this is exactly what I'm referring to. It's not *expanded*, it's just a *different* environment variable – Ana Betts Jan 25 '11 at 02:49
  • Why? What kind of program are you writing? – Ana Betts Jan 25 '11 at 02:52
  • 2
    Try the KEY_WOW64_64KEY option. – Hans Passant Jan 25 '11 at 03:36
  • @Paul Betts: I have to register my network provider dll for my network redirector.(I know you know about a network redirector) To register it, I have to write a path of network provider to a registry which can be referenced by *any* apps. Lanmanager's np is located System32 and SysWow64. So there is no problem. But our np is located program files. – Benjamin Jan 25 '11 at 03:54
  • @Hans, Yes hans. It works! By the way, should I use this option at only 64bit windows? – Benjamin Jan 25 '11 at 04:02
  • @Paul, so I should write *ProviderPath* **%ProgramFiles%\blabla\ournp.dll** – Benjamin Jan 25 '11 at 04:06
  • This works great on Win7 but doesn't on Vista. A better way is to make your registration app 64-bit; this way you can write to both the 64-bit registry, and directly to the 32-bit registry. – Ana Betts Jan 25 '11 at 04:07
  • @Paul, No, even if I port my registration app to 64bit, I must write *%ProgramFiles%*. Because both 64bit app and wow64 app could refer the key. For example, Windows Explorer(64bit) read the key, then the key should be expanded *C:\Program Files*. If MS word(32bit) read the key, the key should be expanded *C:\Program Files (x86)* – Benjamin Jan 25 '11 at 04:15
  • That's fine - on WOW64, any attempts to get to the directory "Program Files" will instead go to "Program Files (x86)" – Ana Betts Jan 25 '11 at 04:20
  • @Hans, By the way, the answer was from your comment. Thanks. – Benjamin Jan 25 '11 at 04:25