title: Passing String from C# to native C++ Library in 1256 Encoding via CLI
body: <p>i have to use an old C++ Library in a C# Project (.Net Framework 4).</p>
  
  <p>In general i have everything set up and running but passing string values to the library is causing problems for me.</p>
  
  <p>I receive string values in utf-8 containing arabic characters.
  The library is handling arabic character in iso-1256 encoding.</p>
  
  <p>No matter what i try i always end up with just a bunch of ??? whenever i am passing arabic strings to the library.</p>
  
  <p>My approach is to convert the utf-8 encoded string to iso-1256 in the C# code and pass the result to the C++ library.</p>
  
  <p>The conversion in C# looks like this:</p>
  
  <pre><code>var bytes = encUtf8.GetBytes((string)value);                   
  String value1256 = enc1256.GetString(Encoding.Convert(encUtf8, enc1256, bytes));
  </code></pre>
  
  <p>I then proceed to pass value1256 to the library.</p>
  
  <p>The CLI function called will accept a String^ sVal parameters.
  The legacy code is using CString internally so i have to convert the string and here lies my problem. 
  No matter how i convert the string i am ending up with just ???.</p>
  
  <p>Here is a list of conversions i have tried so far, all resulting in the same output. When i check the debugger the original sVal is displayed correctly for arabic chars but every single conversion listed below results in just ???:</p>
  
  <pre><code>pin_ptr&lt;const wchar_t&gt; wch = PtrToStringChars(sVal);
              CString cstring6(wch);
              wchar_t* A= ( wchar_t* )( Marshal::StringToHGlobalAnsi(sVal).ToPointer() );
  
              std::string stdString = marshal_as&lt;std::string&gt;(sVal); 
              CStringT&lt;char,StrTraitMFC_DLL&lt;char,ATL::ChTraitsCRT&lt;char&gt;&gt;&gt; cStringT = CStringT&lt;char,StrTraitMFC_DLL&lt;char,ATL::ChTraitsCRT&lt;char&gt;&gt;&gt;( sVal );
              CStringT&lt;char,StrTraitMFC_DLL&lt;char,ATL::ChTraitsCRT&lt;char&gt;&gt;&gt; cStringT2 = CStringT&lt;char,StrTraitMFC_DLL&lt;char,ATL::ChTraitsCRT&lt;char&gt;&gt;&gt;( A );
              CString cString1 = CString( sVal );
              CString cString2 = CString(stdString.c_str());
              CString cString3 = stdString.c_str();
              CString cString4 = CString( _T( stdString.c_str() ));
              CString cString5 = CString( sVal );
  
              marshal_context^ context = gcnew marshal_context();
              const char* c_s = context-&gt;marshal_as&lt;const char*&gt;(sVal);
  </code></pre>
  
  <p>Obviously i am doing something wrong here.
  Please be so kind and point me in the right direction here.</p>