9

Having the window handle for an open application, I'm able to use the GetWindowText function to retrieve the text from the title bar of the app. I would like to take this a step farther and retrieve the icon associated with the same app.

How might I go about doing this? I looked through what I thought would be the relevant Win32 API sections, but nothing jumped out at me.

Any pointers would be appreciated.

Thanks in advance!

efotinis
  • 14,565
  • 6
  • 31
  • 36
Brian K. Carroll
  • 125
  • 1
  • 1
  • 4

2 Answers2

18
Icon ico = Icon.ExtractAssociatedIcon(theProcess.MainModule.FileName);

From TheSoftwareJedi

Initially this appears to be an exact duplicate of How can I get the icon from the executable file only having an instance of it's Process in C# but that one seems to focus largely on how to get it from within it's own self, whereas you may be asking how to get the icon using a program separate from the running process.

-Adam

Community
  • 1
  • 1
Adam Davis
  • 91,931
  • 60
  • 264
  • 330
  • Permission dine, How I can fit it ?? – HoangHieu Sep 23 '15 at 10:32
  • 1
    @HoangHieu Look at these: http://stackoverflow.com/questions/23976825/how-can-i-add-to-listbox1-all-the-processes-names-and-also-each-process-icon-nea and http://www.vbforums.com/showthread.php?599432-RESOLVED-Extracting-Icons-from-Processes for solutions to "Access Denied" runtime errors with `Icon.ExtractAssociatedIcon` – Adam Davis Sep 23 '15 at 10:48
5

You could do the following:

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll")]
static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName);

[DllImport("user32.dll", EntryPoint = "GetClassLong")]
static extern uint GetClassLong32(IntPtr hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "GetClassLongPtr")]
static extern IntPtr GetClassLong64(IntPtr hWnd, int nIndex);

/// <summary>
/// 64 bit version maybe loses significant 64-bit specific information
/// </summary>
static IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex)
{
    if (IntPtr.Size == 4)
        return new IntPtr((long)GetClassLong32(hWnd, nIndex));
    else
        return GetClassLong64(hWnd, nIndex);
}


uint WM_GETICON = 0x007f;
IntPtr ICON_SMALL2 = new IntPtr(2);
IntPtr IDI_APPLICATION = new IntPtr(0x7F00);
int GCL_HICON = -14;

public static Image GetSmallWindowIcon(IntPtr hWnd)
{
    try
    {
        IntPtr hIcon = default(IntPtr);

        hIcon = SendMessage(hWnd, WM_GETICON, ICON_SMALL2, IntPtr.Zero);

        if (hIcon == IntPtr.Zero)
            hIcon = GetClassLongPtr(hWnd, GCL_HICON);

        if (hIcon == IntPtr.Zero)
            hIcon = LoadIcon(IntPtr.Zero, (IntPtr)0x7F00/*IDI_APPLICATION*/);

        if (hIcon != IntPtr.Zero)
            return new Bitmap(Icon.FromHandle(hIcon).ToBitmap(), 16, 16);
        else
            return null;
    }
    catch (Exception)
    {
        return null;
    }
}

This code retrieves the small window icon, which is shown next to the window text in the titlebar.

tklepzig
  • 598
  • 9
  • 19