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

  • SEARCH
  • Home
  • 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 7728827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:48:57+00:00 2026-06-01T05:48:57+00:00

In C++, I create this class: public ref class UConfig { public: UConfig(short nr);

  • 0

In C++, I create this class:

public ref class UConfig
{
    public:
       UConfig(short nr);
       ~UConfig();
       bool checkNr();

    private:
      short _nr;
}

and a public class in C++ that will be called in C#:

public ref class UConfigHandler
{
    public:
        UConfigHandler(UConfig^ uConfig);        
}

Then in C#, I can do it like this:

UConfig uConfig = new UConfig(1);
UConfigHandler uConfigHandler = UConfigHandler(uConfig);

in C++, I debug it and I inside the constructor:

UConfigHandler::UConfigHandler(UConfig^ uConfig)
{
    // while debugging I see that uConfig is: System::Object^
    // how to do the conversion from the uConfig to UConfig inside C++
    // I would like to do something like this but I got an exception
    UConfig myConfig = uConfig; // the program is stopped here but I dont know what is the error
}

So, basically I want to convert the System::Object^ uConfig to the native UConfig. How can I do that?

Something I have done similar with String^ to string:

input is String^

IntPtr stringPointer = (IntPtr)Marshal::StringToHGlobalAnsi(input);

string retrievedString = string((char*)stringPointer.ToPointer());
  • 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-06-01T05:48:59+00:00Added an answer on June 1, 2026 at 5:48 am

    You’re trying to assign a handle of a UConfig instance to a UConfig object. You’ve declared UConfig^ uConfig as a reference, therefore you can only assign it to a reference.

    It would be the equivalent in C++ if you did this:

    MyClass* mcp = new MyClass();
    MyClass mcv = mcp;
    

    In other words, your UConfigHandler constructor should look like this:

    UConfigHandler::UConfigHandler(UConfig^ uConfig)
    {
        UConfig^ myConfig = uConfig;
    }
    

    Update

    You might be able to do it… you can marshal a struct so you should be able to marshal a class too. I haven’t done that, but the documentation of Marshal.StructureToPtr gives a similar example:

    // Initialize unmanged memory to hold the struct.
    IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(uConfig));
    
    // Copy the struct to unmanaged memory.
    Marshal.StructureToPtr(uConfig, pnt, false);
    
    // Create another UConfig.
    UConfig myConfig ;
    
    // Set this UConfig to the value of the 
    // UConfig in unmanaged memory. 
    myConfig = (UConfig)Marshal.PtrToStructure(pnt, typeof(UConfig));
    

    However, you can no longer take advantage of the garbage collection: you’ve allocated unmanaged memory so you’ll also have to free it! If you don’t free the allocated memory you will get a memory leak, so don’t roget to do this:

    // Free the unmanaged memory.
    Marshal.FreeHGlobal(pnt);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a class that's something like this: public class MovieTheaterList { public DateTime
I have a db that is created like this... public class DataBaseManager extends SQLiteOpenHelper{
I have this class that have a function to load other classes and create
I have a .NET class library that I wrote in C++. public ref class
For example I now created a this tiny class: public static class FileSystemInfoComparers<T> where
Say I've got a domain model created from C# classes like this: public class
I have created my own Attached Property like this: public static class LabelExtension {
I need to create a class to handle audio AudioTrack. In this class must
How do i create aliases in c# Take this scenario class CommandMessages { string
i used to create an instance of a singleton class like this: $Singleton =

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.