Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Sink Unmanaged Events from .NET?

This example demonstrates how to sink unmanaged events from .NET code.

In order to use the types defined within a COM library from managed code, you have to obtain an assembly containing definitions of the COM types. Refer to the How Do I...Build a .NET Client That Uses a COM Server? for specific details.

With Visual Basic or with C#, you can reference the assembly using compiler /r switch or you can add reference to your project directly from the Visual Studio development tool.

Once you have a reference, you can create new instances of the event handlers and add them to existing event handlers in unmanaged code. For specific syntax see examples below.


		
public static void Main(){
	SHDocVw.InternetExplorer explorer;
	IWebBrowserApp webBrowser;			
	
	explorer = new SHDocVw.InternetExplorer();
	webBrowser = (IWebBrowserApp) explorer;
	
	//Title Change event
	DWebBrowserEvents2_TitleChangeEventHandler dTitleChangeE
		= new DWebBrowserEvents2_TitleChangeEventHandler(OnTitleChange);
	explorer.TitleChange += dTitleChangeE;	
				
	webBrowser.Visible = true;
	webBrowser.GoHome();	
	...		
}

static void OnTitleChange(String text){
	Console.WriteLine("Title changes to {0}", text);
}
C#

The following example uses the Internet Explorer object methods and events to open an Internet Explorer window, catch all TitleChange events and show them in console window. To do this, an assembly containing definitions of the Internet Explorer types and events is created from SHDocVw.dll and saved into ExplorerLib.dll, which then can be referenced from the code.

TestClient.exe
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Interop\TestClient_2\]



Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.