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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:39:16+00:00 2026-05-17T16:39:16+00:00

I have a large application written in native C++. I also have a class

  • 0

I have a large application written in native C++. I also have a class in C# that I need to call.

If the C# class was static, then it would be trivial (there’s lots of examples on the web) – just write the mixed C++/CLI wrapper, export the interfaces, and you’re done.

However, the C# class is non-static, and can’t be changed to static as it has an interface (the compiler will generate an error if you attempt to make the C# class static).

Has anyone run into this problem before – how do I export a non-static C# class to native C++?


Update 2010-11-09

Final solution: tried COM, this worked nicely but didn’t support structures. So, went with a C++/CLI wrapper, because I absolutely needed to be able to pass structures between C++ and C#. I wrote a mixed mode .dll wrapper based on the code here:

  • How to: Marshal Arrays Using C++ Interop

  • How to: Marshal Structures Using C++ Interop

As the target class was non-static, I had to use the singleton pattern to make sure I was only instantiating one copy of the target class. This ensured everything was fast enough to meet the specs.

Contact me if you want me to post a demo project (although, to be fair, I’m calling C# from C++, and these days most people want to call C++ from C#).

  • 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-17T16:39:17+00:00Added an answer on May 17, 2026 at 4:39 pm

    C++/CLI or COM interop work just as well with non-static classes as with static. Using C++/CLI you just reference your assembly that holds the non-static class and then you can use gcnew to obtain a reference to a new instance.

    What makes you think that this is not possible with your non-static class?

    EDIT: there is example code here.

    using namespace System;
    
    public ref class CSquare
    {
    private:
        double sd;
    
    public:
        CSquare() : sd(0.00) {}
        CSquare(double side) : sd(side) { }
        ~CSquare() { }
    
        property double Side
        {
        double get() { return sd; }
        void set(double s)
        {
            if( s <= 0 )
            sd = 0.00;
            else
            sd = s;
        }
        }
    
        property double Perimeter { double get() { return sd * 4; } }
        property double Area { double get() { return sd * sd; } }
    };
    
    array<CSquare ^> ^ CreateSquares()
    {
        array<CSquare ^> ^ sqrs = gcnew array<CSquare ^>(5);
    
        sqrs[0] = gcnew CSquare;
        sqrs[0]->Side = 5.62;
        sqrs[1] = gcnew CSquare;
        sqrs[1]->Side = 770.448;
        sqrs[2] = gcnew CSquare;
        sqrs[2]->Side = 2442.08;
        sqrs[3] = gcnew CSquare;
        sqrs[3]->Side = 82.304;
        sqrs[4] = gcnew CSquare;
        sqrs[4]->Side = 640.1115;
    
        return sqrs;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.