Hi All.

I am working on a program where I need help to convert a managed strinf type to unamanaged string to send as argument to function.

Any help would be highly appreciated.

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
#endif
// 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";
   Console::WriteLine(strDataManaged);

   CString strDataMfc = CString(strDataManaged);
   puts(strDataMfc.GetBuffer(0));

   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);
   Console::WriteLine(strDataManaged2);
	return 0;
}

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

Edited 4 Years Ago by thines01: added STL example

Hi,

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 4 Years Ago 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 4 Years Ago by BobS0327: n/a

This question has already been answered. Start a new discussion instead.