c++使用抽象类到包装器中的本机代码

发布于 2024-12-05 14:34:40 字数 1862 浏览 6 评论 0原文

我需要为我的非托管代码实现 ac# GUI。所以我设计了一个包装器来处理我的本机代码,但这不能正常工作。

我有一个方法需要为抽象类创建一个实例,但我不知道如何处理它。

首先,对于我的 C++ 类,我使用了一个抽象类:

 class Interface abstract
    {
       public:      Interface (void);
       public:      ~Interface (void);

       public:      virtual double Get() = 0;   
    };

根据我当前的应用程序,我使用了 ClassSpecific1 和 ClassSpecific2,并且我从每个抽象类继承了函数。

    class ClassSpecific1 : public Interface
    {
       public:      ClassSpecific1 (void);
       public:      ~ClassSpecific1 (void);

       private:     double Get();//based on the abstract class

    };


    class ClassSpecific2 : public Interface
    {
       public:      ClassSpecific2 (void);
       public:      ~ClassSpecific2 (void);

       private:     double Get();//based on the abstract class

    };

后来我使用了另一个类,它作为通用类工作,并使用 ClassSpecific1 或 ClassSpecific2 与抽象类的实例。

    class ClassAPI
    {
       public:      ClassLaserAPI(void);
       public:      ~ClassLaserAPI(void);

       public:      double Get(Interface *objToInterface);//This Get() calls the Get() in ClassSpecific1 or ClassSpecific2 
    };

直到这里一切似乎都还好。我已经测试了一切并按预期工作。我的大问题是我不知道如何将我的方法 Get(Interface *objToInterface) 从 ClassAPI 放入我的包装器中。我是否需要首先为我的抽象类创建一个包装器,以便能够在包装器上创建实例**(Interface *objToInterface)**

这就是我到目前为止所拥有的,我希望有人能给我一些帮助,我迷失了如何继续。


    namespace API
    {

            public __gc class APIManaged
            {
                    public: APIManaged(void);
                    public: ~APIManaged(void);

        /** Unmanaged pointer to ClassAPI 
         *  
         */
                    private:    ClassAPI __nogc* cAPI;

                    public:     double Get(Interface __nogc* objInterface);
            };

I need to implement a c# GUI for my unmanaged code. So i have designed a wrapper to deal with my native code, but this does not work porperly.

I have a method which requires to make an instance to an abstract class and i'm not sure how to deal with it.

First for my C++ classes i used an abstract class:

 class Interface abstract
    {
       public:      Interface (void);
       public:      ~Interface (void);

       public:      virtual double Get() = 0;   
    };

And i used a ClassSpecific1 and a ClassSpecific2 depending on my current application, and i inherited the functions from the abstract class for each one.

    class ClassSpecific1 : public Interface
    {
       public:      ClassSpecific1 (void);
       public:      ~ClassSpecific1 (void);

       private:     double Get();//based on the abstract class

    };


    class ClassSpecific2 : public Interface
    {
       public:      ClassSpecific2 (void);
       public:      ~ClassSpecific2 (void);

       private:     double Get();//based on the abstract class

    };

Later i used another class, it works as a general class and uses the ClassSpecific1 or the ClassSpecific2 with an instance of the abstract class.

    class ClassAPI
    {
       public:      ClassLaserAPI(void);
       public:      ~ClassLaserAPI(void);

       public:      double Get(Interface *objToInterface);//This Get() calls the Get() in ClassSpecific1 or ClassSpecific2 
    };

Until here everything seems all right. I have tested everything and works as expected. My big problem is that i don't know how to make my method Get(Interface *objToInterface) from ClassAPI into my wrapper. Do i need to make a wrapper for my abstract class first in order to be able to create the instance**(Interface *objToInterface)** on the wrapper?

This is what i have so far, i hope someone can give me some help, i'm getting lost in how to proceed.


    namespace API
    {

            public __gc class APIManaged
            {
                    public: APIManaged(void);
                    public: ~APIManaged(void);

        /** Unmanaged pointer to ClassAPI 
         *  
         */
                    private:    ClassAPI __nogc* cAPI;

                    public:     double Get(Interface __nogc* objInterface);
            };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落花随流水 2024-12-12 14:34:40

我也遇到了同样的问题,并使用此主题解决了它:
C++/CLI 继承从具有抽象方法的本机 C++ 类并将其公开给 C#

我也使用非托管抽象类,并按照链接中概述的提示进行操作。我最终遇到了一些错误,需要修复并通过更改非托管项目中的链接器选项来修复它们,以不使用 /clr 进行编译,并且只有包装器项目使用 /clr 进行编译。

发布您的输出错误可能有助于澄清您遇到的一些问题......

I'm also having the same problem and got it resolved using this topic:
C++/CLI Inheriting from a native C++ class with abstract methods and exposing it to C#

I too am using an unmanaged abstract class and got things working by following the tips outlined in the link. I ended up with a few errors to fix and fixed them by changing my linker options in my unmanaged projects to not compile with /clr and only the wrapper project compiles using /clr.

Posting your output errors may help clarify some of the troubles you are having...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文