简体   繁体   English

错误C2664:'errno_t wcstombs_s(size_t *,char *,size_t,const wchar_t *,size_t)':无法转换参数4

[英]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实际上是MODULEENTRY32Ame32.szModulechar[] ,但是随后尝试将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 use Module32FirstA() / Module32NextA() , MODULEENTRY32A , etc. Either way, you do not have to change the project settings. 或者,您可以显式使用Module32FirstW() / Module32NextW()MODULEENTRY32Wstd::wcout等,或显式使用Module32FirstA() / Module32NextA()MODULEENTRY32A等。两种方式都无需更改项目设置。 Don't use TCHAR -based APIs anymore. 不再使用基于TCHAR的API。 In this case, since the code wants to end up with a char[] string, it makes sense to use Module32FirstA() / Module32NextA() and just remove wcstombs_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.

相关问题 错误 C2664:“wcscmp”:无法将参数 1 从“CHAR [260]”转换为“const wchar_t *” - error C2664: 'wcscmp' : cannot convert parameter 1 from 'CHAR [260]' to 'const wchar_t *' 使size_t和wchar_t可移植? - Making size_t and wchar_t portable? 参数类型为:(size_t,const char [2]) - argument types are:(size_t, const char[2]) 字符串(const char *,size_t)转换为int吗? - String (const char*, size_t) to int? 错误:无法将参数1的const字符串转换为const char *到size_t strlen(const char *) - error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*) 为什么字符串(const char * s,size_t pos,size_t len = npos)有效? - Why does string (const char* s, size_t pos, size_t len = npos) work? 错误C2664:&#39;strcpy&#39;:无法将参数2从&#39;const wchar_t [9]&#39;转换为&#39;const char *&#39;。如何解决此错误? - Error C2664: 'strcpy' : cannot convert parameter 2 from 'const wchar_t [9]' to 'const char *'.How to solve this error? 将size_t转换为vector <unsigned char> - Convert size_t to vector<unsigned char> 无法将参数5从&#39;SIZE_T *&#39;转换为&#39;size_t *&#39;-为什么? - cannot convert parameter 5 from 'SIZE_T *' to 'size_t *' — why? 无法将参数从“ SIZE_T *”转换为“ size_t *”-如何进行投射? - cannot convert parameter from 'SIZE_T *' to 'size_t *' - how to cast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM