to find instances of form running in c# using FindWindowEx

  • Thread starter Thread starter enahar
  • Start date Start date
E

enahar

Hello,

i want to find out how many instances of a form (say childForm1) is
running using FindWindowEx but value return is always zero by this method.

What wrong I am doing it..Can anybody help me to find the no. of form
instances running ?

Thanks.

Below is the code



I have already declared

[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
string lpszClass, string lpszWindow);



string lpszClass = childForm.Name;

IntPtr ParenthWnd = new IntPtr(0);

ParenthWnd = FindWindowEx(ParenthWnd,hWnd,lpszClass,"");

if (ParenthWnd .Equals(IntPtr.Zero))

Console.WriteLine("childForm.Name is not running?");

else

Console.WriteLine("childForm.Name is running?");

return;
 
FindWindowEx requires you to specify the Windows Class Name (see
RegisterWindowEx in the Platform SDK). You have used Form.Name as the class
name but this is not correct. To get the class name of your form you can use
the Win32 API function GetClassName as in the following example:

[DllImport("user32.dll")]
static extern int GetClassName(IntPtr hWnd, [Out] StringBuilder lpClassName,
int nMaxCount);

StringBuilder className = new StringBuilder(256);
GetClassName(this.Handle, className, className.Capacity);
IntPtr h = FindWindowEx(IntPtr.Zero, IntPtr.Zero, className.ToString(), null);

HTH, Jakob.
 
Back
Top