GetOpenFileName 偶尔不弹文件选择框,程序卡死

banyou527 2020-08-11 12:57:50
如题,GetOpenFileName 偶尔不弹文件选择框,程序卡死;
代码如下:
OPENFILENAME ofn = { 0 };
ZeroMemory(&ofn,sizeof(ofn));

TCHAR strOpenFileNames[80*MAX_PATH] = { 0 };//用于接收文件名
TCHAR szPath[MAX_PATH];
TCHAR szFileName[80*MAX_PATH];

ofn.lStructSize = sizeof(OPENFILENAME);//结构体大小
ofn.hwndOwner = GetForegroundWindow();

char type[MAX_PATH];
memset(type,0,sizeof(type));
string tmpType = "File Type";
memcpy(type,tmpType.c_str(),tmpType.length());
memcpy(type+tmpType.length()+1,fileType.c_str(),fileType.length());
ofn.lpstrFilter = TEXT("All File\0*.*\0Image
Files\0*.jpg;*.JPG;*.jpeg;*.JPEG;*.BMP;*.bmp;*.PNG;*.png;*.docx;*.xlsx;*.xls;*.txt;*.ppt;*.pptx\0\0");//设置过滤

ofn.lpstrFile = strOpenFileNames;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(strOpenFileNames);
ofn.lpstrInitialDir = NULL;
char buf[MAX_PATH] = "file selection";
ofn.lpstrTitle = TEXT(buf);//使用系统默认标题留空即可
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER |
OFN_ALLOWMULTISELECT;

if (GetOpenFileName(&ofn)) //偶尔运行到此处,不弹文件选择框,程序卡在这里
{}
...全文
716 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
banyou527 2020-08-28
  • 打赏
  • 举报
回复
引用 8 楼 赵4老师 的回复:
GDI或句柄泄露?
在任务管理器 进程 查看 选择列 里面选择:内存使用、虚拟内存大小、句柄数、线程数、USER对象、GDI对象
让你的程序(进程)不退出,循环执行主流程很多遍,越多越好,比如1000000次甚至无限循环,记录以上各数值,再隔至少一小时,越长越好,比如一个月,再记录以上各数值。如果以上两组数值的差较大或随时间流逝不断增加,则铁定有对应资源的资源泄漏!

你说的问题我也考虑到了,但是出问题的时候、我查看过GDI和句柄数,大概就是几百,应该不是这个问题吧?
赵4老师 2020-08-26
  • 打赏
  • 举报
回复
GDI或句柄泄露? 在任务管理器 进程 查看 选择列 里面选择:内存使用、虚拟内存大小、句柄数、线程数、USER对象、GDI对象 让你的程序(进程)不退出,循环执行主流程很多遍,越多越好,比如1000000次甚至无限循环,记录以上各数值,再隔至少一小时,越长越好,比如一个月,再记录以上各数值。如果以上两组数值的差较大或随时间流逝不断增加,则铁定有对应资源的资源泄漏!
banyou527 2020-08-24
  • 打赏
  • 举报
回复
引用 3 楼 赵4老师 的回复:
GetOpenFileName
The GetOpenFileName function creates an Open common dialog box that lets the user specify the drive, directory, and the name of a file or set of files to open.

BOOL GetOpenFileName(
LPOPENFILENAME lpofn // address of structure with initialization
// data
);

Parameters
lpofn
Pointer to an OPENFILENAME structure that contains information used to initialize the dialog box. When GetOpenFileName returns, this structure contains information about the user's file selection.
Return Values
If the user specifies a filename and clicks the OK button, the return value is nonzero. The buffer pointed to by the lpstrFile member of the OPENFILENAME structure contains the full path and filename specified by the user.

If the user cancels or closes the Open dialog box or an error occurs, the return value is zero. To get extended error information, call the CommDlgExtendedError function, which can return one of the following values:

CDERR_FINDRESFAILURE CDERR_NOHINSTANCE
CDERR_INITIALIZATION CDERR_NOHOOK
CDERR_LOCKRESFAILURE CDERR_NOTEMPLATE
CDERR_LOADRESFAILURE CDERR_STRUCTSIZE
CDERR_LOADSTRFAILURE FNERR_BUFFERTOOSMALL
CDERR_MEMALLOCFAILURE FNERR_INVALIDFILENAME
CDERR_MEMLOCKFAILURE FNERR_SUBCLASSFAILURE


Remarks
Beginning with Windows 95 and Windows NT version 4.0, the default Open dialog box provides user-interface features that are similar to the Windows Explorer. You can provide an OFNHookProc hook procedure for an Explorer-style Open dialog box. To enable the hook procedure, set the OFN_EXPLORER and OFN_ENABLEHOOK flags in the Flags member of the OPENFILENAME structure and specify the address of the hook procedure in the lpfnHook member.

Windows 95 and Windows NT continue to support the old-style Open dialog box for applications that want to maintain a user-interface consistent with the Windows 3.1 or Windows NT 3.51 user-interface. To display the old-style Open dialog box, enable an OFNHookProcOldStyle hook procedure and ensure that the OFN_EXPLORER flag is not set.

To display a dialog box that allows the user to select a directory instead of a file, call theSHBrowseForFolder function.

Windows CE: Not all members of the OPENFILENAME structure are defined in Windows CE. For information about the defined members, see the reference topic for the OPENFILENAME structure.

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in commdlg.h.
Import Library: Use comdlg32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.

See Also
Common Dialog Box Library Overview, Common Dialog Box Functions, CommDlgExtendedError, GetSaveFileName, OFNHookProc , OFNHookProcOldStyle, OPENFILENAME,SHBrowseForFolder

我这调用没有报错、else的逻辑我这没粘贴。错误码也没法获取。
banyou527 2020-08-24
  • 打赏
  • 举报
回复
引用 4 楼 zgl7903 的回复:
留意下 ofn.hwndOwner = GetForegroundWindow();
GetForegroundWindow 不一定是你的程序窗口

这个会影像文件选择框的弹出么?
我这个程序是一直在后台运行的,运行了一段时间后,偶尔出现不弹文件选择框。
banyou527 2020-08-24
  • 打赏
  • 举报
回复
引用 2 楼 轻箬笠 的回复:
改用CFileDialog咋样?GetOpenFileName这个函数用的人好像不是很多。


这个是MFC的方法、我要用标准C/C++
zgl7903 2020-08-21
  • 打赏
  • 举报
回复
留意下 ofn.hwndOwner = GetForegroundWindow();
GetForegroundWindow 不一定是你的程序窗口

赵4老师 2020-08-21
  • 打赏
  • 举报
回复
GetOpenFileName The GetOpenFileName function creates an Open common dialog box that lets the user specify the drive, directory, and the name of a file or set of files to open. BOOL GetOpenFileName( LPOPENFILENAME lpofn // address of structure with initialization // data ); Parameters lpofn Pointer to an OPENFILENAME structure that contains information used to initialize the dialog box. When GetOpenFileName returns, this structure contains information about the user's file selection. Return Values If the user specifies a filename and clicks the OK button, the return value is nonzero. The buffer pointed to by the lpstrFile member of the OPENFILENAME structure contains the full path and filename specified by the user. If the user cancels or closes the Open dialog box or an error occurs, the return value is zero. To get extended error information, call the CommDlgExtendedError function, which can return one of the following values: CDERR_FINDRESFAILURE CDERR_NOHINSTANCE CDERR_INITIALIZATION CDERR_NOHOOK CDERR_LOCKRESFAILURE CDERR_NOTEMPLATE CDERR_LOADRESFAILURE CDERR_STRUCTSIZE CDERR_LOADSTRFAILURE FNERR_BUFFERTOOSMALL CDERR_MEMALLOCFAILURE FNERR_INVALIDFILENAME CDERR_MEMLOCKFAILURE FNERR_SUBCLASSFAILURE Remarks Beginning with Windows 95 and Windows NT version 4.0, the default Open dialog box provides user-interface features that are similar to the Windows Explorer. You can provide an OFNHookProc hook procedure for an Explorer-style Open dialog box. To enable the hook procedure, set the OFN_EXPLORER and OFN_ENABLEHOOK flags in the Flags member of the OPENFILENAME structure and specify the address of the hook procedure in the lpfnHook member. Windows 95 and Windows NT continue to support the old-style Open dialog box for applications that want to maintain a user-interface consistent with the Windows 3.1 or Windows NT 3.51 user-interface. To display the old-style Open dialog box, enable an OFNHookProcOldStyle hook procedure and ensure that the OFN_EXPLORER flag is not set. To display a dialog box that allows the user to select a directory instead of a file, call theSHBrowseForFolder function. Windows CE: Not all members of the OPENFILENAME structure are defined in Windows CE. For information about the defined members, see the reference topic for the OPENFILENAME structure. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in commdlg.h. Import Library: Use comdlg32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also Common Dialog Box Library Overview, Common Dialog Box Functions, CommDlgExtendedError, GetSaveFileName, OFNHookProc , OFNHookProcOldStyle, OPENFILENAME,SHBrowseForFolder
轻箬笠 2020-08-21
  • 打赏
  • 举报
回复
改用CFileDialog咋样?GetOpenFileName这个函数用的人好像不是很多。
banyou527 2020-08-20
  • 打赏
  • 举报
回复
顶一下、 表沉

65,175

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