HighTechTalks DotNet Forums  

Object reference error in EnumChildWindows

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss Object reference error in EnumChildWindows in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
prithvis.mohanty@gmail.com
 
Posts: n/a

Default Object reference error in EnumChildWindows - 04-26-2007 , 09:08 AM






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);


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.