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.