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

The Archive Base Latest Questions

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

Whats the best way to go about modifying a C++ program to be used

  • 0

Whats the best way to go about modifying a C++ program to be used with pInvoke from C#.NET CF?

I have a large C++ codebase which makes extensive use of STL. Namely iterators, container classes, and standard strings.

On top of this many lightweight classes are passed around by value.

I want to build a C# gui ontop of this codebase for windows mobile touch devices.

Is it worth it?

I have managed to get some examples working using pInvoke to call C++ code from C#.NET, but writing wrappers for every access point and for all the STL return types seems very daunting and ugly. Is there another way or am I somewhat stuffed?

BTW. Managed C++ is not an option becuase its not supported in any form by .NET CF.

–edit:
I have one specific question in regards to pinvoke.

Suppose you had a function returning a C++ string by value

std::string foo () {
   return std::string ("Hi");
}

I understand that its not possible to call this function from C# using pinvoke because there is no way to marshall the STL string, but my problem is that I cannot even write a wrapper without allocating a new array becuase the std::string returned is not on the heap.

char* foo2 () {
   return foo().c_str(); //Very bad
   //the returned pointer is released when the std::string falls out of scope.
   //and will be invalid before the caller is able to do anything with it.
}

So my problem is, how to wrap foo into a pInvoke suitable format without needing to re-allocate the whole string.

char* foo2 () {
   std::string f = foo();
   char* waste = new char[f.length()+1];
   strcpy (waste, f.c_str());
   return f;
}

The thought of doing the above for every point at which I need to return a std::string is enough to make me give up trying to use 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-11T19:50:12+00:00Added an answer on May 11, 2026 at 7:50 pm

    I would personally say it’s worth it, but I agree with the other post that it’s not easy.

    Possible approaches may be:

    1. C interface around the C++ and exposed as a DLL.
    2. COM objects (altho WM does not support COM servers so you are forced to use in-proc servers, basically a DLL COM implementation). This would give you a more OO interface.
    3. Background process exposing some sort of API. You could go down the “CE Services” modal or come up with your own API.

    All are possible and have there pros and cons. Whatever you do, you can’t use STL types in the interface. You have reduced to simple basic types that are easy to marshal between the processes. Since your talking about C#, then COM may be go as you can expose OO interface.

    I would recommend trying to keep the interface between the two as simple as possible.

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

Sidebar

Ask A Question

Stats

  • Questions 234k
  • Answers 234k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Alex Harui from Adobe gave some extremely helpful answers on… May 13, 2026 at 5:59 am
  • Editorial Team
    Editorial Team added an answer It is using POSIX threads. The problem is the GIL.… May 13, 2026 at 5:59 am
  • Editorial Team
    Editorial Team added an answer No you can't just do that because Win32 console doesn't… May 13, 2026 at 5:59 am

Related Questions

I have a rather large project involving spring and hibernate. Right now, I'm backing
I am maintaining an application which has been designed like this: messy code --abuses-->
Whats the best way to go about upgrading a project with major design changes...in
What's the best way to go about validating that a document follows some version

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.