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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimAuthor Commented:
WHat I am trying to do is build a char string using dir->root and dir->Name from the DirectoryInfo class.
You need to allocate a memory. char* is a pointer to a memory.
Check if it helps:
Conversion from System::String::^ to char*:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimAuthor 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?
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

TimAuthor 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.
evilrixSenior 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 :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.