简体   繁体   English

无法从 64 位 C# 应用程序调用 C# 32 位 Com

[英]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.我希望熟悉 COM 的人要有耐心,并帮助那些第一次接触 COM 的人。

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.我的应用程序使用第三方 32 位 DLL 和 64 位 DLL。 There are a bunch of the 64-bit and only one 32-bit.有一堆64位的,只有一个32位的。 This is why I am using a 64-bit application.这就是我使用 64 位应用程序的原因。 If anyone cares, it is the Minolta kmsecs200.dll.如果有人在乎,那就是 Minolta kmsecs200.dll。 The web site is clear that they do not have a 64 bit version.该网站明确表示他们没有 64 位版本。

The first step is to create the 32 bit COM wrapper.第一步是创建 32 位 COM 包装器。 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:以下是我创建 DLL 的步骤:

Create a new C#, windows, class Library (.Net Framework), .NET Framework 4创建新的 C#、windows、类库(.Net Framework)、.NET Framework 4

Name the project “SimpleCom”, rename the class and CS file to “JustOne”将项目命名为“SimpleCom”,将类和 CS 文件重命名为“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在项目属性中: 在应用程序选项卡上:单击“程序集信息”并设置“使程序集 COM 可见”在构建选项卡上:将平台目标设置为 x86 在签名选项卡上:选择“对程序集进行签名”并创建一个新的强名称密钥文件名为“StrongSimpleCom”,无密码保护

This is the JustOne.cs code:这是 JustOne.cs 代码:

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.在目标计算机上注册 32 位程序集:从管理命令提示符,导航到 DLL 文件夹 C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\regasm.exe SimpleCom.dll /codebase /tlb:myTlb。 tlb 我使用 /codebase 是因为如果我将我的 DLL 放在 GAC 中,我还不知道如何调用第三方 DLL。 I do not really understand what the tlb file is for once registered.我真的不明白 tlb 文件是什么一旦注册。 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.此时,COM 对象出现在 Visual Studio 中,但我不能这样称呼它,我理解这一点。 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.所以我认为我应该能够调用我从 32 位 C# 应用程序编写的 32 位 C# DLL。 But I just can not figure out how to do it.但我就是不知道该怎么做。 I can't seem to even find a good example of using invoke.我似乎找不到使用 invoke 的好例子。 I would really appreciate help on how to invoke "IJustOne" in a simple application.我非常感谢有关如何在简单应用程序中调用“IJustOne”的帮助。

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.一旦成功,我可以添加一些注册表项以使其退出进程并使 32 位应用程序成为 64 位应用程序。 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.这是我听说的修改注册表以生成 32 位 COM oup 进程并使用 DLLhost 作为代理的方法之一。

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 Techtalk.gfi.com 2009 年的方法* 导航至:HKEY_CLASSES_ROOT\\WOW6432Node\\CLSID{EAA4976A....} • 添加“AppID”并将值设置为 Guid 导航至:HKEY_CLASSES_ROOT\\WOW6432Node\\AppID • 使用 Guid 创建新密钥名称 • 在键中,添加没有值的“DllSurrogate” 导航至:HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\AppID • 使用 Guid 作为名称创建一个新键

Again I am having trouble finding good straight forward examples of how to invoke the COM object.我再次无法找到有关如何调用 COM 对象的直接示例。 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.我已经创建了一个 C# DLL 包装器,我可以直接将它包含在一个简单的 C# 32 位应用程序中,因此我可以轻松地包装非托管的 3rd 方 DLL。 I would like to know how to ensure my wrapper DLL can find their DLL.我想知道如何确保我的包装 DLL 可以找到它们的 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.我不知道我在注册 DLL 时做错了什么,也不知道如何在 64 位应用程序或 32 位应用程序中调用它。 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.但总体回顾一下:我使用的是第 3 部分 32 位非托管 DLL。 I need to call it from my 64 bit C# application.我需要从我的 64 位 C# 应用程序中调用它。 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.我相信这样做的方法是将它包装在托管的 C# 32 位 DLL 包装器中,并使其成为使用 DLLhost 作为代理的进程外 COM 对象。

Thanks again,再次感谢,

Dave戴夫

Simon pointed me in the right direction above.西蒙为我指出了正确的方向。 My DLL was registered just fine.我的 DLL 注册得很好。 I added it as a component through component services and everything worked after that.我通过组件服务将它添加为组件,之后一切正常。 Thank You Simon.谢谢西蒙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从64位模式运行的c#调用32位COM - Calling 32bit COM from c# running in 64bit mode 64 位 C# 与 32 位 VB6 COM ZA8CFDE6331BD59EB2AC96F8911C4B6 - 64 bit C# with a 32 bit VB6 COM object 使用Environment.Is64BitProcess从c#应用程序动态调用32位或64位DLL - Dynamically calling 32-bit or 64-bit DLL from c# application using Environment.Is64BitProcess c#中64位应用程序中的32位dll - 32bit dll in 64bit application in c# 如何在32位操作系统上构建64位应用程序? [C#] - How to build a 64 bit application on 32 bit OS? [c#] 从64位c#WinForms应用程序调用第三方32位dll时的事件处理 - Event handling at calling a third party 32-bit dll from a 64-bit c# WinForms application 如何使用c#代码从64位机器上的64位应用程序获取32位应用程序数据文件夹 - how to get 32 bit application data folder from 64 bit application on 64 bit Machine using c# code 在具有 32 位应用程序的 64 位机器上从 c# 代码安装 IIS - Installing IIS from c# code on 64bit machine with 32bit application 从64位应用程序(C#)中检索所有32位处理模块 - Retrieving all 32 bit process modules from 64 bit application (C#) 我可以从64位应用程序运行C#程序集(dll)为32位吗? - Can I run a C# assembly (dll) as 32bit from a 64bit application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM