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 6224181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:37:36+00:00 2026-05-24T08:37:36+00:00

I have a method in my native dll, that I want to use. The

  • 0

I have a method in my native dll, that I want to use. The method returns an object of a type that is also in my native dll.I am trying to write a c++/CLI wrapper.

Now,

  • Can I get a return value as the object using C++/CLI and how do I do that?
  • Can we store and pass the native C++ object?
  • Should I need to create my own class resembling the native C++ class?
  • How would I marshal a class?

For Example,My native dll has these classes,

class X
{
    /* some props and methods. */
};


Class Y
{
    X* someMethod();
};

I need to wrap the someMethod class using C++/CLI. Will I be able to get the return value in the CLI?

  • 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-24T08:37:37+00:00Added an answer on May 24, 2026 at 8:37 am

    Returning pointers to C++ objects from an exported function in a DLL is a pretty bad idea. It is a nasty memory management problem, you’d expect the client code to release the object. That can only come to a good end when both DLLs use the exact same version of the DLL version of the CRT (/MD compile option). If you can’t recompile the native DLL then stop right now, you cannot make it work reliably or you’ll have a big maintenance problem in the future.

    Anyhoo, you need a wrapper for both classes. They should resemble this:

    #pragma managed(push, off)
    #include "xandy.h"
    #pragma managed(pop)
    
    using namespace System;
    
    namespace something {
    
        public ref class XWrapper {
            X* mX;
        public:
            XWrapper(X* obj) : mX(obj) {}
            ~XWrapper() { this->!XWrapper(); }
            !XWrapper() {
                // Trouble is here!!!
                delete mX;
            }
        };
    
        public ref class YWrapper {
            Y* mY;
        public:
            YWrapper() { mY = new Y; }
            ~YWrapper() { this->!YWrapper(); }
            !YWrapper() { delete mY; }
            XWrapper^ someMethod() {
                return gcnew XWrapper(mY->someMethod());
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have method that returns Drawable , and if its Bitmap object is recycled
I have COM dll that I am using in C++/Cli, one of the method
i have imported a c++ native dll method in c#. below is how the
I have method that returns module path of given class name def findModulePath(path, className):
I have a c# .net 2.0 CF application that interfaces with a native DLL
We have a native app that we can access via JNI->DLL or by invoking
I am trying to write a C++/CLI wrapper using VS2010 against an C++ API
I have a managed dll that calls into a native library. This native library
I have a Vendor.DLL (Native DLL, written in C++) that exposes many methods. Typically
I have a C++ dll that contains methods that I need to use in

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.