5 Years
Discussion Span
Last Post by SAM2012

I tried it like this; where I converted a managed string through MFC to native to STL and back to managed.

#include "stdafx.h"
#include <iostream>
#include "DW_414826_CPP_MIXED.h"
#ifdef _DEBUG
#define new DEBUG_NEW
// The one and only application object
CWinApp theApp;
using namespace std;
using namespace System;

int _tmain(void)
   HMODULE hModule = ::GetModuleHandle(NULL);

   if (NULL == hModule)
      _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
      return -1;

   // initialize MFC and print and error on failure
   if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
      // TODO: change error code to suit your needs
      _tprintf(_T("Fatal Error: MFC initialization failed\n"));
      return 1;

   String^ strDataManaged = "This is neat";

   CString strDataMfc = CString(strDataManaged);

   char* pstrDataNative = strDataMfc.GetBuffer(0);
   printf("%s\n", pstrDataNative);

   string strDataStl = CString(strDataManaged).GetBuffer(0);
   cout << strDataStl.c_str() << endl;

   String^ strDataManaged2 = gcnew String(pstrDataNative);
	return 0;

I started with a native Win32 Console app, MFC enabled, /clr enabled and Multi-Byte character set (instead of Unicode).

Edited by thines01: added STL example



The error I am having is 'marshal_as' : undeclared identifier. std::string' : illegal use of this type as an expression


Hi Barcode Maven , since morning I am stuck with it. Please help with Marshal-as error

Dear Tom, your given solution is also not working

Edited by SAM2012: n/a


You have to allocate a native buffer for the characters by using the Marshall StringToHGlobalAnsi method. This method allocates and fills a native memory buffer with a null terminated ANSI string initialized from a managed string.

Then you can copy this data to something like a STL string. Finally, free the allocated native buffer with a call to Marshall::FreeHGlobal.

BTW, The System::Runtime::InteropServices::Marshal static class contains a lot of methods useful for interoperability between managed and native world. For example, pointers, data buffers, strings, errors, COM stuff, memory allocations etc.

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <string>
using namespace System;

static void ConvertManagedStringToStdString(std::string &outStr, String ^str) 
	IntPtr ansiStr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str); 
	outStr = (const char*)ansiStr.ToPointer(); System::Runtime::InteropServices::Marshal::FreeHGlobal(ansiStr); 

int main(array<System::String ^> ^args)
	std::string myStr;
	System::String^ myManagedString = "My test string";
	ConvertManagedStringToStdString(myStr, myManagedString);
	cout << myStr;
	return 0;

Edited by BobS0327: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.