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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:06:41+00:00 2026-05-13T01:06:41+00:00

I’m a bit rusty, actually really rusty with my C++. Haven’t touched it since

  • 0

I’m a bit rusty, actually really rusty with my C++. Haven’t touched it since Freshman year of college so it’s been a while.

Anyway, I’m doing the reverse of what most people do. Calling C# code from C++. I’ve done some research online and it seems like I need to create some managed C++ to form a bridge. Use __declspec(dllexport) and then create a dll from that and use the whole thing as a wrapper.

But my problem is – I’m really having a hard time finding examples. I found some basic stuff where someone wanted to use the C# version to String.ToUpper() but that was VERY basic and was only a small code snippet.

Anyone have any ideas of where I can look for something a bit more concrete? Note, I do NOT want to use COM. The goal is to not touch the C# code at all.

  • 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-13T01:06:42+00:00Added an answer on May 13, 2026 at 1:06 am

    While lain beat me to writing an example, I’ll post it anyhow just in case…

    The process of writing a wrapper to access your own library is the same as accessing one of the standard .Net libraries.

    Example C# class code in a project called CsharpProject:

    using System;
    
    namespace CsharpProject {
        public class CsharpClass {
            public string Name { get; set; }
            public int Value { get; set; }
    
            public string GetDisplayString() {
                return string.Format("{0}: {1}", this.Name, this.Value);
            }
        }
    }
    

    You would create a managed C++ class library project (example is CsharpWrapper) and add your C# project as a reference to it. In order to use the same header file for internal use and in the referencing project, you need a way to use the right declspec. This can be done by defining a preprocessor directive (CSHARPWRAPPER_EXPORTS in this case) and using a #ifdef to set the export macro in your C/C++ interface in a header file. The unmanaged interface header file must contain unmanaged stuff (or have it filtered out by the preprocessor).

    Unmanaged C++ Interface Header file (CppInterface.h):

    #pragma once
    
    #include <string>
    
    // Sets the interface function's decoration as export or import
    #ifdef CSHARPWRAPPER_EXPORTS 
    #define EXPORT_SPEC __declspec( dllexport )
    #else
    #define EXPORT_SPEC __declspec( dllimport )
    #endif
    
    // Unmanaged interface functions must use all unmanaged types
    EXPORT_SPEC std::string GetDisplayString(const char * pName, int iValue);
    

    Then you can create an internal header file to be able to include in your managed library files. This will add the using namespace statements and can include helper functions that you need.

    Managed C++ Interface Header file (CsharpInterface.h):

    #pragma once
    
    #include <string>
    
    // .Net System Namespaces
    using namespace System;
    using namespace System::Runtime::InteropServices;
    
    // C# Projects
    using namespace CsharpProject;
    
    
    //////////////////////////////////////////////////
    // String Conversion Functions
    
    inline
    String ^ ToManagedString(const char * pString) {
     return Marshal::PtrToStringAnsi(IntPtr((char *) pString));
    }
    
    inline
    const std::string ToStdString(String ^ strString) {
     IntPtr ptrString = IntPtr::Zero;
     std::string strStdString;
     try {
      ptrString = Marshal::StringToHGlobalAnsi(strString);
      strStdString = (char *) ptrString.ToPointer();
     }
     finally {
      if (ptrString != IntPtr::Zero) {
       Marshal::FreeHGlobal(ptrString);
      }
     }
     return strStdString;
    }
    

    Then you just write your interface code that does the wrapping.

    Managed C++ Interface Source file (CppInterface.cpp):

    #include "CppInterface.h"
    #include "CsharpInterface.h"
    
    std::string GetDisplayString(const char * pName, int iValue) {
     CsharpClass ^ oCsharpObject = gcnew CsharpClass();
    
     oCsharpObject->Name = ToManagedString(pName);
     oCsharpObject->Value = iValue;
    
     return ToStdString(oCsharpObject->GetDisplayString());
    }
    

    Then just include the unmanaged header in your unmanaged project, tell the linker to use the generated .lib file when linking, and make sure the .Net and wrapper DLLs are in the same folder as your unmanaged application.

    #include <stdlib.h>
    
    // Include the wrapper header
    #include "CppInterface.h"
    
    void main() {
     // Call the unmanaged wrapper function
     std::string strDisplayString = GetDisplayString("Test", 123);
    
     // Do something with it
     printf("%s\n", strDisplayString.c_str());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.