0

I have written two managed C++ wrappers for native C++ classes and I need a unmanaged object of native Class B as a return param in function of managed Wrapper A that construct native Class A!

Example:

// Wrapper A

WrapperA::WrapperA(ClassB *classB)
{
    ClassA *classA = new ClassA(classB);
    ...
}

// native c++
ClassA::ClassA(ClassB *classB)
{
    m_classB = classB; // ClassB *m_classB; in .h
    ...
}

// Wrapper B

ClassB* WrapperB::GetNativeClassB()
{
    return m_classB; // ClassB *m_classB; in .h
}


// in C#
...
WrapperB wrapperB = new WrapperB();

unsafe // need for C++ pointer
{
WrapperA wrapperA = new WrapperA(wrapperB.GetNativeClassB() ); 
// Error: is inaccessible due to its protection level 
// -> is set to public
}
...

Is there a better way without unsafe and why I get an access error ???

Thank you in advance!

greets leon22

2
  • Are you sure that both WrapperB::GetNativeClassB and WrapperA::WrapperA are public? Commented Jul 12, 2011 at 11:40
  • Constr of Wrapper A/B is public and also GetNativeClassB is set to public Commented Jul 12, 2011 at 11:50

2 Answers 2

1
  1. Protection level: i'm sure you have public defined, but what about the dll containing the symbol? Are you sure you have the last release?

  2. Unsafe: in order to use/wrap unsafe/native code as C++, the best option it is to use C++/CLI (ex Managed C++), provided starting from the Visual Studio 2005 release. just define a ref class that wraps your native/unmanaged class, that one will be directly accessible from managed code, as C#. Hint to start with Visual Studio: open a new dll CLR project from the Visual C++ section;

C++/CLI is the best solution in my opinion

Sign up to request clarification or add additional context in comments.

8 Comments

I'm using the classB object already in WrapperB (loading from a dll with GetProcAddress) and it works fine, but returning the object to C# and bypass to WrapperA does not work (due the protection error)
And sure the wrappers are written in C++/CLI
I think you do not write properly your C++/CLI wrapper classes, because you use unsafe pointer in your wrapper interface (WrapperA(ClassB *classB) ), consequently you need to use the unsafe block. exemple for the contructor of wrapperA-> WrapperA::WrapperA(WrapperB ^classB). use the native pointers only inside the wrapper class
I have tested on other way: give the WrapperB with managed ^ as param to WrapperA and then I call wrapperB->GetNativeClassB() -> error: error C3767: candidate function(s) not accessible (method and class are public) I don't understand this (the wrappers are included per add reference in C#)
Ok, the last one, i hope it can help you. have you already tried #pragma make_public(ClassB)? msdn.microsoft.com/en-us/library/ms235607%28VS.80%29.aspx.
|
0

Solution from: Pass a C++/CLI wrapper of a native type to another C++/CLI assembly

// in WrapperB
property IntPtr _classB 
{
    IntPtr get() { return IntPtr(classB); }
}

// in WrapperA
ClassB *classB = static_cast<ClassB*>(wrapperB->_classB.ToPointer());
// ... do something ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.