Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;本机代码DLL使用抽象类装入包装器以在C#模块中使用_C#_C++_Dll_Wrapper - Fatal编程技术网

c++;本机代码DLL使用抽象类装入包装器以在C#模块中使用

c++;本机代码DLL使用抽象类装入包装器以在C#模块中使用,c#,c++,dll,wrapper,C#,C++,Dll,Wrapper,嗨,我在抽象类上遇到了同样的问题。我正在尝试包装我的cpp Dll调用,以便在C#模块中使用 Dll正在使用Factory模式。该模式包含一个类Factory、MiddileWare以及它们的costrong textr响应子类。有人能帮我启动包装器吗。我被夹在这两者之间。你的帮助将不胜感激。我在这里给出流程: 米德尔瓦雷工厂 #pragma once #include "MiddleWareConnection.h" #include "std afx.h" #ifdef MIDDLEW

嗨,我在抽象类上遇到了同样的问题。我正在尝试包装我的cpp Dll调用,以便在C#模块中使用

Dll正在使用Factory模式。该模式包含一个类Factory、MiddileWare以及它们的costrong textr响应子类。有人能帮我启动包装器吗。我被夹在这两者之间。你的帮助将不胜感激。我在这里给出流程:


米德尔瓦雷工厂

#pragma once
#include "MiddleWareConnection.h"
#include "std afx.h"

  #ifdef MIDDLEWAREFACTORY_EXPORTS
#define MIDDLEWAREFACTORY_API __declspec(dllexport)
#else
#定义MIDDLEWAREFACTORY\u API\u declspec(dllimport) #恩迪夫

  MIDDLEWAREFACTORY_API enum eMiddleWareEngine
      {
          eRabitMQ = 0,  
          eZeroMQ,                           
          eActiveMQ          

        };
     // This class is exported from the MiddleWareFactory.dll

      class MIDDLEWAREFACTORY_API CMiddleWareFactory
      {
      public:
         CMiddleWareFactory(void);
         ~CMiddleWareFactory();
        // TODO: add your methods here.

        //Function to Create the  object of Broker module:implemnted the                   Factory   concept.
      BOOL CreateInstance(CMiddleWareConnection** pMObj);
     int m_eMomEngine;//To define which MOM need to enable.

};



extern MIDDLEWAREFACTORY_API int nMiddleWareFactory;

MIDDLEWAREFACTORY_API int fnMiddleWareFactory(void);
MiddleWareConnection.h

#布拉格语一次

类CMiddleWareConnection { 公众:

  virtual ~CMiddleWareConnection(void)
  {
   }
  //Pure virtual fuctions for interfacing 
  virtual BOOL Connect(int nServerType)=0;
   virtual BOOL CreateSessionExchange() = 0;
   virtual BOOL CreateQueue(LPCTSTR lpszQueueName) = 0;
   virtual BOOL Disconnect() = 0;
   virtual BOOL Send(void *MomItem,LPCTSTR lpszKey, int               &nSendCount)=0;
   virtual BOOL Receive() = 0;
   virtual void StopReceiver() = 0;
   virtual void GetData(void* pMsg, int &nMsgLen,int nMsgType,int &nReceiveCount)=0;
   };
RabbitMQ.h

  #pragma once
  #include "MiddleWareConnection.h"
  #include "Amqp.h"
  #pragma comment(lib, "rabbitmq.4.lib")
  #define GET_DATA(a){memcpy(&a, pDataPtr,  sizeof(a));pDataPtr+=sizeof(a);}
  #define GET_DATA_EX(s,n){memcpy(s, pDataPtr, n);pDataPtr+=n;}

 typedef struct _ROUTINE_KEY
{
 CString RoutingKey;             

 }ROUTEKEY, *LPROUTEKEY;

   class CRabbitMQ :
   public CMiddleWareConnection
      {
          public:

      CRabbitMQ(CAppConfig &rConfig);
     ~CRabbitMQ();
      void   InitializeRBMQ(CAppConfig &rConfig);//Initialize RBMQ Config;
         BOOL   Connect(int nServerType);
         BOOL   Disconnect(void);
         BOOL   Send(void *MomItem, LPCTSTR lpszKey, int &nSendCount);
         BOOL   Receive(void);
         BOOL   CreateQueue(LPCTSTR lpszQueueName);
         BOOL   CreateSessionExchange();
         BOOL   BindQueue(LPCTSTR lpszQueue, LPCTSTR lpszExchangeName, LPCTSTR lpszKey);
         bool   IsConnected(){return m_bConnected;}
         void   SetKeyQueueCombination( TCHAR *pszQueueName, TCHAR *pszRoutingKey);
         void   StopReceiver();
         bool   ReEstablishRMQMWConnection();
        void   GetData(LPBYTE &pMsg, int &nMsgLen,int &nReceiveCount);
        void   GetData(void* pMsg, int &nMsgLen,int nMsgType,int &nReceiveCount);
     BOOL   GetNext_JasonListItem(LPJASON_ITEM pItem);
      LPRABBIT_MQ_ITEM GetNextItem();

      };
在这里,我想公开rabbitMq类函数[Connect、send、receive ete]从MiddleWareConnection.h到C#module


感谢下面的

我发布了一个包装C++/CLI类的小代码示例。您可以创建一个新的C++/CLI项目,该项目链接您的本机库,并按照下面的示例创建一个类。这将生成一个托管程序集,您可以从C#端引用。希望有帮助

#include <Windows.h>
#include <vcclr.h>  

using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace System::Runtime::InteropServices;

public ref class CMiddleWareConnection_Net
{
  public:

    CMiddleWareConnection_Net()
    {
        pMiddleWareConnection = NULL;
    }

    ~CMiddleWareConnection_Net()
    {
        //Release pMiddleWareConnection
        if (pMiddleWareConnection)
            delete pMiddleWareConnection;
    }

    void   InitializeRBMQ(CAppConfig_Net config)
    {
        // CAppConfig_Net is another ref class 
        // which provides a toNative method 
        // returning a plain C++ CAppConfig object
        CAppConfig rConfig = config.toNative();  

        pMiddleWareConnection = CRabbitMQ(rConfig);
    }

    bool   Connect(int nServerType)
    {
        return (pMiddleWareConnection->Connect(nServerType) == TRUE);
    }

    bool   Disconnect(void)
    {
        return (pMiddleWareConnection->Disconnect() == TRUE);
    }

    bool   Send(array<Byte>^ MomItem, String^ lpszKey, int% nSendCount)
    {
        //Assuming MomItem is a byte array...
        unsigned char *pData = new unsigned char[MomItem->Length];
        Marshal::Copy(MomItem, 0, IntPtr(pData), MomItem->Length);          

        //Marshal String^ to LPCSTR
        IntPtr ip = Marshal::StringToHGlobalAnsi(lpszKey);  
        LPCSTR str = static_cast<LPCSTR>(ip.ToPointer());

        bool bRet = (pMiddleWareConnection->Send(pData, str, nSendCount) == TRUE);

        //Freeing memory
        delete[] pData;
        Marshal::FreeHGlobal(ip);

        return bRet;
    }

    bool   Receive(void)
    {
        return (pMiddleWareConnection->Receive() == TRUE);
    }

    /* Other methods */
    ... 

  private:
    CMiddleWareConnection *pMiddleWareConnection;

};
#包括
#包括
使用名称空间系统;
使用名称空间系统::Text;
使用命名空间System::Collections::Generic;
使用名称空间System::Runtime::InteropServices;
公共引用类CMiddleWareConnection\u Net
{
公众:
CMiddleWareConnection_Net()
{
pmidlewawReconnection=NULL;
}
~CMiddleWareConnection_Net()
{
//释放重新连接
if(重新连接)
删除PMIDL重新连接;
}
void InitializeRBMQ(CAppConfig_Net config)
{
//CAppConfig_Net是另一个ref类
//它提供了一种调音方法
//返回一个简单的C++ CppCopFIG对象
CAppConfig rConfig=config.toNative();
pmidlewareconnection=CRabbitMQ(rConfig);
}
布尔连接(int nServerType)
{
返回(pmidlewareconnection->Connect(nServerType)==TRUE);
}
bool断开(无效)
{
返回(pmidlewareconnection->Disconnect()==TRUE);
}
bool发送(数组^MomItem,字符串^lpszKey,int%nSendCount)
{
//假设MomItem是一个字节数组。。。
unsigned char*pData=新的unsigned char[MomItem->Length];
Marshal::Copy(MomItem,0,IntPtr(pData),MomItem->Length);
//封送字符串^LPCSTR
IntPtr ip=Marshal::StringToHGlobalAnsi(lpszKey);
LPCSTR str=静态_cast(ip.ToPointer());
bool-bRet=(pmidlewareconnection->Send(pData、str、nSendCount)=TRUE);
//释放内存
删除[]pData;
元帅:自由全球(ip);
返回布雷特;
}
bool接收(无效)
{
返回(pmidlewareconnection->Receive()==TRUE);
}
/*其他方法*/
... 
私人:
CMiddleWareConnection*pMiddleWareConnection;
};

*****MiddleWareFactory.h****

     BOOL CreateInstance(CMiddleWareConnection** pMObj)
        { 
          //if( some config value)
            *pMObj = new RabbitMq();
          // esle
               /***other MOm like OBCS,OSCS etec...**/

        }
****MiddlewareConnection.h****

  have the basic abstract functions....

**Rabbitmq.h**

it is one of the child class.
****包装纸****

CLRWrapper::CppMathWrapper::CppMathWrapper()
{
    cppMath = new cCppMath();
    oFact->createInstance(&cppMath);'// here i want to pass the reference & wanna get back the child class type:now i am stuck here..how can we pass/marshal the object:im getting the error

} 
BOOL CLRWrapper::CppMathWrapper::connect(type)
{
  return (cppMath->Connect(nServerType) == TRUE);
}

..........And so On............
CLR Wrapper.cpp(11):错误C2664:“Factory::createInstance”:无法将参数1从“cli::interior\u ptr”转换为“Base**” 1> 与 1> [ 1> 类型=cCppMath* 1> ] 1> 无法将托管类型转换为非托管类型

我们使用的不是这个,而是createinstance()[InitializeRBMQ()在connect()内部,因此我认为它将在connect()内部处理]

void   InitializeRBMQ(CAppConfig_Net config)
    {
    // CAppConfig_Net is another ref class 
    // which provides a toNative method 
    // returning a plain C++ CAppConfig object
    CAppConfig rConfig = config.toNative();  

    pMiddleWareConnection = CRabbitMQ(rConfig);
}
C侧 我想揭露:

private static void Main(string[] args)
{
CppMathWrapper wrapper = new CppMathWrapper();
wrapper.connect(0);

}

我在CPPMath课堂上尝试了一个例子。在现实中,我附上了下面的代码:

CLRWrapper::CppMathWrapper::CppMathWrapper()
{
   middlewareConnection *oConn;// MiddleConnection.h pointer
   middleWareFactory oFact= new middleWareFactory ();//  MiddleWareFactory.h object
   oFact->createInstance(&oConn);'// here i want to pass the reference & wanna get back the child class type:now i am stuck here..how can we pass/marshal the object:im getting the error.How could i wrap the object....the same like 

}                                                                                                                                           
BOOL CLRWrapper::CppMathWrapper::connect(type)
{
  return (oConn->Connect(nServerType) == TRUE);
}

..........And so On............

本机dll仅导出CMiddleWareFactory?CMiddilwareFactory.h&MiddlewareConnection.hCMiddilwareFactory.h&MiddlewareConnection.h。导出调用方仅在工厂类中。MiddlewareConnection.h包含纯虚拟方法(我从该类继承代理类)我已经从CPP模块端测试了dll(包括dll和两个头diles)现在我想把它也用在C#Module上。最干净的方法是用包装的ref类做一个编组dll程序集。ref类是一个C++/CLI类,从C#端可以看到。明天我将试着用一个代码示例发布一个答案非常感谢你。我将尝试用这个。西蒙,你能给出下面的步骤吗?我有点困惑如何开始…&在哪里使用此代码。“CAppConfig_Net”是另一个类?[这是哪个类?],这是干什么用的?你能告诉我这个的总体前景吗?我该如何使用它。提前谢谢。是的,它是另一个封送CAppConfig的类。我这样写是因为CAppConfig没有发布。如果你想发布它。MiddleWareFactory。h@Fazna我猜在新帖子中,
CppMathWrapper
是ref类,
CppMath
n如果是C++,那么C的边就可以了。但是我不理解包装部分:什么是OffACT?
CLRWrapper::CppMathWrapper::CppMathWrapper()
{
   middlewareConnection *oConn;// MiddleConnection.h pointer
   middleWareFactory oFact= new middleWareFactory ();//  MiddleWareFactory.h object
   oFact->createInstance(&oConn);'// here i want to pass the reference & wanna get back the child class type:now i am stuck here..how can we pass/marshal the object:im getting the error.How could i wrap the object....the same like 

}                                                                                                                                           
BOOL CLRWrapper::CppMathWrapper::connect(type)
{
  return (oConn->Connect(nServerType) == TRUE);
}

..........And so On............