How to get and use the icon of exe-files (programs) in own applications

R

Rolf Welskes

Hello,

I have in windows forms, on the other hand in windows presentation the
following problem:

I have a program with a list of programs (exe-files).
Now I want to display the (standard) icons of these programs, similiar as in
explorer.

So, for example I have the pathes:

f:/aaaa/bbb/office/excel.exe or
f:/aaa/ccc/adobe/premiere.exe

So I have access to the program files,
but how do I get the (standard) icons of these program files and how do I
use
it in windows forms and in windows presentation foundation?


Thank you for any help.

Best Regards
Rolf Welskes
 
J

Josip Medved

Hi,

you may want to use interop here.

E.g. (works for executables)
--
System.IntPtr hLibrary = NativeMethods.LoadLibrary(fileName);
if (!hLibrary.Equals(System.IntPtr.Zero)) {
System.IntPtr hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
if (!hIcon.Equals(System.IntPtr.Zero)) {
Icon icon = System.Drawing.Icon.FromHandle(hIcon);
}
}
--

private static class NativeMethods {

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet =
CharSet.Unicode)]
static extern internal System.IntPtr LoadIcon(System.IntPtr hInstance,
string lpIconName);

[System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet =
CharSet.Unicode)]
static extern internal System.IntPtr LoadLibrary(string lpFileName);

}
 
J

Josip Medved

BTW. Don't forget to do FreeLibrary like I just did. :)
Icon dispose would also be nice after you are done with it.

Josip Medved said:
Hi,

you may want to use interop here.

E.g. (works for executables)
--
System.IntPtr hLibrary = NativeMethods.LoadLibrary(fileName);
if (!hLibrary.Equals(System.IntPtr.Zero)) {
System.IntPtr hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
if (!hIcon.Equals(System.IntPtr.Zero)) {
Icon icon = System.Drawing.Icon.FromHandle(hIcon);
}
}
--

private static class NativeMethods {

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet =
CharSet.Unicode)]
static extern internal System.IntPtr LoadIcon(System.IntPtr hInstance,
string lpIconName);

[System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet =
CharSet.Unicode)]
static extern internal System.IntPtr LoadLibrary(string lpFileName);

}




Rolf Welskes said:
Hello,

I have in windows forms, on the other hand in windows presentation the
following problem:

I have a program with a list of programs (exe-files).
Now I want to display the (standard) icons of these programs, similiar as
in explorer.

So, for example I have the pathes:

f:/aaaa/bbb/office/excel.exe or
f:/aaa/ccc/adobe/premiere.exe

So I have access to the program files,
but how do I get the (standard) icons of these program files and how do I
use
it in windows forms and in windows presentation foundation?


Thank you for any help.

Best Regards
Rolf Welskes
 
R

Rolf Welskes

Hello,
thank you, but does not work,
debugging show, for example if i take as file name excel.exe,
Module is found, all, ok,
but:
System.IntPtr hIcon = NativeMethods.LoadIcon(hLibrary,
"#32512");

gives back zero.
So, it does not work.
Furthermore the problem seems to be "#32512" .
Think I want to build a tree like explorer and have all icons of all
applications, normal windows exe, exe from dotnet, exe from wpf
applications,
so as explorer get the icons.

Thank you for any help.

Rolf Welskes


Josip Medved said:
BTW. Don't forget to do FreeLibrary like I just did. :)
Icon dispose would also be nice after you are done with it.

Josip Medved said:
Hi,

you may want to use interop here.

E.g. (works for executables)
--
System.IntPtr hLibrary = NativeMethods.LoadLibrary(fileName);
if (!hLibrary.Equals(System.IntPtr.Zero)) {
System.IntPtr hIcon = NativeMethods.LoadIcon(hLibrary, "#32512");
if (!hIcon.Equals(System.IntPtr.Zero)) {
Icon icon = System.Drawing.Icon.FromHandle(hIcon);
}
}
--

private static class NativeMethods {

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet =
CharSet.Unicode)]
static extern internal System.IntPtr LoadIcon(System.IntPtr hInstance,
string lpIconName);

[System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet =
CharSet.Unicode)]
static extern internal System.IntPtr LoadLibrary(string lpFileName);

}




Rolf Welskes said:
Hello,

I have in windows forms, on the other hand in windows presentation the
following problem:

I have a program with a list of programs (exe-files).
Now I want to display the (standard) icons of these programs, similiar
as in explorer.

So, for example I have the pathes:

f:/aaaa/bbb/office/excel.exe or
f:/aaa/ccc/adobe/premiere.exe

So I have access to the program files,
but how do I get the (standard) icons of these program files and how do
I use
it in windows forms and in windows presentation foundation?


Thank you for any help.

Best Regards
Rolf Welskes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top