首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从本机.NET调用C++ dll

从本机.NET调用C++ dll
EN

Stack Overflow用户
提问于 2013-05-14 15:02:51
回答 2查看 11.1K关注 0票数 8

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

  • MCLWrapper.dll,中,我添加了mclNET.dll作为引用,然后定义接口以使mclNET.dll方法可见。下面是我的一些代码:使用系统;使用System.Collections.Generic;使用System.Linq;使用System.Text;使用mclNET;名称空间MCLWrapper {公共接口MCLControl { void (String SerialNumber);MCLDisconnect();};公共类MCLControlClass:ref控件{私有USB_RF_SwitchBox _sb =新USB_RF_SwitchBox();公共空USB_RF_SwitchBox(字符串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();}}

这是AssemblyInfor.cs for MCLWrapper.dll:

代码语言:javascript
复制
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命令注册it,以生成一个.tlb文件。
  • 然后,在我的本机C++应用程序中,我导入了tlb文件,并尝试使用引用NET.dllWrapper.dll。以下是本机C++应用程序中的一些代码: #包括"stdafx.h“#包括"math.h”#包括"DetectorSwitch.h“#包括"DetectorSwitchSetupXML.h”#包括"OleAuto.h“#import "C:/MyPath/MCLWrapper.tlb”raw_interfaces_only wchar_t message256;使用命名空间MCLWrapper;long DetectorSwitch::Find设备( long &deviceCount) {long ret = TRUE;deviceCount=0;HRESULT hCoInitialize = CoInitialize(NULL);MCLControlPtr MySwitch(__uuidof(MCLControlClass));HRESULT hConnet = MySwitch->MCLConnect(_SN);//连接到sc短输出= 1;MySwitch->MCLSet_Switch(&_A & output );}

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-14 16:43:56

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

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

也许我在以前的项目中搞砸了一些事情,但我没有意识到。也许是新的NET.dll做了这个魔术。我没有头绪。我基本上重复了我做过的事情,但这次非常干净。

不知怎么我让它起作用了。因此,基本上,我最初的线程主要是如何从本机.NET代码调用C++ dll。希望我的经历能对你有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2013-05-14 16:43:09

代码语言:javascript
复制
#import <mscorlib.tlb> raw_interfaces_only
#import C:/MyPath/MCLWrapper.tlb" no_namespace named_guids

Try是上面提到的导入语句--为我工作,并调用您的.net代码,而不需要从本地C++调用任何名称空间。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16546667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档