C ++ / CLI混合托管/本机DLL无法正常工作

Ano*_*non 0 c# c++ clr c++-cli command-line-interface

我正在创建一个C ++ / CLI DLL,该DLL应该用作包装器。它的目的是包装C#SDK并将函数呈现给本机C ++代码。我总是收到混合类型的错误,并且托管类中的using语句被标记为红色,因此,到目前为止,这是我得到的:

#pragma once

#include <iostream>
#include <memory>
#include <string>

namespace TPInterface
{
class ITPFactory
{
public:
  static __declspec(dllexport) std::shared_ptr<ITPFactory> CreateTPFactory();
};
}
Run Code Online (Sandbox Code Playgroud)

这将创建TPFactory的实例。

#pragma once

#include "ITPSSITotalStation.h"
#include "TPSSITotalStation.h"
#include "ITPFactory.h"
#include <iostream>
#include <memory>
#include <string>

namespace TPInterface
{
class TPFactory : public ITPFactory
{
public:
  static std::shared_ptr<SensorSoftwareInterface::TotalStation::ITPSSITotalStation> CreateTPSSITotalStation(std::string pathToDriver);
};
}
Run Code Online (Sandbox Code Playgroud)

这将创建一个TPSSITotalStation对象,该对象是ITPSSITotalStation接口。

TPSSITotalStation-> TPSSIBase->TPBase

二者TPSSIBaseTPBase含有被写入在托管代码(参照类)的类(gcroot和报头)。

现在,编译器告诉我,那些ref类是混合的,不允许的,依此类推。我在这里没问题...我在做什么错?

抱歉,我很笨,我是C ++的新手,来自C#。

错误:

Error   7   error C4368: cannot define 'm_selectedPath' as a member of managed 'TPInterface::Driver': mixed types are not supported

Error   8   error C4368: cannot define 'm_assemblyNameAndVersion' as a member of managed 'TPInterface::Driver': mixed types are not supported   

Error   9   error C2678: binary '=' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)    

Error   28  error C3265: cannot declare a managed '_Ptr' in an unmanaged 'std::tr1::_Ptr_base<_Ty>'

Error   51  error C3699: '*' : cannot use this indirection on type 'TPInterface::Sensor'    

Error   65  error C3642: 'TPInterface::Sensor msclr::gcroot<T>::operator ->(void) const' : cannot call a function with __clrcall calling convention from native code
Run Code Online (Sandbox Code Playgroud)

理解目的的小例子:

ref class Driver // Contains errors in using (C#) statements
{
  // Does something managed
    private:
  std::string m_selectedPath;
  std::string m_assemblyNameAndVersion;
}
ref class Sensor // Contains errors in using (C#) statements
{
  // Does something managed
}

class TPBase
{
  // includes Driver class and holds it also inside msclr::gcroot<Driver^> 
}
class TPSSIBase : TPBase
{
  // includes Sensor and Driver class and holds sensor also inside msclr::gcroot<Sensor^> 
}
class TPSSITotalStation : TPSSIBase, public ITPSSITotalStation
{
  // contains functions which should be exported to native C++ 
}
Run Code Online (Sandbox Code Playgroud)

其余内容已在上面说明。

xMR*_*MRi 7

  1. 您不能从托管类派生非托管类。
  2. 您不能从非托管类派生托管类。
  3. 您不能将托管成员添加到非托管类中
  4. 您不能将非托管数据成员(指针除外)添加到托管类。
  5. 在非托管类中,您可以编写返回托管类型的函数。
  6. 在托管类中,您只能形成使用允许的托管类型的函数。

那么该怎么做,创建一个包装器:

  1. 创建一个非托管类,其中包含gcroot<..>创建和保存所需的所有托管对象的指针/对象。在文档中查找gcroot<..>模板。
  2. 在托管类中,您可以自由保存指向非托管世界的指针。

只要使用gcroot和普通指针,您就可以轻松地从非托管世界访问.NET世界,反之亦然。