GetOpenFileName()存在问题

Problems with GetOpenFileName()

本文关键字:问题 存在 GetOpenFileName      更新时间:2023-10-16

我对以下代码有问题:

HANDLE hFile;
DWORD bytesRead;
OPENFILENAME ofn;
DWORD problem;
WCHAR title[260];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = (LPWSTR)title;
ofn.nMaxFile = sizeof(title);
ofn.lpstrFilter = TEXT("All files(*.*)*.*");
ofn.nFilterIndex = 1;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn) == false)
{
    problem = CommDlgExtendedError();
    return false;
}

从GetOpenFileName,它只需转到problem = CommDlgExtendedError();,而不显示对话框。

您必须为lpstrFile结构成员分配内存,并将nMaxFile设置为其大小。缓冲区的第一个字符也应该设置为,以防止文件名初始化。MSDN示例:

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
// Set lpstrFile[0] to '' so that GetOpenFileName does not 
// use the contents of szFile to initialize itself.
ofn.lpstrFile[0] = '';