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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:01:00+00:00 2026-05-11T19:01:00+00:00

I have a C++ class I’d like to access from a C# application. I’ll

  • 0

I have a C++ class I’d like to access from a C# application. I’ll need to access the constructor and a single member function. Currently the app accepts data in the form of stl::vectors but I can do some conversion if that’s not likely to work?

I’ve found a few articles online which describe how to call C++ DLLs and some others which describe how to make .dll projects for other purposes. I’m struggling to find a guide to creating them in Visual Studio 2008 for use in a C# app though (there seem to be a few for VS 6.0 but the majority of the options they specify don’t seem to appear in the 2008 version).

If anyone has a step-by-step guide or a fairly basic example to get going from, I’d be very grateful.

  • 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-11T19:01:00+00:00Added an answer on May 11, 2026 at 7:01 pm

    The easiest way to interoperate between C++ and C# is by using managed C++, or C++/CLI as it is called. In VisualStudio, create a new C++ project of type “CLR Class Library”. There is some new syntax for the parts that you want to make available to C#, but you can use regular C++ as usual.

    In this example, I’m using std::vector<int> just to show that you can use standard types – however, in an actual application, I’d prefer to use the .NET types where possible (in this case a System::Collections::Generic::List<int>).

    #pragma unmanaged
    #include <vector>
    #pragma managed
    
    public ref class CppClass
    {
    public:
       CppClass() : vectorOfInts_(new std::vector<int>)
       {}
    
       // This is a finalizer, run when GC collects the managed object
       !CppClass()
       { delete vectorOfInts_; }
    
       void Add(int n)
       { vectorOfInts_->push_back(n); }
    
    private:
        std::vector<int>* vectorOfInts_;
    };
    

    EDIT: Changed the class to hold the vector by pointer instead of by value.

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

Sidebar

Related Questions

I have class with a member function that takes a default argument. struct Class
I have class User that contains protected constructor and class Account that has access
I have class with member functions: typedef std::function<bool (const std::string &)> InsertFunction; bool insertSourceFile(
I have class A: public class ClassA<T> Class B derives from A: public class
I have class GUI so I can create object like this: GUI g1 =
I have class instantiated in a web service that, in a static member, holds
I have class methods like these in a message model: def delete_all_users_messages(user_id, parent_id) message
I have class Fruit with data and logic. Now I need a lot of
I have class Base and classes Derived_1 , Derived_2 ... I need derived classes
I have class MyForm which inherited from QMainWindow . Here's my code: std::auto_ptr<MyForm> pForm(new

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.