COM and .NET

This article was originally published in VSJ, which is now part of Developer Fusion.
A few months ago (VSJ Dec 2002/Jan 2003), I outlined the use of CAPICOM, a COM object using classic VB to access the facilities of the crypto API. .NET has its own set of cryptographic classes, but these are relatively complex compared to the simplicity of CAPICOM. The .NET cryptography classes are much closer to the underlying CryptoAPI. This is fine if you want to spend a lot of time implementing cryptography in your C# or VB.NET application, but for many situations it is worth considering using CAPICOM – despite some of the inconveniences that using COM objects from a .NET language entails. In fact one of the advantages of VS.NET 2003 is that it has made it even easier to use COM objects.

Using a COM object from C# or VB.NET isn't difficult, it's just badly explained – and you might well be put off by terms such as "COM interop". If you are using Visual Studio.NET, the steps in using any COM object are the same. First load the COM object as a reference. This might well sound like the process of loading a reference in VB6, but it isn't.

It may sound like adding a reference, but it isn't the same at all!
It may sound like adding a reference, but it isn't the same at all!

What happens is that it runs the TlbImp.exe command line program – the Type Library importer. This converts the type library into .NET metadata, which "wraps" the COM object in .NET classes. So to use CAPICOM all we have to do is load a reference to CAPICOM 1.0 Type Library.

To make programming easy you also have to add a "using x" at the start of the code, where x is the name of the COM object. So again in this particular case we have to add in C#:

using CAPICOM;
…and in VB.NET:
imports CAPICOM
…to the start of the program. Once you have added the "using" the automatic listing of parameters, members and code completion work making it easier to write code and you don't have worry about using fully qualified names.

When it comes to writing the code the way that you use the COM object is very like how you would use it in VB6, but there are some important differences. You can examine the way that the objects have been "wrapped" using the Object Browser. The methods for each class might well have slight differences in their detailed definitions, but you should be able to recognise everything.

Use the Object Browser to find out about your new COM classes
Use the Object Browser to find out about your new COM classes

The way that objects are wrapped has changed a little in VS.NET 2003. For example, VS.NET wraps the Encrypt and SetSecret methods with the parameter non-optional, because there is no default value specified. In the VS.NET 2003 version the reading of the type library is better and here the parameters are shown as optional.

In other words using COM objects under .NET 2003 is more like using them under VB6.

For example, to encode some data using a password-based key you would follow the same steps as in the VB6 example (VSJ Dec 2002/Jan 2003). First create a new EncryptedData object C#:

EncryptedDataClass Crypt1=
	new EncryptedDataClass();
Or in VB.NET:
Dim Crypt1 As New EncryptedDataClass
Notice the addition of the word "Class" to the end of the object's name. Next we can set the password using the SetSecret method:
Crypt1.SetSecret("MyPassword",
CAPICOM_SECRET_TYPE.CAPICOM_SECRET_PASSWORD)
The very long second parameter is needed because the type library hasn't been used to automatically define it as an optional parameter with a default value. However, if you're using VS.NET 2003 you can write:
Crypt1.SetSecret("MyPassword")
In this case the type library has defined it as an optional parameter with a default value.

Once again, how you use the Encrypt method depends on which version of VS you are using. For VS.NET we have:

Crypt1.Content = "Some text to encrypt"
Dim data As String
data = Crypt1.Encrypt(CAPICOM_
ENCODING_TYPE.CAPICOM_ENCODE_BASE64)
…and in VS.NET 2003 we have:
Crypt1.Content = "Some text to encrypt"
Dim data As String
data = Crypt1.Encrypt()
That's all there is to it. You can now work your way through using CAPICOM by converting each of the examples given in the VB6 programs earlier. This is not only good practice in VB.NET, C# and cryptography but in using general COM objects in .NET.

You might also like...

Comments

About the author

Ian Elliot United Kingdom

Ian Elliot is a development programmer with I/O Workshops Ltd, a consultancy which solves real-world problems for clients with widely varying requirements.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon