Microsoft .NET marks a huge advance in software development. However,organizations have invested heavily in their existing software infrastructure. Consequently, a lengthy transition period will be necessary in which the new .NET platform must interoperate with old systems.
There are major areas of incompatibility between the COM and .NET technologiesthat prevent a simple automated strategy from migrating much VB6 code intoVisual Basic .NET. This book investigates these incompatibilities and shows youhow to work around them. We start by covering COM interoperability in detail, andthen broaden the focus to examine a range of issues that must be handled inorder for VB6 and COM code to work with .NET.
Who's the book for?
This book is aimed at Visual Basic developers who need to make COM and VB6 code interoperate with the new .NET platform. This book assumes experience with VB6 and COM, as well as Visual Basic .NET and the .NET Framework.
What does this book cover?
Using COM components from .NET, and .NET assemblies from COM
Calling COM components in MTS/COM+ from .NET
Visual Basic .NET and COM in COM+
Custom marshaling
Threading issues
DCOM, Remoting, and Web Services
Sharing configuration information
Binary and random fiat file access in .NET
Using ADO in .NET
Calling APl or static DLL functions from .NET
Preparing your VB6 code for migration to .NET
Introduction
Who is this Book for?
What's Covered in this Book?
What Do I Need to Use this Book?
Conventions
Customer Support
How to Download the Sample Code for the Book
Errata
E~mail Support
p2p.wrox.com
Why this System Offers the Best Support
Chapter 1: The Importance of Interoperability
Going Forward with Visual Basic .NET
Two Strategies - Migration and Interoperability
The Migration Option
Major Areas of Incompatibility Between VB6 and Visual Basic .NET
Changes in Data Types
Changes Related to Object-Oriented Programming
Radically Different Means for Creating a Web Interface
Functionality Moved from Keywords to the Underlying Framework
Removal of Obsolete Syntax
Good Candidates for Migration to Visual Basic .NET
Bad Candidates for Migration
Impossible Migration
Implications for Interoperability
Design Situations that Need Interoperability
Web Services
Smart Clients
New ASP.NET Front Ends
Extending Old Components
Distributed Processing of Data
Major Areas of Interoperability
Calling COM Components from .NET
Calling .NET Components from COM
Sharing Data Access Between Old Code and New
Sharing Configuration Information
Calling Non-COM DLLs with Static Entry Points
Wrapping Up
Chapter 2: Using COM Components from .NET
COM Component Creation
Building a COM Component
Creating the Project
Project Properties
Class Properties
Sub, Function, and Property Methods
Events
Passing Data
Modules
Binary Compatibility
Importing COM Components
Importing During Design
Visual Studio .NET
Type Library Importer
Primary Interop Assemblies
Handling Dependencies
Importing Dynamically
CreateObject Function
.NET System Class Library Support
Using COM Components
Early Binding
Calling Simple Methods
Using Complex Data Types
Handling Events
Late Binding
Late Binding with a Wrapper Assembly
Late Binding with Dynamic Creation
Manual Invocation of Methods
Object Lifetimes
Multiple Interfaces
Design*Time References
Late Binding
Inheritance
Inspecting a COM Component from .NET
ildasm.exe
Object Browser
Class View
Reflection/Type Object
Summary
Chapter 3: Using .NET Assemblies from COM
.NET Assembly Creation
Building a .NET Assembly
Creating the Project
Controlling Object Creation
Adding Methods to the Class
Events
Passing Data
Structures
Enumerated Types
ByVal and ByRef
Setting up for Interop
Using the Attribute
Applying the Attribute
Defining the CLSID
Hiding Elements
Exposing the Class Manually
Understanding Interfaces and Classes
Registering .NET Assemblies with COM
Using Visual Studio .NET
Providing a Strong Name
Using Command-Line Utilities
Building the Assembly
Registering the Assembly Directly
Inserting the Assembly into the GAC
Generating the Type Library Only
Using .NET Objects from COM and Windows
Early Binding
Referencing the Assembly
Creating an Instance of the Class
Calling Methods and Properties
Handling Events
Late Binding
Creating an Instance of the Class
Specialized Types Unavailable
Events Unavailable
Dealing with Locked Files
Object Lifetimes
Implementing a Dispose Method
Using the Dispose Method
Multiple and Default Interfaces
implementing a Secondary Interface
Changing the Default Interface
Inspecting a .NET Assembly from COM
The Object Browser
Using the TypeLib Information Component
Summary
Chapter 4: Calling COM Components in MTS and COM+ from .NET
Using COM+ Components
Creating a COM+ Component
Creating the Project
Creating the Class
Taking Advantage of COM+ Transactions
Binary Compatibility
Putting the DLL into COM+
Calling COM+ Components from Visual Basic .NET
Creating the Project
Running the Project
Queued Components
Creating a Queued Component in VB6
Calling the Queued Component from Visual Basic .NET
Transferring Data
Using ADO Recordset Objects
Using Variant Arrays
Using XML Documents
Calling .NET Components in COM+ from VB6
Creating a COM+ Component in .NET
Setting Up the Project
Building a Class
Registering the Assembly with COM+
Making the Class Available to COM Clients
Adding
Viewing the Interface
Summary
Chapter ,5: Visual Basic .NET and COM in COM+
COM+ and VB6
Creating an ActiveX DLL for COM+
Registering the DLL in COM+
Creating a VB6 Test Application
COM+ and Visual Basic ,NET
Registering the DLL in COM+
Creating a Visual Basic .NET Test Application
Calling VB6 Components from Visual Basic .NET in COM+
Calling Visual Basic .NET Components from VB6 in COM+
Summary
Chapter 6: Custom Marshaling
The Four Different Methods of Marshaling
Type I Marshaling: Using Tlbimp.exe
Type II Marshaling (Simple Data Type Marshaling): Adding Attributes to the Interface to Use a Standard Type
Type III Marshaling: Marshaling into a Custom Data Structure to be Passed Between COM~nd .NET
Type IV Marshaling: Implementing the ICustomMarshaler Interface
Performance Issues
Type II Marshaling
Ildasm
Ilasm
Using Ildasm or llasm with Type II Marshaling
Create the VB6 COM Component
Use TIbimp to Create the Runtime-Callable Wrapper
Use Ildasm to Create the Intermediate Language Code
Modify the IL to use Booleans Instead of Integers
Create the New Runtime-Callable Wrapper
Make a New Managed Project to Use Our Modified RCW
Run the Example
Type III Marshaling
A Type III Example
Make the COM Object
Create the Runtime Callable Wrapper
Make a New Managed Project
Modify the Runtime-Callable Wrapper to Return a Usable Type
Finish the Managed Interface
Type IV Marshaling
How it Works
The ICustomMarshaler Interface
Create an Unmanaged COM Component
Create the Managed Person Class and the Custom Marshaler
Modify the Marshal Attributes on the Runtime-Callable Wrapper
Create an interface to Test the Marshaler
Managed Code to Unmanaged Code
Summary
Chapter 7: Threading Issues
Threading Basics
Threading Complexity
Using Single versus Multi-Threading
Threading in VB6
Threading in Visual Basic .NET
Threading Interop
Calling COM from .NET
Calling Multiple COM Components from .NET
Calling VB6 Components from a Multi-Threaded .NET Application
Calling ActiveX EXE Components from Visual Basic .NET
Calling VB6 Components from ASP.NET
Calling .NET from COM
Summary
Chapter 8: DCOM, Remoting, and Web Services
XML Web Services
.NET Remoting
DCOM
XML Web Services and Interop
Calling COM from .NET
Creating the Web Service in VB6
Creating the .NET Client
Calling ,NET from COM
Creating the XML Web Service
Creating the VB6 Client
.NET Remoting and Interop
Calling COM from .NET
Creating the Wrapper Project
Hosting the Wrapper Assembly for Remoting
Creating a Remoting Client
Calling .NET from COM
Creating the .NET Proxy
Creating the VB6 Client
Summary
Chapter 9: Sharing Configuration Information
Private INI Files
The Structure of an INI File
Accessing INI Files in VB6
Accessing INI Files in Visual Basic .NET
Using the Windows Registry to Store Configuration Settings
XML-Based Configuration Files
Accessing XML Configuration Info with the .NET Framework Classes
Accessing Settings Using System.XML in Visual Basic .NET
Accessing Settings Using MSXML in VB6
Concurrency Problems
Summary
Chapter 10: Flat File Access in .NET
Random Access to Flat Files
Random Access in VB6
Using FreeFile to Get the File Number
Random Access in Visual Basic .NET
Replacements for File Access Keywords
Structures Replace UDTs in Visual Basic .NET
Handling Fixed Length Strings in Visual Basic .NET
Putting It All Together
Binary Access to Flat Files
Binary Access in VB6
Binary Access in Visual Basic .NET
Sequential File Access
Sequential Access in VB6
Sequential Access in Visual Basic .NET
Streams as an Alternative in Visual Basic .NET
Sequential Access with Streaming Techniques
Binary Access with Streaming Techniques
Summary
Chapter 11: Using ADO in .NET
Comparison Between ADO and ADO.NET
ADO Advantages and Disadvantages
When is ADO Still Needed in .NET?
ADO.NET Only Has Optimistic Concurrency
ADO Suppor[s Pessimistic Concurrency
ADO.NET Does Not Support Most Cursors
ADO Supports a Variety of Cursors, Including Server-Side
Using ADO in .NET
ADO Code in Visual Basic .NET
Data Binding Limitations
Problems in Setting Some ADO Properties to Strings
Converting an ADO Recordset into an ADO.NET DataSet
DAO and RDO Implications
Summary
Chapter l ~- Calling APl or Static DLL Functions from .NET
Reduced Need for Using DLLs with Static Entry Points in .NET
Reduced Need for Access to Windows APl
Retirement of Older Code
Platform Invocation Services
Declaring the APl
Calling the APl Function
Aliasing a Function
Passing Structures as Parameters
Example of an APl with a Structure Parameter
Data Marshaling Issues
Dealing with Strings
Using Automatic ANSI/Unicode Location
Getting More Control
Control Over Data Marshaling
Using DLLImport Instead of Declare
Performance Considerations
Summary
Chapter 13: Interoperability of ActiveX Controls in ,NET
When to Use ActiveX Controls in .NET
The Differences Between Windows Forms Controls and ActiveX Controls
Comparison of Properties
Hosting an ActiveX Control in .NET
Example - Windows Media Player Control
Notes on the Example
Custom Property Moved from Properties List
Creating a Wrapper with Aximp.exe
Importing VB6 UserControls
Controls that Cannot be Imported
Security
.NET Windows Forms Controls in VB6
Summary
Chapter 14' How Do I Prepare VB6 Code for Migration?
Stop Using Default Properties and Methods
Avoid Non-Zero Lower Bounds for Arrays
Make All Parameters Explicitly ByRef or ByVal
Place Default Values on All Optional Parameters
Declare All Variables on a Separate Line
Be Careful Where You Declare Variables
Avoid Fixed Length Strings in UDTs
Get Rid of Obsolete Keywords
Remove Implicit Object Instantiation
Stop Implicit Loading of Forms
Switch Data Binding to ADO
Use Intrinsic Constants Whenever Possible
Stop Writing DHTML Pages and WebClasses
Wrap Your APl Calls
Get Your Logic Out of the UI and Into Components and Classes
Avoid Late Binding
Summary
Who is this Book for?
This book is written for programmers who are experienced in VB6, and who need to understand how VB6 and Visual Basic .NET can interoperate.
It is expected that the reader has already become reasonably familiar with the overall structure of .NET and the syntactical differences between VB6 and Visual Basic .NET. The book is not a tutorial in the changes between VB6 and Visual Basic .NET, nor is it an introduction to .NET technologies. If you are interested in learning about .NET in general, or Visual Basic .NET in particular, you may wish to consult the following books from Wrox Press'
Proyfesional .NET Framework, ISBN 1-861005-56-3
Profesional VB. NET, ISBN 1-861004-97-4
What's Covered in this Book?
Chapter 1, The Importance oflnteroperability, looks at why interoperability between VB6 and COM and the new .NET platform will be necessary.
Chapter 2, Using COM Components from .NET, shows how to import a COM component into the .NET platform, making it available for use from .NET even though it remains a COM component running in COM. It also covers how to design COM components and interfaces to make them easier to deal with from a .NET perspective.
Chapter 3, Using .NET Assemblies from COM, looks at the reverse scenario - the issues involved in creating a .NET assembly for use by COM applications, and how to use it effectively.
Chapter 4, Calling COM Components in MTS and COM+from .NET, focuses on the use of COM+ or MTS- based COM components by Visual Basic .NET client applications.
Chapter 5, Visual Basic .NET and COM in COM+, shows how a Visual Basic .NET assembly running in COM+ interacts with VB6 components also running in COM+, and visa versa.
Chapter 6, Custom Marshaling, gives an overview of the different mechanisms for using marshaling - moving information bi-directionally from unmanaged code to managed code.
Chapter 7, Threading Issues, looks at the threading issues that are likely to arise when interoperating between Visual Basic .NET and VB6. We show how threading works in VB6 with COM, and then in Visual Basic .NET on the .NET platform.
Chapter 8, DCOM, Remoting, and Web Services, covers using DCOM via COM interop, and the alternatives to DCOM provided by the .NET platform - XML Web Services and .NET Remoting.
Chapter 9, Sharing Configuration Information, looks at using private INI files, the Windows registry, and XML-based configuration files to share configuration information in both VB6 and Visual Basic .NET.
Chapter 10, Flat File Access in .NET, shows how binary, random, and sequential access to flat files has changed from VB6 to Visual Basic .NET.
Chapter 11, UsingADO in .NET, compares ADO and ADO.NET and discusses situations where ADO should still be used, even for new projects in Visual Basic .NET. We also examine some of the differences between using ADO in .NET as opposed to VB6.
Chapter 12, Calling AP1 or Static DLL Functions from .NET, looks at interoperability with DLLs that do not have COM interfaces, and which are generally used to allow VB programs to work with the Windows API.
Chapter 13, Interoperability of ActiveX Controls in .NET, looks at one kind of COM component that requires special treatment - ActiveX controls.
Chapter 14, How Do IPrepare VB6 Code for Migration?, provides some general guidelines that, if implemented while maintaining and enhancing existing VB6 code, will ease the eventual migration of the code to Visual Basic .NET.
What Do I Need to Use this Book?
All you'll need is a PC running:
Windows 2000 or XP
Visual Studio .NET Professional edition (or higher versions)
Visual Studio 6.0 with Visual Basic 6.0
Conventions
We have used a number of different styles of text and layout in the book to help differentiate between the different kinds of information. Here are examples of the styles that we use and an explanation of
what they mean.
Bullets appear indented, with each new bullet marked as follows:
Important words are in a bold type font
Words that appear on the screen in menus like the File or Window are in a similar font to the
one that you see on screen
Keys that you press on the keyboard, like Ctrl and Enter, are in italics
If you see something like Oloj eot, you'll know that it's a filename, object name, or function name
Code in a gray box is new, important, pertinent code:
Dim objMyClass as New MyClass("Hello World"
Debug .WriteLine (objMyClass. ToString)
Sometimes you'll see code in a mixture of styles, like this:
Dim objVar as Object
objVar = Me
CType(objVar, Form) .Text = "New Dialog Title Text"
The code on a white background is code that we've already looked at and that we don't wish to examine further.
Advice, hints, and background information come in an italicized, indented JCnt like this.
Important pieces of information come in boxes like this.
Customer Support
We always value hearing from our readers, and we want to know what you think about this book: what you liked, what you didn't like, and what you think we can do better next time. You can send us your comments, either by returning the reply card in the back of the book, or by e-mail to feedback@ wrox.com. Please be sure to mention the book title in your message.
How to Download the Sample Code for the Book
When you visit the Wrox site, http://www.wrox.com/, simply locate the title through our Search facility or by using one of the title lists. Click on Download in the Code column, or on Download Code on the book's detail page.
The files that are available for download from our site have been archived using WinZip. When you, have saved the attachments to a folder on your hard drive, you need to extract the files using a de- compression program such as WinZip or PKUnzip. When you extract the files, the code is usually extracted into chapter folders. When you start the extraction process, ensure that your software (WinZip, PKUnzip, etc.) is set to use folder names.
Errata
We've made every effort to make sure that there are no errors in the text or in the code. However, no one is perfect and mistakes do occur. If you find an error in one of our books, like a spelling mistake or a faulty piece of code, we would be very grateful for feedback. By sending in errata, you may save another reader hours of frustration and, of course, you will be helping us to provide even higher quality information. Simply e-mail the information to support@wrox.com. Your information will be checked and, if correct, posted on the errata page for that title or used in subsequent editions of the book.
To find errata on the web site, go to http://www.wrox.com/and simply locate the title through our Advanced Search or title list. Click on the Book Errata link, which is below the cover graphic on the book's detail page.
E-mail Support
If you wish to directly query a problem in the book with an expert who knows the book in detail, then e-mail support @ wrox.com with the following information:
The title of the book, last four digits of the ISBN, and page number of the problem in the Subject field
Your name, contact information, and the problem in the body of the message
We won't send you junk mail. We need the details to save your time and ours. When you send an e-mail message, it will go through the following chain of support:
Customer Support - Your message is delivered to our customer support staff who are the first people to read it. They have files on the most frequently asked questions and will answer anything general about the book or the web site immediately.
Editorial - Deeper queries are forwarded to the technical editor responsible for that book.
They have experience with the programming language or particular product, and are able to answer detailed technical questions on the subject.
The Authors - Finally, in the unlikely event that the editor cannot answer your problem, they will forward the request to the author. We do try to protect the authors from any distractions to their writing; however, we are quite happy to forward specific requests to them. All Wrox authors help with the support on their books. They will e-mail the customer and the editor with their response and, again, all readers should benefit.
The Wrox Support process can only offer support to issues that are directly pertinent to the content of our published title. Support for questions that fall outside the scope of normal book support is provided via the community lists of our http://p2p.wrox.com/forum.
p2p.wrox.com
For author and peer discussion, join the P2P mailing lists. Our unique system provides programmer to programmerTM contact on mailing lists, forums, and- newsgroups, all in addition to our one-to-one e-mail support system. If you post a query to P2P, you can be confident that the many Wrox authors and other industry experts who are present on our mailing lists are examining it. At p2p.wrox.com, you will find a number of different lists that will help you, not only while you read this book, but also as you develop your own applications. Particularly appropriate to this book are the pro vb, pro vb dotnet, and vb_dotnet lists.
To subscribe to a mailing list, just follow these steps:
1. Go to http://p2p.wrox.com/
2. Choose the appropriate category from the left menu bar
3. Click on the mailing list you wish to join
4. Follow the instructions to subscribe and fill in your e-mail address and password
5. Reply to the confirmation e-mail you receive
6. Use the subscription manager to join more lists and set your e-mail preferences
Why this System Offers the Best Support
You can choose to join the mailing lists or you can receive them as a weekly digest. If you don't have the time, or facility, to receive the mailing list, then you can search our online archives. Junk and spam mails are deleted, and your own e-mail address is protected by the unique Lyris system. Queries about joining or leaving lists, and any other general queries about lists, should be sent to listsuppo rt @ p2p.wrox.com.