[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Object reference error in EnumChildWindows

prithvis.mohanty

4/26/2007 1:08:00 PM

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[i] , 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;
}


}
}

The purpose of this code is to enumurate through all open IE windows.
If the matching strign is found then it Navigates the new url there.

Its able to find the window and navigate but after that the below line
is throwing an error "Object reference not set to an instance of an
object"

EnumChildWindows((int)myAl[i] , chldCallBack, 0);