Converting System::String^ to char *


I am having an issue converting a Managed System::String to an unmanaged char *. Can someone see the flaw in my code below:

1>..\..\Main.cpp(26) : error C2440: 'initializing' : cannot convert from 'System::String ^' to 'char *'
1>        No user-defined-conversion operator available, or
1>        Cannot convert a managed type to an unmanaged type
#include <atlstr.h>
#include <iostream>
#include "ACLInfo.h"
#include <fstream>
#include <string>
using namespace System;
using namespace System::IO;
//#include <windows.h>
using namespace std;

int main()
	char str[2000];
         ifstream file_op; ("C:\\shares.txt",ios::in);
		String^ str1 = gcnew String(str);
		DirectoryInfo^ di = gcnew DirectoryInfo( str1 );
		array<DirectoryInfo^>^diArr = di->GetDirectories();
		Collections::IEnumerator^ myEnum = diArr->GetEnumerator();
		while ( myEnum->MoveNext() )
			DirectoryInfo^ dri = safe_cast<DirectoryInfo^>(myEnum->Current);
---> Error Here			char* currentDir = ( "{0}\\{1}", dri->Root, dri->Name );
	return 0;

Open in new window

Who is Participating?
pgnatyukConnect With a Mentor Commented:
You need to allocate a memory. char* is a pointer to a memory.
Check if it helps:
Conversion from System::String::^ to char*:
tr57285Author Commented:
WHat I am trying to do is build a char string using dir->root and dir->Name from the DirectoryInfo class.
tr57285Author Commented:
So I need to do the following:

[const] char* System::String^ String(char*)
[const] wchar_t* System::String^ String(wchar_t*)
BSTR System::String^ String(wchar_t*)
System::String^ [const] char*
Marshal::StringToCoTaskMemAnsi or Marshal::StringToHGlobalAnsi
System::String^ wchar_t*
Marshal::StringToCoTaskMemUni or Marshal::StringToHGlobalUni
System::String^ const wchar_t*
System::String^ BSTR

Are there any easier workarounds?
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

tr57285Author Commented:
In VBScript I would do something like this to build a string:

str1 = dri->Root & " \\" &  dri->Name

I want to do this in C++ but am unsure how to go about it. This is where I am stuck.
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
How to: Convert System::String to Standard String

System::String -> std::string
@tr57285: sorry, your comment is wrong.

@evilrix: the links you posted contain the full answer.

In the link I posted you could find:
char* szAnsi = static_cast(System::Runtime::Interop Services::Marshal::StringToHGlobalAnsi(s).ToPointer());

That's the idea. Services::Marshal::StringToHGlobalAnsi(s).ToPointer().

evilrixSenior Software Engineer (Avast)Commented:
>> @evilrix: the links you posted contain the full answer
I know :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.