gpt4 book ai didi

c# - 从 native C++ 调用 .NET dll

转载 作者:可可西里 更新时间:2023-11-01 18:29:32 25 4
gpt4 key购买 nike

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

  • MCLWrapper.dll中,我添加了mclNET.dll作为引用,然后定义接口(interface)来制作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 文件

  • 然后在我的 native C++ 应用程序中,我导入了 tlb 文件,并尝试使用引用 NET.dllWrapper.dll。这是 native 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++ 代码。

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

最佳答案

#import <mscorlib.tlb> raw_interfaces_only
#import C:/MyPath/MCLWrapper.tlb" no_namespace named_guids

尝试上面提到的导入语句 - 对我和从您的 native C++ 调用您的 .net 代码而不使用任何 namespace 。

关于c# - 从 native C++ 调用 .NET dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16546667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com