Dimitris
Dimitris

Reputation:

UNICODE_STRING to Null terminated

I need to convert a UNICODE_STRING structure to a simple NULL TERMINATED STRING.

typedef 
struct _UNICODE_STRING 
{
    USHORT  Length;  
    USHORT  MaximumLength;  
    PWSTR   Buffer;
} 
UNICODE_STRING, *PUNICODE_STRING;

I can't find a clean sollution on MSDN about it. Anyone been there? I am not using .net so I need a native API sollution.

Thanks a lot!

Upvotes: 3

Views: 6242

Answers (5)

SecurityMatt
SecurityMatt

Reputation: 6753

WCHAR* UnicodeStringToNulTerminated(UNICODE_STRING* str)
{
  WCHAR* result;
  if(str == NULL)
    return NULL;
  result = (WCHAR*)malloc(str->Length + 2);
  if(result == NULL)
    // raise?
    return NULL;
  memcpy(result, str->Buffer, str->Length);
  result[str->Length] = L'\0';
  return result;
}

Upvotes: 0

glagolig
glagolig

Reputation: 1120

Alternative code that converts to ANSI and does not require number of unicode characters in UNICODE_STRING that has to be passed as a parameter to WideCharToMultiByte. (Note that UNICODE_STRING.Length is a number of bytes, not unicode characters, and wcslen does not work if buffer is not zero-terminated).

UNICODE_STRING tmp;
// ...
STRING dest; // or ANSI_STRING in kernel mode

LONG (WINAPI *RtlUnicodeStringToAnsiString)(PVOID, PVOID, BOOL);
*(FARPROC *)&RtlUnicodeStringToAnsiString = 
    GetProcAddress(LoadLibraryA("NTDLL.DLL"), "RtlUnicodeStringToAnsiString");
if(!RtlUnicodeStringToAnsiString)
{
    return;
}

ULONG unicodeBufferSize = tmp.Length;
dest.Buffer = (PCHAR)malloc(unicodeBufferSize+1); // that must be enough...
dest.Length = 0;
dest.MaximumLength = unicodeBufferSize+1;

RtlUnicodeStringToAnsiString(&dest, &tmp, FALSE);
dest.Buffer[dest.Length] = 0; // now we get it in dest.Buffer

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 598414

Since you did not say whether you need an ANSI or UNICODE null-terminated string, I'm going to assume UNICODE:

#include <string>

UNICODE_STRING us;
// fill us as needed...

std::wstring ws(us.Buffer, us.Length);
// use ws.c_str() where needed...

Upvotes: 1

RectangleEquals
RectangleEquals

Reputation: 1825

When compiling for unicode and converting to ansi, this appears to work for me
(Modified from http://support.microsoft.com/kb/138813):

HRESULT UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA){
    ULONG cbAnsi, cCharacters;
    DWORD dwError;
    // If input is null then just return the same.    
    if (pszW == NULL)    
    {
        *ppszA = NULL;
        return NOERROR;
    }
    cCharacters = wcslen(pszW)+1;
    cbAnsi = cCharacters*2;

    *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
    if (NULL == *ppszA)
        return E_OUTOFMEMORY;

    if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA, cbAnsi, NULL, NULL)) 
    {
        dwError = GetLastError();
        CoTaskMemFree(*ppszA);
        *ppszA = NULL;
        return HRESULT_FROM_WIN32(dwError);
    }
    return NOERROR;
}


Usage:

LPSTR pszstrA;
UnicodeToAnsi(my_unicode_string.Buffer, &pszstrA);
cout << "My ansi string: (" << pszstrA << ")\r\n";

Upvotes: 2

Martin v. L&#246;wis
Martin v. L&#246;wis

Reputation: 127587

You should use WideCharToMultiByte. As an estimate for the output buffer size, you can use the Length field - but do consider the case of true multi-byte strings, in which case it will fail with ERROR_INSUFFICIENT_BUFFER, and you need to start over with a larger buffer. Or, you call it with an output buffer size of 0 first always, so it tells you the necessary size of the buffer.

Upvotes: 4

Related Questions