Call .NET dll from native C ++

I have a problem calling a .NET dll ( mclNET.dll ) using my COM shell. This is the third part of the dll in which I have no source code. Basically I want to use this mclNET.dll in my pure native C ++ application, so I am developing a C # shell. MCLWrapper.dll , which makes the methods in mclNET.dll visible. Here is what I did:

  • In MCLWrapper.dll, I added the mclNET.dll link as a link, then we define an interface to make the mclNET.dll methods visible. here is my code:

    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(); } } } 

And this is AssemblyInfor.cs for MCLWrapper.dll:

 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")] 
  • Then, after I built this Wrapper.dll , I registered it with the regasm command to generate the .tlb file

  • Then, in my native C ++ application, I imported the tlb file and tried to use Wrapper.dll , which referenced NET.dll . Here is some code in a native C ++ application:

     #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); } 

Now the problem is that it does not recognize the MySwitch->MCLSet_Switch(&_A,&output) function, which means that mclNET.dll is not yet fully open for my own C ++ code.

I wonder what the problem is? How can i fix this? How can I call .NET-dll in my native C ++ application? Thanks a lot to the front.

+7
source share
2 answers

I finally solved the problem. I think I basically did two things:

  • Download the latest version of NET.dll ;

  • I created a new project, "MCLWrapper", which generates a new MCLWrapper.dll and clears the code from the old project.

Perhaps I ruined something in my old project, which I did not understand. Perhaps this was the new NET.dll magic. I have no idea. I basically repeated what I did, but this time is pretty clean.

Somehow I earned. So basically, my original thread is pretty much called calling a .NET dll from native C ++ code. I hope my experience will be useful to you.

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

Try the above import operations - it works for me and call your .net code without any namespaces from your native C ++.

+6
source

All Articles