Windows form < Use texbox values for system commands
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Windows form < Use texbox values for system commands

  1. #1
    Join Date
    Aug 2011
    Posts
    80

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,423

    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.

  3. #3
    Join Date
    Aug 2011
    Posts
    80

    Re: Windows form < Use texbox values for system commands

    Quote Originally Posted by Paul McKenzie View Post
    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 ?

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    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...546-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.

  5. #5
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,021

    Re: Windows form < Use texbox values for system commands

    [ Moved thread ]
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center