Thread: Problems with spawnl()

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    13

    Problems with spawnl()

    I'm writing an app that needs to be able to run other applications. I chose the spawnl function in process.h for the purpose. Here's a little test program I wrote:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <process.h>
    
    ///////////////////////////////////////////////////////////////////////
    //////Small app that launches itself using the spanwl function/////////
    ///////////////////////////////////////////////////////////////////////
    
    int main(int argc, char *argv[])
    {
    int error;
    if(argc == 1){
            error = spawnl(P_WAIT,"D:\\Dev\\Projects\\Tests\\Spawn\\spawn.exe",
            "D:\\Dev\\Projects\\Tests\\Spawn\\spawn.exe","\"This is the first argument passed by spawn.exe!\"",NULL );
            if(error == 0)printf("Program lauched successfully!\n");
            else {
                 printf("%d\n",error);
                 system("PAUSE");
            }
    }
    else {
         printf("%s\n\n",argv[1]);
         system("PAUSE");
    }	
     return 0;
    }
    This program works fine. It launches the app succsessfully. But when I try to launch an app that have a space in the file path (for example: D:\Dev\Projects\Tests\Spawn 1\spawn.exe) it doesn't work. Is there a solution to this problem?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You could try to quote the path
    e.g.
    Code:
    error = spawnl(P_WAIT,"\"D:\\Dev\\Projects\\Tests\\Spawn 1\\spawn.exe\"",
            "\"D:\\Dev\\Projects\\Tests\\Spawn 1\\spawn.exe\"","\"This is the first argument passed by spawn.exe!\"",NULL );
            if(error == 0)printf("Program lauched successfully!\n");
    using forward slashes would make it easier to read.

    Code:
    error = spawnl(P_WAIT,"\"D:/Dev/Projects/Tests/Spawn 1/spawn.exe\"",
            "\"D:/Dev/Projects/Tests/Spawn 1/spawn.exe\"","\"This is the first argument passed by spawn.exe!\"",NULL );
            if(error == 0)printf("Program lauched successfully!\n");
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    I've already tried that but it doesn't work. Spawnl returns -1.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Which OS/Compiler are you using?

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Mr.Modem
    I've already tried that but it doesn't work. Spawnl returns -1.
    I know windows doesn't support filenames containing spaces very well.
    One way could be to find out that funny 8.3-style path and use that.
    Kurt

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    Quote Originally Posted by Salem
    Which OS/Compiler are you using?
    I use DevCpp.

    Quote Originally Posted by ZuK
    I know windows doesn't support filenames containing spaces very well.
    One way could be to find out that funny 8.3-style path and use that.
    Kurt
    That wouldn't be a good solution. The program I'm planning to make will read the program path from an ini file and launch it. Then I would need a function to convert the path to the 8.3 format. There must be an easier solution.

    EDIT: After I looked in your FAQ I found a solution to the problem. It isn't exactly what I want but it works:

    Code:
    #include <windows.h>
    
    int main(void)
    {
      char szPath[] = "C:\\Program\\Windows Media Player\\wmplayer.exe";
      PROCESS_INFORMATION pif;  //Gives info on the thread and..
                               //..process for the new process
      STARTUPINFO si;          //Defines how to start the program
    
      ZeroMemory(&si,sizeof(si)); //Zero the STARTUPINFO struct
      si.cb = sizeof(si);         //Must set size of structure
    
      BOOL bRet = CreateProcess(
            szPath, //Path to executable file
            NULL,   //Command string - not needed here
            NULL,   //Process handle not inherited
            NULL,   //Thread handle not inherited
            FALSE,  //No inheritance of handles
            0,      //No special flags
            NULL,   //Same environment block as this prog
            NULL,   //Current directory - no separate path
            &si,    //Pointer to STARTUPINFO
            &pif);   //Pointer to PROCESS_INFORMATION
    
      if(bRet == FALSE)
      {
        MessageBox(HWND_DESKTOP,"Unable to start program","",MB_OK);
        return 1;
      }
    
      CloseHandle(pif.hProcess);   //Close handle to process
      CloseHandle(pif.hThread);    //Close handle to thread
    
      return 0;
    }
    The only problem is that it's limited to windows. Are there any other, more portable functions that does the same thing?
    Last edited by Mr.Modem; 02-11-2006 at 05:56 AM.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Mr.Modem
    That wouldn't be a good solution. The program I'm planning to make will read the program path from an ini file and launch it. Then I would need a function to convert the path to the 8.3 format.
    you could use GetShortPathName() to convert the path.
    Kurt

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    How does GetShortPathName() work? I've never done any windows programming before.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://msdn.microsoft.com/library/de...rtpathname.asp
    You give it a long path, and somewhere to put the short path.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    I tried to use an 8.3 file path but it didn't work either. Damn windows!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <process.h>
    
    ///////////////////////////////////////////////////////////////////////
    //////Small app that launches itself using the spanwl function/////////
    ///////////////////////////////////////////////////////////////////////
    
    int main(int argc, char *argv[])
    {
    int error;
    
    
    if(argc == 1){
            error = spawnl(P_WAIT,"C:\\PROGRAM\\WINDOW~1\\WMPLAY~1.exe",
            "C:\\PROGRAM\\WINDOW~1\\WMPLAY~1.exe","\"This is the first argument passed by spawn.exe!\"",NULL );
            if(error == 0)printf("Program lauched successfully!\n");
            else {
                 printf("%d\n",error);
                 system("PAUSE");
            }
    }
    else {
         printf("%s\n\n",argv[1]);
         system("PAUSE");
    }	
     return 0;
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > "\"This is the first argument passed by spawn.exe!\""
    Maybe it works all along, and it's just windows media player rejecting your attempt to post garbage strings to it.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    13
    Oops, I forgot to take away that argument. But it doesn't really matter. The program don't start anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM