24.3 Automatically Generating an Interop Assembly

 <  Day Day Up  >  

You want your .NET application to automatically pick up the changes of a referenced COM object.


Technique

Before we explain the steps to perform this task, note that this method does not apply to ASP.NET-based projects. Instead, you have to manually update your ASP.NET project any time a referenced COM library changes.

To automatically generate an interop assembly, you have to create a custom-build event that calls an interop-assembly “generating tool supplied by Visual Studio .NET. Click on Project, Properties from the main menu and select the Common Properties, Build Events property page. Because you need to generate the assembly before you create your application's assembly, you have to enter a value for a prebuild event. In the Pre-Build Event Command Line, call the tlbimp.exe program, passing the path of the COM library as the first argument, followed by the name you want for the resulting interop assembly prefaced by the argument switch /out: . The following example uses the full path to the tlbimp.exe file (as it appears on our computer), followed by Visual Studio macros for the two filenames. The $(SolutionDir) is the directory where the main solution is located, which contains both the ATL project and .NET client. The $(ConfigurationName) expands to the values Debug or Release , depending on the build configuration used when building. Finally, the $(TargetDir) is the location where the final .NET assembly for the .NET client is placed:

 
 "D:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\tlbimp.exe " "$(SolutionDir)_CmdShell$(ConfigurationName)_CmdShell.dll"     /out:"$(TargetDir)\Interop.CmdShell.dll" 

Comments

Once you add a reference to a COM library for a .NET project, the interop assembly is generated, but Visual Studio .NET does not continue looking for changes in the main COM library during subsequent builds. If your COM library doesn't change or it only occasionally changes, this setup might be okay. However, if you are developing a COM object concurrently with a .NET client, you might want to consider making the interop assembly generation automatic.

The tlbimp.exe takes as input the resultant binary for a COM project and generates an interop assembly based on the contents of the type library. The tlbimp.exe tool is actually an application built with the .NET Framework. The TypeLibConverter class defined in the System.Runtime.Interop namespace contains methods to convert a type library to an assembly ( ConvertTypeLibToAssembly ) as well as converting an assembly to a type library ( ConvertAsmToTypeLib ), which is the process used in Recipe 24.7.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net