Click here to Skip to main content
15,890,690 members
Home / Discussions / C#
   

C#

 
GeneralRe: How can I dictate the position of the run prompt? Pin
turbosupramk329-Apr-14 9:14
turbosupramk329-Apr-14 9:14 
GeneralRe: How can I dictate the position of the run prompt? Pin
Dave Kreskowiak29-Apr-14 10:36
mveDave Kreskowiak29-Apr-14 10:36 
GeneralRe: How can I dictate the position of the run prompt? Pin
turbosupramk330-Apr-14 6:02
turbosupramk330-Apr-14 6:02 
GeneralRe: How can I dictate the position of the run prompt? Pin
Dave Kreskowiak30-Apr-14 8:16
mveDave Kreskowiak30-Apr-14 8:16 
GeneralRe: How can I dictate the position of the run prompt? Pin
turbosupramk330-Apr-14 8:49
turbosupramk330-Apr-14 8:49 
GeneralRe: How can I dictate the position of the run prompt? Pin
Alan N29-Apr-14 11:25
Alan N29-Apr-14 11:25 
GeneralRe: How can I dictate the position of the run prompt? Pin
turbosupramk330-Apr-14 6:11
turbosupramk330-Apr-14 6:11 
GeneralRe: How can I dictate the position of the run prompt? Pin
Alan N1-May-14 3:12
Alan N1-May-14 3:12 
I see you've got Dave's FindWindow suggestion working and it's certainly a lot simpler than my method.

As you asked about EnumWindows here goes, although you may have to do some reading yourself, as getting callback functions to work reliably requires some advanced techniques.

First part of the answer on how EnumWindows works
C#
EnumWindows(SelectWindowsByPID, (IntPtr)selectedProcess.Id);

The .NET program calls EnumWindows and supplies a method that will be called by Windows for every handle that is found. This is the callback method, the c# signature of which is fixed as
C#
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr state);

Windows sets the first parameter to the window handle it has found. The second parameter is a copy of the second argument supplied to EnumWindows, which provides a way for the programmer to pass information into the callback. I've used it to pass in the process Id.

Fully operational examples have additional complexity as it is essential to stop the .NET garbage collector moving any object that is referenced by the callback function. Rather than modify the original code I'll show you a complete class that gives a list of window handles.

C#
using System;
using System.Runtime.InteropServices;

/// <summary>
/// Get a list of window handles associated with a specific process ID
/// </summary>
public class ProcessWindowList2 : System.Collections.Generic.List<IntPtr> {

  [DllImport("user32.dll", SetLastError = true)]
  [return: MarshalAs(UnmanagedType.Bool)]
  private static extern bool EnumWindows(EnumWindowsProc callbackMethod, IntPtr state);

  private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr state);

  [DllImport("user32.dll")]
  private static extern UInt32 GetWindowThreadProcessId(
    [In] IntPtr hwnd,
    out UInt32 processId);

  private readonly Int32 pid;

  private static Int32 GetWindowPID(IntPtr hwnd) {
    UInt32 PID;
    GetWindowThreadProcessId(hwnd, out PID);
    return unchecked((Int32)PID);
  }

  private static Boolean SelectWindows(IntPtr hwnd, IntPtr state) {
    GCHandle gch = GCHandle.FromIntPtr(state);
    ProcessWindowList2 list = (ProcessWindowList2)gch.Target;

    Int32 windowPid = GetWindowPID(hwnd);
    if (list.pid == windowPid) {
      list.Add(hwnd);
    }
    return true;
  }

  /// <summary>
  /// Create and populate a list of window handles associated with a process
  /// </summary>
  /// <param name="processId">The process Id</param>
  public ProcessWindowList2(Int32 processId) {
    this.pid = processId;

    EnumWindowsProc callback = new EnumWindowsProc(SelectWindows);

    GCHandle gch = GCHandle.Alloc(this);
    try {
      EnumWindows(callback, GCHandle.ToIntPtr(gch));
    } finally {
      gch.Free();
    }
  }
}


This is a modified version of published code http://msdn.microsoft.com/en-us/magazine/bb985713.aspx[^]. It's derived from List<IntPtr> but has a special constructor that populates the list with handles. The code makes use of GCHandle.Alloc(this) to inform the garbage collector that objects of this class must not be moved until GCHandle.Free is called. This is essential for the safe operation of the callback.

Using the class is very simple
C#
Process p = Process.Start(@"c:\Windows\System32\rundll32.exe", "shell32.dll,#61");
ProcessWindowList2 hwndList = new ProcessWindowList2(p.Id);
foreach (IntPtr hwnd in hwndList) {
  // do something
}


Have fun!

Alan.
GeneralRe: How can I dictate the position of the run prompt? Pin
turbosupramk32-May-14 1:46
turbosupramk32-May-14 1:46 
QuestionImplementing transaction in MVC as Attribute Pin
nitin_ion28-Apr-14 0:12
nitin_ion28-Apr-14 0:12 
AnswerRe: Implementing transaction in MVC as Attribute Pin
Dave Kreskowiak28-Apr-14 3:18
mveDave Kreskowiak28-Apr-14 3:18 
GeneralRe: Implementing transaction in MVC as Attribute Pin
nitin_ion28-Apr-14 17:19
nitin_ion28-Apr-14 17:19 
GeneralRe: Implementing transaction in MVC as Attribute Pin
Dave Kreskowiak28-Apr-14 17:55
mveDave Kreskowiak28-Apr-14 17:55 
QuestionHow to use Multithreading in Async Socket Http request Pin
sbmzhcn27-Apr-14 15:22
sbmzhcn27-Apr-14 15:22 
Questionlooking for a decent book on c# for advanced programmers Pin
Nico Haegens27-Apr-14 1:10
professionalNico Haegens27-Apr-14 1:10 
AnswerRe: looking for a decent book on c# for advanced programmers Pin
Pete O'Hanlon27-Apr-14 1:59
mvePete O'Hanlon27-Apr-14 1:59 
AnswerRe: looking for a decent book on c# for advanced programmers Pin
BillWoodruff27-Apr-14 7:08
professionalBillWoodruff27-Apr-14 7:08 
AnswerRe: looking for a decent book on c# for advanced programmers Pin
jschell27-Apr-14 9:36
jschell27-Apr-14 9:36 
GeneralRe: looking for a decent book on c# for advanced programmers Pin
Ravi Bhavnani28-Apr-14 4:44
professionalRavi Bhavnani28-Apr-14 4:44 
AnswerRe: looking for a decent book on c# for advanced programmers Pin
thatraja27-Apr-14 20:59
professionalthatraja27-Apr-14 20:59 
GeneralRe: looking for a decent book on c# for advanced programmers Pin
Dnyaneshwar@Pune28-Apr-14 0:11
Dnyaneshwar@Pune28-Apr-14 0:11 
GeneralRe: looking for a decent book on c# for advanced programmers Pin
rutja_deore28-Apr-14 0:12
rutja_deore28-Apr-14 0:12 
GeneralRe: looking for a decent book on c# for advanced programmers Pin
thatraja28-Apr-14 4:29
professionalthatraja28-Apr-14 4:29 
AnswerRe: looking for a decent book on c# for advanced programmers Pin
Ravi Bhavnani28-Apr-14 4:43
professionalRavi Bhavnani28-Apr-14 4:43 
QuestionRunning thread to refresh image disable scrollbars in DataGridView Pin
neualex26-Apr-14 5:44
neualex26-Apr-14 5:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.