1

I need to retrieve a string from the registry in 32/64 bit windows computers.

I have successfully retrieved info with registry.getvalue in a 64 bit environment, but it returns null in a 32 bit environment.

RegistryKey localKey;

localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default);

string value = localKey.OpenSubKey(@"Software\MCS").GetValue("path").ToString();

if (value != null)
{
    EventLog.WriteEntry("SMS_Service", "Path:" + value);
}
else
{
    EventLog.WriteEntry("SMS_Service", "Registry path not found.");
}

Reg query Verification

event viewer

Everything checks out as far as I can tell. I feel like it has to be some kind of permission problem with the registry on the 32 bit machine.

Nathan
  • 19
  • 2
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mixxiphoid Dec 12 '17 at 14:14
  • 1
    Did you debug and check exactly which value is null? – Meta-Knight Dec 12 '17 at 14:16
  • 8
    @Mixxiphoid I don't think this is a duplicate. The OP knows he is getting null but wants to know why - and the answer to that is very specifically concerned with how RegistryKey works. – DodgyCodeException Dec 12 '17 at 14:18
  • 2
    @DodgyCodeException The OP does not seem to have debugged his code and seems to be stuck at the NullReferenceException. To me that makes it a duplicate of said question. – Mixxiphoid Dec 12 '17 at 14:20
  • 4
    Possible duplicate of [Reading and writing to x86 and x64 registry keys from the same application](https://stackoverflow.com/questions/15424205/reading-and-writing-to-x86-and-x64-registry-keys-from-the-same-application) – MethodMan Dec 12 '17 at 14:21
  • 1
    Why don't you just use RegistryView.Default? – DodgyCodeException Dec 12 '17 at 14:35
  • the "value" string is null, but it should have "c", which is what is in the registry for that value. – Nathan Dec 12 '17 at 15:21
  • @Nathan: An exception occurs on that line so the "value" string never gets assigned actually. A NullReferenceException means that either `localKey` is null or `localKey.OpenSubKey("MCS")` returns null or `localKey.OpenSubKey("MCS").GetValue("path")` returns null. By debugging you can watch the values and check what is null exactly. – Meta-Knight Dec 12 '17 at 15:41
  • 1
    @Meta-Knight: It seems to be localKey.OpenSubKey("MCS") that is returning null, but I can't figure out why, the key is there in the registry. – Nathan Dec 12 '17 at 16:17
  • did you try using RegistryView.Default as suggested by @DodgyCodeException – Meta-Knight Dec 12 '17 at 17:17
  • I did, with the same result. – Nathan Dec 12 '17 at 17:28
  • You didn't, by any chance, mistakenly define the value in Wow6432Node on your 32-bit machine, did you? Can you show us a screenshot of the value definition in the Registry on your 32-bit machine? – DodgyCodeException Dec 12 '17 at 18:15
  • I don't believe so. But I have added a screenshot of the registry. – Nathan Dec 12 '17 at 18:25
  • You defined the value in HKCU\Software\MCS but are looking for it in HKCU\MCS. – DodgyCodeException Dec 14 '17 at 10:52
  • @DodgyCodeException Sorry about that, that was an issue, but I don't think it's been the issue the whole time. I am back now to my original problem, it is getting the values on a 64 but machine but not on a 32 bit machine. I have updated my code. On the 64 bit machine it shows timer3_ticked, 64 bit os, and the path in the event viewer. On the 32 bit machine it only shows timer3_ticket and 32 bit os. – Nathan Dec 14 '17 at 14:46
  • You're using a verbatim string literal, so your string contains double backslashes. Either remove the `@` from the front of the string literal or change the double backslash to a single one. – DodgyCodeException Dec 14 '17 at 15:11
  • I have tried this both ways with the same result, works on 64, not on 32 :-| any other suggestions? – Nathan Dec 14 '17 at 17:43
  • I ran the added code and got The specified registry key does not exist. at Microsoft.Win32.RegistryKey.Win32Error(Int32 errorCode, String str) at Microsoft.Win32.RegistryKey.GetValueKind(String name) at SMS_Service.SMS_Service.timer3_Tick(Object sender, EventArgs e) – Nathan Dec 14 '17 at 23:03
  • Your latest result is not surprising. You're enumerating HKEY_CURRENT_USER itself, so each iteration contains a subkey name of HKCU, but then you are erroneously using that name as if it was a value in HKCU\Software\MCS, which obviously is not, hence the "key does not exist". – Klitos Kyriacou Dec 15 '17 at 09:26
  • But going back to basics, if I've understood correctly, your code doesn't need to check if it's running on 32-bit or 64-bit architecture. Keep it simple and just use RegistryView.Default (as Dodgycodeexception suggested). It's probably some silly spelling mistake somewhere in the registry. Use the Visual Studio debugger, make sure you are calling OpenSubKey and GetValue with the correctly spelt arguments, and check by using the REG QUERY command (in a Command Prompt window). – Klitos Kyriacou Dec 15 '17 at 09:36
  • @KlitosKyriacou Ok, I have changed it to registryview.default and checked all of my values. I copied and pasted between the 64 and 32 bit machines and have posted the results. The registry check using reg query works on both machines, but the registrykey in c# only works on the 64 bit machine. – Nathan Dec 15 '17 at 14:42
  • So I put this on a xp machine and it's doing the same thing that the win7 32 bit is, but it is giving me this error in the event log: System.NullReferenceException: Object reference not set to an instance of an object. at SMS_Service.SMS_Service.timer3_Tick(Object sender, EventArgs e) in c:\Users\Nathan\Documents\Visual Studio 2012\Projects\SMS_Service\SMS_Service\SMS_Service.cs:line 67 The interesting part to me is that the path that it shows is the path from the computer that I am building the project on. Could this be the problem? – Nathan Dec 20 '17 at 19:15

0 Answers0