GetOpenFileName函数打开选择文件的对话框,“我的电脑”里面什么都显示不出来的解决办法

如下图所示,其他文件夹都正常,就是“我的电脑”里面什么都没有。

GetOpenFileName函数打开选择文件的对话框,“我的电脑”里面什么都显示不出来的解决办法_第1张图片

#include 
#include 
#include 

#pragma comment(lib, "comctl32.lib")
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' language='*' publicKeyToken='6595b64144ccf1df'\"")

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	char szFileName[MAX_PATH] = {0};
	OPENFILENAMEA ofn = {0};

	InitCommonControls();
	CoInitializeEx(NULL, COINIT_MULTITHREADED);

	ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
	ofn.hInstance = hInstance;
	ofn.hwndOwner = NULL;
	ofn.lStructSize = sizeof(OPENFILENAMEA);
	ofn.lpstrDefExt = "hex";
	ofn.lpstrFile = szFileName;
	ofn.lpstrFilter = "HEX文件 (*.hex)\0*.HEX\0所有文件 (*.*)\0*.*\0";
	ofn.nMaxFile = sizeof(szFileName);
	GetOpenFileNameA(&ofn);

	CoUninitialize();
	return 0;
}

原因是因为GetOpenFileName函数和COINIT_MULTITHREADED不兼容。把COINIT_MULTITHREADED改成COINIT_APARTMENTTHREADED,或者把CoInitializeEx和CoUninitialize删了,问题就解决了。

GetOpenFileName函数打开选择文件的对话框,“我的电脑”里面什么都显示不出来的解决办法_第2张图片
 

Calling shell functions and interfaces from a multithreaded apartment

When you call or access a shell function or shell interface from a thread that has been initialized as a multithreaded apartment, the function or interface may have its functionality impaired or completely fail.

https://docs.microsoft.com/en-us/troubleshoot/windows/win32/shell-functions-multithreaded-apartment

你可能感兴趣的:(Win32,GetOpenFileName)