从本机C++调用.NET dll
Calling .NET dll from native C++
我使用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文件,并尝试使用Wrapper.dll,它引用了NET.dll
#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?非常感谢。
#import <mscorlib.tlb> raw_interfaces_only
#import C:/MyPath/MCLWrapper.tlb" no_namespace named_guids
Try是上面提到的导入语句-适用于我和在不使用任何命名空间的情况下从本机C++中调用.net代码。
我终于解决了这个问题。我想我基本上做了两件事:
-
下载最新的NET.dll;
-
我创建了一个新项目"MCLWrapper",它生成一个新的MCLWrapper.dll,并从我的旧项目中干净粘贴了代码。
也许我在旧项目中搞砸了一些我没有意识到的事情。也许是新的NET.dll发挥了神奇的作用。我不知道。我基本上重复了我所做的,但这次做得很干净。
不知怎么的,我成功了。所以基本上,我最初的线程基本上是如何从本机C++代码中调用.NET dll。希望我的经验对你有帮助。
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- VB.NET DLL中的C++DLL函数复制
- 是否可以编译使用 .NET DLL 和 GCC 的C++?
- 列出 C# 中的 non-.NET DLL 函数
- 如何为64位应用程序注册.NET DLL
- System.DllNotFoundException in .NET DLL
- 获取传递给子模块(C#.NET dll)中的主模块(VC++ exe)的参数
- 使用JNI集成java和.net dll
- 如何解决"原生DLL libmbusmaster.dll的问题不见了!请将 DLL 文件部署到与 mbusmaster.net.dll 相同的目录中。
- 如何更改.net dll的guid
- 动态加载.NET DLL包装C++DLL
- 使用 CoCreateInstance 从C++调用 COMVisible VB.Net dll
- .Net DLL vs C++ DLL
- 一个C++客户端无法使用我们新的 COM 注册的 .NET .dll加载,另一个客户端可以正常工作
- 如何在 Ubuntu 中将 c++ 编译为 Windows 的 .net Dll 文件
- Calling .NET DLL from Java
- 从本机C++调用.NET dll
- 从ASP调用后端.net dll.. NET Web api
- 从.Net Dll创建.Ocx文件
- 如何创建一个使用AFX库的混合模式CLR . net DLL