LPCWSTR char2LPCWSTR(char* c_temp, WCHAR* temp /*= temp_wchar */)
{
wmemset(temp, 0, MAX_PATH);
swprintf_s(temp, MAX_PATH, L"%s", c_temp);
return temp;
}
报了个warning 查msdn如下:
warning C6302: format string mismatch: character string passed as parameter <number> when wide character string is required in call to <function>
This warning indicates that the format string specifies that a wide character string is required. However, a character string is being passed. This defect is likely to cause a crash or a corruption of some form.
The following sample code generates this warning because a character string is passed to wprintf function:
#include<stdio.h> void f() { char buff[5] = "hi"; wprintf(L"%s", buff); }
#include<stdio.h> void f() { char buff[5] = "hi"; wprintf(L"%s", buff); }
The following sample code uses %hs to specify a single-byte character string with wprintf function:
#include<stdio.h> void f() { char buff[5] = "hi"; wprintf(L"%hs", buff); }
#include<stdio.h> void f() { char buff[5] = "hi"; wprintf(L"%hs", buff); }
The following sample code uses safe string manipulation function wprintf_s to correct this warning:
#include<stdio.h> void f() { char buff[5] = "hi"; wprintf_s(L"%hs", buff); }
总结 : 使用安全函数wprintf_s 指定格式为%hs就OK