none
How do I get the window class and window name of an application without Spy++?

    Question

  • I am using express and it does not come with Spy++. I am trying to have my program enter keystrokes as demonstrated on this page http://msdn.microsoft.com/en-us/library/ms171548.aspx and I can get the example with calculator to work. But the the article assumes you have Spy++ and without it you can't go any further.

    I need the parameters for World of Warcraft. I am trying to make an application that presses the Num Lock key every time you press the Num Lock key so that it doesn't switch between arrows and numbers. This way when I press Num Lock to do a spell on some monster it doesn't turn off my other spells.

    As you can tell this is extremely important so your edpediant response is appreciated.

    Wednesday, May 05, 2010 2:26 PM

Answers

  • You can get process's main window name like this

    Process p = Process.GetProcessById(Pid);// supply process id.

    then p.MainWindowHandle and pMainWindowTitle . using this main window name, you call win32 API GetClassName ( http://msdn.microsoft.com/en-us/library/ms633582(VS.85).aspx) to get the class name. Since its native api, use pInvoke method to call this.


    Thanks Mike -------- Mark best answers as Answer ----------
    Wednesday, May 05, 2010 2:45 PM
  • Okay I am pretty sure I have it figured out. This link was very helpful as well http://www.pinvoke.net/default.aspx/user32/GetClassName.html

    I'm not positive this works but it looks like it does. Here is the code I ended up using:

     

    [DllImport("USER32.DLL")]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
    
    Process process = Process.GetProcessById(8064); //PID of what you want
          IntPtr handle = process.MainWindowHandle;
          string title = process.MainWindowTitle;
          StringBuilder className = new StringBuilder(100);
          int nret = GetClassName(handle, className, className.Capacity);
    Thanks for your help!
    Wednesday, May 05, 2010 5:42 PM

All replies

  • You can get process's main window name like this

    Process p = Process.GetProcessById(Pid);// supply process id.

    then p.MainWindowHandle and pMainWindowTitle . using this main window name, you call win32 API GetClassName ( http://msdn.microsoft.com/en-us/library/ms633582(VS.85).aspx) to get the class name. Since its native api, use pInvoke method to call this.


    Thanks Mike -------- Mark best answers as Answer ----------
    Wednesday, May 05, 2010 2:45 PM
  • Okay I need some help because I am not good with Win32. I have:

    [DllImport("USER32.DLL")]
        public static extern int GetClassName(IntPtr hWnd, string lpClassName, int nMaxCount);

    and

    Process process = Process.GetProcessById(6064);
          IntPtr handle = process.MainWindowHandle;
          string title = process.MainWindowTitle;
          string className;
          GetClassName(handle, className, ???
    I'm not sure what to use as the third parameter. Also, I'm not sure if I need to specify lpClassName as out.
    Wednesday, May 05, 2010 4:27 PM
  • className is Out parameter, here you will get the result.
    Thanks Mike -------- Mark best answers as Answer ----------
    Wednesday, May 05, 2010 5:03 PM
  • I know the documentation says it is an out parameter but I'm not sure if I need to use

    [DllImport("USER32.DLL")]
        public static extern int GetClassName(IntPtr hWnd, out string lpClassName, int nMaxCount);

    or

    [DllImport("USER32.DLL")]
        public static extern int GetClassName(IntPtr hWnd, string lpClassName, int nMaxCount);
    When using the first I get null and when using the second I get "". But I am also passing 0 for nMaxCount. The documentation says nMaxCount is "the length, in TCHARs, of the buffer pointed to by the lpClassName parameter". Well what is that?
    Wednesday, May 05, 2010 5:23 PM
  • Okay I am pretty sure I have it figured out. This link was very helpful as well http://www.pinvoke.net/default.aspx/user32/GetClassName.html

    I'm not positive this works but it looks like it does. Here is the code I ended up using:

     

    [DllImport("USER32.DLL")]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
    
    Process process = Process.GetProcessById(8064); //PID of what you want
          IntPtr handle = process.MainWindowHandle;
          string title = process.MainWindowTitle;
          StringBuilder className = new StringBuilder(100);
          int nret = GetClassName(handle, className, className.Capacity);
    Thanks for your help!
    Wednesday, May 05, 2010 5:42 PM
  • Yes, this did work!
    Wednesday, May 05, 2010 7:29 PM