Note that there are some explanatory texts on larger screens.

plurals
  1. POC#/C++ pInvoke tips
    primarykey
    data
    text
    <p>Whats the best way to go about modifying a C++ program to be used with pInvoke from C#.NET CF?</p> <p>I have a large C++ codebase which makes extensive use of STL. Namely iterators, container classes, and standard strings.</p> <p>On top of this many lightweight classes are passed around by value.</p> <p>I want to build a C# gui ontop of this codebase for windows mobile touch devices.</p> <p>Is it worth it?</p> <p>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?</p> <p>BTW. Managed C++ is not an option becuase its not supported in any form by .NET CF.</p> <p>--edit: I have one specific question in regards to pinvoke.</p> <p>Suppose you had a function returning a C++ string by value</p> <pre><code>std::string foo () { return std::string ("Hi"); } </code></pre> <p>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.</p> <pre><code>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. } </code></pre> <p>So my problem is, how to wrap foo into a pInvoke suitable format without needing to re-allocate the whole string.</p> <pre><code>char* foo2 () { std::string f = foo(); char* waste = new char[f.length()+1]; strcpy (waste, f.c_str()); return f; } </code></pre> <p>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#.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload