Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6254601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:12:30+00:00 2026-05-24T14:12:30+00:00

I am trying to write a C++/CLI wrapper using VS2010 against an C++ API

  • 0

I am trying to write a C++/CLI wrapper using VS2010 against an C++ API for which I only have access to header files, and a dll/lib file.

I’m running into a problem with the bubbling back up of a native reference from the native code, to the tracking reference in the C# code. I get a SystemAccessViolation. The code is as follows:

Unmanaged Native C++ Headers for native Functions that returns a reference to Create the as well as to Modify the CustomObject:

class ManageCustomObject;
    _declspec(dllimport) errorCode CreateCustomObject(CustomObject& customObject);
    _declspec(dllimport) errorCode ModifyCustomObject(CustomObject& customObject);

Important thing to note about CustomObject is that it has private constructors only:

CustomObject(const CustomObject&) { }
CustomObject& operator=(const CustomObject&) { return *this; }

Now, my C++/CLI layer Method looks like this:

public ref class WrapperManageCustomObject {
public:
        errorCode WrapperCreateCustomObject(CustomObject% customObject)
        {
            return CreateCustomObject(customObject);
       }

        errorCode WrapperModifyCustomObject(CustomObject% customObject)
        {
            return ModifyCustomObject(customObject);
        }
};

And the wrapper for the CustomObject looks like this:

public class WrapperCustomObject : public CustomObject {
    public:
        WrapperCustomObject(const CustomObject&) {};
};

In C# the code goes as follows:

WrapperCustomObject wrapperCustomObject;
WrapperManageCustomObject wrapperManageCustomObject = new WrapperManageCustomObject();

long result;

// this works great
result = wrapperManageCustomObject.CreateCustomObject(ref wrapperCustomObject);

// can utilize wrapperCustomObject here with other native functions no problem, so long as I don't try to modify it...
...

// this throws an AccessViolationException was unhandled message
// essentially I am modifying / returning a different native customObject (though I'm not 100% sure what it does as I do not have access to the code)
result = wrapperManageCustomObject.ModifyCustomObject(ref wrapperCustomObject);

As you can see in my comments, the attempt to modify that object via reference throw an “AccessViolationException was unhandled : Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”

Now, if I eliminate C# and the tracking reference from this equation and do this all within C++ or C++/CLI (so long as the tracking reference is not present), this works fine. I assume this has something to do with native code trying to access updating a reference that is now in managed-land.

Is there any way around any of this to essentially make this code “work” from within C#? There is no way to pass a native reference to C# that I am aware of.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:12:31+00:00Added an answer on May 24, 2026 at 2:12 pm

    Managed references are absolutely nothing like native references. Managed references are more like native pointers. As far as I am aware, you can’t do somthing like public class WrapperCustomObject : public CustomObject (where CustomObject is a native class) and ever expect that to work. When you create a managed class, you are telling the CLR that your class will be well behaved in the face of being moved around in memory. If CustomObject does anything with pointers at all, that’s no longer going to be true. CustomObject does not use managed references, and therefore the runtime has no way of knowing where to update clients if/when that class instance gets moved around.

    Even worse, your wrapper is allowing that class to be instantiated without going through the factory method. Therefore, if the factory method CreateCustomObject does anything important to the object you get back, you’ve violated the contract of the code you’re calling, which asked you to create instances of that object through the factory method only.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying write a query to find records which don't have a matching record
I am trying to write a wrapper library for MIDI functions in WinMM.dll, but
I'm trying to write a blog post which includes a code segment inside a
I am trying to write a unit test for an action method which calls
I'm trying to write a stored procedure to select employees who have birthdays that
I'm trying to write a little CLI Hangman game, just to create something in
I am trying to write an app in python to control a motor using
I'm trying to write a query I don't want to have Cartesian products on.
Im trying to write an algorithm using the algorithm package, but when I use
Im trying to write something which very quickly calculates random numbers and can be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.