我尝试这样调用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);程序在"if (GetOpenFileName(&ofn)==TRUE)“处停止(消息: example.exe已触发断点(不是我放置的断点))。当我中断时,我收到一条消息,表明没有可用的源。如果我没有中断,只需按下continue,对话框就会弹出,并按预期工作。我做错了什么?我只是注意到它在发布模式下工作没有问题...
发布于 2012-01-21 12:18:29
一个可能的问题是:ofn.nMaxFile应该是字符的数量,而不是缓冲区的字节大小。试着这样做:
ofn.nMaxFile = sizeof(szFile) / sizeof(wchar_t);https://stackoverflow.com/questions/8949441
复制相似问题