SIGN UP MEMBER LOGIN:    
ARTICLE

C# and ActiveX DLL

Posted by Anand Narayanswamy Articles | COM Interop December 14, 2001
As you know with Visual Basic 6.0, it is possible develop a COM server and implement them in a Visual Basic client program. But this is being done by using Visual Basic. You may wonder about the idea of calling this DLL in a C# Application. Well, C# language provides us a way to call this COM server in a program. When we compile a C# program an Intermediate Language is generated and it is called as Managed Code.
Reader Level:

As you know with Visual Basic 6.0, it is possible develop a COM server and implement them in a Visual Basic client program. But this is being done by using Visual Basic. You may wonder about the idea of calling this DLL in a C# Application. Well, C# language provides us a way to call this COM server in a program. When we compile a C# program an Intermediate Language is generated and it is called as Managed Code. A Visual Basic 6.0 DLL is Unmanaged, meaning it is not being generated by Common Language Runtime. But we can make this VB DLL to interoperate with C#, by converting the same into .NET compatible version. Following example shows how to create a simple server by using Visual Basic 6.0 and implementing it in a C# client program.

Creating an ActiveX DLL using Visual Basic 6.0 

1.  Fire up Visual Basic and select ActiveX DLL Icon from the
     New Project dialog box.
2.  Change the class name to something meaningful like
     "Our_csharp"
3.  Now supply the following code:

               Public Function Show()
               MsgBox("Message box created by using Visual Basic")
               End Function

4.  For creating a function, you can use Tools | Add procedure menu.
5.  Save the project by supplying relevant class and project names.
6.  Change the Project name and Description by selecting Project | Properties menu.
7.  Set the Binary Compatibility from the components tab of the above menu. 
    This will not create separate GUID upon each compilation.
8.  Finally create your DLL by selecting File | Make Csharpcorner.dll. This is the name by which you save your 
     VB project.
9.  That's all. Your DLL is now ready to use in a C# program.

It is not possible for a C# program to communicate with a VB DLL without converting the same into .NET equivalent. For doing so .NET SDK provides a tool called tlbimp. It stands for Type Library Import and converts your DLL to its equivalent .NET Assembly. For the above project supply the following command after properly setting the

Environment Variables:  

tlbimp Csharpcorner.dll /out:Csharp.dll  

A new .NET compatible file called Csharp.dll will be placed in the appropriate directory. Type in the following C# client program and execute as usual: 

using Csharp;
using System;
public class Csharpapply {
public static void main() {
Our_csharp c =
new Our_csharp();
s.Show();
}
}

Upon execution, you can be able to see the message box from our VB DLL. 

Notes: 

If you are using Visual Studio. NET, then you can refer the DLL from Project | Add Reference | COM Tab. Select your DLL by using the browse button. This will add reference to your project. After adding a reference copy the above code and execute.  

.NET SDK users have to run a batch file named corvars.bat before attempting compilation. This batch file is included with the SDK and located in the bin directory.

Login to add your contents and source code to this article
share this article :
post comment
 

I face the problem to use ocx in .net web service

your post help me a lot..

Thank you.

Posted by Adisorn Sirisomboon Mar 30, 2010

dear sir,
i wnat you to tell me can i create activex using C# and when open the  browser activex appear and ask for install

need help quicly plz

Posted by mahmoud hussien Oct 26, 2009

hi i'm student of computer science,i have problem while running the code in mobile simulator. plz help me out how to run the code using simulator in C# thanks

Posted by maire K Feb 06, 2008
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor