#include <windows.h>
#include <winternl.h>
#include <cstdio>

// https://www.dllhook.com
std::wstring ConvertAnsiToUnicode(const std::string& ansiStr)
{
    ANSI_STRING ansiString;
    RtlInitAnsiString(&ansiString, ansiStr.c_str());

    UNICODE_STRING unicodeString;
    RtlAnsiStringToUnicodeString(&unicodeString, &ansiString, TRUE);
    const size_t bufferLength = unicodeString.MaximumLength;

    std::unique_ptr<wchar_t[]> buffer(new wchar_t[bufferLength]);

    unicodeString.Buffer = buffer.get();
    unicodeString.Length = 0;
    unicodeString.MaximumLength = static_cast<USHORT>(bufferLength);

    RtlAnsiStringToUnicodeString(&unicodeString, &ansiString, FALSE);

    const std::wstring result(unicodeString.Buffer, unicodeString.Length / sizeof(wchar_t));

    return result;
}

std::string ConvertUnicodeToAnsi(const std::wstring& unicodeStr)
{
    ANSI_STRING ansiString;
    RtlInitAnsiString(&ansiString, NULL);

    UNICODE_STRING unicodeString;
    RtlInitUnicodeString(&unicodeString, unicodeStr.c_str());

    RtlUnicodeStringToAnsiString(&ansiString, &unicodeString, TRUE);

    std::string str(ansiString.Buffer, ansiString.Length);

    RtlFreeAnsiString(&ansiString);

    return str;
}

你可能感兴趣的文章

评论区

已有1220位网友发表了看法:

发表评论

必填

选填

选填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。