none
Microsoft.Win32.Registry Error in .net 4 RRS feed

  • Question

  • Hi I have the following code which works fine in Visual Studio 2008 in a .net 3.5 Win forms Program and returns the correct Registry Key Value

    Imports Microsoft.Win32
    

        Dim regkey As RegistryKey
        Dim LicenseKey As String
        regkey = Registry.LocalMachine.OpenSubKey("Software\Laurus Media\Laurus Event Master", True)
        LicenseKey = regkey.GetValue("SharedPath", "Error")
        MsgBox(LicenseKey)

    but with the same code in Visual Studio 2010 in a .net 4 project I get a "Object reference not set to an instance of an object" Error?? I really dont understand it. Iv tried everything.

     

    If I copy the registry Key to Current User Hive and change the code to point to Current User Hive it works fine.

     

    So .net 4 just doesn't want to read from Local Machine Hive

     

    thanks

     

    Friday, October 1, 2010 11:58 AM

Answers

  • It depends on the bitness of the code after JIT. By default the target building configuration is AnyCPU, which will generate code based on the bitness of the host program or Windows but will have different behavior on 32bit and 64bit Windows when reading registry keys that are different in different views.

    The registry view difference is OS-dependent so if you write platform-dependent code it will get messy. If you want a deterministic registry view I suggest you to start a broker process in the desired bitness.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Capitan Matt Sunday, October 3, 2010 4:31 PM
    Sunday, October 3, 2010 4:30 PM

All replies

  • Why you demand write access? It does not look like you are writing back to the key.

    You are supposed to check if regkey is null before dereferencing it



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Friday, October 1, 2010 2:29 PM
  • I have tried allowing and disallowing write access to no avail. Also the key is coming out null on .net 4 after line
     regkey = Registry.LocalMachine.OpenSubKey("Software\Laurus Media\Laurus Event Master", True)
    

    so .net 4 isn't even opening the sub key.  

    and checking if its null or not still wouldn't help this problem would it?

    thanks

    Friday, October 1, 2010 2:54 PM
  • At least you won't get the costly exception

    Use Process Monitor to check where your program is looking in registry.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Friday, October 1, 2010 3:08 PM
  • But I shouldn't be getting the exception or a Null value I should be getting the value in the registry key the same as when I run the code under .net 3.5

    have you tested the code under both environments?

    Saturday, October 2, 2010 12:00 AM
  • But I shouldn't be getting the exception or a Null value I should be getting the value in the registry key the same as when I run the code under .net 3.5

    have you tested the code under both environments?

    Ok so just figured out what the problem is! I found this article http://stackoverflow...-returning-null and it got me thinking.

    I am running Windows 7 64bit which I didn't think would make any difference but it seems that is I changed the target architecture to x64 from x86 it works. Problem is why is it not working when targeting x86?

    So how are you meant to develop x86 projects on a x64 bit system? 
    Sunday, October 3, 2010 3:59 PM
  • 32bit and 64bit processes have different views of the registry. That's why i suggested Process Monitor to find out where you were actually looking at.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Sunday, October 3, 2010 4:09 PM
  • ok but that never caused a Problem in Visual Studio 2008 so why does it in 2010?
    Sunday, October 3, 2010 4:14 PM
  • It depends on the bitness of the code after JIT. By default the target building configuration is AnyCPU, which will generate code based on the bitness of the host program or Windows but will have different behavior on 32bit and 64bit Windows when reading registry keys that are different in different views.

    The registry view difference is OS-dependent so if you write platform-dependent code it will get messy. If you want a deterministic registry view I suggest you to start a broker process in the desired bitness.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Capitan Matt Sunday, October 3, 2010 4:31 PM
    Sunday, October 3, 2010 4:30 PM
  • many thanks and Thankyou for your help.
    Sunday, October 3, 2010 4:32 PM