Search code examples
c#pythoncargumentsscreensaver

Redirecting Command-line Arguments for Bootstrapping


I am trying to rewrite the following program in C instead of C# (which is less portable). It is obvious that "int system ( const char * command )" will be necessary to complete the program. Starting it with "int main ( int argc, char * argv[] )" will allow getting the command-line arguments, but there is still a problem that is difficult to understand. How do you successfully escape arguments with spaces in them? In the program below, arguments with spaces in them (example: screensaver.scr "this is a test") will be passed to the script as separate arguments (example: screensaver.scr this is a test) and could easily cause problems.

namespace Boids_Screensaver
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            System.Diagnostics.Process python = new System.Diagnostics.Process();
            python.EnableRaisingEvents = false;
            python.StartInfo.FileName = "C:\\Python31\\pythonw.exe";
            python.StartInfo.Arguments = "boids.pyw";
            foreach (string arg in args)
            {
                python.StartInfo.Arguments += " " + arg;
            }
            python.Start();
        }
    }
}

Solution

  • The correct way to do this under windows is to use _spawnv

    Its equivalent under unix like OSes is fork() followed by execv.