question

Amernauth-7541 avatar image
0 Votes"
Amernauth-7541 asked ·

Accessing Protected C++ Methods using C++/CLR Wrappers

Hello,

Is there a syntax used in C++/CLI to access protected C++ methods from a C# application?

What I have currently in my MFC header file:


class CMFCApplication1Doc : public CDocument
{
protected: // create from serialization only
CMFCApplication1Doc() noexcept;
DECLARE_DYNCREATE(CMFCApplication1Doc)

// Attributes
public:

// Operations
public:
int OnTest();

// Overrides
public:
virtual BOOL OnNewDocument();
virtual void Serialize(CArchive& ar);

// Implementation
public:
virtual ~CMFCApplication1Doc();

protected:
int TestFunction();

};

And in my Wrapper.cpp

int CSharpMFCWrapper::CSharpMFCWrapperClass::Test()
{
pCC->TestFunction();

    return 0;

}

Accessing public functions pose no problems. Just the protected ones.

The Test() is declared in my Wrapper.h file and the pCC pointer is pointing to the protected C++ method.

I'm trying to not alter the underlying C++ code I am referencing hence the question.

dotnet-csharpc++
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JeanineZhang-MSFT avatar image
0 Votes"
JeanineZhang-MSFT answered ·

Hi,

For subclasses, protected members are accessible. As far as I'm concerned you could access protected members through subclasses.

You could create a new class that derives from your existing unmanaged class, and re-exposes the protected members (TestFunction) as public. Then create a managed class to wrap your newly-derived class, and have it expose those originally-protected members as protected in the managed type hierarchy.

Best Regards,

Jeanine



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi!

This would require no change to the underlying C++ code? All of this can be done within the C++/CLI wrapper?

Do you have some code I can reference?

Thanks!

0 Votes 0 ·

@Amernauth-7541

I suggest you could refer to the link: https://stackoverflow.com/a/29080305/11872808


0 Votes 0 ·