1

I used this code below and I get all applications that are running in a Window.

Now I only want get the applications that are .Net (.exe). Can you help me?

public Form1()
{
    InitializeComponent();
    EnumWindows(new WindowEnumCallback(this.AddWnd), 0);
}

public delegate bool WindowEnumCallback(int hwnd, int lparam);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(WindowEnumCallback lpEnumFunc, int lParam);

[DllImport("user32.dll")]
public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);

[DllImport("user32.dll")]
public static extern bool IsWindowVisible(int h);

private List<string> Windows = new List<string>();
private bool AddWnd(int hwnd, int lparam)
{
    if (IsWindowVisible(hwnd))
    {
        StringBuilder sb = new StringBuilder(255);
        GetWindowText(hwnd, sb, sb.Capacity);
        Windows.Add(sb.ToString());
        txtTest.Text += hwnd.ToString()+" ";
        lst.Items.Add(sb.ToString());
    }

    return true;
}
John Willemse
  • 6,608
  • 7
  • 31
  • 45
user2208401
  • 95
  • 1
  • 1
  • 5
  • 1
    Umm, `.NET` != `.exe`. You somehow need to figure out if the application uses dlls of .net framework and I'm not sure if this can be easily determined... Maybe yes and someone will provide an answer, but.. Good luck. :) – walther Apr 15 '13 at 09:59
  • " Umm, .NET != .exe. " Thanks ^^ – user2208401 Apr 15 '13 at 12:40

1 Answers1

0

You can use a method like below, together with the P/Invoke function GetFileVersion():

[DllImport("mscoree.dll", CharSet = CharSet.Unicode)]
private static extern int GetFileVersion(string path, StringBuilder buffer, int buflen, out int written);

public static bool IsDotNetExecutable(string path)
{
    StringBuilder sBuilder = new StringBuilder(256);
    int written;
    int gvf = GetFileVersion(path, sBuilder, sBuilder.Capacity, out written);

    return gvf == 0;
}

The function does not throw an exception when it fails. A return value other than 0 indicates an error occured and the executable was not a .Net assembly. Therefore the IsDotNetExecutable method above returns true or false accordingly.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
  • Why not? Do you get an error? – John Willemse Apr 15 '13 at 12:46
  • Maybe you don't understand my question.^^ . Ok , now when I build this application , and have more applicaton .net is running, I will get all application .net is running by application for me. And in answer for you, .net application must have path. I think it don't work because I don't get path for all applicaton is running. Sorry for my english. My english for me very bad, maybe I can say with you for good my thing. ^^ – user2208401 Apr 15 '13 at 13:47
  • Okay, I understand what you mean. You don't always or never have the path to the executable. Please check [this question](http://stackoverflow.com/questions/11961137/getting-a-path-of-a-running-process-by-name) to learn how to get the path from a running process. – John Willemse Apr 15 '13 at 13:52
  • ok, thanks ! Let me try ^^ . Oh, Do you know "hook" ? I am building one applicaton, when my applicaton is running, it will get all application .net is running and get all control (button,gridview,table,label) in applicaton .net is running. I know I must use "hook" by dll, I must inject dll in .net applicaton. But I don't know Where do I should start? sr for my english ^^ Thanks for share :) – user2208401 Apr 15 '13 at 14:04