Ex No 1
Ex No 1
Ex No 1
from the classes exposed by the COM component. Visual Basic .NET code then manipulates the COM object's properties, methods, and events. COM components run in their own process thread, outside of the .NET application's process space. This means that Windows independently manages the COM component's resource requirements, and passes messages between the component and its consumer. Although the COM out-of-process integration architecture is bound to be a somewhat slower than .NET's inprocess design, most users will never notice the difference. Follow these steps to create a .NET Windows application that incorporates Microsoft Word as a COM-based ActiveX component (Microsoft Word must be installed on your computer in order for this example to work):
1. Open Visual Studio .NET. On the File menu, click New, and then click Project. 2. In the New Project dialog box, under Project Types, select Visual Basic Projects.
Under Templates, select Windows application. Name the new Windows application project COMComponentIntegration and click OK.
3. In Solution Explorer, right-click References, and on the context menu, click Add
Reference.
4. In the Add Reference dialog box, click the COM tab and locate Microsoft Word in the
list of components. (The exact version of Word is relatively unimportant for this demonstration.)
5. Highlight Microsoft Word, click Select to add it to the Selected Components list, and
click OK.
6. .NET responds by asking you whether you would like a wrapper (described below)
generated for the Word object library. Click Yes to indicate that you want .NET to build the wrapper for you.
information in the type library to provide an interface between a .NET application and a COM component. A primary interop assembly is a DLL prepared by the COM component's vendor and is intended to be used when the component is integrated with a .NET application. In the absence of an interop assembly, .NET will prepare a DLL that includes references to each of the classes exposed by the component's type library. The DLL is created by importing the class interfaces that .NET finds in the COM component's type library. The COM type library wrapper is added to the Visual Basic .NET project as a new reference. In Solution Explorer, right-click the wrapper DLL and select Properties from the context menu to see a number of interesting property values. Among these properties is the path to the wrapper DLL. You'll see that .NET has placed the DLL in the same obj directory containing the other component libraries included in the consumer application's assembly. You may see other wrapper DLLs in the obj folder in addition to the component's wrapper DLL. These other DLLs provide interfaces to secondary type libraries used by the COM component. In the case of Microsoft Word, you'll see both a Microsoft Office and a VBIDE interop DLL in addition to the DLL created for Word.
Dim objWord As Word.Application Dim strPath As String objWord = New Word.Application() With objWord .Visible = True .Documents.Add( _ Template:="Normal.dot", _ NewTemplate:=False) .Selection.TypeText( _ Text:="this is a test") .ActiveDocument.SaveAs( _ FileName:="Test.doc") .Documents.Close( _ SaveChanges:=Word.WdSaveOptions.wdDoNotSaveChanges) .Quit() End With objWord = Nothing End Sub Notice the value supplied as the SaveChanges argument to the Close method near the bottom of this code example. The value refers to an intrinsic constant (wdDoNotSaveChanges) that has been stored in the Word interop DLL. The Word interop DLL contains a large number of properties, methods, events, and constant values that are accessible to your .NET projects.
responsibility to discard the memory occupied by the object when the last reference to the object goes out of scope. This is not true of COM objects. Once a COM object has been instantiated, Windows normally leaves the object in memory until it is explicitly dismissed by setting its references (objWord, in the case of the listing above) to Nothing. When working with a large COM component such as Word, failure to explicitly dispose of the object at the conclusion of the application can result in enormous memory leaks. For the most part, the DLL wrapper handles other differences between COM objects and .NET components. For instance, one of these differences occurs when Windows moves a component (.NET or COM) in memory. This often happens as Windows tries to optimize the memory used by applications. A .NET application has no trouble keeping track of the location of its own components because the component and its data are being managed by the common language runtime. However, because a COM object contains unmanaged code, there is no way for the .NET application to keep track of the COM object's reference if the object is moved. The COM object's runtime wrapper prepared by .NET takes care of keeping memory references updated whenever the COM object is moved. From the consumer application's perspective, the address has not changed, even though the wrapper automatically translates the static memory reference to the updated location.
tree and registry keys. Generally speaking, if the COM server operates properly on the user's computer, it should work within the context of a .NET application.
.NET projects provide many features that make organizing the project's files easier. Instead of one class per module (a .CLS file),Visual Basic .NET code modules may contain multiple classes while in Visual Basic 6.0 each class occupied a separate .CLS file. The .NET approach allows you to logically group classes within a single code module to make it easier to recognize when classes are related (such as data access, financial, disk and file management, etc.)
.NET components are not registered on the user's computer; Visual Basic ActiveX components must be installed and registered on each user's computer. Each .NET component carries all of the interface information necessary for consumer applications to use the component. A component's interface (classes, properties, methods, and events) are documented in the .NET Object Browser, and through IntelliSense.
.NET error handling is far superior to Visual Basic 6.0's "On Error GoTo" model. It's easy to recognize a .NET error-handling construct, and .NET components can pass back the entire Exception object if necessary.
Class constructors accept parameters. This allows you to specify start-up information at the moment an object is instantiated, rather than waiting for instantiation to be complete, and then setting initial property values.
.NET classes can be logically organized within Namespaces. A Namespace can include classes with the same names as classes in another Namespace. A single DLL may include multiple Namespaces. Visual Basic 6.0 does not support Namespaces.