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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:15:13+00:00 2026-05-19T05:15:13+00:00

I have an native C++ dll, some header files and the import library. Is

  • 0

I have an native C++ dll, some header files and the import library. Is there a way how to instantiate an object within C# that is defined in the dll?

The two ways I’m aware of are:

  1. to wrap the C++ code into COM
  2. to use DLLImport and external C functions
  • 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-19T05:15:14+00:00Added an answer on May 19, 2026 at 5:15 am

    C++/CLI is your friend for this. You’ll run into one problem though: it is not possible to store standard C++ objects inside C++/CLI ref or value classes (the ones for .NET). So you’ll have to resort to the following class (that you can modify) that I use in production code:

    #pragma once
    #include <boost/shared_ptr.hpp>
    
    template <typename T>
    ref class Handle
    {
        boost::shared_ptr<T>* t;
    
        !Handle() 
        {
            if (t != nullptr)
            {
                delete t;
                t = nullptr;
            }
        }
    
        ~Handle() { this->!Handle(); }
    
    public:
        Handle() : t(new boost::shared_ptr<T>((T*)0)) {}
    
        Handle% operator=(T* p)
        {
            if (p != t->get()) t->reset(p);
            return *this;
        }
    
        static T* operator&(Handle% h) { return h.t->get(); }
        static boost::shared_ptr<T> operator->(Handle% h) { return *h.t; }
    
        T& reference() { return *t->get(); }
        T const& const_reference() { return *t->get(); }
    };
    

    Usage: Handle<MyCppClass>^ handle; inside a C++/CLI class. You then implement stub methods, forwarding them to the handle member. Garbage collected objects will call destructors of the C++ class instance iff there is no more pointer to it:

    public ref class Foo
    {
        void bar() { handle->bar(); }
    
    internal:
        Handle<CppNamespace::Foo>^ handle;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a managed dll that calls into a native library. This native library
I have some applications (some native, some .NET) which use manifest files so that
I have a native release dll that is built with symbols. There is a
I have a function in a native DLL defined as follows: #include <string> void
I have a native VC++ project that uses a dll (which is not in
I have a native C DLL being invoked by a C++/CLI object which is
I have a native C Dll that calls 'LoadLibrary' to load another Dll that
I have a 32-bit .net application that uses a native 32-bit DLL via DllImport().
I have a c# .net 2.0 CF application that interfaces with a native DLL
I have a .NET application that makes calls to a native Win32 DLL using

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.