Calling vb6 dlls from c#

19,475

Solution 1

A VB6 DLL is a COM DLL. Usually you would register the DLL (in the registry) and then add a reference to the VB6 DLL from your .NET project.

This MSDN article gives a walkthrough of using registry-free COM from a .Net app.

Solution 2

Your VB6 dll as MarkJ mentions is a COM Dll, and they usually need to be registered using regsvr32 before you can use them.

Once it's registered you can add a reference to it the same as you would with a .NET dll, i.e. right click on References in the project, click Add Reference, then select the COM tab on the window and look for your COM Dll name.

Then you should be able to use it like a .NET reference.
Here is an example of how to use a COM reference to Microsoft Excel.
How to: Use COM Interop to Create an Excel Spreadsheet

If you specifically want late binding, then your dll still needs to be registered but you don't manually add a reference, you use Activator.CreateInstance() to get an instance of your COM object.
Calling COM component from C# using late binding

Solution 3

Assuming that the method show is in the export table in the dll, try using DllImportAttribute to call the show method.

Share:
19,475
asp developer
Author by

asp developer

Updated on June 09, 2022

Comments

  • asp developer
    asp developer almost 2 years

    I have been trying to call a vb6 dll from a C sharp application, without using the registry. I want to use the path of the dll while using it. I am unable to create an object of the class of the vb dll. Please help! The code I have written so far is as follows:

    Assembly assem = Assembly.LoadFile("dll path");
    Type classType = assem.GetType("classname");
    MethodInfo method = classType.GetMethod("show"); //My methos is called show
    method.Invoke(null,null); // I have to invoke the method using class object, which I am unable to create
    
  • MarkJ
    MarkJ over 12 years
    99 times out of 100 a VB6 DLL will not expose anything useful through the export table. They are COM DLls. (Rarely a DLL author would use blackbelt techniques to export functions as if the DLL was an old-style DLL.)
  • asp developer
    asp developer over 12 years
    Hi, I need to use it by late binding, without using the registry information. Thats the problem!
  • asp developer
    asp developer over 12 years
    Can you please give me an example of the code required to do the same?
  • asp developer
    asp developer over 12 years
    And how should I put the method in export table?
  • Nanhydrin
    Nanhydrin over 12 years
    Can you give me more detail on your scenario please? Will the dll be registered on the target systems and just not on yours? Because I don't think it's actually possible to use COM dlls without having them registered.
  • Nanhydrin
    Nanhydrin over 12 years
    This article might be relevant Registration free activation of COM components
  • MarkJ
    MarkJ over 12 years
    That's the same link I posted in my answer. @bhavna, I think you need to explain exactly what you need to do and why. What problem are you trying to solve? Why can't you use the registry?
  • Nanhydrin
    Nanhydrin over 12 years
    Sorry, I hadn't noticed, I'd just come across it googling.
  • veena
    veena over 12 years
    For eg. you can call windows sendmessage in user32.dll from managed code [DllImportAttribute("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); public void yourMethod() { SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); } and this can be done only if the dll has the method in export table.
  • asp developer
    asp developer about 12 years
    @Mark - Two or more dlls may have the same name. If I use registry information, the system would automatically use the last registered dll with the same name. This is something I do not want. I need the dll to be used by giving its file path rather than registry.
  • MarkJ
    MarkJ about 12 years
    @bhavna Then you need registry-free COM. See the link in my answer. Or, and it might be simpler, just make sure you only have one DLL registered. When you create new versions of the DLL, make sure they are backward compatible with the older versions. Use "binary compatibility" in the VB6 project settings to keep the syntax and COM interfaces constant, and make sure not to change the contract / semantics of your DLL.
  • MarkJ
    MarkJ about 12 years
    Registry-free COM will be much simpler than exporting methods through the export table in VB6 and then trying to P/Invoke them from .Net. That's seriously advanced stuff.
  • asp developer
    asp developer about 12 years
    Thanks guys for your help, but these articles provide information about the usage of win32 dlls only :'( I have a vb6 dll to be used in C#.
  • asp developer
    asp developer about 12 years
    Thanks @Mark! Will try what you have suggested :)
  • MarkJ
    MarkJ about 12 years
    The link includes info on .Net clients and VB6 servers. Look at this section, Step 2 part B msdn.microsoft.com/en-us/library/… and this section msdn.microsoft.com/en-us/library/…. I agree it would be better if the article didn't try to cover C++, C#, VB.Net and VB6 all in one article!
  • Fandango68
    Fandango68 over 6 years
    What if you don't know the methods inside the DLL? I've simply referenced it directly into my project and I can see its methods and functions, etc in the Object Browser.