Any C++ .NET programmers out there?

engineereeyore

Platinum Member
Jul 23, 2005
2,070
0
0
In Visual Studio 6.0, you could do a simply system() call and pass it the path of the program to execute and it did it. Now I can still do a system() call, but the path I have I retrieve out of my GUI, so it's associated with a String. I can't pass a String to system(). Is there another equivalent function I can call that does take a String, or is there some way to convert a String * to a char *. I can't for the life of me figure out how to do the convertion. I've tried everything I could.

Any suggestions?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I believe the String class has a member function called c_string or similar that will return a C style char array.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
First question is which string are you using? There are about 2^16 types, each with their own behavior. So the advice is different if you are using a CString, bstr_t, bstr, ATL string, etc.

In general, look at the conversion operators and methods supplied for each type to find a way to get to the underlying character array. Be careful, as some strings store characters in unicode (wchar), and some don't. Same with system APIs that take strings.

Post a snip of code and I'll see if I can point you in the right dir.
 

engineereeyore

Platinum Member
Jul 23, 2005
2,070
0
0
String *drive, *path;
TreeNode *tn = treeView1->SelectedNode;
drive = tn->Text;
if(drive->get_Length() > 3)
{
MessageBox::Show(drive,S"Invalid Path for Defrag: ");
}
else
{
path = String::Concat(S"defrag ",drive);
system(path);
}

This is the code that doesn't work. Just get a conversion error "error C2664: 'system' : cannot convert parameter 1 from 'System::String __gc *' to 'const char *'"

So I've tried this

String *drive, *path;
Encoding * ascii = Encoding::ASCII;
Encoding * unicode = Encoding::Unicode;


Char new_path[];
TreeNode *tn = treeView1->SelectedNode;
drive = tn->Text;
if(drive->get_Length() > 3)
{
MessageBox::Show(drive,S"Invalid Path for Defrag: ");
}
else
{
path = String::Concat(S"defrag ",drive);
Byte unicodeBytes[] = unicode -> GetBytes(path);
Byte asciiBytes[] = Encoding::Convert(unicode, ascii, unicodeBytes);
Char asciiChars[] = new Char[ascii -> GetCharCount(asciiBytes, 0, asciiBytes -> Length)];
ascii -> GetChars(asciiBytes, 0, asciiBytes->Length, asciiChars, 0);
system(asciiChars);
}

That doesn't work either. "error C2664: 'system' : cannot convert parameter 1 from '__wchar_t __gc[]' to 'const char *'"

I've tried receiveing a __wchar_t array as well, but get a similar error about converting that to a const char *. Tried doing a reinterpret_cast and any other cast you can think of and nothing works.

Is there another function that does the same thing as system() that takes a String?

 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Is there another function that does the same thing as system() that takes a String?

Not really.

Find a way to get a 'char *' or 'char[]', NOT a "Char *" or "Char[]" or "String". STL strings have an operator to get a 'char *' version of themselves; the .NET structures you are using probably have the same thing.
 

Varun

Golden Member
Aug 18, 2002
1,161
0
0
#include <string>
//strings require this include file
const int MAX_ARRAY=20; //set to whatever you want your array to be in length
string name="Hi how are you today?"; //junk string
char message[MAX_ARRAY];
for(int nerd=0; nerd<name.length(); nerd++)
{
message[nerd]=name[nerd];
}
Just don't forget you can easily go out of bounds on your array if your array is smaller than the entire string length.

edit - my arrays were not showing up since i was turning on italics - changed it now.
 

engineereeyore

Platinum Member
Jul 23, 2005
2,070
0
0
Thanks for all the tips guys. I tried several, but none worked. But when I went to the link oog gave, I saw System::String to char*. So I typed that into google and found an example on MSDN. Here's the working code now if any of you are interested.

private: System::Void Defrag_Click(System::eek:bject * sender, System::EventArgs * e)
{
String *drive, *path;
char *new_path;
IntPtr ptr;

TreeNode *tn = treeView1->SelectedNode;
drive = tn->Text;
if(drive->get_Length() > 3)
{
MessageBox::Show(drive,S"Invalid Path for Defrag: ");
}
else
{
path = String::Concat(S"defrag ",drive);
try
{
new_path = (char*)(void*)Marshal::StringToHGlobalAnsi(path);
system(new_path);
Marshal::FreeHGlobal(new_path);
}
catch(System::Exception *pe)
{
}
}
}

Why don't we have a programming forum here? Isn't there a petition to get that started or something?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
There is an ongoing effort. It's really a hardware forum, with some sections devoted to stuff you run on hardware, so I think I understand the mods' reluctance.

Glad you got it worked out. Not too far down the road I'm sure we'll have a .Net managed layer to the entire Windows API, and it will resolve some of these mismatches. Strings have always sucked.