Monthly Archives: July 2006

Exposing your .NET assembly through COM

I wanted to write a simple .NET assembly, make some of the methods exposed through COM and use these methods in another program. It sounded so simple that I thought I will be done in an hour. It took me two days to figure out how to do this. Blame me on my poor googling skills, but the information was so scattered that I felt bad for my fellow programmers who might spend the same amount of time looking for a solution. So I have decided to put up an example here so that it saves time for others.

Let’s first write the application which we want to expose through COM. We will define an interface (not mandatory, you can do away with the interface) and put in all the methods which should be exposed through COM. Then, we will write the class which will implement this interface. There are three things extra we need to do to make our application COM compatible.

i. Create a GUID and it add as an attribute for the interface.
ii. Create a GUID and it add as an attribute for the class.
iii. Add the Comvisible(true) attribute to all the functions you want to expose through COM.

So, here is the step by step detail.

1. Create a class library project in Visual Studio. Name it ComExample.
2. Copy and paste the code below into Class1.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ComExample
{
[Guid("77699130-7D58-4d29-BE18-385871B000D1")]
public interface IExample
{
string GetText();
void SetText(string text);
}

[Guid("F91E5EE1-D220-43b5-90D1-A48E81C478B7")]
[ComVisible(true)]
public class Example : IExample
{
private string m_text = “default text”;

public string GetText()
{
return m_text;
}

public void SetText(string text)
{
m_text = text;
}

}
}

3. Open the project properties page.
3a.Under the Build tab, check the box ‘Register for COM interop’
3b. Under the Signing tab, check the box ‘Sign the assembly’, click on the <New…> in the drop down box,
give a name to your key file and save it. This file will be automatically added to your project.

4. Build the project. You should have a dll named ComExample.dll
5. Open Visual Studio Command prompt and go to the directory where the dll is stored.
6. Type in the following command.

gacutil -i ComExample.dll

The response should be: “Assembly successfully added to the cache”

7. Type in the following command.

regasm ComExample.dll

You should get ‘Types registered successfully’.

Your .NET assembly is ready. Now let’s use it.

Open any editor and put the following code:

Dim object

set object = CreateObject(“ComExample.Example”)
MsgBox(“Created the object.”)

defaultText = object.GetText()
MsgBox(“Default text : ” & defaultText)

object.SetText(“My new text”)
newText = object.GetText()

MsgBox(“New text is ” & newText)

8. Save it as use_com.vbs.
9. From the command prompt, type the command:
cscript use_com.vbs

10. You should see some message boxes.

11. You are done.

Happydent: Eesmile

The other day, this ad cuaght my eye when I was changing channels. Happydent chewing gum brand has come up with a brilliant ad which will give its rival, Orbit, a run for money.

The previous ad was about using white teeth as a flash for taking photographs. This simple concept is taken a step further and the new ad shows that Happydent white teeth can be used any place where you need light. Street lights, car headlights, reading lights… get the idea?

The lead actor is superb. He has the right amount of anxiety and tension on his face. The wonderful background score has a touch of sufi music and has been composed by none other than Shantanu Moitra. For lesser mortals who don’t recognise the name, he is the music director of Parineeta. The agency behind this ad is McCann-Erickson.

If there is anything worth watching on the idiot box, then this is it.

If you are lucky enough to not have TV, then you can watch this ad here.

IE: Losing steam?

While trying to download the Beta 3 of Internet Explorer 7, I noticed something really funny. It has a snapshot of its rival browser, Firefox! Does this count as healthy rivalry?

I read this when I was installing IE 7 Beta 3.

We recommend that you backup your important files and close all programs.

That’s when I changed my mind.

Travel tips!

I came across this site (which I think is the official Kodaikanal site) which has some travel advice for tourists. No offence meant, but this page can have some editing.

First-Aid Kit should be taken with us.

While travelling in vehicle the diesel should be filled little above the need. In hill stations starting problem will occur. To avoid this it is better to fill the diesel there itself which contains some chemicals to prevent the starting problem.

To Make us happy the horses are sacrificing their life taking us riding. Is it a needy one

Mumbai blasts

Mumbaikars were holding their breath for July 26th to pass by and the terror struck much before that. A series of 7-8 bomb blasts rocked Mumbai and left people spell bound.

As the local train compartments were ripped open, people panicked and started calling their near and dear ones. Needless to say, that jammed the phone lines.

People on the platform, who witnessed the bomb blasts and those who heard the blast came rushing to the station to help the victims. A person was interviwed by a DH correspeondent and he said, “I don’t have time to talk to you now. I need to get back here with some food, water and medicines.

Common public helped the police in the rescue operations. They could have chosen to stay at home and thank their stars, but they didn’t. Would you call these people rude? Helping someone pick up a stack of papers in one thing; but helping the police in rescuing people and supplying food and water to people stuck at the station is a completely different thing. What would you want help with: saving your life or picking up a heap of papers? And somebody had the nerve to call Mumbaikars rude? Reader’s Digest, are you listening?

A picture is worth a thousand words

Chromasia has been on my blogroll for quite some time now. The author is a professional photographer (and a very good one at that) and he posts one photograph a day, with an optional caption. His photographs are so interesting and so beautiful that you can’t take your eyes off it. Today’s photograph needs a special mention. It’s awesome. You need to see it to believe it.

Did Raymond Chandler invent Google?

Raymond Chandler used the word ‘Google’ in one of his letters. Did he coin the word?

The sudden brightness swung me round and the Fourth Moon had already risen. I had exactly four seconds to hot up the disintegrator and Google had told me it wasn’t enough.’ “

Books to read before you die

Wordwing Editor has come out with a list of 30 books you should read before you die. I have read 5 in that list list and I haven’t heard of 10. The list is not ‘correct’ in any sense. Mark Haddon’s Curious incident of the dog in the night time and Jane Auten’s Pride and Prejudice do not go together. I personally do not agree with Gone with the wind being on the list. Life of Pi: definitely not! And I whole-heartedly support To kill a mocking bird. Now, that is one book that you have to read before you die.

So how many of these have you read?

Considering that I have to read 25 books in that list, I better not die too soon.

Follow

Get every new post delivered to your Inbox.