首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

SHGetFileInfo 调用3000多次后失败,写了代码,重现了这个异常

2012-04-05 
SHGetFileInfo 调用3000多次后失败,写了代码,重现了这个错误,void CaDlg::OnBnClickedOk(){// TODO: 在此

SHGetFileInfo 调用3000多次后失败,写了代码,重现了这个错误,
void CaDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
CoInitialize(NULL);
long num=0;
while(true)
{

SHFILEINFO FileInfo;
FileInfo.hIcon=0;
memset(&FileInfo,0,sizeof(SHFILEINFO));

SHGetFileInfo(L"C:\\", 0, &FileInfo, sizeof(FileInfo),SHGFI_ICON);
if(FileInfo.hIcon==0)
{
CString str;
str.Format(L"调用%d次后失败",num);
MessageBox(str);
break;
}else
{
//CloseHandle(FileInfo.hIcon);//写上这句话,也是不行的,
}


num++;
}




CoUninitialize();



}

求解决办法。。。。。。我要得到文件的图标句柄,所以这个函数要调用很多次。

[解决办法]
MSDN解释:
You should call this function from a background thread. Failure to do so could cause the UI to stop responding.

If SHGetFileInfo returns an icon handle in the hIcon member of the SHFILEINFO structure pointed to by psfi, you are responsible for freeing it with DestroyIcon when you no longer need it.

MSDN例子,
LPITEMIDLIST pidl = NULL;
hr = SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, 0, &pidl);

if (SUCCEEDED(hr))
{
SHFILEINFOW sfi = {0};
hr = SHGetFileInfo((LPCTSTR)pidl,
-1,
&sfi,
sizeof(sfi),
SHGFI_PIDL | SHGFI_DISPLAYNAME)

if (SUCCEEDED(hr))
{
// The display name is now held in sfi.szDisplayName.
}
}

ILFree(pidl);

仅供参考!

可能你已经看过了,建议模仿例子,开个线程跑下,上面说如果不需要的时候调用 DestroyIcon, 试下呢。
[解决办法]
进程内存耗尽?
CloseHandle(FileInfo.hIcon改为
DestroyIcon(FileInfo.hIcon);//写上这句话,也是不行的
msdn:

If SHGetFileInfo returns an icon handle in the hIcon member of the SHFILEINFO 
structure pointed to by psfi, you are responsible for freeing it with DestroyIcon when you no longer need it.

热点排行