Share via


編譯器警告 (層級 1) C4906

字串常值轉換成 'LPWSTR'

編譯器偵測到不安全的轉換。 轉換確實成功,但您應該使用轉換常式。

此警告預設為關閉。 如需詳細資訊,請參閱 預設為關閉的編譯器警告

範例

下列範例會產生 C4906:

// C4906.cpp
// compile with: /W1
#pragma warning(default : 4906)
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
    LPWSTR x = (LPWSTR)"1234";   // C4906

    // try the following lines instead
    // char y[128];
    // size_t numberOfCharConverted = 0;
    // errcode err = 0;
    // err = wcstombs_s(&numberOfCharConverted , &y[0], 128,
    //                  L"12345", 4);
    // if (err != 0)
    // {
    //     printf_s("wcstombs_s failed!");
    //     return -1;
    // }
    // printf_s("%s\n", y);

    return 0;
}