What the...?

Something bad happened

We're not sure what, but we have a few guesses.

Problem: Object reference not set to an instance of an object.
Ticket: 7864836
Server: Web04
Using the .NET Library and XML in VB6 - CodeProject Click here to Skip to main content
11,715,043 members (69,961 online)
Click here to Skip to main content

Using the .NET Library and XML in VB6

, 11 Mar 2008 CPOL 34.3K 1.2K 25
Rate this:
Please Sign up or sign in to vote.
How to use the .NET Library and XML in VB6.

Introduction

INI files and the registry are generally things of the past for .NET applications. But what to use? XML seems appropriate, but one look at System.XML is enough to scare most developers off, especially just to store a few fields. Fortunately, there is a very easy way in .NET to solve this, but one that is usually not seen by developers. In this article, I’ll explain how you can easily store and retrieve your application settings with just a few lines of code.

Background

In Windows 3.1, developers used INI files to store settings. In general, they worked pretty well for simple settings, but were not appropriate for more complex data. INI files also did not account for multiple users, and thus Microsoft invented the Registry.

Along came the Registry with Win32. The registry was fast, hierarchical, multi user, and allowed storage of typed data. But unfortunately, the Registry was a central system component and was not contained as part of the application install.

Next, XML became popular. XML offers fast, hierarchical storage of typed data. However, XML is so flexible that, for most users, doing anything simple is quite an undertaking. Fortunately, there are easier ways than using System.XML and handling everything yourself.

Using the code

To use the class, there are only two methods you will need to worry about:

  • void GetSettings(String Key)
  • Sets the path and filename for the XML settings file.

  • void WriteSetting(String Key, String KeyValue, bool encode)
  • Writes a value to the settings file, given a key and value name. The encode option is to encrypt the value.

// Read Data
TxtServerName.Text = Configure.getSetting("DBServer");
//Save data
Configure.writeSetting("DBServer", TxtServerName.Text, false);
//Read data from .net in vb6
Dim x As New Configuration.configure
TxtDBServer.Text = CStr(x.getSettingFromVB6("DBServer"))

If the XML file does not exist or if a key/value does not exist, the Write methods will create it and the Get methods will return the default value.

Stage 1: creating the control

This took the longest of the three stages in our case, simply because of the nature of the control and calculating the positions of text, etc. I will not delve into the details of our control, but only the steps that are required for VB6 Interop.

  1. Create a new Windows Control Library project from within Visual Studio.
  2. In both the Debug and Release modes of the Property Pages, set the "Register for COM Interop" checkbox.
  3. Inside the AssemblyInfo.cs file, change the assembly wide attribute ComVisible to true. If it's not already in the configuration file, add it.
  4. [assembly: ComVisible(true)]

That is all that is required to make the project visible to VB6 projects.

Properties

A quick word about these: properties are exposed to VB6 so, like .NET controls, if you want to expose a value, you must wrap it in a Property expression. You cannot just have it visible as a field.

Stage 2: registering the assembly

The library must be registered on the client machine before use by VB6. If it is not registered on the development machine, then it will not be visible in the References dialog of VB6. If it's not registered on the installation machine, then it is a similar problem to if you have not registered a classic DLL or ActiveX control. The "Register for COM Interop" checkbox in VS2005 performs this registration automatically while the environment is running, but un-registers it when VS is closed.

To register the assembly, you must use the .NET equivalent of regsvr32, regasm. This is located in the framework directory, usually "C:\WINDOWS\Microsoft.NET\Frmaework\v2.0.50727". To register it, open a command prompt and run the following command, assuming that the framework directory and the assemblies directory are in the environment's current path.

regasm.exe Assembly.dll

Stage 3: adding to VB6 projects

The secret here is the VBControlExtender object, which allows a .NET control to be hosted on a VB6 form. However, the first stage is to add a reference to the assembly you just registered. This is accomplished by checking the box in the Project | References menu.

Once that has been accomplished, then the following code can be added to the form's code in the project.

Conclusion

XML files are a widespread standard that allows easy storage of structured typed data. Use of XML files allows easy editing by end users, and even by other software. Using System.XML, you can easily store your settings in XML files.

History

  • March 10, 2008 - Original article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nelson Kosta Souto
Engineer
Portugal Portugal
My name is Nelson Souto, I develop software for fun and profit since 1992.

You may also be interested in...

Comments and Discussions

 
Generalpassword Pin
Donsw6-Oct-10 11:22
memberDonsw6-Oct-10 11:22 
AnswerRe: password Pin
Nelson Kosta Souto8-Oct-10 1:41
memberNelson Kosta Souto8-Oct-10 1:41 
GeneralRe: password Pin
Donsw12-Oct-10 3:23
memberDonsw12-Oct-10 3:23 
GeneralVery useful Pin
tkrafael_net11-Mar-08 7:13
membertkrafael_net11-Mar-08 7:13 
GeneralRe: Very useful Pin
Nelson Kosta Souto11-Mar-08 7:45
memberNelson Kosta Souto11-Mar-08 7:45 
GeneralRe: Very useful Pin
lincyang3-Aug-09 15:28
memberlincyang3-Aug-09 15:28 
AnswerRe: Very useful Pin
Nelson Kosta Souto4-Aug-09 0:03
memberNelson Kosta Souto4-Aug-09 0:03 
GeneralRe: Very useful Pin
lincyang4-Aug-09 22:19
memberlincyang4-Aug-09 22:19 
GeneralRe: Very useful Pin
Nelson Kosta Souto5-Aug-09 22:26
memberNelson Kosta Souto5-Aug-09 22:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 11 Mar 2008
Article Copyright 2008 by Nelson Kosta Souto
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid