Thread: Having trouble with using CreateProcess and reading RSS feeds

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    Having trouble with using CreateProcess and reading RSS feeds

    Code:
    #define WIN_32_LEAN_AND_MEAN
    #include <windows.h>
    #include <stdio.h>
    #include <process.h>
    #include <tchar.h>
    
    
    int main( int argc, char * argv[] )
    {
        char * feeds[] = { "http://www.nytimes.com/services/xml/rss/"};
        
        int times = 1;
        char *phrase = argv[1];
        int i;
        
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
       
        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );
    
    
        if( argc != 2 )
        {
            printf("Usage: %s [cmdline]\n", argv[0]);
            return 1;
        }
        
        for ( i = 0; i < times; i++ )
        {
            char var[255];
            sprintf( var, "RSS_FEED=%s", feeds[i]);
            char *vars[] = {var, NULL};
            
             // Start the child process. 
            if( !CreateProcess( "C:\\CProjects\rss.exe",   // module name
                                GetCommandLine( ),        // Command line
                                NULL,           // Process handle not inheritable
                                NULL,           // Thread handle not inheritable
                                FALSE,          // Set handle inheritance to FALSE
                                0,              // No creation flags
                                NULL,           // Use parent's environment block
                                NULL,           // Use parent's starting directory 
                                &si,            // Pointer to STARTUPINFO structure
                                &pi )           // Pointer to PROCESS_INFORMATION structure
            ) 
            {    
                    printf( "CreateProcess failed (%s).\n", strerror(GetLastError()));
                    return 1;
            }
            
            pid_t child_id = pi.dwProcessId;
            
            if ( GetCurrentProcessId( ) == child_id )
            {
                if ( execle("/python27", "/python27", "gossip.py", phrase, NULL, vars) == -1 ) 
                {
                    fprintf(stderr, "Can't run script : %s\n", strerror(errno));
                    return 1;
                }
            }
            
            /* Wait until child process exits. */
            WaitForSingleObject( pi.hProcess, INFINITE );
            
            /* Clear out the information for the next run */
            
            ZeroMemory( &si, sizeof(si) );
            si.cb = sizeof(si);
            ZeroMemory( &pi, sizeof(pi) );
    
    
            CloseHandle( pi.hProcess );
            CloseHandle( pi.hThread );
        
        }
      
        return 0;
    }
    Basically, what I'm trying to do is run a python file that will search for a string entered as an argument by creating a separate process and running a exec() function in that process, and then continuing to the next iteration ( if there is another feed to test ) as needed until returning with 0. I'm fairly sure I didn't put in this parameter of CreateProcess right :

    Code:
    "C:\\CProjects\rss.exe"
    In this case, that would this code's application, but I wasn't sure how to set that and still be able to run the python file. Also the command line parmeter where I entered the function GetCommandLine( ) may be wrong too ( My idea was that would make sure to pass the command-line to the python program, but since that was handled in execle, it might've screwed it up anyways ). Basically, my book really goes into the full-code with Mac and just leaves Windows users like me on my own( it just suggested this function to adapt it, and that was it ). This is my first time creating processes for Windows, so I got sort of confused on the details of how to make this program work. CreateProcess fails with Unknown error in the case of my code.

    Edit : Oh, and the RSS url I stored may be invalid, if you could check it or something that would be nice. Also, I just kept most of the error coding and stuff that microsoft used in their sample for Creating Processes. I just modified it a litte.

    Any help would be appreciated.
    Last edited by HelpfulPerson; 07-02-2013 at 07:56 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    \r
    is line feed - so your path is not what your think
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Can you post a correct RSS feed please? I've never messed with it before.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to use a double-backslash for every backslash you want:
    Code:
    "C:\\CProjects\\rss.exe"

  5. #5
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by oogabooga View Post
    You need to use a double-backslash for every backslash you want:
    Code:
    "C:\\CProjects\\rss.exe"
    Thanks, that fixed the call to create the process, but the program basically slowed my computer down and I had to restart it after running it. My guess is that it kept calling itself recursively and creating processes. I don't know how to fix it, as I've never messed with creating processes. I ran the program with this as suggested in the book( with my few extra commands ) :

    Code:
    chdir C:\CProjects
    del rss.exe
    gcc RSS_feed.c -o rss.exe
    rss 'Business'
    /* Extreme computer slowdown now occurs */

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if( !CreateProcess( "C:\\CProjects\rss.exe",
    Explain why you're doing this?

    Especially when the program you just run is actually C:\CProjects\rss.exe

    Your analysis is correct, you have an infinite process loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Presumably because in the book, that's where the fork() is and s/he tried to make a copy of the program like it does.

    Windows doesn't have a useful equivalent to fork so perhaps the easiest way to port this is to delete all the CreateProcess specific stuff and change the execle(stuff) to _spawnle(_P_WAIT, stuff). You'll probably also have to change the path to the python executable, unless it really is installed at C:/python27.exe

  8. #8
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by Salem View Post
    > if( !CreateProcess( "C:\\CProjects\rss.exe",
    Explain why you're doing this?

    Especially when the program you just run is actually C:\CProjects\rss.exe

    Your analysis is correct, you have an infinite process loop.
    I was trying to copy fork() in the book, but as you can see, it wasn't really going the way I might've hoped it would. I got confused on the correct module name to use, should I just make a seperate program with the loop code and keep calling that instead? That was the only solution I thought of.

    Quote Originally Posted by adeyblue View Post
    Presumably because in the book, that's where the fork() is and s/he tried to make a copy of the program like it does.

    Windows doesn't have a useful equivalent to fork so perhaps the easiest way to port this is to delete all the CreateProcess specific stuff and change the execle(stuff) to _spawnle(_P_WAIT, stuff). You'll probably also have to change the path to the python executable, unless it really is installed at C:/python27.exe
    Yea, I tried to do spawn from the FAQ, but I called it with my program name so I ran into the same problem. My biggest problem is me not knowing what to call so I just make my program call itself as a process, but that just creates an infinite process loop. As I said above, my only idea to fix it is to change the program I call with the loop code in a totally seperate program ( which I could manage )

    Oh, and yes, I should've changed the application I ran with my exec function to this :

    Code:
    "C:\\Python27\\python.exe"
    Last edited by HelpfulPerson; 07-03-2013 at 03:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Monitoring Video & Audio feeds
    By dgamwell in forum C++ Programming
    Replies: 1
    Last Post: 08-24-2010, 01:32 PM
  2. Beeps and form feeds???
    By 00Sven in forum C Programming
    Replies: 11
    Last Post: 02-24-2006, 02:06 PM
  3. RSS Feeds
    By ober in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 04-02-2005, 03:36 PM
  4. RSS Feeds
    By Scribbler in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-11-2005, 09:07 PM
  5. Carriage Return Line Feeds
    By dattanij in forum C Programming
    Replies: 1
    Last Post: 09-07-2001, 04:41 AM