- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在尝试将 LPCWSTR 转换为 LPCSTR 时遇到问题。
一些背景
我正在编写一个 VC++ 2010 应用程序,并且已经遇到了我必须从类型 System::String^ 转换为 std::string 的情况。我使用这段代码来这样做:
private: std::string toss(System::String^ s)
{
msclr::interop::marshal_context context;
return context.marshal_as<std::string>(s);
}
这样做,我想我会解决我所有的问题。我错了。由此,我需要将 std::string 转换为 LPCSTR,以便与某些 Windows API 一起使用。
看了一些关于SO的现成问题,感觉卡住了。我被告知使用:
LPCSTR str = existingstr.c_str();
但是,当我使用它时,出现错误:无法从 LPCWSTR 转换为 LPCSTR
有没有人更好地了解我在这种特定情况下应该做什么,或者我如何从 LPCWSTR 转换为 LPCSTR?
提前致谢。
最佳答案
我在 wchar 和基于 char 的字符串之间来回转换时使用以下两个例程:
include <string> // std::string, std::wstring
include <algorithm> // std::copy
std::wstring StringToWString(const std::string& s)
{
std::wstring temp(s.length(),L' ');
std::copy(s.begin(), s.end(), temp.begin());
return temp;
}
std::string WStringToString(const std::wstring& s)
{
std::string temp(s.length(), ' ');
std::copy(s.begin(), s.end(), temp.begin());
return temp;
}
您现在可以通过以下假设 'existingstr' 是一个宽字符字符串来解决您的问题:
std::string narrow_str(WStringToString(existingstr));
LPCSTR win_str = narrow_str.c_str();
给定一些 'narrowstr',你可以轻松地走另一条路:
std::wstring wide_str(StringToWString(narrowstr));
LPWCSTR win_wstr = wide_str.c_str();
很明显,这是不高效的,因为你们都在分配一个局部变量,然后通过堆栈将其复制回来(我通过使用构造函数初始化而不是赋值来减轻一点......一些编译器优化掉了额外的拷贝方式)。
如果可能,最好使用 UNICODE 宏来使用适当版本的 Windows API(请参阅 http://msdn.microsoft.com/en-us/library/ff381407%28v=vs.85%29.aspx - 使用字符串 (Windows))。
但是,假设出于兼容性原因这是不可能的,它确实可以让您了解需要发生什么。
关于.net - 从 LPCWSTR 转换为 LPCSTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5112633/
我有一个由 Install Shield 通过 InstallScript 调用的 C++ API 函数: SQLHELPER_API LPCSTR GetAvailableAppName(LPCST
我收到这个错误 UINT GetDriveTypeA(LPCSTR)': cannot convert argument 1 from 'LPCWSTR' to 'LPCSTR' 下面是我的代码。 v
在 vc++ 中,我有一个包含两个项目的解决方案。项目 A 有一个 dllLoader.h 和 dllLoader.cpp 用 LoadLibrary 加载一个 dll 我需要调用它在项目 B 中的功
我目前有一个名为 TextureObject 的类。在创建函数中,我创建纹理,并将类中的 LPCSTR 分配给函数中给定的参数。当我稍后返回那个 LPCSTR 时,它以一种意想不到的方式返回。 某些类
简单的问题。 如何检查 LPCSTR 上第一个位置的字符是什么? 或者只检查第一个位置是否有空格? 最佳答案 给定一个名为 str 的 LPCSTR,您可以使用 str[0] 访问第一个字符。例如:
这个问题在这里已经有了答案: How do I iterate over the words of a string? (83 个回答) 关闭 5 年前。 我现在已经苦苦挣扎了好几天,因为我对 C+
我正在编写一个简单的 XOR 加密程序,然后我注意到函数的返回值不是我期望看到的那种。 我在我的代码中找不到问题,任何人都可以帮助我吗? 程序代码如下: #include "windows.h" #i
你好,我的代码有问题。 LPCSTR mergeString(LPCSTR firstString, std::string secondString) { string convertedS
我已经尝试过 strcmp 和 lstrcmp。我什至尝试用 strlen 来做,但也没有用,这就是我所拥有的 void check(LPCSTR lpText) { if( strmp(lp
我有两个 LPCSTR,我需要像这样连接起来: if (!rename(directory + originalFileName, directory + fileName)){ std::c
我正在读取一个 ini 文件,然后想执行一个外部程序(VBS 文件)。但是我在字符串类型方面遇到了问题。 这是我的代码。 LPCTSTR path = _T(".\\my.ini"); TCHAR f
我发现了一个比较两个 LPCSTR 的实现,它执行以下操作: void check(LPCSTR lpText) { if(lpText == input) { //
我正在尝试使用 atoi() 将 LPCSTR 转换为整数,并验证转换是否成功我想计算它生成的整数和原始 LPCSTR 中的位数(它应该只包含整数) 我很难找到计算 LPCSTR 长度的好方法。到目前
我在 C 中使用 Win32 API,需要将 GUID 结构转换为 LPCSTR。我对 Win32 比较陌生,并没有找到太多关于这种类型转换的信息。 我确实设法使用 StringFromGUID2 函
我目前正在进入 ctypes 模块,并尝试调用 user32 函数 GetWindowText带有我已经通过使用 FindWindow 收到的 HWND 句柄。这次我想进一步处理并使用函数原型(pro
我在尝试将 LPCWSTR 转换为 LPCSTR 时遇到问题。 一些背景 我正在编写一个 VC++ 2010 应用程序,并且已经遇到了我必须从类型 System::String^ 转换为 std::s
请问如何将 LPCSTR 转 WCHAR* 转换成 Unicode 项目?我在网上找不到太多内容。 最佳答案 如果您不包含AtlBase.h或AtlConv.h header , #include
我试图在一个 dll 包装器中创建一个 c++ 函数,它接受一个 char *,将它转换为 LPCSTR,然后将它用作 MessageBox 中的一个变量。该函数和转换在原始类中运行良好,但当我尝试在
从帮助我创建按钮和其他东西的网站获得此代码。这些按钮工作但由于某种原因我在创建静态时遇到编译器错误。 无法从“const wchar_t [5]”转换为“char” 无法将参数 3 从“char”转换
这个问题在这里已经有了答案: Why does calling c_str() on a function that returns a string not work? (4 个答案) 关闭 6
我是一名优秀的程序员,十分优秀!