CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Posts
    19

    Question Get Icon from other application.

    HICON LoadIcon(
    HINSTANCE hInstance, // handle to application instance
    LPCTSTR lpIconName // icon-name string or icon resource
    // identifier
    );

    Hi, I want to use LoadIcon function to get the icon from other application, I can provide the hInstance, but how can I know the icon's name?
    Thank you!

    How can taskmgr.exe retrieve those running application's icon?

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    Search the MSDN for ExtractIcon.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396
    Get Icon from other application.
    ...LoadIcon(...)
    LoadIcon(...) worked good for your purpose in Win NT, but in 2000 you could get some problems using it.
    The best way to "Get Icon from other application", as Marc G already wrote, is to use ExtractIcon (or ExtractIconEx if you want to get a small icon, not only large one).

    Victor

  4. #4
    Join Date
    Nov 2003
    Posts
    19

    Question

    Thank you very much! But I want to grab the window's handle on the fly and display it, just like that in taskmgr.exe. I hope you can give me more info and help! Best regards!

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    If you have a window handle, you can get the icon as follows:
    Code:
    HICON hIcon = (HICON)::SendMessage(hwnd, WM_GETICON, ICON_BIG, 0);
    If you really want to get the EXE icon of the exe that created the window, you first need to get the exe from the hwnd as follows:
    Code:
    	DWORD dwProcessID = 0;
    	DWORD dwThreadID = GetWindowThreadProcessId(hWnd, &dwProcessID);
    	TCHAR pszModuleFileName[MAX_PATH+1] = {0};
    	HANDLE hProcessSnap = NULL; 
    	PROCESSENTRY32 pe32 = {0}; 
    
    	//  Take a snapshot of all processes in the system. 
    	hProcessSnap = NuonSoftShellEnhancer::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
    
    	if (hProcessSnap == INVALID_HANDLE_VALUE) 
    		return; 
    
    	//  Fill in the size of the structure before using it. 
    	pe32.dwSize = sizeof(PROCESSENTRY32); 
    
    	//  Walk the snapshot of the processes, and for each process, 
    	//  display information. 
    	if (NuonSoftShellEnhancer::Process32First(hProcessSnap, &pe32)) 
    	{ 
    		do 
    		{ 
    			if (pe32.th32ProcessID == dwProcessID)
    			{
    				_tcscpy(pszModuleFileName, pe32.szExeFile);
    				break;
    			}
    		} 
    		while (NuonSoftShellEnhancer::Process32Next(hProcessSnap, &pe32)); 
    	} 
    	else 
    	{
    		CloseHandle (hProcessSnap); 
    		return;    // could not walk the list of processes 
    	}
    
    	// Do not forget to clean up the snapshot object. 
    	CloseHandle (hProcessSnap);
    Now pszModuleFileName should contain the module name that created the window.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Nov 2003
    Posts
    19
    But ::SendMessage(hWnd, WM_GETICON, ...) doesn't work with every window. I still wonder how can taskmgr.exe retrieve the correct icons from the open windows.

  7. #7
    Join Date
    Nov 2003
    Posts
    19
    PROCESSENTRY32 + ExtractIconEx also not effective all the time, since one process can open multiple windows at the same time.
    I really want to know how can Task Manager -- taskmgr.exe in windows 2000 -- list those icon correctly. Can somebody give me further help? Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured