在C#托管类的方法中,如何传递C++;上课? 我正在研究一个联合项目,需要包装一个C++ DLL并从C中引用它。

在C#托管类的方法中,如何传递C++;上课? 我正在研究一个联合项目,需要包装一个C++ DLL并从C中引用它。,c#,c++,unmanaged,managed,C#,C++,Unmanaged,Managed,我不是微软技术背景的人,所以如果我的术语不正确,或者这是一个直接的回答,或者是一个已经回答了的问题,我真的很抱歉,但是我肯定找不到这个答案,尽管搜索得很好 到目前为止,另一方已经完成了将相关DLL转换为托管DLL的工作。这使我能够将其作为我的C#项目的参考(我使用的是VS2017)。另一方还向我发送了一个C#示例(仅实现了一个“Clear”方法),说明如何提供包装器,如下所示: #pragma once #include "cgcl.h" using namespace System; usi

我不是微软技术背景的人,所以如果我的术语不正确,或者这是一个直接的回答,或者是一个已经回答了的问题,我真的很抱歉,但是我肯定找不到这个答案,尽管搜索得很好

到目前为止,另一方已经完成了将相关DLL转换为托管DLL的工作。这使我能够将其作为我的C#项目的参考(我使用的是VS2017)。另一方还向我发送了一个C#示例(仅实现了一个“Clear”方法),说明如何提供包装器,如下所示:

#pragma once
#include "cgcl.h"

using namespace System;
using namespace ThirdParty_DOPRL;

namespace ThirdParty_GCL 
  {
    public ref class CGCL_MAN
      {
        public:
            // Allocate the native object on the C++ Heap via a constructor
            CGCL_MAN() : m_pGCL( new CGCL ) {}

            // Deallocate the native object on a destructor
            ~CGCL_MAN() {
              delete m_pGCL;
            }

        protected:
            // Deallocate the native object on the finalizer just in case no destructor is called
            !CGCL_MAN() {
                delete m_pGCL;
            }

        public:
            void Clear() {
                return m_pGCL->Clear();
            }

        private:
            CGCL * m_pGCL;
      };
  }
  STATUS GetListOfDevProperties(CDeviceObjectPropertyReferenceList &DevicePropertyList) const;
#pragma once

#include "cdoprl.h"

using namespace System;

namespace ThirdParty_DOPRL
{
    public ref class CDOPRL_MAN
    {
    public:
        // Allocate the native object on the C++ Heap via a constructor
        CDOPRL_MAN() : m_pDevObjPropRefList(new CDeviceObjectPropertyReferenceList) {}

        // Deallocate the native object on a destructor
        ~CDOPRL_MAN() {
            delete m_pDevObjPropRefList;
        }

    protected:
        // Deallocate the native object on the finalizer just in case no destructor is called
        !CDOPRL_MAN() {
            delete m_pDevObjPropRefList;
        }

    public:
        bool IsEmpty() {
            return m_pDevObjPropRefList->empty();
        }

        void Clear() {
            return m_pDevObjPropRefList->clear();
        }

        size_t Size() {
            return m_pDevObjPropRefList->size();
        }

    private:
        CDeviceObjectPropertyReferenceList * m_pDevObjPropRefList;
    };
}
            STATUS GetListOfDevProperties(CDOPRL_MAN devPropList) {
                return m_pGCL->GetListOfDevProperties(devPropList);
            }
