如何在托管类中创建非托管类的对象?

时间:2018-03-22 09:24:24

标签: c++ winforms

我正在使用Windows Forms for GUI作为基于C ++的应用程序,该应用程序用作表单的后端。表单是托管类,其余的C ++代码是不受管理的。我需要在Managed Class(WinForm)中创建一个Unmanaged Class对象。我怎么能这样做?

我的(相关)代码:

#include "DataCapture.h"

public ref class LandingPage : public System::Windows::Forms::Form
{
  public:
    LandingPage(void) {
      InitializeComponent();
      this->dataCapture = new datacaptureapi::DataCapture();
      this->dataCapture->TurnOnResearchCapability();
    }

  private: static datacaptureapi::DataCapture* dataCapture;
};

这里,LandingPage是Managed Class,DataCapture是Unmanaged类。

我试过的东西:

  1. 我尝试添加&删除static,const,const static关键字并将非托管类的对象作为指针创建/删除。
  2. 我尝试过使用gcroot。代码将成为:
  3. private: static gcroot<datacaptureapi::DataCapture*> dataCapture; private: static gcroot<datacaptureapi::DataCapture> dataCapture;

    1. 在课堂上添加了#pragma unmanaged。给了太多错误。
    2. 我读过一些关于编写一个与我的非托管类具有相同功能的包装器的地方,但它将在.Net生态系统中重写,使其得到管理。我没有尝试过,因为我想尽可能使用一些简单的方法。我不认为我也真的明白这一点:/

      首次使用WinForms进行编码,我能够包含我所需的DataCapture.h文件并调用函数(如果不在类中)。所以错误不是因为链接或其他设置问题。

      错误:

      Severity    Code    Description Project File    Line    Suppression State
      Error   LNK2028 unresolved token (0A0004D7) "public: __cdecl datacaptureapi::DataCapture::DataCapture(void)" (??0DataCapture@datacaptureapi@@$$FQEAA@XZ) referenced in function "public: __clrcall FOVEImageCapture::LandingPage::LandingPage(void)" (??0LandingPage@FOVEImageCapture@@$$FQE$AAM@XZ)    FOVEImageCapture    C:\Users\kapil\Desktop\Project1\Project1\MyForm.obj 1
      
      Severity    Code    Description Project File    Line    Suppression State
      Error   LNK2028 unresolved token (0A0004D8) "public: void __cdecl datacaptureapi::DataCapture::TurnOnResearchCapability(void)" (?TurnOnResearchCapability@DataCapture@datacaptureapi@@$$FQEAAXXZ) referenced in function "public: __clrcall FOVEImageCapture::LandingPage::LandingPage(void)" (??0LandingPage@FOVEImageCapture@@$$FQE$AAM@XZ)   FOVEImageCapture    C:\Users\kapil\Desktop\Project1\Project1\MyForm.obj 1   
      

      如果我在private中保留给定的行并注释掉构造函数中的行,则代码会运行。

      如果我制作非托管课程&#39;对象作为非指针,我得到错误:

      Severity    Code    Description Project File    Line    Suppression State
      Error (active)  E2244   a member of a managed class cannot be of a non-managed class type   FOVEImageCapture    c:\Users\kapil\Desktop\Project1\Project1\MyForm.h   55  
      
      Severity    Code    Description Project File    Line    Suppression State
      Error   C4368   cannot define 'dataCapture' as a member of managed 'FOVEImageCapture::LandingPage': mixed types are not supported   FOVEImageCapture    c:\users\kapil\desktop\project1\project1\myform.h   55  
      
      Severity    Code    Description Project File    Line    Suppression State
      Error   C2663   'datacaptureapi::DataCapture::DataCapture': 2 overloads have no legal conversion for 'this' pointer FOVEImageCapture    c:\users\kapil\desktop\project1\project1\myform.h   55  
      

0 个答案:

没有答案