documentcomplete event  
Author Message
Mystret





PostPosted: Visual C# General, documentcomplete event Top

I've got a really weird problem with my application; it works on some pc's, and it doesn't on some others. What I've got:

A Visual C# 2005 console application with references to MSHTML and SHDOCVW. I use mshtml to get a list of currently open browser windows, and attach events to it to edit the list when a new browser window is created or closed. I attach a documentComplete event to each open browser window and handle it the event there (let's say it prints out the url where is navigated to). So when I run this program, it should detect when I open a new browser window, when I close one, and it should print out the url of a site when it is done loading it.

And there's my problem. On a few pc's, it all works perfectly. But on one other I tried it on, the documentcomplete event refuses to fire. I've tried many things, but I just can't get it to fire. On google I read about bugs that the visible property has to be set to true in order for the event to fire, but I've checked that; and it is set to true! Apart from that, why would it work on a few pc's, and not work on some others

On two pc's I tried it on it worked: one Vista and one XP SP2 pc, one running IE7 en one running IE6.

The pc it didn't work on runs Windows 2000 and IE6.

Does anyone know how to fix this, or does anyone know a workaround to this problem

Thanks in advance,

Floris

public class GetIE

{

private DShellWindowsEvents_WindowRegisteredEventHandler registerHandler;

private DShellWindowsEvents_WindowRevokedEventHandler revokeHandler;

private DWebBrowserEvents2_DocumentCompleteEventHandler docCompleteHandler;

private ShellWindows shellWindows;

private ArrayList windowsList;

public GetIE()

{

registerHandler = new DShellWindowsEvents_WindowRegisteredEventHandler(shellWindows_WindowRegistered);

revokeHandler = new DShellWindowsEvents_WindowRevokedEventHandler(shellWindows_WindowRevoked);

docCompleteHandler = new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);

windowsList = new ArrayList();

}

public void Run()

{

shellWindows = new ShellWindowsClass();

shellWindows.WindowRegistered += registerHandler;

shellWindows.WindowRevoked += revokeHandler;

copyWindowsList();

}

private void copyWindowsList()

{

ArrayList c_windowsList = (ArrayList)windowsList.Clone();

windowsList.Clear();

foreach (WebBrowser ie in shellWindows) // A SHDOCVW WebBrowser. NOT a System.Windows.Forms one!

{

windowsList.Add(ie);

if (!c_windowsList.Contains(ie))

{

ie.DocumentComplete += docCompleteHandler;

}

}

}

private void shellWindows_WindowRegistered(int lCookie)

{

Console.WriteLine("\nNew window registered. lCookie = " + lCookie.ToString());

copyWindowsList();

printWindowsList();

}

private void shellWindows_WindowRevoked(int lCookie)

{

Console.WriteLine("\nWindow revoked. lCookie = " + lCookie.ToString());

copyWindowsList();

printWindowsList();

}

private void ie_DocumentComplete(object pDisp, ref object URL)

{

string mode;

Console.WriteLine("DOCUMENTCOMPLETE EVENT: " + URL.ToString());

}

}



Visual C#20  
 
 
bri_n1





PostPosted: Visual C# General, documentcomplete event Top

I've the exact same problem. I've tried it on 3 machines running xp sp2, and documentComplete() does not fire on one of them. That machine doesn't have dev software installed... I'm not sure if that is the problem. Please post if you find a solution.


 
 
Mystret





PostPosted: Visual C# General, documentcomplete event Top

Yeah, that could also be the problem. The machine on which it didn't work also doesn't have Visual Studio installed, and the machines on which it did work have Visual Studio installed. But I have no idea why it wouldn't work without dev software..
 
 
bri_n1





PostPosted: Visual C# General, documentcomplete event Top

i also read something somewhere about the .net framework needing to be installed. I assume that .net framework 2.0 was installed on all machines, but i'm not really sure... and I don't know why any of these should cause this problem.
 
 
Mystret





PostPosted: Visual C# General, documentcomplete event Top

All programs written in C# 2005 need the .NET Framework 2.0 to be installed in order to run, otherwise the program will immediately crash at the beginning. So if any of those pc's didn't have the .NET Framework installed, you'd have noticed it.
 
 
bri_n1





PostPosted: Visual C# General, documentcomplete event Top

I wrote using 2003 (c#), and I used the webbrowser activex object through COM interop. We are still switching to 2005 ;)
 
 
Mystret





PostPosted: Visual C# General, documentcomplete event Top

Yeah okay, basically the same. But instead of .NET Framework 2.0 you need .NET Framework 1.0 then :P
 
 
bri_n1





PostPosted: Visual C# General, documentcomplete event Top

I uninstalled visual studio 2003 and 2005 from a workstation, and the problem appears. I'm guessing that having any visual studio installed is required for the event to fire.

Unfortunately, I don't have any further explanation, nor do I have a decent solution for this problem :(


 
 
Mystret





PostPosted: Visual C# General, documentcomplete event Top

This is really weird.. I also tried to run in on a few more pc's without VS and then on a few more with VS and indeed, on the pc's with VS it all works fine, and without VS everything works except the DocumentComplete event... Exactly the same as you experienced.

I really have no idea why and also have no idea how to fix it.. It just doesn't make sense that VS needs to be installed in order for one specific COM event to fire, where other events from the same COM DLL do fire, also without VS! The DLL ships with my program, it's exactly the same for every pc it runs on!

I'm very confused right now.. It would be really nice if someone from Microsoft would jump in now and tell us a solution. ;)


 
 
Mystret





PostPosted: Visual C# General, documentcomplete event Top

Made a post about it on Microsoft Connect. I hope someone can answer me there. In the meantime, I'd still very much appreciate it if someone here would help us out. I still haven't found a solution or workaround.
 
 
bri_n1





PostPosted: Visual C# General, documentcomplete event Top

I found the problem. Apparently, Visual Studio will install a Primary Interop Assembly (PIO) under Program Files/Microsoft.Net folder, called Microsoft.mshtml.dll. Unfortunately, it's not installed with windows / .net. This needs to be distributed with the executable or installed on the client machines.

It won't throw an exception from documentcomplete(), but it will throw one if you reference the corresponding htmldocument after documentcomplete is called.


 
 
Mystret





PostPosted: Visual C# General, documentcomplete event Top

You're right. It works when I copy the mshtml.dll locally.. But DocumentComplete is a SHDocVW event, not MSHTML. :/ And the MSHTML functions were working properly; enumerating all open windows, event whenever a window is created or revoked..

I don't understand why it works when I distribute mshtml with my programs, but at least it works.. That's the most important right now. ;)

Thanks!

Floris