GetOpenFileName触发断点

时间:2012-01-21 00:00:18

标签: c++ dialog breakpoints

我正试图像这样调用GetOpenFileName:

int main(int argc, char* argv[])
{

OPENFILENAME ofn;       // common dialog box structure
wchar_t szFile[260];       // buffer for file name
HWND hwnd;              // owner window
HANDLE hf;              // file handle
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
wchar_t title[500];  // to hold title
GetConsoleTitle( title, 500 );
HWND hwndConsole = FindWindow( NULL, title );
ofn.hwndOwner = hwndConsole;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

// Display the Open dialog box. 

if (GetOpenFileName(&ofn)==TRUE) 
    hf = CreateFile(ofn.lpstrFile, 
                    GENERIC_READ,
                    0,
                    (LPSECURITY_ATTRIBUTES) NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL,
                    (HANDLE) NULL);

编程停止(消息:example.exe已触发断点(不是我放置的))“if(GetOpenFileName(& ofn)== TRUE)”当我中断时,我收到一条消息,说明没有源可用。 如果我没有中断并按下继续,则弹出对话框并按预期工作。我究竟做错了什么? 我只是注意到它在发布模式下没有问题......

1 个答案:

答案 0 :(得分:1)

一个可能的问题: ofn.nMaxFile应该是字符数,而不是缓冲区的字节大小。试试这个:

ofn.nMaxFile = sizeof(szFile) / sizeof(wchar_t);