Object reference error on EnumChildWindows


P

prithvis.mohanty

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Diagnostics;

public delegate bool IECallBack(int hwnd, int lParam);
public delegate bool IEChildCallBack(int hWndParent,int lpEnumFunc,int
lParam);


namespace RunningInstanceOfIE
{
/// <summary>
/// Summary description for IEHelper.
/// </summary>
public class IEHelper
{
[DllImport("user32.Dll")]
public static extern int EnumWindows(IECallBack x, int y);
[DllImport("User32.Dll")]
public static extern void GetWindowText(int h, StringBuilder s, int
nMaxCount);
[DllImport("User32.Dll")]
public static extern void GetClassName(int h, StringBuilder s, int
nMaxCount);
[DllImport("User32.Dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int
wParam, int lParam);
[DllImport("User32.dll")]
public static extern Boolean EnumChildWindows(int
hWndParent,IEChildCallBack lpEnumFunc,int lParam);
[DllImport("User32.dll",CharSet=CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr
wParam, string lParam);


static IntPtr windowHandle;
static StringBuilder sb, sbc;
static int i =0;
static ArrayList myAl;
private const int WM_SETTEXT = 0x000C;
private const int WM_KEYDOWN = 0x100;
public static string m_strToFind = "";
public static string m_strURL = "";
public IEHelper()
{
//
// TODO: Add constructor logic here
//
}
public static bool NavigateURL(string findStr, string urlStr)
{
m_strToFind = findStr.ToLower();
m_strURL = urlStr.ToLower();
myAl = new ArrayList();
IEChildCallBack chldCallBack = new
IEChildCallBack(EnumChildWindowCallBack);
EnumWindows (new IECallBack(EnumWindowCallBack), 0) ;
if(chldCallBack != null)
{
for(int i = 0 ; i < myAl.Count ; i++)
{
EnumChildWindows((int)myAl , chldCallBack, 0);
return true;

}
}
return true;
}
private static bool EnumChildWindowCallBack(int hwnd, int
lpEnumFunc, int lparam)
{
windowHandle = new IntPtr(hwnd);
sb = new StringBuilder(1024);
sbc = new StringBuilder(256);
GetClassName(hwnd,sbc,sbc.Capacity);
GetWindowText((int)windowHandle, sb, sb.Capacity);
int x;
String xMsg = sb+" "+sbc+" "+windowHandle;
if(sbc.ToString().IndexOf("Edit") >= 0 )
{
x = SendMessage(windowHandle, WM_SETTEXT, new IntPtr(0),
m_strURL);
x = SendMessage(windowHandle, WM_KEYDOWN, new IntPtr(0x0D), "");
}
return true;
}
private static bool EnumWindowCallBack(int hwnd, int lParam)
{
windowHandle = (IntPtr)hwnd;
sb = new StringBuilder(1024);
sbc = new StringBuilder(256);
GetClassName(hwnd,sbc,sbc.Capacity);
GetWindowText((int)windowHandle, sb, sb.Capacity);
String xMsg = sb+" "+sbc+" "+windowHandle;
if( sbc.Length > 0 )
{
if( sbc.ToString().Equals("IEFrame") &&
sb.ToString().ToLower().IndexOf(m_strToFind) >= 0 )
{
myAl.Add(hwnd);
i++;
}
}
return true;
}


}
}

I am trying to enumurate through all the open IE winodows to find out
a window with a specific title. once found brows a new url into it.
It works fine for one instance but after navigating I am getting an
object reference error on line

EnumChildWindows((int)myAl , chldCallBack, 0);

Can any one help on it ?
 
Ad

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

Why not use automation to get all running instances of IE? The
following KB article shows how to connect to all running instances:

http://support.microsoft.com/kb/176792

It's in VB6, but you can easily adapt this code for .NET.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections;
using System.Diagnostics;

public delegate bool IECallBack(int hwnd, int lParam);
public delegate bool IEChildCallBack(int hWndParent,int lpEnumFunc,int
lParam);


namespace RunningInstanceOfIE
{
/// <summary>
/// Summary description for IEHelper.
/// </summary>
public class IEHelper
{
[DllImport("user32.Dll")]
public static extern int EnumWindows(IECallBack x, int y);
[DllImport("User32.Dll")]
public static extern void GetWindowText(int h, StringBuilder s, int
nMaxCount);
[DllImport("User32.Dll")]
public static extern void GetClassName(int h, StringBuilder s, int
nMaxCount);
[DllImport("User32.Dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int
wParam, int lParam);
[DllImport("User32.dll")]
public static extern Boolean EnumChildWindows(int
hWndParent,IEChildCallBack lpEnumFunc,int lParam);
[DllImport("User32.dll",CharSet=CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr
wParam, string lParam);


static IntPtr windowHandle;
static StringBuilder sb, sbc;
static int i =0;
static ArrayList myAl;
private const int WM_SETTEXT = 0x000C;
private const int WM_KEYDOWN = 0x100;
public static string m_strToFind = "";
public static string m_strURL = "";
public IEHelper()
{
//
// TODO: Add constructor logic here
//
}
public static bool NavigateURL(string findStr, string urlStr)
{
m_strToFind = findStr.ToLower();
m_strURL = urlStr.ToLower();
myAl = new ArrayList();
IEChildCallBack chldCallBack = new
IEChildCallBack(EnumChildWindowCallBack);
EnumWindows (new IECallBack(EnumWindowCallBack), 0) ;
if(chldCallBack != null)
{
for(int i = 0 ; i < myAl.Count ; i++)
{
EnumChildWindows((int)myAl , chldCallBack, 0);
return true;

}
}
return true;
}
private static bool EnumChildWindowCallBack(int hwnd, int
lpEnumFunc, int lparam)
{
windowHandle = new IntPtr(hwnd);
sb = new StringBuilder(1024);
sbc = new StringBuilder(256);
GetClassName(hwnd,sbc,sbc.Capacity);
GetWindowText((int)windowHandle, sb, sb.Capacity);
int x;
String xMsg = sb+" "+sbc+" "+windowHandle;
if(sbc.ToString().IndexOf("Edit") >= 0 )
{
x = SendMessage(windowHandle, WM_SETTEXT, new IntPtr(0),
m_strURL);
x = SendMessage(windowHandle, WM_KEYDOWN, new IntPtr(0x0D), "");
}
return true;
}
private static bool EnumWindowCallBack(int hwnd, int lParam)
{
windowHandle = (IntPtr)hwnd;
sb = new StringBuilder(1024);
sbc = new StringBuilder(256);
GetClassName(hwnd,sbc,sbc.Capacity);
GetWindowText((int)windowHandle, sb, sb.Capacity);
String xMsg = sb+" "+sbc+" "+windowHandle;
if( sbc.Length > 0 )
{
if( sbc.ToString().Equals("IEFrame") &&
sb.ToString().ToLower().IndexOf(m_strToFind) >= 0 )
{
myAl.Add(hwnd);
i++;
}
}
return true;
}


}
}

I am trying to enumurate through all the open IE winodows to find out
a window with a specific title. once found brows a new url into it.
It works fine for one instance but after navigating I am getting an
object reference error on line

EnumChildWindows((int)myAl , chldCallBack, 0);

Can any one help on it ?
 

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