2013-05-14 8 views
7

से .NET dll को कॉल करना मुझे अपने COM wrapper का उपयोग करते हुए .NET dll (mclNET.dll) पर कॉल करने में समस्या है। यह एक तीसरा हिस्सा डीएल है जिसमें मेरे पास स्रोत कोड नहीं है। असल में मैं अपने शुद्ध देशी सी ++ एप्लिकेशन में इस mclNET.dll का उपयोग करना चाहता हूं, इसलिए मैं एक सी # रैपर विकसित कर रहा हूं। 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 फ़ाइल

  • तब मेरे देशी सी ++ आवेदन में, मैं 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 को नहीं पहचानता है पूरी तरह से अभी तक अपने मूल सी ++ कोड के संपर्क में नहीं है: यहाँ देशी सी ++ अनुप्रयोग में कुछ कोड है ।

मुझे आश्चर्य है कि यहां समस्या क्या है? किसी भी मौके से मैं इसे कैसे सही कर सकता हूं? मैं अपने मूल सी ++ एप्लिकेशन में एक .NET डीएल को कैसे ठीक से कॉल कर सकता हूं? बहुत आगे धन्यवाद।

+0

क्या आपने इंटरफ़ेस और कक्षा पर एक कॉमविज़िबल विशेषता जोड़ने की कोशिश की है? –

+0

मुझे लगता है कि मेरे पास एक COM दृश्य विशेषता है जो AssemblyInfo.cs में सत्य है। मुझे इसे पेस्ट करने दें, फिर आप देख सकते हैं कि क्या आप इसका जिक्र कर रहे हैं। –

+0

क्या यह संभव है कि आपको MCLSet_Switch गलत पर पैरामीटर मिले? और आउटपुट एक छोटे से पते की तरह दिखता है लेकिन समारोह एक int चाहता है। – Sarien

उत्तर

1

मैंने अंत में समस्या हल की। मुझे लगता है कि मैं मूल रूप से दो बातें किया:

  1. डाउनलोड वहाँ नवीनतम NET.dll;

  2. मैंने एक नई परियोजना "एमसीएलड्रैपर" बनाई है जो एक नया MCLWrapper.dll उत्पन्न करता है और मेरे पुराने प्रोजेक्ट से कोड चिपकाता है।

शायद मेरे पुराने प्रोजेक्ट में कुछ गड़बड़ है जो मुझे नहीं पता था। शायद यह नया NET.dll जादू था। मुझे पता नहीं है।मैंने मूल रूप से दोहराया जो मैंने किया है, लेकिन इस बार बहुत साफ है।

किसी भी तरह से मुझे यह काम मिल गया। तो मूल रूप से, मेरा मूल धागा देशी सी ++ कोड से .NET डीएल को कॉल करने के लिए काफी कुछ है। आशा है कि मेरा अनुभव आपके लिए सहायक होगा।

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

कोशिश ऊपर आयात बयान में उल्लेख किया है - मेरे लिए काम करता है और अपनी मूल सी ++ से किसी भी नामस्थान के बिना अपने .net कोड कहते हैं।

+2

क्षमा करें, मुझे लगता है कि आप सही हैं, लेकिन मुझे अभी तक मेरी समस्या ठीक हो गई है। लेकिन मैं आपकी युक्तियों को ध्यान में रखूंगा। धन्यवाद। –

संबंधित मुद्दे