Results 1 to 8 of 8

Thread: [RESOLVED] convert C++ to Visual Basic help

  1. #1

    Thread Starter
    New Member Rodent2008's Avatar
    Join Date
    Jul 2008
    Posts
    10

    Resolved [RESOLVED] convert C++ to Visual Basic help

    Hi, I need to convert some C++ code into Visual Basic code but have not been able to complete the task.
    Can anyone help.

    Here is the C++ code:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    DWORD UpdateDefaultPassword(WCHAR * pwszSecret)
    {
    
        LSA_OBJECT_ATTRIBUTES ObjectAttributes;
        LSA_HANDLE LsaPolicyHandle = NULL;
    
        LSA_UNICODE_STRING lusSecretName;
        LSA_UNICODE_STRING lusSecretData;
        USHORT SecretNameLength;
        USHORT SecretDataLength;
    
        NTSTATUS ntsResult = STATUS_SUCCESS;
        DWORD dwRetCode = ERROR_SUCCESS;
    
        //  Object attributes are reserved, so initialize to zeros.
        ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
    
        //  Get a handle to the Policy object.
        ntsResult = LsaOpenPolicy(
            NULL,    // local machine
            &ObjectAttributes, 
            POLICY_CREATE_SECRET,
            &LsaPolicyHandle);
    
        if( STATUS_SUCCESS != ntsResult )
        {
            //  An error occurred. Display it as a win32 error code.
            dwRetCode = LsaNtStatusToWinError(ntsResult);
            wprintf(L"Failed call to LsaOpenPolicy %lu\n", dwRetCode);
            return dwRetCode;
        } 
    
        //  Initialize an LSA_UNICODE_STRING for the name of the
        //  private data ("DefaultPassword").
        SecretNameLength = (USHORT)wcslen(L"DefaultPassword");
        lusSecretName.Buffer = L"DefaultPassword";
        lusSecretName.Length = SecretNameLength * sizeof(WCHAR);
        lusSecretName.MaximumLength =
            (SecretNameLength+1) * sizeof(WCHAR);
    
        //  If the pwszSecret parameter is NULL, then clear the secret.
        if( NULL == pwszSecret )
        {
            wprintf(L"Clearing the secret...\n");
            ntsResult = LsaStorePrivateData(
                LsaPolicyHandle,
                &lusSecretName,
                NULL);
            dwRetCode = LsaNtStatusToWinError(ntsResult);
        }
        else
        {
            wprintf(L"Setting the secret...\n");
            //  Initialize an LSA_UNICODE_STRING for the value
            //  of the private data. 
            SecretDataLength = (USHORT)wcslen(pwszSecret);
            lusSecretData.Buffer = pwszSecret;
            lusSecretData.Length = SecretDataLength * sizeof(WCHAR);
            lusSecretData.MaximumLength =
                (SecretDataLength+1) * sizeof(WCHAR);
            ntsResult = LsaStorePrivateData(
                LsaPolicyHandle,
                &lusSecretName,
                &lusSecretData);
            dwRetCode = LsaNtStatusToWinError(ntsResult);
        }
    
        LsaClose(LsaPolicyHandle);
    
        if (dwRetCode != ERROR_SUCCESS)
            wprintf(L"Failed call to LsaStorePrivateData %lu\n",
                dwRetCode);
        
        return dwRetCode;
    
    }
    Also I need to know how to define the API call for:
    LsaNtStatusToWinError
    LsaOpenPolicy
    LsaStorePrivateData
    ZeroMemory
    LsaNtStatusToWinError

    So basically I need this function to work with Visual Basic 2010.

    Regards
    Rodney
    Last edited by Rodent2008; Sep 16th, 2012 at 07:35 PM.

  2. #2

    Thread Starter
    New Member Rodent2008's Avatar
    Join Date
    Jul 2008
    Posts
    10

    Re: convert C++ to Visual Basic help

    What I have tried so far:

    I have converted the code with a converter and this I what I have come up with so far:
    Code:
    Imports Microsoft.VisualBasic
    Imports System
    Imports System.Security
    Imports System.Runtime.InteropServices
    
    Module Module1
        Structure LSA_UNICODE_STRING
            Dim Length As Short
            Dim MaximumLength As Short
            Dim Buffer As IntPtr
        End Structure
    
        Structure LSA_OBJECT_ATTRIBUTES
            Dim Length As UInt32
            Dim RootDirectory As IntPtr
            Dim ObjectName As LSA_UNICODE_STRING
            Dim Attributes As UInt32
            Dim SecurityDescriptor As IntPtr
            Dim SecurityQualityOfService As IntPtr
        End Structure
    
        Private Const POLICY_CREATE_ACCOUNT = &H10
        Private Const POLICY_CREATE_SECRET = &H20
        Private Const POLICY_CREATE_PRIVILEGE = &H40
    
        Private Declare Unicode Function LsaOpenPolicy Lib "advapi32.dll" (ByRef SystemName As LSA_UNICODE_STRING, ByRef ObjectAttributes As LSA_OBJECT_ATTRIBUTES, ByVal DesiredAccess As Int32, ByRef PolicyHandle As IntPtr) As Int32
        Private Declare Sub LsaStorePrivateData Lib "advapi32.dll" (ByRef PolicyHandle As IntPtr, ByRef KeyName As LSA_UNICODE_STRING, ByRef PrivateData As LSA_UNICODE_STRING)
        Private Declare Function LsaNtStatusToWinError Lib "advapi32.dll" (ByVal Status As Integer) As Integer
    
        <DllImport("kernel32.dll")> _
        Public Sub ZeroMemory(ByVal addr As IntPtr, ByVal size As IntPtr)
        End Sub
    
    
        Public Function UpdateDefaultPassword(ByRef pwszSecret As Char) As UInteger
    
            Dim ObjectAttributes As New LSA_OBJECT_ATTRIBUTES()
            Dim LsaPolicyHandle As Integer
    
            Dim lusSecretName As New LSA_UNICODE_STRING()
            Dim lusSecretData As New LSA_UNICODE_STRING()
            Dim SecretNameLength As UShort
            Dim SecretDataLength As UShort
    
            Dim ntsResult As Integer
            Dim dwRetCode As Integer
    
            '  Object attributes are reserved, so initialize to zeros.
            'C++ TO VB CONVERTER TODO TASK: There is no VB equivalent to 'sizeof':
            ZeroMemory(ObjectAttributes, Len(ObjectAttributes))
    
            '  Get a handle to the Policy object.
            ntsResult = LsaOpenPolicy(Nothing, ObjectAttributes, POLICY_CREATE_SECRET, LsaPolicyHandle) ' local machine
    
            If STATUS_SUCCESS IsNot ntsResult Then
                '  An error occurred. Display it as a win32 error code.
                'dwRetCode = LsaNtStatusToWinError(ntsResult)
                'Console.Write("Failed call to LsaOpenPolicy {0:D}" & vbLf, dwRetCode)
                Return dwRetCode
            End If
    
            '  Initialize an LSA_UNICODE_STRING for the name of the
            '  private data ("DefaultPassword").
            SecretNameLength = CUShort("DefaultPassword".Length)
            lusSecretName.Buffer = "DefaultPassword"
            'C++ TO VB CONVERTER TODO TASK: There is no VB equivalent to 'sizeof':
            lusSecretName.Length = SecretNameLength * sizeof(Of Char)()
            'C++ TO VB CONVERTER TODO TASK: There is no VB equivalent to 'sizeof':
            lusSecretName.MaximumLength = (SecretNameLength + 1) * sizeof(Of Char)()
    
            '  If the pwszSecret parameter is NULL, then clear the secret.
            If pwszSecret Is Nothing Then
                Console.Write("Clearing the secret..." & vbLf)
                ntsResult = LsaStorePrivateData(LsaPolicyHandle, lusSecretName, Nothing)
                dwRetCode = LsaNtStatusToWinError(ntsResult)
            Else
                Console.Write("Setting the secret..." & vbLf)
                '  Initialize an LSA_UNICODE_STRING for the value
                '  of the private data. 
                SecretDataLength = CUShort(pwszSecret.Length)
                lusSecretData.Buffer = pwszSecret
                'C++ TO VB CONVERTER TODO TASK: There is no VB equivalent to 'sizeof':
                lusSecretData.Length = SecretDataLength * sizeof(Of Char)()
                'C++ TO VB CONVERTER TODO TASK: There is no VB equivalent to 'sizeof':
                lusSecretData.MaximumLength = (SecretDataLength + 1) * sizeof(Of Char)()
                ntsResult = LsaStorePrivateData(LsaPolicyHandle, lusSecretName, lusSecretData)
                dwRetCode = LsaNtStatusToWinError(ntsResult)
            End If
    
            LsaClose(LsaPolicyHandle)
    
            If dwRetCode <> ERROR_SUCCESS Then
                Console.Write("Failed call to LsaStorePrivateData {0:D}" & vbLf, dwRetCode)
            End If
    
            Return dwRetCode
    
        End Function
    End Module
    There are a lot of problem still but I am hoping that I can get some help with the conversion.
    Regards
    Rodney
    Last edited by Rodent2008; Sep 16th, 2012 at 11:03 PM.

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: convert C++ to Visual Basic help

    I can't help noticing that you're a VB6 personage. If you're asking for conversion to VB6, please ask a Moderator to move your question to the appropriate forum.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: convert C++ to Visual Basic help

    So basically I need this function to work with Visual Basic 2010.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: convert C++ to Visual Basic help

    Oops. Missed that!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    16

    Re: convert C++ to Visual Basic help

    Why not just create a DLL file and declare that you are using the function in VB?

  7. #7

    Thread Starter
    New Member Rodent2008's Avatar
    Join Date
    Jul 2008
    Posts
    10

    Re: convert C++ to Visual Basic help

    Quote Originally Posted by pogrady View Post
    Why not just create a DLL file and declare that you are using the function in VB?
    I need a single exe file with no external DLL's.

    This exe will only run on Widows 7 and above, and use .NET 3.5.

    I was hoping there where some smart guy's out there that have used LsaStorePrivateData before and could help convert this code, I will continue to try and convert it myself searching for code snippet here and there with bits and peaces that I need but most of the code for LsaStorePrivateData is in C++.

    Regards
    Rodney

    PS. I Joined this site in 2008 at which time I was using VB6, I am now using VB2010 and am loving it.

  8. #8

    Thread Starter
    New Member Rodent2008's Avatar
    Join Date
    Jul 2008
    Posts
    10

    Resolved Re: convert C++ to Visual Basic help

    Found a bit of code that is just what I need.

    here

    Thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width