Converting System::String^ to char *

All,

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;
	file_op.open ("C:\\shares.txt",ios::in);
         while(!file_op.eof()) 
         {
		file_op.getline(str,2000);
		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 );
		}
	}
	file_op.close();
	return 0;
}

Open in new window

TimAsked:
Who is Participating?

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

x
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.
0
pgnatyukCommented:
You need to allocate a memory. char* is a pointer to a memory.
Check if it helps:
Conversion from System::String::^ to char*:
http://bytes.com/topic/net/answers/467933-conversion-system-string-char
 
 
0

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*)
constructor
[const] wchar_t* System::String^ String(wchar_t*)
constructor
BSTR System::String^ String(wchar_t*)
constructor
System::String^ [const] char*
Marshal::StringToCoTaskMemAnsi or Marshal::StringToHGlobalAnsi
System::String^ wchar_t*
Marshal::StringToCoTaskMemUni or Marshal::StringToHGlobalUni
System::String^ const wchar_t*
PtrToStringChars(String^)
System::String^ BSTR
Marshal::StringToBSTR

Are there any easier workarounds?
0
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.
0
evilrixSenior Software Engineer (Avast)Commented:
How to: Convert System::String to Standard String
http://msdn.microsoft.com/en-us/library/1b4az623(VS.80).aspx

System::String -> std::string
http://blogs.msdn.com/slippman/archive/2004/06/02/147090.aspx
0
pgnatyukCommented:
@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().


0
evilrixSenior Software Engineer (Avast)Commented:
>> @evilrix: the links you posted contain the full answer
I know :)
0
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.