Ok, I have got it working.
Changed
<dllimport("advapi32.dll",> _
Private Shared Function LsaRetrievePrivateData(ByVal policyHandle As IntPtr, ByRef KeyName As LSA_UNICODE_STRING, ByRef PrivateData As Long) As UInteger
End Function
To
<dllimport("advapi32.dll",> _
Private Shared Function LsaRetrievePrivateData(ByVal policyHandle As IntPtr, ByRef KeyName As LSA_UNICODE_STRING, ByRef PrivateData As LSA_UNICODE_STRING) As UInteger
End Function
and the function looks like this.
Private Function GetSecret() As String
Dim lusSecretData As New LSA_UNICODE_STRING()
Dim PrivateData As IntPtr
Dim Value As String = ""
Dim LsaPolicyHandle As IntPtr = GetLsaPolicy(LSA_AccessPolicy.POLICY_GET_PRIVATE_INFORMATION)
Dim result As UInteger = LsaRetrievePrivateData(LsaPolicyHandle, secretName, PrivateData)
ReleaseLsaPolicy(LsaPolicyHandle)
If result <> 0 Then
Dim lastError As String = "RetrievePrivateData failed: " & Marshal.GetLastWin32Error & " " & New System.ComponentModel.Win32Exception().Message
Throw New Exception(lastError)
End If
lusSecretData = Marshal.PtrToStructure(PrivateData, GetType(LSA_UNICODE_STRING))
If lusSecretData.Length > 0 Then
Value = Marshal.PtrToStringAuto(lusSecretData.Buffer).Substring(0, lusSecretData.Length / 2)
End If
LsaFreeMemory(PrivateData)
Return Value
End Function
This is now resolved.