Solved

SHGetFileInfo

Posted on 1997-05-15
10
857 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:yakky
  • 4
  • 4
  • 2
10 Comments
 
LVL 4

Expert Comment

by:mbhakta
ID: 1301570
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 ?
0
 
LVL 4

Expert Comment

by:mbhakta
ID: 1301571
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 ?
0
 

Author Comment

by:yakky
ID: 1301572
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
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Accepted Solution

by:
papirus earned 100 total points
ID: 1301573
to convert HICON into CBitmap :

Create a memory DC, create a bitmap, select this bitmap in DC,
call DrawIcon-function, than you'll have in this created bitmap the icon's image.
0
 

Author Comment

by:yakky
ID: 1301574
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);

}
0
 
LVL 1

Expert Comment

by:papirus
ID: 1301575
try this :

CBitmap Bmp ;
CDC MemDC  ;

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

....

0
 

Author Comment

by:yakky
ID: 1301576
It Almost works however, Ive lost the color.
Can you suggest how to get the color back?
0
 
LVL 1

Expert Comment

by:papirus
ID: 1301577
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) ;
}
0
 

Author Comment

by:yakky
ID: 1301578
Thanks very much, youve done an excellent job. Im very grateful.
Yours sincerely
Ian Robertson
0
 
LVL 1

Expert Comment

by:papirus
ID: 1301579
You are always wellcome.

Gena Pergament
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question