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

tr57285Asked:
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*:
http://bytes.com/topic/net/answers/467933-conversion-system-string-char
 
 
0
 
tr57285Author Commented:
WHat I am trying to do is build a char string using dir->root and dir->Name from the DirectoryInfo class.
0
 
tr57285Author 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
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.
0
 
evilrixConnect With a Mentor Senior 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
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.