Search code examples
c#.netdllcom

Trouble calling C# 32 bit Com from 64 bit C# application


First thing, I know there are several articles about doing what I want to do. It is nothing special. But I have read numerous articles here and elsewhere and read multiple examples and I seem to get conflicting information as well as very mediocre success. I am asking people familiar with COM to be patient and help somebody who is just getting into COM for the first time.

I will explain what I believe to be what needs done based on my research and ask knowledgeable people to point out what I am doing wrong and help me fill in the knowledge gaps. My application uses third party 32-bit DLLs and 64-bit DLLs. There are a bunch of the 64-bit and only one 32-bit. This is why I am using a 64-bit application. If anyone cares, it is the Minolta kmsecs200.dll. The web site is clear that they do not have a 64 bit version.

The first step is to create the 32 bit COM wrapper. At this point, my sample code does not wrap anything. It just has one simple function. Here are the steps I took to create the DLL:

Create a new C#, windows, class Library (.Net Framework), .NET Framework 4

Name the project “SimpleCom”, rename the class and CS file to “JustOne”

In the project properties: On Application Tab: Click "Assembly Information" and set “Make assembly COM-Visible” On Build tab: Set Platform target to x86 On Signing tab: Select "Sign the assembly" and create a new Strong Name Key file named "StrongSimpleCom", no password protection

This is the JustOne.cs code:

using System.Runtime.InteropServices;

namespace SimpleCom
{
  [Guid("EAA4976A-45C3-4BC5-BC0B-E47474C3C83F")]
  public interface IJustOne
  {
    [DispId(1)]
    string AddName(string name);
  }

  [Guid("0D53A3E8-E51A-49C7-BC0B-E47474C3C83F"),
      ClassInterface(ClassInterfaceType.None)]
  public class JustOne : IJustOne
  {
    public JustOne() { }

    public string AddName(string name)
    { return "My name is: " + name; }

  }
}

Register the 32-bit Assembly on the target machine: From an administrative command prompt, navigate to the DLL folder C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe SimpleCom.dll /codebase /tlb:myTlb.tlb I used /codebase because I do not yet know how to call the third party DLL if I put my DLL in GAC. I do not really understand what the tlb file is for once registered. I would like to know if someone can tell me. At this point, the COM object shows up in Visual Studio but I can not call it that way and I understand that. It just tells me it is registered at least in some sense of the word. So I think I should be able to Invoke the 32 bit C# DLL that I wrote from a 32 bit C# application. But I just can not figure out how to do it. I can't seem to even find a good example of using invoke. I would really appreciate help on how to invoke "IJustOne" in a simple application.

Once that works, I can add some registry entries to make it out of process and make the 32 bit application into a 64 bit application. Here is one of the ways I have heard to modify the registry to make the 32 bit COM oup of process and use the DLLhost as the surrogate.

Techtalk.gfi.com method from 2009* Navigate to: HKEY_CLASSES_ROOT\WOW6432Node\CLSID{EAA4976A….} • Add “AppID” with value set to Guid Navigate to: HKEY_CLASSES_ROOT\WOW6432Node\AppID • Create a new key using the Guid as the name • In the key, Add “DllSurrogate” with no value Navigate to: HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID • Create a new key using the Guid as the name

Again I am having trouble finding good straight forward examples of how to invoke the COM object. I have created a C# DLL wrapper that I can directly include in a simple C# 32 bit application so I am comfortable with wrapping the unmanaged 3rd party DLL. I would like to know how to ensure my wrapper DLL can find their DLL.

I do not know what I am doing wrong with registering the DLL and I do not know how to invoke it in a 64 bit application, or a 32 bit for that matter. I appreciate any assistance. I have spent several days trying to chase this down but in the end, my knowledge base is just not strong enough. But a recap of the big picture: I am using 3rd part 32 bit unmanaged DLL. I need to call it from my 64 bit C# application. I believe the way to do it is wrap it in a managed C# 32 bit DLL wrapper and make it an out of process COM object the uses DLLhost as the surrogate.

Thanks again,

Dave


Solution

  • Simon pointed me in the right direction above. My DLL was registered just fine. I added it as a component through component services and everything worked after that. Thank You Simon.