Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2012
    Location
    RI, USA
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    ProcessStartInfo.Arguments Problems...

    I know this forum is for ASP.NET, but I could not find a dedicated C# forum to post this problem in. If I missed the correct forum, please feel free to move it.

    I am trying to get C# and PHP to talk with each other via the command prompt.

    Code:
    sing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.Diagnostics;
    using System.IO;
    
    namespace sampServerWatch
    {
    
        
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
             public bool IsValidIP(string addr) {
                 IPAddress ip;
                 bool valid = false;
                 if (string.IsNullOrEmpty(addr))
                 {
                     valid = false;
                 }
                 else
                 {
                     valid = IPAddress.TryParse(addr, out ip);
                 }
                 return valid;
             }
    
             string[,] ips = {
                    {"Find server IP and Port...", "", "" },
                    {"Server1", "46.105.79.69", "7777" },
                    {"Server2", "8.2.121.99", "7777" },
             };
    
            private void Form1_Load(object sender, EventArgs e) {
                for (int i = 0; i < ips.Rank + 1; i++) {
                    cbServers.Items.Add(ips[i, 0]);
                }
                cbServers.SelectedIndex = 0;
            }
    
            private void cbServers_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (cbServers.SelectedIndex != 0)
                {
                    txtIP.Text = ips[cbServers.SelectedIndex, 1];
                    txtPort.Text = ips[cbServers.SelectedIndex, 2];
                }
                else {
                    txtIP.Text = "";
                    txtPort.Text = "";
                }
            }
    
            private void btnWatch_Click(object sender, EventArgs e) {
                string ip = txtIP.Text;
                string port = txtPort.Text;
    
                if (IsValidIP(ip) && port.Length == 4) {
                    try
                    {
                        IPAddress ipAddr = IPAddress.Parse(ip);
                        string ipArgument = @"""ip""";
                        string portArgument = @"""port""";
                        string call = @"""C:\Program Files/EasyPHP-5.3.8.1/php/php.exe""";
    
                        string param2 = "-f"; 
                        string param1 = @"""C:\Program Files/EasyPHP-5.3.8.1/www/ip-connect/callCMD.php""";
    
                        Process theProcess = new Process();
                        ProcessStartInfo theProcessStartInfo = new ProcessStartInfo(call, "spawn");
                        theProcessStartInfo.UseShellExecute = false;
                        theProcessStartInfo.RedirectStandardOutput = true;
    
                        string args = string.Format("{0} {1} {2} {3}", call, param1, param2, ip + ":" + port);
                        MessageBox.Show(args);
                        theProcessStartInfo.Arguments = args;
                        theProcess.StartInfo = theProcessStartInfo;
    
                        theProcess.Start();
                        StreamReader theStreamReader = theProcess.StandardOutput;
    
                        string theString = "";
                        while (theStreamReader.ReadLine() != null)  {
                             theString += theStreamReader.ReadLine() + "\n";
                        }
    
                        Console.WriteLine(theString);
                        rtbDebug.Text = theString;
                    }
                    catch (Exception ex) {
                        MessageBox.Show("Error: " + ex, "Error");
                    }
                }
                else {
                    MessageBox.Show("Error: Invalid IP '" + ip + "' or port '" + port + "'.", "Error");  
                }
            }
        }
    }
    My problem is that it seems theProcessStartInfo.Arguments = args; is not getting all the information that I am specifying in the "string args" declaration. I have tried removing the @ from the param1, param2 and call, passing the variables directly from the Arguments method call, MessageBox.show'ing the values to ensure the arguments are what I expect them to be, and cannot find the reason why this will not work.

    Here are the contents of callCMD.php:

    Code:
    <?php
    include 'statPageStripped.php'; 
    $inputArg = $argv[1]; #argument 0 is file name.
    
    $output = statPageStripped::writeStats($inputArg);
    echo "callCMD: output is " . $output . "\n";
    
    echo $output; 
    
    ?>
    code of statPageStripped.php:

    Code:
    class statPageStripped {
    	function writeStats($args) {
    		if (!isset($ip))
    			$ip = '46.105.79.69';
    		if (!isset($port))
    			$port = 7777;
    
    		$fp = fsockopen('udp://' . $ip, $port, $errno, $errstr);
    		if (!$fp)
    		{
    			echo "<tr><td bgcolor=\"#2B5486\">Socket Error: $errno - $errstr</td></tr>\n";
    		}
    		else
    		{
    			$packet = 'SAMP';
    			$packet .= chr(strtok($ip, '.'));
    			$packet .= chr(strtok('.'));
    			$packet .= chr(strtok('.'));
    			$packet .= chr(strtok('.'));
    			$packet .= chr($port & 0xFF);
    			$packet .= chr($port >> 8 & 0xFF);
    
    			fwrite($fp, $packet.'i');
    			fread($fp, 11);
    			$is_passworded = ord(fread($fp, 1));
    			$plr_count = ord(fread($fp, 2));
    			$max_plrs = ord(fread($fp, 2));
    			$strlen = ord(fread($fp, 4));
    			$hostname = fread($fp, $strlen);
    			$strlen = ord(fread($fp, 4));
    			$gamemode = fread($fp, $strlen);
    			$strlen = ord(fread($fp, 4));
    			$mapname = fread($fp, $strlen);
    			
    			echo $hostname ."\n";
    			echo $plr_count . ' / ' . $max_plrs . "\n";
    			echo $gamemode ."\n";
    			echo $mapname . "\n";
    
    			fwrite($fp, $packet.'c');
    			fread($fp, 11);
    			$plr_count = ord(fread($fp, 2));
    			if ($plr_count > 0)
    			{
    				for ($i=0; $i<$plr_count; $i++)
    				{
    					$strlen = ord(fread($fp, 1));
    					$plrname = fread($fp, $strlen);
    					$score = ord(fread($fp, 4));
    					echo $plrname . "\n";
    				}
    			}
    			fclose($fp);
    		}
    	}
    }
    Now C# is supposed to send the string "C:\Program Files/EasyPHP-5.3.8.1/php/php.exe" -f "C:\Program Files/EasyPHP-5.3.8.1/www/ip-connect/callCMD.php" to the command prompt, and run it. I have used the above command directly in the command prompt and have confirmed it works fine. Which makes me think my C# parameter parsing / passing is the problem.

    When I run the code now, it shows a MessageBox.Show window with the correct string that I want the command prompt to have. However after that, it shows the command prompt for a breif second, disappears, and then freezes the application. Any ideas on what the problem may be? If you need more information, let me know.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,740
    Thanks
    41
    Thanked 191 Times in 190 Posts
    what is the "-f" and what does "MessageBox.Show(args);" show?

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New Coder
    Join Date
    Jan 2012
    Location
    RI, USA
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts
    MessageBox.Show(args) shows this:

    "C:\Program Files/EasyPHP-5.3.8.1/php/php.exe" "C:\Program Files/EasyPHP-5.3.8.1/www/ip-connect/callCMD.php" -f 8.2.121.99:7777

    If I type this exact string into the command prompt, it works fine.

    -f shows this:


    C:\Users\Admin>"C:\Program Files/EasyPHP-5.3.8.1/php/php.exe" "C:\Program Files/Ea
    syPHP-5.3.8.1/www/ip-connect/callCMD.php" -f 8.2.121.99:7777
    PHP Warning: PHP Startup: Unable to load dynamic library 'C:\php\php_mysql.dll'
    - The specified module could not be found.
    in Unknown on line 0
    PHP Warning: PHP Startup: Unable to load dynamic library 'C:\php\php_mcrypt.dll
    ' - The specified module could not be found.
    in Unknown on line 0
    PHP Warning: Module 'mysqli' already loaded in Unknown on line 0
    PHP Warning: Module 'SQLite' already loaded in Unknown on line 0
    callCMD: Just entered callCMD.
    callCMD: Included the stripped stat page.
    Array
    (
    [0] => C:\Program Files/EasyPHP-5.3.8.1/www/ip-connect/callCMD.php
    [1] => -f
    [2] => 8.2.121.99:7777
    )
    callCMD: 1
    WriteStats: Just entered. args = -f
    (Lists all the data that I want)

    C:\Users\Admin>



    I highly doubt that the PHP warnings are responsible. It deals with MySQL and MySQLI, which are not involved here at all.

    I should also mention that if I put in the IP of server2, it shows server1's info because I had that hard-code set as a default in my statPageStripped page.
    Last edited by Alternative; 01-26-2012 at 03:50 PM.

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,740
    Thanks
    41
    Thanked 191 Times in 190 Posts
    me thinks because of this...
    ProcessStartInfo theProcessStartInfo = new ProcessStartInfo(call, "spawn");
    you are calling a file name @ that path with and then sending arguments that also include that path in this line
    string args = string.Format("{0} {1} {2} {3}", call, param1, param2, ip + ":" + port);
    the first string is the file name and the second string is the arguments... if the program is called "spawn" then it needs to come first and since you are passing arguments I would assemble the string before doing all of that... so for example I will assume you program is really what you defined as "call"
    Code:
    string args = string.Format("{0} {1} {2}:{3}" , param1, param2, ip, port");
    ProcessStartInfo _demo = new ProcessStartInfo(call, args);

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

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