-
June 28th, 2012, 02:43 AM
#1
Windows form < Use texbox values for system commands
Hi all,
i just started playing with gui apps. My first test is a small ping app.
I've created a new project (windows forms) containing:
1: image
2: textbox
3: 2 x button (1 exit, 1 ping)
Now when i click on exit, the app extits... "really "
When i click on ping, i''m getting a popup with the information from the textfield. This is cool, but i want to run the system command ping with the textbox value.
This is what i have:
Code:
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Application::Exit();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
String^ result = textBox1->Text;
MessageBox::Show( result );
////////////// When i comment the messagebox and try:
system("ping -n 2 " + result + " > C:\\pingtest.txt");
////////////// i get errors (shown below)
}
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
ERROR:
Code:
error C2664: 'system' : cannot convert parameter 1 from 'System::String ^' to 'const char *'
Only with:
Code:
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
system("ping -n 2 www.google.nl > C:\\pingtest.txt");
}
it does work, but i can't use the textbox value.
So i need to convert "result" i think, but i don't know how to do this correctly and let ping run with the result as parameter
Any ideas ?
Last edited by peewster; June 28th, 2012 at 02:48 AM.
-
June 28th, 2012, 04:03 AM
#2
Re: Windows form < Use texbox values for system commands
Managed C++ and Windows Forms questions are off-topic for this forum.
You want the Managed C++ forum.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; June 28th, 2012 at 04:06 AM.
-
June 28th, 2012, 04:32 AM
#3
Re: Windows form < Use texbox values for system commands
Originally Posted by Paul McKenzie
Managed C++ and Windows Forms questions are off-topic for this forum.
You want the Managed C++ forum.
Regards,
Paul McKenzie
Ow sorry, can somebody move this thread for me ?
-
June 28th, 2012, 08:45 AM
#4
Re: Windows form < Use texbox values for system commands
The error message tells it all: You try to pass a managed string to the function, but that expects a native C-string (a native array of char), and there's no implicit conversion available. (There is one in the opposite direction, though, which is why you can assign a plain quoted string literal to a managed string.)
Now you could get the raw characters out of the managed string by means of the Encoding::GetBytes() method, but that would return a managed array that would need further conversion (and probably wouldn't even be null-terminated unless you append "\0" to the managed string prior to conversion). Since I prefer doing things the .NET way in .NET (the "Romans" thing... ) whenever possible, I refrained from further research at this point.
Here's a purely .NET-based function that (for the most part) emulates the behavior of the C library system() function and probably isn't significantly more complicated than all that string conversion mumbo-jumbo either:
Code:
// Usual namespace stuff omitted
using namespace Diagnostics;
using namespace IO;
int WinSystem(String ^strCommand)
{
if (strCommand) {
ProcessStartInfo ^psi = gcnew ProcessStartInfo(Environment::GetEnvironmentVariable("ComSpec"),
"/c " + strCommand);
psi->UseShellExecute = false;
Process ^proc = Process::Start(psi);
proc->WaitForExit();
int nExitCode = proc->ExitCode;
delete proc;
return nExitCode;
} else
return File::Exists(Environment::GetEnvironmentVariable("ComSpec"));
}
Here's a related thread (in the right forum section ) that, among other things, demonstrates a more elegant way to get hands on the shell command output than redirecting it to a temporary file (which as such wouln't work with the unmodified function above, though): http://forums.codeguru.com/showthrea...Extern-āCā
Well, now that the thread has a real (non-trivial) .NET reply, I admit it really demands being moved...
peewster, after 80 posts I think you really should know by now that that didn't belong here...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
June 29th, 2012, 10:15 PM
#5
Re: Windows form < Use texbox values for system commands
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|