Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB10.0 VB.NET
Hi,
 
I am having trouble getting data from LsaRetrievePrivateData.
 
here is the code
<DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, PreserveSig:=True)> _
    Private Shared Function LsaRetrievePrivateData(ByVal policyHandle As IntPtr, ByRef KeyName As LSA_UNICODE_STRING, ByRef PrivateData As Long) As UInteger
    End Function
 
<DllImport("advapi32.dll", PreserveSig:=True)> _
    Private Shared Function LsaOpenPolicy(ByRef SystemName As LSA_UNICODE_STRING, ByRef ObjectAttributes As LSA_OBJECT_ATTRIBUTES, ByVal DesiredAccess As Int32, ByRef PolicyHandle As IntPtr) As UInt32
    End Function
 
Private Function GetSecret() As String
    Dim lusSecretData As New LSA_UNICODE_STRING()
    Dim PrivateData As Long
    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 = DirectCast(Marshal.PtrToStructure(PrivateData, GetType(LSA_UNICODE_STRING)), LSA_UNICODE_STRING)
    Value = Marshal.PtrToStringUni(lusSecretData.Buffer)

    'lusSecretData = Marshal.PtrToStructure(PrivateData, GetType(LSA_UNICODE_STRING))
    'Value = Marshal.PtrToStringAuto(lusSecretData.Buffer).Substring(0, lusSecretData.Length / 2)

    Return Value
End Function
GetLsaPolicy is a function that returns the LsaPolicyHandle
ReleaseLsaPolicy is a function that closes the LsaPolicyHandle
 
I have a function that Set the Secret and that all works fine.
 
What isn't working is the bold sections to get the value back, is there anyone that can guide me in the right direction.
 
Thanks
Rodney
Posted 5-Oct-12 16:56pm
Edited 5-Oct-12 18:15pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100