C# 从本机C++;

C# 从本机C++;,c#,c++,com,native,wrapper,C#,C++,Com,Native,Wrapper,使用COM包装器调用.NET dll(mclNET.dll)时遇到问题。这是第三部分dll,我没有它的源代码。基本上,我想在我的纯C++应用中使用这个“强> McNET.DLLMCLWrapper.dll,使mclNET.dll中的方法可见。以下是我所做的: 在MCLWrapper.dll中,我添加了mclNET.dll作为参考,然后定义接口以使mclNET.dll方法可见。以下是我的一些代码: using System; using System.Collection

使用COM包装器调用.NET dll(mclNET.dll)时遇到问题。这是第三部分dll,我没有它的源代码。基本上,我想在我的纯C++应用中使用这个“强> McNET.DLL<强”,所以我正在开发一个C语言包装器。strong>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")]
  • 然后在我构建了这个包装器.dll之后,我使用regasm命令注册了它,以生成一个.tlb文件

  • <> L> > P>在我的C++应用程序中,我导入了TLB文件,并尝试使用<强>包装器.dll <强>,引用了<强> .nll .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);
    
    }
    

现在的问题是,它不承认代码> MySwitk:> McStIdIsLoad(&,A,and Suffic)< /C>函数,即<强> McNET.DLL<强>还没有完全暴露于我的本机C++代码。 我想知道这里有什么问题?我怎么可能纠正它呢?在我的C++应用程序中,我如何才能调用.NET DLL?非常感谢。

仅导入原始接口
#import <mscorlib.tlb> raw_interfaces_only
#import C:/MyPath/MCLWrapper.tlb" no_namespace named_guids
#导入C:/MyPath/MCLWrapper.tlb“没有命名为\u guids的\u命名空间
Try是上面提到的导入语句-适用于我和
从你的本地C++调用你的.NET代码,没有任何命名空间。

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

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

  • 我创建了一个新项目“MCLWrapper”,它生成一个新的MCLWrapper.dll,并从我的旧项目中清晰地粘贴了代码

  • 也许在我的旧项目中有一些我没有意识到的地方弄糟了。也许是新的NET.dll发挥了神奇的作用。我不知道。我基本上重复了我所做的事情,但这次相当干净


    <> p>不知如何,我的基本线程是如何从本地C++代码调用.NET DLL。希望我的经验对您有帮助。

    您是否试图在接口和类上添加一个可编辑属性?我想在CysLimoF.CS中有一个COM可见属性集为真。让我粘贴它,然后您可以看到它是否是I?这就是您所指的。是否可能是您在MCLSet_开关上获取的参数错误?&输出看起来像一个short的地址,但函数似乎需要一个int。问题是,当我键入MySwitch->时,它不会自动显示MCLSet_开关();当我遵从MCLWrapper时,它说MCLSet_开关()不知道。好的,它在汇编级别很好。也许C++中使用的命名空间不是MCLRePAPER,检查在“调试”或“释放”配置目录中由“*导入指令”创建的.TLH文件的开始。对不起,我认为你是对的,但我只是解决了我的问题。但是我会记住你的提示。谢谢。