# RegWow64 problem in VB.NET



## FordGT90Concept (Jul 14, 2010)

I'm basically using the code from here:
http://www.pinvoke.net/default.aspx/advapi32.RegOpenKeyEx

It works fine in C# but when I try to run it in VB.NET, I get a NullReferenceException:

```
Private Shared Function PointerToRegistryKey(ByVal hKey As IntPtr, ByVal writable As Boolean, ByVal ownsHandle As Boolean) As RegistryKey
        ' Create a SafeHandles.SafeRegistryHandle from this pointer - this is a private class
        Dim privateConstructors As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic
        Dim safeRegistryHandleType As Type = GetType(Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType("Microsoft.Win32.SafeHandles.SafeRegistryHandle")
        Dim safeRegistryHandleConstructorTypes As Type() = {GetType(IntPtr), GetType(System.Boolean)}
        Dim safeRegistryHandleConstructor As ConstructorInfo = safeRegistryHandleType.GetConstructor(privateConstructors, Nothing, safeRegistryHandleConstructorTypes, Nothing)
        Dim safeHandle As Object = safeRegistryHandleConstructor.Invoke(New Object() {hKey, ownsHandle}) ' [b]HERE: safeRegistryHandleConstructor == Nothing[/b]

        ' Create a new Registry key using the private constructor using the safeHandle - this should then behave like a .NET natively opened handle and disposed of correctly
        Dim registryKeyType As Type = GetType(RegistryKey)
        Dim registryKeyConstructorTypes As Type() = {safeRegistryHandleType, GetType(Boolean)}
        Dim registryKeyConstructor As ConstructorInfo = registryKeyType.GetConstructor(privateConstructors, Nothing, registryKeyConstructorTypes, Nothing)
        Dim result As RegistryKey = DirectCast(registryKeyConstructor.Invoke(New Object() {safeHandle, writable}), RegistryKey)
        Return result
    End Function
```

The only difference between the C# code when it runs is that hKey is over 1500 instead of 932.  Everything else matches.  Why isn't GetConstructor returning a constructor?


----------



## FordGT90Concept (Jul 14, 2010)

I figured it out.  GetConstructor (at least in this instance) doesn't work in .NET Framework 4.0.  I set it to .NET Framework 3.5 and it worked fine.


----------



## Kreij (Jul 14, 2010)

Why was your VB set for .Net Fw 4.0 and not C#? (Since you said it worked in C#)


----------



## FordGT90Concept (Jul 14, 2010)

I was converting my code (C# -> VB) for someone else to use.  Because I'm using VS2010, it defaulted to 4.0 Client Profile when I created the Visual Basic solution/project.  This code will end up in a project coded for .NET Framework 3.5.


FYI, GetConstructor returns null in C# under .NET Framework 4.0 as well.  It appears to be a bug in .NET Framework 4.0, not necessarily C# or VB.


----------



## ctrain (Jul 14, 2010)

Null seems to be intentional if it fails.

http://msdn.microsoft.com/en-us/library/h93ya84h.aspx

Return Value:
Type: System.Reflection.ConstructorInfo
An object representing the public instance constructor whose parameters match the types in the parameter type array, if found; otherwise, null.


----------



## FordGT90Concept (Jul 14, 2010)

But why would it only fail under .NET Framework 4.0?

I opened a bug report on it here:
https://connect.microsoft.com/Visua...-is-situations-where-it-did-in-net-2-0-to-3-5


----------



## FordGT90Concept (Sep 4, 2010)

Microsoft replied with the an answer: .NET 4.0 has native support for RegistryViews negating the need for all that code.  That code was broken because the constructors are no longer private, they are public.  Examples are on the bug report link.


----------

