System::String to standard::string ?

Lazy8s

Golden Member
Jun 23, 2004
1,503
0
0
Ok, so I used

array<String^>^drives = Environment::GetLogicalDrives();

and I'm trying to pass it into my function

deliver(string thisDir)

but simply typing

for(int i=0; i < drives->Length; i++)
drliver(drives);

does not work. Obviously the simple thing to do would be to allow deliver() to take a managed string but on theother side I am doing all kinds of string manipulation so that's an option but only if I can convert from managed to standard string on the other side....

I tried

char* str = (char*)(void*)Marshal::StringToHGlobalAnsi(drives);

but it gives me all kinds of errors. I'm using Microsoft Visual Studio 2005 pro. Any help would be REALLY appreciated. Thanks in advance.
 

Lazy8s

Golden Member
Jun 23, 2004
1,503
0
0
That gave me a managed char array, but I can find even less info about how to turn the managed char array into standard chars...


array<String^>^drives = Environment::GetLogicalDrives();
array<Char>^charDrives;
for(int i=0; i<drives->Length; i++){
charDrives=drives->ToCharArray();
deliver(charDrives[0]+":");
}


Still gives the error: error C2664: 'deliver' : cannot convert parameter 1 from 'System::String ^' to 'std::string'
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: Lazy8s
That gave me a managed char array, but I can find even less info about how to turn the managed char array into standard chars...


array<String^>^drives = Environment::GetLogicalDrives();
array<Char>^charDrives;
for(int i=0; i<drives->Length; i++){
charDrives=drives->ToCharArray();
deliver(charDrives[0]+":");
}


Still gives the error: error C2664: 'deliver' : cannot convert parameter 1 from 'System::String ^' to 'std::string'


You're right. Bad suggestion, sorry :). Hopefully this isn't another one. Take a look at System.Runtime.InteropServices.Marshall.StringToBSTR or .StringToPtr. I think either of these might get you closer to what you need.