c++ - GetOpenFileName API intermittently slow on 2003 server -
when running code below on windows server 2003, there intermittent slowdowns while getopenfilename
common dialog initializes. startup time varies @ around ~30 seconds. code below scratch program made exemplify problem i'm having in larger project problem exists in both.
one important note, when network interfaces disabled, time taken initialize closer normal - typically ~2 seconds. enabled again, problem returns.
i have put initialization time against other programs file open common dialogs (like notepad) not have same problem, or without network interfaces enabled.
code:
getnewfilename.h:
#define _twinmain wwinmain #include "windows.h" #include <string> #include "resource.h" using std::wstring; #define isolation_aware_enabled 1 #if defined _m_ix86 #pragma comment(linker,"/manifestdependency:\"type='win32' name='microsoft.windows.common-controls' version='6.0.0.0' processorarchitecture='x86' publickeytoken='6595b64144ccf1df' language='*'\"") #elif defined _m_ia64 #pragma comment(linker,"/manifestdependency:\"type='win32' name='microsoft.windows.common-controls' version='6.0.0.0' processorarchitecture='ia64' publickeytoken='6595b64144ccf1df' language='*'\"") #elif defined _m_x64 #pragma comment(linker,"/manifestdependency:\"type='win32' name='microsoft.windows.common-controls' version='6.0.0.0' processorarchitecture='amd64' publickeytoken='6595b64144ccf1df' language='*'\"") #else #pragma comment(linker,"/manifestdependency:\"type='win32' name='microsoft.windows.common-controls' version='6.0.0.0' processorarchitecture='*' publickeytoken='6595b64144ccf1df' language='*'\"") #endif
getnewfilename.cpp:
#include "getnewfilename.h" hinstance hinst; bool initinstance(hinstance); hwnd hwnd; wstring lastfile; bool getnewfilename(hwnd owner, wstring title, wstring defaultfile, lpwstr filetypes, openfilename &ofn, wstring lastdir, wstring lastfile, int mode, wstring extension); int apientry _twinmain(hinstance hinstance, hinstance hprevinstance, lptstr lpcmdline, int ncmdshow) { unreferenced_parameter(hprevinstance); unreferenced_parameter(lpcmdline); msg msg; haccel hacceltable; if (!initinstance (hinstance)) { return false; } hacceltable = loadaccelerators(hinstance, makeintresource(idr_accelerator1)); // main message loop: while (getmessage(&msg, null, 0, 0)) { if (!translateaccelerator(hwnd, hacceltable, &msg)) { translatemessage(&msg); dispatchmessage(&msg); } } return (int) msg.wparam; } bool initinstance(hinstance hinstance) { hinst = hinstance; openfilename ofn; wstring newfn; hwnd = createwindow(null, l"test", null, 0, 0, 0, 0, null, null, hinst, null); bool ret = getnewfilename(hwnd, l"test", l"*.xml", l"xml files\0*.xml\0", ofn, l"c:\\", lastfile, 1, l"xml"); if (ret) { newfn = ofn.lpstrfile; messagebox(hwnd,newfn.c_str(),l"test",mb_ok); } exitprocess(0); return true; } void addprefix(wstring &path) { wstring test = path.substr(0,4); if (path.substr(0,8) == l"\\\\?\\unc\\" || path.substr(0,4) == l"\\\\?\\") { return; } wstring prefix = path.substr(0,2); if (prefix.compare(l"\\\\") == 0) { path.assign(l"\\\\?\\unc\\"+path.substr(2,wstring::npos)); } else { path.assign(l"\\\\?\\"+path); } return; } uint_ptr callback ofnhookproc(hwnd hdlg, uint uimsg, wparam wparam, lparam lparam) { hicon hicon; int width, height, xleft, ytop; rect recttocenteron, rectsubdialog; switch (uimsg) { case wm_initdialog: hicon = loadicon(hinst, makeintresource(idi_pmmico)); sendmessage(getparent(hdlg), wm_seticon, icon_big, (lparam)hicon); sendmessage(getparent(hdlg), wm_seticon, icon_small, (lparam)hicon); getwindowrect(getdesktopwindow(), &recttocenteron); getwindowrect(getparent(hdlg), &rectsubdialog); width = rectsubdialog.right-rectsubdialog.left; height = rectsubdialog.bottom-rectsubdialog.top; xleft = recttocenteron.right / 2 - (width) / 2; ytop = recttocenteron.bottom / 2 - (height) / 2; bool test = setwindowpos(getparent(hdlg), null, xleft, ytop, width, height, swp_nosize); return (int_ptr)false; break; } return (int_ptr)false; } bool getnewfilename(hwnd owner, wstring title, wstring defaultfile, lpwstr filetypes, openfilename &ofn, wstring lastdir, wstring lastfile, int mode, wstring extension) { wstring lastdir_pre(lastdir); addprefix(lastdir_pre); if (lastfile.compare(l"") == 0) { lastfile = defaultfile; } bool ret = false; wchar_t buffer[30000]; ofn.lstructsize = sizeof(openfilename); ofn.hwndowner = owner; ofn.lpstrfile = buffer; lastfile.copy(ofn.lpstrfile,lastfile.size(),0); ofn.lpstrfile[lastfile.size()] = '\0'; ofn.nmaxfile = 30000; ofn.flags = ofn_overwriteprompt | ofn_enablehook | ofn_explorer; ofn.lpfnhook = ofnhookproc; ofn.lpstrfilter = filetypes; ofn.lpstrcustomfilter = null; ofn.nfilterindex = 0; ofn.lpstrinitialdir = lastdir_pre.c_str(); ofn.lpstrfiletitle = null; ofn.lpstrdefext = extension.c_str(); ofn.lpstrtitle = title.c_str(); if (mode == 1) { ofn.flags = ofn.flags | ofn_pathmustexist; ret = getopenfilename(&ofn); } else if (mode == 2) { ret = getsavefilename(&ofn); } if (ret == false) { ofn.lpstrfile[0] = '\0'; } return ret; }
Comments
Post a Comment