提取EXE图标

来源:互联网 发布:网络交流环境 编辑:程序博客网 时间:2024/05/08 22:45


void CTest::OnBnClickedButton()
{
LPCTSTR lpszExePath=L"C:\\Windows\\System32\\notepad.exe";
LPCTSTR lpszExePath1 = L"C:\\Users\\Administrator\\AppData\\Roaming\\360se6\\Application\\360se.exe";
HICON hIcon = NULL;
SHFILEINFO FileInfo;

  DWORD_PTR dwRet = ::SHGetFileInfo(lpszExePath1, 0, &FileInfo, sizeof(SHFILEINFO), SHGFI_ICON);
// hIcon = ExtractIcon(NULL, lpszExePath1, 0);


// 目标文件不存在


if (dwRet)
{
hIcon = FileInfo.hIcon;
}


PICTDESC pdiconsrc;
pdiconsrc.cbSizeofstruct = sizeof(PICTDESC);
pdiconsrc.icon.hicon = hIcon;
pdiconsrc.picType = PICTYPE_ICON;
IPicture* pIPicture = NULL;


LPBYTE lpBits = NULL;
long size = 0;
HRESULT hr;
IDispatch* pDisp = NULL;
hr = OleCreatePictureIndirect(&pdiconsrc, IID_IDispatch, TRUE, (void**)&pDisp);
if (SUCCEEDED(hr))
{
hr = pDisp->QueryInterface(&pIPicture);
if (SUCCEEDED(hr))
{
lpBits = (LPBYTE)GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, 64 * 1024);
IStream* pStream;
CreateStreamOnHGlobal(lpBits, TRUE, &pStream);
hr = pIPicture->SaveAsFile(pStream, TRUE, &size);
if (pStream)
pStream->Release();
if (SUCCEEDED(hr) && size>0)
{
//you can use   
char* pchar = (char*)lpBits;
// SaveIcon(hIcon, L"d:\\1.bmp");
}
pIPicture->Release();
if (lpBits)
GlobalFree((HGLOBAL)lpBits);
}
pDisp->Release();
}

}





void CTestMouseDlg::SaveIcon(HICON hIconToSave, LPCTSTR sIconFileName)
{
if (hIconToSave == NULL || sIconFileName == NULL)
return;
//warning: this code snippet is not bullet proof.  
//do error check by yourself [masterz]  
PICTDESC picdesc;
picdesc.cbSizeofstruct = sizeof(PICTDESC);
picdesc.picType = PICTYPE_ICON;
picdesc.icon.hicon = hIconToSave;
IPicture* pPicture = NULL;
OleCreatePictureIndirect(&picdesc, IID_IPicture, TRUE, (VOID**)&pPicture);
LPSTREAM pStream;
CreateStreamOnHGlobal(NULL, TRUE, &pStream);
LONG size;
HRESULT hr = pPicture->SaveAsFile(pStream, TRUE, &size);
TCHAR pathbuf[1024];
wcsncpy(pathbuf, sIconFileName, wcslen(sIconFileName));
// strcpy(pathbuf, sIconFileName);
CFile iconfile;
iconfile.Open(sIconFileName, CFile::modeCreate | CFile::modeWrite);
LARGE_INTEGER li;
li.HighPart = 0;
li.LowPart = 0;
ULARGE_INTEGER ulnewpos;
pStream->Seek(li, STREAM_SEEK_SET, &ulnewpos);
ULONG uReadCount = 1;
while (uReadCount>0)
{
pStream->Read(pathbuf, sizeof(pathbuf), &uReadCount);
if (uReadCount>0)
iconfile.Write(pathbuf, uReadCount);
}
pStream->Release();
iconfile.Close();
}

MFC中CFile 的读写会使图标失真严重

试过QT中的QFile基本满足使用

0 0
原创粉丝点击