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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:17:47+00:00 2026-05-29T07:17:47+00:00

I am learning how to call methods in C++ from C#. I have done

  • 0

I am learning how to call methods in C++ from C#. I have done some research and it seems Pinvoke is a nice way.

How can I translate this simple C++ code to the way how it should be in order to be called in C#, and how can I write the methods to be called in C#?

I have a header file:

MathFuncsLib.h

namespace MathFuncs
{
    class MyMathFuncs
    {
         public:            
       double Add(double a, double b);
       MyMathFuncs getClass();
    };
}

MathFuncsLib.cpp

#include "MathFuncsLib.h"
namespace MathFuncs
{
   MyMathFuncs MyMathFuncs::getClass() {
       return *(new MyMathFuncs());
   }

   double MyMathFuncs::Add(double a, double b) {
       return a + b;
   }
}

In C#,

I would like to have:

main()
{
    MyMathFuncs abd = MyMathFuncs::getClass();
    abd.Add(1.2, 2.3);
}

I have no clue how it should be achieved, so I think it is better to ask about it.

  • 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-29T07:17:48+00:00Added an answer on May 29, 2026 at 7:17 am

    P/Invoke is more suited when you have a lot of static functions. You could instead use C++/CLI, which is more appropriate when you have a set of C++ classes, or a structured domain model hierarchy of classes. This is how you could do it with your sample:

    The .H:

    namespace MathFuncs {
    
        public ref class MyMathFuncs
        {
            public:            
                double Add(double a, double b);
        };
    }
    

    The .CPP:

    namespace MathFuncs
    {
       double MyMathFuncs::Add(double a, double b) {
           return a + b;
       }
    }
    

    The .CS:

    static class Program
    {
        static void Main()
        {
            MyMathFuncs abd = new MyMathFuncs();
            abd.Add(1.2, 2.3);
        }
    }
    

    As you see, you don’t need the getClass function as with C++/CLI, your MathFuncs class becomes a full-blown .NET class.

    EDIT: if you want the actual computation (a+b in the sample) to happen in unmanaged code, you could do it like this; for example:

    The .H:

    namespace MathFuncs {
    
        public ref class MyMathFuncs
        {
            public:            
                double Add(double a, double b);
        };
    
        class MyMathFuncsImpl
        {
            public:            
                double Add(double a, double b);
        };
    }
    

    The .CPP:

    namespace MathFuncs
    {
        double MyMathFuncs::Add(double a, double b) {
            MyMathFuncsImpl *p = new MyMathFuncsImpl();
            double sum = p->Add(a, b);
            delete p;
            return sum;
        }
    
    #pragma managed(push, off)
        double MyMathFuncsImpl::Add(double a, double b) {
            return a + b;
        }
    #pragma managed(pop)
    }
    

    In this case, MyMathFuncsImpl::Add is generated as native code, not as IL (calling from C# is the same). See here: managed, unmanaged for more on how to mix managed and unmanaged code.

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

Sidebar

Related Questions

I have a conundrum, I do know how to call method from other activity..
I just started learning C++ couple of weeks ago. So now I have this
I want to call a C subroutine from Java. I'm using JNI. I have
This is a learning exercise in expression trees. I have this working code: class
I just started learning objective c. I have this little problems and questions, hope
I'm learning Java and I have a problem that I can't understand or solve!
As someone learning Java from scratch, who knows a little Python, I have been
I'm learning JS prototypes. From Java language point I expect,that SpecificRectangle object will have
Learning from my last question , most member names seem to get included in
Learning Scala currently and needed to invert a Map to do some inverted value->key

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.