VS C++打开文件对话框报错:不能将char[]转换为LPWSTR

这个是字符集的问题,打开属性管理器->常规->字符集->使用多字节字符集


VS C++打开文件对话框报错:不能将char[]转换为LPWSTR_第1张图片

测试代码:

#include 
#include 
#include 

void readFile() {
    //调用系统对话框
    OPENFILENAME  fname;
    ZeroMemory(&fname, sizeof(fname));
    char strfile[200] = "*.txt";
    char szFilter[] = TEXT("text Files(*.txt)\0");
    fname.lStructSize = sizeof(OPENFILENAME);
    fname.hwndOwner = NULL;
    fname.hInstance = NULL;
    fname.lpstrFilter = szFilter;
    fname.lpstrCustomFilter = NULL;
    fname.nFilterIndex = 0;
    fname.nMaxCustFilter = 0;
    fname.lpstrFile = strfile;
    fname.nMaxFile = 200;
    fname.lpstrFileTitle = NULL;
    fname.nMaxFileTitle = 0;
    fname.lpstrTitle = NULL;
    fname.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT;
    fname.nFileOffset = 0;
    fname.nFileExtension = 0;
    fname.lpstrDefExt = 0;
    fname.lCustData = NULL;
    fname.lpfnHook = NULL;
    fname.lpTemplateName = NULL;
    fname.lpstrInitialDir = NULL;
    HDC hDC = wglGetCurrentDC();
    HGLRC hRC = wglGetCurrentContext();
    GetOpenFileName(&fname);
    wglMakeCurrent(hDC, hRC);

    std::cout << fname.lpstrFile << std::endl;
}

你可能感兴趣的:(VS C++打开文件对话框报错:不能将char[]转换为LPWSTR)