Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ CLI Wrapper

Tags:

c++-cli

I’ve a question about creating a C++ CLI Wrapper for a native C++ class to be used in C#.

Here is an example code:

#include "stdafx.h"

#pragma once

using namespace System;

namespace Wrapper {

    class NativeClass
    {
    public:
        NativeClass() {}
        int Add(int a, int b)
        {
            return a+b;
        }
    };

    public ref class Wrapper
    {
    public:
        Wrapper() {pNative = new NativeClass();}
        int Add(int a, int b)
        {
            return(pNative->Add(a,b));
        }
        ~Wrapper()
        {
            delete pNative;
            pNative = 0;
        }
        !Wrapper()
        {
            this->~Wrapper();
        }
        //My problem is here.
        NativeClass* GetNative()
        {
            return pNative;
        }
    private:
        NativeClass* pNative;
    };
}

This code works fine. I need to retrieve the pointer that refers the native class to use it in the other wrapper classes. However, I don’t want the function “GetNative” to be visible in C# when I’m using this wrapper class. How can I hide it?

like image 233
KooKoo Avatar asked Aug 13 '26 22:08

KooKoo


1 Answers

If the other wrapper classes are in the same assembly, make the access internal instead of public. – Roger Rowland Apr 25 '13 at 9:47

.

if they are not in the same assembly? ...

Look into friend assemblies – Sebastian Cabot Feb 1 at 15:43

like image 128
Martin Ba Avatar answered Aug 17 '26 22:08

Martin Ba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!