gpt4 book ai didi

c++ - wstring 到 LPCWSTR 不使用 c_str()

转载 作者:行者123 更新时间:2023-11-28 03:01:06 24 4
gpt4 key购买 nike

<分区>

我目前正在执行 DirectX11 并尝试将 UTF8 字符串转换为 LPCWSTR。我编写了一个实用函数来帮助我进行转换:

// Convert an UTF8 string to a wide Unicode String
std::wstring WidenString(const std::string &string)
{
int size_needed = MultiByteToWideChar(CP_UTF8, 0, string.c_str(), string.size(), NULL, 0);
std::wstring wstring(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, string.c_str(), string.size(), &wstring[0], size_needed);
return wstring;
}

我已经使用调试器来验证它是否有效。这是有效的:

调试器说 wndClassEx.lpszClassName = L"Hello"

std::wstring str = WidenString("Hello");
wndClassEx.lpszClassName = str.c_str();

这是行不通的:

调试器说 wndClassEx.lpszClassName = L"ﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮ...”

wndClassEx.lpszClassName = WidenString("Hello").c_str();

有人可以向我解释我的代码有什么问题吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com