C++/CLI 中无法识别 LPVOID
我正在尝试使用以下代码将本机字符串转换为 C++\CLI 中的托管字符串:
System::String^ NativeToDotNet( const std::string& input )
{
return System::Runtime::InteropServices::Marshal::PtrToStringAnsi( (static_cast<LPVOID>)( input.c_str() ) );
}
我最初找到了代码 这里:
但是当我尝试构建它时会抛出错误:
syntax error : identifier 'LPVOID'
知道如何解决这个问题吗?
I'm trying to use the following code to convert a native string to a managed string in C++\CLI:
System::String^ NativeToDotNet( const std::string& input )
{
return System::Runtime::InteropServices::Marshal::PtrToStringAnsi( (static_cast<LPVOID>)( input.c_str() ) );
}
I originally found the code here:
But when I try to build it throws the error:
syntax error : identifier 'LPVOID'
Any idea how to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种情况经常以各种形式出现 - 最简单的答案是:不要编写自己的函数,请参见此处:
http://msdn.microsoft.com/en-us/library/bb384865。 ASPX
This crops up quite often in various guises - the simplest answer is: don't write your own function, see here:
http://msdn.microsoft.com/en-us/library/bb384865.aspx
LPVOID 只是 void * 的别名。 LP 代表“长指针”,这是“机器大小指针”的一种旧式说法,根据进程的不同,可以是 32 位,也可以是 64 位。
只需使用
static_cast
在一个或多个头文件的某处,有一个
#define LPVOID (void *)
您尚未包含这样的文件。
LPVOID is just an alias for void *. LP stands for "long pointer," which is an old-style way of saying "machine-sized pointer", either 32 or 64 bit depending on the process.
Just use
static_cast<void *>
In one or more header files somewhere, there's a
#define LPVOID (void *)
You haven't included such a file.
强制转换为(相同的cv-qualifiers)
void*
始终是隐式可能的,您永远不应该看到强制转换尝试这样做。该错误是由于尝试使用static_cast
删除const
尝试这样做,它也可以正确处理嵌入的 NUL 字符:
const_cast
解决了 .NET 中缺乏常量正确性的愚蠢问题Casting to (same cv-qualifiers)
void*
is always implicitly possible, you should never see a cast trying to do so. The error is from trying to removeconst
with astatic_cast
Try this, which also handles embedded NUL characters correctly:
The
const_cast<char*>
takes care of the stupidity which is the lack of const-correctness in .NET