7

I have a DLL I wrote in C# which I want to use in my VB6 application.

In VS2008 the project property "Register for COM interop" is checked, and when I compile the DLL and try to use it on my development machine - it runs ok.

I need to run it on a computer which does not have VS2008, so I tried to register this DLL like so:

C:\WINDOWS\system32>..\Microsoft.NET\Framework\v2.0.50727\regasm myDLL.dll /tlb: myDLL.tlb /codebase

but then when I try to run it I get this error:

Automation Error. The system cannot find the file specified.

Can anybody tell me what I'm doing wrong?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
La La La
  • 71
  • 1
  • 2
  • 7

2 Answers2

4

Just like you specified the full path to regasm.exe, you need to specify the full path to your .dll ;-)

Dabblernl
  • 15,831
  • 18
  • 96
  • 148
1

The reason this is happening is because you have not assigned a GUID to your classes. Your class in .NET should be decorated like this:

[GuidAttribute("BA713700-522D-466e-8DD4-225884504678")]
public class MyClass

This way your class will get compiled with the same GUID attribute every time you run regasm against it. If you do not include this attribute, regasm will auto-assign a different GUID every time.

To be completely safe, your class must inherit from an interface

[Guid("9AC71CA7-6F82-44A3-9ABE-75354B514A46")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IManager
{
    [DispId(1)]
    void Display(ADODB.Recordset recordSet);
    [DispId(2)]
    void Close();
}

[Guid("B9BB5B84-8FBD-4095-B846-EC072163ECD3")]
[ClassInterface(ClassInterfaceType.None)]    
[ProgId("This.Is.GonnaBe.MyClass")]    
public class Manager : IManager
{ 
    public void Display(ADODB.Recordset recordset)
    {
        // do stuff
    }
    public void Close()
    {
        // do stuff
    }
}
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • BTW, I create the instance with "CreateObject". – La La La Jul 03 '11 at 05:06
  • With CreateObject, I don't know - don't really have experience with that. But if you choose to do early binding, the process is this: when you compile your main app against the lib, it uses the existing lib GUID to access it. On other PCs, you are doing a regasm, which generates new GUIDs, but you do not compile your main app, do you. Try this experiment. Compile your main app. Go to the registry and remove any GUIDs associated with your lib, redo regasm and then kick off the main app (without recompile). I am guessing it won't work. – AngryHacker Jul 03 '11 at 19:45
  • IIRC, with CreateObject, Interop/COM uses a different interface to access the lib, that is why you might be facing challenges. But I don't know enough about it. – AngryHacker Jul 03 '11 at 19:46