到目前为止,所有构建都正常工作(即从C#调用Clear()似乎正在执行)

现在,我想为来自同一非托管类的另一个方法添加存根,如上所述。其在原始头文件中的签名如下:

#pragma once
#include "cgcl.h"

using namespace System;
using namespace ThirdParty_DOPRL;

namespace ThirdParty_GCL 
  {
    public ref class CGCL_MAN
      {
        public:
            // Allocate the native object on the C++ Heap via a constructor
            CGCL_MAN() : m_pGCL( new CGCL ) {}

            // Deallocate the native object on a destructor
            ~CGCL_MAN() {
              delete m_pGCL;
            }

        protected:
            // Deallocate the native object on the finalizer just in case no destructor is called
            !CGCL_MAN() {
                delete m_pGCL;
            }

        public:
            void Clear() {
                return m_pGCL->Clear();
            }

        private:
            CGCL * m_pGCL;
      };
  }
  STATUS GetListOfDevProperties(CDeviceObjectPropertyReferenceList &DevicePropertyList) const;
#pragma once

#include "cdoprl.h"

using namespace System;

namespace ThirdParty_DOPRL
{
    public ref class CDOPRL_MAN
    {
    public:
        // Allocate the native object on the C++ Heap via a constructor
        CDOPRL_MAN() : m_pDevObjPropRefList(new CDeviceObjectPropertyReferenceList) {}

        // Deallocate the native object on a destructor
        ~CDOPRL_MAN() {
            delete m_pDevObjPropRefList;
        }

    protected:
        // Deallocate the native object on the finalizer just in case no destructor is called
        !CDOPRL_MAN() {
            delete m_pDevObjPropRefList;
        }

    public:
        bool IsEmpty() {
            return m_pDevObjPropRefList->empty();
        }

        void Clear() {
            return m_pDevObjPropRefList->clear();
        }

        size_t Size() {
            return m_pDevObjPropRefList->size();
        }

    private:
        CDeviceObjectPropertyReferenceList * m_pDevObjPropRefList;
    };
}
            STATUS GetListOfDevProperties(CDOPRL_MAN devPropList) {
                return m_pGCL->GetListOfDevProperties(devPropList);
            }
因此,我做的第一件事是围绕非托管类CDeviceObjectPropertyReferenceList创建另一个包装器类,如下所示:

#pragma once
#include "cgcl.h"

using namespace System;
using namespace ThirdParty_DOPRL;

namespace ThirdParty_GCL 
  {
    public ref class CGCL_MAN
      {
        public:
            // Allocate the native object on the C++ Heap via a constructor
            CGCL_MAN() : m_pGCL( new CGCL ) {}

            // Deallocate the native object on a destructor
            ~CGCL_MAN() {
              delete m_pGCL;
            }

        protected:
            // Deallocate the native object on the finalizer just in case no destructor is called
            !CGCL_MAN() {
                delete m_pGCL;
            }

        public:
            void Clear() {
                return m_pGCL->Clear();
            }

        private:
            CGCL * m_pGCL;
      };
  }
  STATUS GetListOfDevProperties(CDeviceObjectPropertyReferenceList &DevicePropertyList) const;
#pragma once

#include "cdoprl.h"

using namespace System;

namespace ThirdParty_DOPRL
{
    public ref class CDOPRL_MAN
    {
    public:
        // Allocate the native object on the C++ Heap via a constructor
        CDOPRL_MAN() : m_pDevObjPropRefList(new CDeviceObjectPropertyReferenceList) {}

        // Deallocate the native object on a destructor
        ~CDOPRL_MAN() {
            delete m_pDevObjPropRefList;
        }

    protected:
        // Deallocate the native object on the finalizer just in case no destructor is called
        !CDOPRL_MAN() {
            delete m_pDevObjPropRefList;
        }

    public:
        bool IsEmpty() {
            return m_pDevObjPropRefList->empty();
        }

        void Clear() {
            return m_pDevObjPropRefList->clear();
        }

        size_t Size() {
            return m_pDevObjPropRefList->size();
        }

    private:
        CDeviceObjectPropertyReferenceList * m_pDevObjPropRefList;
    };
}
            STATUS GetListOfDevProperties(CDOPRL_MAN devPropList) {
                return m_pGCL->GetListOfDevProperties(devPropList);
            }
现在回到ThirdParty_GCL(在本文顶部),我假设必须在存根中使用托管类类型,并声明存根如下:

#pragma once
#include "cgcl.h"

using namespace System;
using namespace ThirdParty_DOPRL;

namespace ThirdParty_GCL 
  {
    public ref class CGCL_MAN
      {
        public:
            // Allocate the native object on the C++ Heap via a constructor
            CGCL_MAN() : m_pGCL( new CGCL ) {}

            // Deallocate the native object on a destructor
            ~CGCL_MAN() {
              delete m_pGCL;
            }

        protected:
            // Deallocate the native object on the finalizer just in case no destructor is called
            !CGCL_MAN() {
                delete m_pGCL;
            }

        public:
            void Clear() {
                return m_pGCL->Clear();
            }

        private:
            CGCL * m_pGCL;
      };
  }
  STATUS GetListOfDevProperties(CDeviceObjectPropertyReferenceList &DevicePropertyList) const;
#pragma once

#include "cdoprl.h"

using namespace System;

namespace ThirdParty_DOPRL
{
    public ref class CDOPRL_MAN
    {
    public:
        // Allocate the native object on the C++ Heap via a constructor
        CDOPRL_MAN() : m_pDevObjPropRefList(new CDeviceObjectPropertyReferenceList) {}

        // Deallocate the native object on a destructor
        ~CDOPRL_MAN() {
            delete m_pDevObjPropRefList;
        }

    protected:
        // Deallocate the native object on the finalizer just in case no destructor is called
        !CDOPRL_MAN() {
            delete m_pDevObjPropRefList;
        }

    public:
        bool IsEmpty() {
            return m_pDevObjPropRefList->empty();
        }

        void Clear() {
            return m_pDevObjPropRefList->clear();
        }

        size_t Size() {
            return m_pDevObjPropRefList->size();
        }

    private:
        CDeviceObjectPropertyReferenceList * m_pDevObjPropRefList;
    };
}
            STATUS GetListOfDevProperties(CDOPRL_MAN devPropList) {
                return m_pGCL->GetListOfDevProperties(devPropList);
            }
但是,当然,VS警告我,在上面的返回语句中提到devPropList是无效的(“简单的非跟踪引用不能绑定到托管堆上的实体”)——简单来说,这意味着我非法混合了对非托管类和托管类的引用

简而言之,有没有一种方式可以在原来的C++ DLL?

所要求的托管CoprrLHman类类型与非托管CdVice对象属性的引用类型之间转换?
希望我提供了足够的信息。

您是否错过了符号?它在第一个例子中,而不是在第二个例子中。谢谢,但没有乐趣。我不擅长C++,我尝试了所有的参数组合,在声明和返回语句中都预先设置了参数,所有这些都与标记无效。我认为问题是变量位于哪里。变量不能在C++执行堆栈上(参数列表或返回对象)。变量必须在全局内存中。本条可能会中止: