Announcement

Collapse
No announcement yet.

RtlUnicodeStringToAnsiString

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RtlUnicodeStringToAnsiString

    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "Win32Api.inc"
    
    'typedef struct _LSA_UNICODE_STRING {
    '  USHORT Length;
    '  USHORT MaximumLength;
    '  PWSTR  Buffer;
    '} LSA_UNICODE_STRING, *PLSA_UNICODE_STRING, UNICODE_STRING, *PUNICODE_STRING;
    
    TYPE UNICODE_STRING
      Length        AS WORD
      MaximumLength AS WORD
      Buffer        AS STRING POINTER
    END TYPE
    
    'typedef struct _STRING {
    '  USHORT Length;
    '  USHORT MaximumLength;
    '  PCHAR  Buffer;
    '} ANSI_STRING, *PANSI_STRING;
    
    TYPE ANSI_STRING
      Length        AS WORD
      MaximumLength AS WORD
      Buffer        AS ASCIIZ POINTER
    END TYPE
    
    'NTSTATUS RtlUnicodeStringToAnsiString(
    '  _Inout_ PANSI_STRING     DestinationString,
    '  _In_    PCUNICODE_STRING SourceString,
    '  _In_    BOOLEAN          AllocateDestinationString
    ');
    DECLARE FUNCTION RtlUnicodeStringToAnsiString LIB "NtDll.dll" ALIAS "RtlUnicodeStringToAnsiString" _
    (BYREF DestinationString AS ANSI_STRING, BYREF SourceString AS UNICODE_STRING, _
     BYVAL AllocateDestinationString AS LONG) AS LONG
    
    'VOID WINAPI RtlFreeAnsiString(
    '  _In_ PANSI_STRING AnsiString
    ');
    DECLARE FUNCTION RtlFreeAnsiString LIB "NtDll.dll" ALIAS "RtlFreeAnsiString" _
    (BYREF DestinationString AS ANSI_STRING) AS LONG
    
    DECLARE FUNCTION MemCpy CDECL LIB "msvcrt.dll" ALIAS "memcpy" _
    (BYREF Dst AS DWORD, BYREF Src AS DWORD, BYVAL dwSize AS DWORD) AS DWORD
    '______________________________________________________________________________
    
    FUNCTION PBMAIN() AS LONG
      LOCAL UniStringVar  AS UNICODE_STRING
      LOCAL AnsiStringVar AS ANSI_STRING
      LOCAL sUni          AS STRING
      LOCAL sAnsi         AS STRING
      LOCAL RetVal        AS LONG
    
      sUni                       = UCODE$("I was once a unicode string.")
      UniStringVar.Length        = LEN(sUni)
      UniStringVar.MaximumLength = UniStringVar.Length
      UniStringVar.Buffer        = STRPTR(sUni)
    
      'RtlUnicodeStringToAnsiString using AllocateDestinationString
      RetVal = RtlUnicodeStringToAnsiString(AnsiStringVar, UniStringVar, %TRUE)
      'Last param is TRUE if this routine is to allocate the buffer space for the DestinationString.
      'If it does, the buffer must be deallocated by calling RtlFreeAnsiString.
      sAnsi = NUL$(AnsiStringVar.Length)
      CopyMemory(BYVAL STRPTR(sAnsi), AnsiStringVar.Buffer, AnsiStringVar.Length)
      RtlFreeAnsiString(AnsiStringVar)
      MessageBox(%HWND_DESKTOP, sAnsi & $CRLF & "with AllocateDestinationString", _
                 "RtlUnicodeStringToAnsiString", %MB_OK OR %MB_TOPMOST)
    
      'RtlUnicodeStringToAnsiString not using AllocateDestinationString
      sAnsi                       = NUL$(LEN(sUni) / 2)
      AnsiStringVar.Length        = LEN(sAnsi) + 1
      AnsiStringVar.MaximumLength = AnsiStringVar.Length
      AnsiStringVar.Buffer        = STRPTR(sAnsi)
      RetVal = RtlUnicodeStringToAnsiString(AnsiStringVar, UniStringVar, %FALSE)
      'AllocateDestinationString [in] TRUE if this routine is to allocate the buffer space for the DestinationString.
      '                               If it does, the buffer must be deallocated by calling RtlFreeAnsiString.
      MessageBox(%HWND_DESKTOP, sAnsi & $CRLF & "without AllocateDestinationString", _
                 "RtlUnicodeStringToAnsiString", %MB_OK OR %MB_TOPMOST)
    
    END FUNCTION
    '______________________________________________________________________________
    '
    Last edited by Pierre Bellisle; 12 Jun 2021, 06:46 PM.
Working...
X