Friday, May 06, 2005

interop When not to use Register for COM interop?

interop When not to use Register for COM interop?: "I have a solution with several .NET assemblies that are required to be

COM registered for use in C++, VB, Delphi etc.

I have the 'Register for COM Interop' build setting set to true.
However this causes problems with DLLs being cached in the
'C:\Documents and Settings\jjboswel\Local Settings\Application
Data\assembly\dl2\
' subfolders (as reported in other threads).
So periodically if we see strange debugging problems we delete our
cache folder and everything works again.

VB RAD tips on .net interop

VB RAD


  1. Create a strong key by running sn –k mykey.snk from the command line.
  2. Add the path of this generated file to your AssemblyInfo.vb or .cs file in the following manner:
    VB.NET: <Assembly: AssemblyKeyFile("c:\path\mykey.snk")>
    C#: [assembly: AssemblyKeyFile(@"c:\path\mykey.snk")]
  3. After compiling your assembly to a DLL, you must register it for COM Interop from the command line: regasm c:\path\MyDll.dll /tlb:MyDll.tlb
  4. Finally, you should install the DLL into the Global Assembly Cache by dragging the .DLL file into the c:\windows\assembly folder
  5. VB.NET: <ClassInterface(ClassInterfaceType.AutoDual)>
  6. Verify, VB.NET: VB.NET: <GuidAttribute("BA713700-522D-466e-8DD4-225884504678")>
  7. Verify, VB.NET: Imports System.Runtime.InteropServices

Wednesday, May 04, 2005

VB6 calling .NET DLL Interop

Imports System.Runtime.InteropServices

<System.Runtime.InteropServices.ProgId("HC65Reports_NET.HC65Reports"), _
ClassInterface(ClassInterfaceType.None)> _
Public Class cHC65Reports
Implements _IReports

The imports and the class interface go together. This is what gives VB6 the references in the object explorer. Without it you will only have a high level class stub in your VB6 object browser.

You use the REGASM to register the dll and create the typelib file for visual basic to reference. Once you have created the tlb file you can reference it in visual basic.

All the files created in the bin directory need to reside in the same directory as your visual basic exe. I compiled my VB6 program exe and copied it to the .net bin directory to test.

Alternatively, the dll could be assign a strong name key (snk) and registered in the GAC. However, are trying to avoid GAC entries. We would like to use copy deployment if possible.

When VB6 in debug mode, it will get a runtime error stating that it cannot find a file or assembly name. I am guessing that this is because VB6 is running the code from a temporary directory and not the actual directory of your project.

You can run your visual studio .net in debug mode using your vb6 executable as the startup program.

See http://radio.weblogs.com/0124037/stories/2003/08/18/debuggingComInterop.html

I would like to be able to tell VB to use the .net bin folder when running in debug mode.

If you try to reference the .net dll file instead of the .tlb file you will get an error when trying to reference it and you will get an "Two different copies of MSCOREE.DLL have been loaded" when you try to run the VB6 application in debug mode. Restart VB6 to clear the error.

Visual Studio Command Prompt Open Here

Would you like to right click on a directory in windows explorer and open a Visual Studio Command Prompt already set to the current directory? http://objectsharp.com/Blogs/dave/archive/2003/12/10/227.aspx Enter the following text into a file called VsShell.reg Save the file and double click on it to have the entries added to your registery. Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\cmd_vs]@="Open VS Command Prompt Here"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\cmd_vs\command]@="cmd.exe /k \"C:\\Program Files\\Microsoft Visual Studio .NET 2003\\Common7\\Tools\\vsvars32.bat\""
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Drive\shell\cmd_vs]@="Open VS Command Prompt Here"
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Drive\shell\cmd_vs\command]@="cmd.exe /k \"C:\\Program Files\\Microsoft Visual Studio .NET 2003\\Common7\\Tools\\vsvars32.bat\""