Link to home
Start Free TrialLog in
Avatar of yakky
yakky

asked on

SHGetFileInfo

Heres my problem, Im trying to get the path/filename to the icon from  an .exe file. Can any one modify the following bit of code to do this (since it doesnt work at the moment).

SHFILEINFO shfi;
if(SHGetFileInfo("c:\\windows\\regedit.exe",0,&shfi,sizeof(SHFILEINFO),SHGFI_ICONLOCATION))
{
CString me = (char*)shfi.szDisplayName;
TRACE("Succeded getting icon path  
from .exe*%s*\n",me);
}
else
{
TRACE("FAILED\n");
}

Thanks in advance
Ian Robertson
Avatar of mbhakta
mbhakta

SHGFI_ICONLOCATION only gets the display name of the application as it appears from on the desktop. Can you make it clear why would you like to get the filename of the icon ?
SHGFI_ICONLOCATION only gets the display name of the application as it appears from on the desktop. Can you make it clear why would you like to get the filename of the icon ?
Avatar of yakky

ASKER

I have a program which converts a filename of an icon into a bitmap. I was hoping to use SHGetFileInfo (SHGFI_ICONLOCATION) because according to the documentation it retrieves and I quote "The name of the file that contains the icon representing the file. The name is copied to the szDisplayName member of the structure specified by psfi".

That is why I would like to be able to get hold of a programs, associated icon's filename.

Alternatively could someone tell me how to convert a HICON to a CBitmap.
Thanks again
Ian
ASKER CERTIFIED SOLUTION
Avatar of papirus
papirus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yakky

ASKER

Ok I dont know that much about MFC/windows calls
Ive tried papirus suggestion and nothing happens what am I doing wrong.


HICON hGen32Icon;
SHFILEINFO shfi;
CBitmap *ian = new CBitmap;
HDC ianDC=::CreateCompatibleDC(NULL);

if(SHGetFileInfo("c:\\windows\\regedit.exe",0,&shfi,sizeof(SHFILEINFO),SHGFI_ICON))
{
      hGen32Icon = shfi.hIcon;
TRACE("1Succeded getting icon from .exe\n");
      DrawIcon(ianDC,0,0,hGen32Icon);
      GetMenu()->SetMenuItemBitmaps( IDM_ONE, MF_ENABLED,ian,NULL);

}
try this :

CBitmap Bmp ;
CDC MemDC  ;

MemDC.CreateCompatibleDC(NULL) ;
Bmp.CreateCompatibleBitmap(&MemDC,32,32) ;
MemDC.SelectObject(&Bmp) ;
::DrawIcon(MemDC.m_hDC,0,0,hIcon) ;

....

Avatar of yakky

ASKER

It Almost works however, Ive lost the color.
Can you suggest how to get the color back?
CBitmap Bmp ;
CDC MemDC ;
CDC *pWndDC = AfxGetMainWnd()->GetDC() ;
if (pWndDC)
{
      MemDC.CreateCompatibleDC(pWndDC) ;
      Bmp.CreateCompatibleBitmap(&pWndDC,32,32) ;
      MemDC.SelectObject(&Bmp) ;
      ::DrawIcon(MemDC.m_hDC,0,0,hIcon) ;
      AfxGetMainWnd()->ReleaseDC(pWndDC) ;
}
Avatar of yakky

ASKER

Thanks very much, youve done an excellent job. Im very grateful.
Yours sincerely
Ian Robertson
You are always wellcome.

Gena Pergament