views:

416

answers:

3

I'm creating an MSBuild task that will read the registry for a specific registry key. If I write the same line of code (see below) in a console application, it returns the expected result, but when it is within the MSBuild task, it returns nothing.

Return Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing)

I would expect the above code to return Nothing if the key/value pair doesn't exist, and return the value if it does exist. I am getting Nothing when the MSBuild task gets executed. Is there some attribute that I need to apply to the Execute function of the MSBuild task to tell it that it needs to read the registry?

EDIT:

Here is what is being executed from the MSBuild task:

Return Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing)

I beleive this to be caused by the Registry Redirector on my Vista x64 machine running MSBuild in 32bit. Is there any way that you can tell the custom MSBuild task (written in VB .Net) to look in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\ then only if nothing exists there then look in HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\?

Thank you,

Scott Blue

A: 

How about using VB's If() ternary function?

Function GetSqlPathFromReg() As Object
    Return If(Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing), _
              Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing))
End Function

Assuming you have an Output() property:

Private _sqlPath As String

<Output()> _
Public ReadOnly Property SqlPath() As String
    Get
        Return _sqlPath
    End Get
End Property

Then all you have to do is calling it from the Execute() method:

_sqlPath = GetSqlPathFromReg().ToString()
KMoraz
Good thought, but doesn't work. Because of the Registry Redirector, `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\` gets turned into `HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\`automatically, thus your solution would effectively be: `Return If(Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing), _ Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Setup\", "SQLPath", Nothing))`Wow6432Node is injected automatically.
Scott
Maybe you just need to look in a different location, see this:http://support.microsoft.com/kb/936491
KMoraz
+1  A: 

You can read the registry directly from MSBuild, without a custom task, like this:

$(registry:Hive\MyKey\MySubKey@Value)

E.g.,

$(registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup\)

You said that you're looking to do this from a custom task, so this may not apply, but I'm posting it in case it helps.

fatcat1111
A: 

Our msbuild script runs from an x86 Visual Studio Command Prompt. It does not read the 64 bit registry when using this syntax. It there a different syntax which would allow the x86 to read the 64 bit registry?

mgb69