【问题标题】:Calling .NET dll from native C++从本机 C++ 调用 .NET dll
【发布时间】:2013-05-08 22:33:30
【问题描述】:

我在使用我的 COM 包装器调用 .NET dll (mclNET.dll) 时遇到问题。这是我没有源代码的第三方 dll。基本上我想在我的纯原生 C++ 应用程序中使用这个 mclNET.dll,所以我正在开发一个 C# 包装器。 MCLWrapper.dll,使 mclNET.dll 中的方法可见。这是我所做的:

  • MCLWrapper.dll 中,我添加了 mclNET.dll 作为引用,然后定义接口以使 mclNET.dll 方法可见.这是我的一些代码:

    using System;            
    using System.Collections.Generic;            
    using System.Linq;            
    using System.Text;           
    using mclNET;       
    
    namespace MCLWrapper       
    {            
         public interface MCLControl            
         {                
              void MCLConnect(string SerialNumber);                
              void MCLSet_Switch(string SwitchName, int Val);                
              void MCLDisconnect();         
         };
    
         public class MCLControlClass:MCLControl
         {
             private USB_RF_SwitchBox _sb = new USB_RF_SwitchBox();
    
             public void MCLConnect(string SerialNumber)
             {            
                 _sb.Connect(ref SerialNumber);
             }
    
             public void MCLSet_Switch(string SwitchName, int Val)
             {
                 _sb.Set_Switch(ref SwitchName, ref Val);
             }
    
             public void MCLDisconnect()
             {
                 _sb.Disconnect();
             }
         }
     }
    

这是 MCLWrapper.dll 的 AssemblyInfor.cs:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MCLWrapper")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MCLWrapper")]
[assembly: AssemblyCopyright("Copyright ©  2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
//[assembly: AssemblyKeyFile("..\\MCLWrapper.SNK")]


// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("14fa8796-ee52-4e39-8481-f893ad92bb68")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
  • 然后在我构建了这个 Wrapper.dll 之后,我使用 regasm 命令注册了它,以生成一个 .tlb 文件

  • 然后在我的原生 C++ 应用程序中,我导入了 tlb 文件,并尝试使用引用 NET.dllWrapper.dll。以下是本机 C++ 应用程序中的一些代码:

    #include "stdafx.h"
    #include "math.h"
    #include "DetectorSwitch.h"
    #include "DetectorSwitchSetupXML.h"
    #include "OleAuto.h"
    
    #import "C:/MyPath/MCLWrapper.tlb" raw_interfaces_only   
    
    wchar_t message[256];
    using namespace MCLWrapper;
    
    long DetectorSwitch::FindDevices(long &deviceCount)
    {
        long ret = TRUE;
    
        deviceCount=0;
    
           HRESULT hCoInitialize = CoInitialize(NULL);
    
        MCLControlPtr MySwitch(__uuidof(MCLControlClass));
    
        HRESULT hConnet = MySwitch->MCLConnect(_SN);    // connect to sc
    
        short output  = 1;
        MySwitch->MCLSet_Switch(&_A,&output);
    
    }
    

现在的问题是,它无法识别 MySwitch->MCLSet_Switch(&_A,&output) 函数,这意味着 mclNET.dll 还没有完全暴露给我的原生 C++ 代码。

我想知道这里有什么问题?我怎么可能纠正它?如何在我的本机 C++ 应用程序中调用 .NET dll?非常感谢。

【问题讨论】:

  • 你试过在接口和类上添加 ComVisible 属性吗?
  • 我想我在 AssemblyInfo.cs 中有一个 COM 可见属性设置为 true。让我粘贴它,然后您可以查看它是否是您所指的。
  • 是不是你把 MCLSet_Switch 上的参数弄错了? &output 看起来像一个 short 的地址,但函数似乎需要一个 int。
  • 问题是,当我输入MySwitch->时,并没有自动显示MCLSet_Switch();当我遵守 MCLWrapper 时,它说 MCLSet_Switch() 无法识别。
  • 好的,它在装配级别上很好。可能在 C++ 中使用的命名空间不是 MCLWrapper,请检查 #import 指令在 Debug 或 Release 配置目录中创建的 .TLH 文件的开头。

标签: c# c++ com native wrapper


【解决方案1】:
#import <mscorlib.tlb> raw_interfaces_only
#import C:/MyPath/MCLWrapper.tlb" no_namespace named_guids

Try 是上面提到的 import 语句 - 适用于我和 从您的本地 C++ 调用您的 .net 代码,而无需任何命名空间。

【讨论】:

  • 对不起,我认为你是对的,但我刚刚以某种方式解决了我的问题。但我会记住你的建议。谢谢。
【解决方案2】:

我终于解决了这个问题。我想我基本上做了两件事:

  1. 在那里下载最新的NET.dll

  2. 我创建了一个新项目“MCLWrapper”,它会生成一个新的 MCLWrapper.dll 并从旧项目中干净粘贴代码。

也许我在旧项目中搞砸了一些我没有意识到的事情。也许是新的 NET.dll 发挥了作用。我不知道。我基本上重复了我所做的,但这次非常干净。

不知何故,我得到了它的工作。所以基本上,我的原始线程几乎是如何从本机 C++ 代码调用 .NET dll。希望我的经验对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 2011-08-25
    • 2015-01-16
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多