Question : Help Using RtlMoveMemory from kernel32 in VB.NET

I've got a function for encrypting data using the CryptProtectData function which I am upgrading from VB6 but I am having trouble copying the DATA_BLOB to a Byte array because I don't know what to change my declaration of the method's input variablejs to (from type 'Any' in VB6).

Any help would be appreciated.

Here's the code:

    Private Structure DATA_BLOB
        Dim cbData As Integer
        Dim pbData As Integer
    End Structure

    Private Structure CRYPTPROTECT_PROMPTSTRUCT
        Dim cbSize As Integer
        Dim dwPromptFlags As Integer
        Dim hwndApp As IntPtr
        Dim szPrompt As String
    End Structure

    Private Declare Function CryptProtectData Lib "crypt32.dll" (ByRef pDataIn As DATA_BLOB,
                                                               ByVal szDataDescr As String,
                                                               ByRef pOptionalEntropy As IntPtr,
                                                               ByRef pvReserved As IntPtr,
                                                               ByRef pPromptStruct As CRYPTPROTECT_PROMPTSTRUCT,
                                                               ByVal dwFlags As Integer,
                                                               ByRef pDataOut As DATA_BLOB) As Integer
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef hpvDest As Any,
                                                               ByRef hpvSource As Any,
                                                               ByVal cbCopy As Integer)

    Public Function VarPtr(ByVal o As Object) As Integer
        Dim GC As System.Runtime.InteropServices.GCHandle
           = System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned)
        Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
        GC.Free()
        Return ret
    End Function

    Public Function EncryptData(ByVal istrValue As String,
                                ByRef isEncryptedValue As String,
                                ByVal istrDesc As String) As Boolean
        Dim i As Integer
        Dim aDataIn() As Byte
        Dim aDataOut() As Byte
        Dim udtDataIn As DATA_BLOB
        Dim udtDataOut As DATA_BLOB
        Dim intSize As Short
        Dim rc As Integer
        Dim TStr1 As String
        Dim TStr2 As String
        Dim lngLen As Integer

        'encrypt content as unicode string
        'UPGRADE_TODO: Code was upgraded to use System.Text.UnicodeEncoding.Unicode.GetBytes() which may not have
        ' the same behavior. Click for more: 'ms-help:/....'
        aDataIn = System.Text.UnicodeEncoding.Unicode.GetBytes(istrValue)
        udtDataIn.cbData = UBound(aDataIn) + 1
        'UPGRADE_ISSUE: VarPtr function is not supported. Click for more: 'ms-help:/....'
        udtDataIn.pbData = VarPtr(aDataIn(0))

        rc = CryptProtectData(udtDataIn, istrDesc, IntPtr.Zero, IntPtr.Zero,
                              New CRYPTPROTECT_PROMPTSTRUCT(), 4, udtDataOut)
        If rc <> 1 Then
            EncryptData = False
            Exit Function
        End If
        ReDim Preserve aDataOut(udtDataOut.cbData)
        CopyMemory(aDataOut(0), udtDataOut.pbData, udtDataOut.cbData)

        'Convert array to string of 2 character Hex values
        TStr2 = ""
        lngLen = UBound(aDataOut)
        For i = 0 To lngLen
            TStr1 = Hex(aDataOut(i))
            If Len(TStr1) = 1 Then TStr1 = "0" & TStr1
            TStr2 = TStr2 + TStr1
        Next
        isEncryptedValue = TStr2

        EncryptData = True

    End Function


Answer : Help Using RtlMoveMemory from kernel32 in VB.NET

PAQed, with points refunded (75)

Computer101
E-E Admin
Random Solutions  
 
programming4us programming4us