[英]error C2664: 'errno_t wcstombs_s(size_t *,char *,size_t,const wchar_t *,size_t)' : cannot convert parameter 4
error C2664: 'errno_t wcstombs_s(size_t *,char *,size_t,const wchar_t *,size_t)' : cannot convert parameter 4 from 'CHAR [260]' to 'const wchar_t *' 1> 错误C2664:'errno_t wcstombs_s(size_t *,char *,size_t,const wchar_t *,size_t)':无法将参数4从'CHAR [260]'转换为'const wchar_t *'1>
Types pointed to are unrelated; 指向的类型无关。 conversion requires reinterpret_cast, C-style cast or function-style cast 转换需要reinterpret_cast,C样式强制转换或函数样式强制转换
What does this error mean? 这个错误是什么意思?
As my function is: 由于我的职能是:
BOOL DependentDLLDisplay()
{
char arr[200];
if(!Module32First(hProcessSnap,&me32))
{
cout<<" ERROR : Failed to Get DLL Information"<<endl;
CloseHandle(hProcessSnap);
return FALSE;
}
cout<<endl<<"DEPENDENT DLL OF THIS PROCESS :"<<endl;
do
{
wcstombs_s(NULL,arr,200,me32.szModule,200);
cout<<arr<<endl;
}while(Module32Next(hProcessSnap,&me32));
CloseHandle(hProcessSnap);
return TRUE;
}
Your object me32
is of the type MODULEENTRY32
as defined here: 您的对象me32
的类型为MODULEENTRY32
如下所示:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684225.aspx https://msdn.microsoft.com/zh-CN/library/windows/desktop/ms684225.aspx
The szModule
field that you pass as the 4th parameter to 'wcstombs_s` is defined as: 您将作为第四个参数传递给'wcstombs_s`的szModule
字段定义为:
TCHAR szModule[MAX_MODULE_NAME32 + 1];
In the Windows API, TCHAR
is defined as char
in MBCS encoding, and wchar
in UNICODE encoding. 在Windows API中, TCHAR
在MBCS编码中定义为char
,在UNICODE编码中定义为wchar
。
The error you are seeing is indicating that you are including the MBCS version of the Windows library, thus MODULEENTRY32
is actually MODULEENTRY32A
and me32.szModule
is a char[]
, but are then trying to treat me32.szModule
as if it were a wide wchar_t[]
string when it is in fact an Ansi char[]
string. 您看到的错误表明您正在包括Windows库的MBCS版本,因此MODULEENTRY32
实际上是MODULEENTRY32A
而me32.szModule
是char[]
,但是随后尝试将me32.szModule
视为宽wchar_t[]
当它实际上串一个ANSI char[]
字符串。
You can either switch to the UNICODE libraries by changing your project settings, or using a normal char
string copy to obtain the value of that field. 您可以通过更改项目设置切换到UNICODE库,也可以使用普通的char
字符串副本获取该字段的值。
Or, as Remy stated: 或者,正如雷米所说:
Or, you can explicitly use
Module32FirstW()
/Module32NextW()
,MODULEENTRY32W
,std::wcout
, etc, or explicitly useModule32FirstA()
/Module32NextA()
,MODULEENTRY32A
, etc. Either way, you do not have to change the project settings. 或者,您可以显式使用Module32FirstW()
/Module32NextW()
,MODULEENTRY32W
,std::wcout
等,或显式使用Module32FirstA()
/Module32NextA()
,MODULEENTRY32A
等。两种方式都无需更改项目设置。 Don't useTCHAR
-based APIs anymore. 不再使用基于TCHAR
的API。 In this case, since the code wants to end up with achar[]
string, it makes sense to useModule32FirstA()
/Module32NextA()
and just removewcstombs_s()
altogether. 在这种情况下,由于代码希望以char[]
字符串结尾,因此使用Module32FirstA()
/Module32NextA()
并完全删除wcstombs_s()
是有意义的。
One last note: You should probably expand your local variable to be the same size of szModule
rather than potentially truncate the contents. 最后一点:您应该将局部变量扩展为szModule
大小,而不是可能截断内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.