473,672 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object reference error on EnumChildWindow s

using System;
using System.Drawing;
using System.Componen tModel;
using System.Windows. Forms;
using System.Runtime. InteropServices ;
using System.Text;
using System.Collecti ons;
using System.Diagnost ics;

public delegate bool IECallBack(int hwnd, int lParam);
public delegate bool IEChildCallBack (int hWndParent,int lpEnumFunc,int
lParam);
namespace RunningInstance OfIE
{
/// <summary>
/// Summary description for IEHelper.
/// </summary>
public class IEHelper
{
[DllImport("user 32.Dll")]
public static extern int EnumWindows(IEC allBack x, int y);
[DllImport("User 32.Dll")]
public static extern void GetWindowText(i nt h, StringBuilder s, int
nMaxCount);
[DllImport("User 32.Dll")]
public static extern void GetClassName(in t h, StringBuilder s, int
nMaxCount);
[DllImport("User 32.Dll")]
public static extern IntPtr PostMessage(Int Ptr hWnd, int msg, int
wParam, int lParam);
[DllImport("User 32.dll")]
public static extern Boolean EnumChildWindow s(int
hWndParent,IECh ildCallBack lpEnumFunc,int lParam);
[DllImport("User 32.dll",CharSet =CharSet.Auto)]
private static extern int SendMessage(Int Ptr 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(str ing findStr, string urlStr)
{
m_strToFind = findStr.ToLower ();
m_strURL = urlStr.ToLower( );
myAl = new ArrayList();
IEChildCallBack chldCallBack = new
IEChildCallBack (EnumChildWindo wCallBack);
EnumWindows (new IECallBack(Enum WindowCallBack) , 0) ;
if(chldCallBack != null)
{
for(int i = 0 ; i < myAl.Count ; i++)
{
EnumChildWindow s((int)myAl[i] , chldCallBack, 0);
return true;

}
}
return true;
}
private static bool EnumChildWindow CallBack(int hwnd, int
lpEnumFunc, int lparam)
{
windowHandle = new IntPtr(hwnd);
sb = new StringBuilder(1 024);
sbc = new StringBuilder(2 56);
GetClassName(hw nd,sbc,sbc.Capa city);
GetWindowText(( int)windowHandl e, sb, sb.Capacity);
int x;
String xMsg = sb+" "+sbc+" "+windowHan dle;
if(sbc.ToString ().IndexOf("Edi t") >= 0 )
{
x = SendMessage(win dowHandle, WM_SETTEXT, new IntPtr(0),
m_strURL);
x = SendMessage(win dowHandle, WM_KEYDOWN, new IntPtr(0x0D), "");
}
return true;
}
private static bool EnumWindowCallB ack(int hwnd, int lParam)
{
windowHandle = (IntPtr)hwnd;
sb = new StringBuilder(1 024);
sbc = new StringBuilder(2 56);
GetClassName(hw nd,sbc,sbc.Capa city);
GetWindowText(( int)windowHandl e, sb, sb.Capacity);
String xMsg = sb+" "+sbc+" "+windowHan dle;
if( sbc.Length 0 )
{
if( sbc.ToString(). Equals("IEFrame ") &&
sb.ToString().T oLower().IndexO f(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

EnumChildWindow s((int)myAl[i] , chldCallBack, 0);

Can any one help on it ?

Apr 26 '07 #1
1 4395
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]
- mv*@spam.guard. caspershouse.co m

<pr************ **@gmail.comwro te in message
news:11******** *************@r 35g2000prh.goog legroups.com...
using System;
using System.Drawing;
using System.Componen tModel;
using System.Windows. Forms;
using System.Runtime. InteropServices ;
using System.Text;
using System.Collecti ons;
using System.Diagnost ics;

public delegate bool IECallBack(int hwnd, int lParam);
public delegate bool IEChildCallBack (int hWndParent,int lpEnumFunc,int
lParam);
namespace RunningInstance OfIE
{
/// <summary>
/// Summary description for IEHelper.
/// </summary>
public class IEHelper
{
[DllImport("user 32.Dll")]
public static extern int EnumWindows(IEC allBack x, int y);
[DllImport("User 32.Dll")]
public static extern void GetWindowText(i nt h, StringBuilder s, int
nMaxCount);
[DllImport("User 32.Dll")]
public static extern void GetClassName(in t h, StringBuilder s, int
nMaxCount);
[DllImport("User 32.Dll")]
public static extern IntPtr PostMessage(Int Ptr hWnd, int msg, int
wParam, int lParam);
[DllImport("User 32.dll")]
public static extern Boolean EnumChildWindow s(int
hWndParent,IECh ildCallBack lpEnumFunc,int lParam);
[DllImport("User 32.dll",CharSet =CharSet.Auto)]
private static extern int SendMessage(Int Ptr 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(str ing findStr, string urlStr)
{
m_strToFind = findStr.ToLower ();
m_strURL = urlStr.ToLower( );
myAl = new ArrayList();
IEChildCallBack chldCallBack = new
IEChildCallBack (EnumChildWindo wCallBack);
EnumWindows (new IECallBack(Enum WindowCallBack) , 0) ;
if(chldCallBack != null)
{
for(int i = 0 ; i < myAl.Count ; i++)
{
EnumChildWindow s((int)myAl[i] , chldCallBack, 0);
return true;

}
}
return true;
}
private static bool EnumChildWindow CallBack(int hwnd, int
lpEnumFunc, int lparam)
{
windowHandle = new IntPtr(hwnd);
sb = new StringBuilder(1 024);
sbc = new StringBuilder(2 56);
GetClassName(hw nd,sbc,sbc.Capa city);
GetWindowText(( int)windowHandl e, sb, sb.Capacity);
int x;
String xMsg = sb+" "+sbc+" "+windowHan dle;
if(sbc.ToString ().IndexOf("Edi t") >= 0 )
{
x = SendMessage(win dowHandle, WM_SETTEXT, new IntPtr(0),
m_strURL);
x = SendMessage(win dowHandle, WM_KEYDOWN, new IntPtr(0x0D), "");
}
return true;
}
private static bool EnumWindowCallB ack(int hwnd, int lParam)
{
windowHandle = (IntPtr)hwnd;
sb = new StringBuilder(1 024);
sbc = new StringBuilder(2 56);
GetClassName(hw nd,sbc,sbc.Capa city);
GetWindowText(( int)windowHandl e, sb, sb.Capacity);
String xMsg = sb+" "+sbc+" "+windowHan dle;
if( sbc.Length 0 )
{
if( sbc.ToString(). Equals("IEFrame ") &&
sb.ToString().T oLower().IndexO f(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

EnumChildWindow s((int)myAl[i] , chldCallBack, 0);

Can any one help on it ?

Apr 27 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10541
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server Error in '/Cr_Dataset' Application. ----------------------------------------------------------- ---------------------
0
2717
by: kinryuu | last post by:
I'm trying to write a program that captures selected text from the active window. I have everything setup as far as getting the foreground window (with GetForegroundWindow()) and I'm using EnumChildWindows to iterate through the child windows until I get something useful. The problem is that every single child window of the foreground window returns a single space as the selected text, even if I know that a control on that window has...
1
9230
by: Ryan Ross | last post by:
Hello, I am having the oddest error with the EnumChildWindows function. Below are the imports, and the methods. public static extern int EnumChildWindows(IntPtr hWndParent, EnumChildProc callback, int lParam);
3
10119
by: John F | last post by:
I have a question about EnumChildWindows(). Currently I'm using this API function to iterate through all the controls in another application. In my call back function I call SendMessage() and pass WM_GETTEXT to return the text in each child control. Everything works perfectly. My question pertains to the Z-Order and expectations. Currently I take all the values I pull out of this 3rd party app and populate a string array. On my...
2
2648
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
1
2216
by: pigeonrandle | last post by:
Hi, This could also be read as 'Does the EnumChildWindows callback block the rest of my application until the enumeration has finished?'. Basically i want to create a treeview similar to spy++... and i don't want to start building the tree until all the windows have been enumerated (obviously). Cheers, James.
2
596
by: Jonathan Boivin | last post by:
Hi people, Let me introduce to how I get this error. I have a form which load all my bills representation depending upon filters which each bill is a usercontrol of my own having some textboxes containing bill's datas. (I already replaced the labels by painting the text instead.) When I click on my filter button, it clears the current usercontrol bills and load the new ones upon the current chosen filters. The bug happens when
2
2281
by: =?Utf-8?B?c3lzdGVtQ29uc3VsdGFudA==?= | last post by:
Can I use EnumchildWindows from a windows service to find the IE windows and Solitaire windows belonging to the logged on user? And if so then I'll need to know what call to use to get the users desktop window handle to enum them - Assuming GetDesktopWindow doesn't at this point. I can do all this in a Forms application (trying to convert to service) but as a service it simply doesn't think there are any of those windows open.
0
1198
by: Emilio | last post by:
Good morning to everyone, I've tested this "format" od this code to enumerate some controls in a window and it works fine. Private Delegate Function EnumChildProcDelegate _ (ByVal hWnd As IntPtr, _ ByVal lParam As Integer) As Boolean Private Declare Function EnumChildWindows Lib "user32" _
0
8486
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8931
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7446
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.