GetOpenFileName()的问题

时间:2016-03-27 17:15:33

标签: c++ winapi visual-c++

我遇到以下问题:

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(*.*)\0*.*\0");
ofn.nFilterIndex = 1;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

if (GetOpenFileName(&ofn) == false)
{
    problem = CommDlgExtendedError();
    return false;
}

从GetOpenFileName,它只是转到problem = CommDlgExtendedError();而没有建立对话框。

1 个答案:

答案 0 :(得分:2)

您必须为nMaxFile struct成员分配一个内存,并将\0设置为其大小。此外,缓冲区的第一个字符应设置为// Initialize OPENFILENAME ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFile = szFile; // Set lpstrFile[0] to '\0' so that GetOpenFileName does not // use the contents of szFile to initialize itself. ofn.lpstrFile[0] = '\0'; 以防止文件名初始化。 MSDN示例:

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("ALERTTILESTRING");
builder.setMessage("alertNameString");
builder.setCancelable(false);
builder.setNegativeButton("Close",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});

AlertDialog alert = builder.create();
alert.show();