I'm having problems with sending the input to another program... Here is what I'm supposed to do. Ask for nth number in the fibonacci sequence. The program will call another program (fibonacci.exe) genterate the nth number and pass back the information to the parent process, and display all numbers of the fibonacci seq up to the users number. I have no problems running the fibonacci program, it is passing the input from the user and returning it. Here is what I have so far:

#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;

int main(VOID)
{
    STARTUPINFOA si;
    PROCESS_INFORMATION pi;

    //allocate memory
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    int parameter1=0;
    cout << "Enter a number: " << endl;
    cin >> parameter1;

    //create child process
    if (!CreateProcessA(NULL, //use command line
        "Desktop\ConsoleApplication1.exe", //command line
        parameter1, //don't inherit process handle
        NULL, //don't inherit thread handle
        FALSE, //disable handle inheritance
        0, //no creation flags
        NULL, //use parent's environment block
        NULL, //use parent's existing directory
        &si,
        &pi))
    {
        cout << stderr << "Create Process Failed";
        system("PAUSE");
        return -1;
    }

    //parent will wait for the child to complete
    WaitForSingleObject(pi.hProcess, INFINITE);
    cout << "Child Complete";
    system("PAUSE");
    //close handles
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
}

Thanks for any and all help.

Recommended Answers

All 3 Replies

Process A passes parameters to Process B thru the command line, just as if you typed them yourself from the console screen. That means you have to implement the first parameter to CreateProcess() on line 21 of the code you posted. Then Process B can return an integer value back to Process A by using the return N from main(). Process A gets that value by calling GetExitCodeProcess(), which you should put after line 38 of your program.

Thanks for the quick reply. I think I'm confused on where I am supposed to pass the parameter and return it. Does all of that code go in the "if" statement? When using CreateProcessA() where does the parameter go? I've moved it around and it still doesn't seem to work. I am getting an error "argument of type int is incompatible with parameter of type lpcstr" anytime my parameter is in the code. From research it seems as if it is the difference in Unicode and Multi-Byte character sets. If I wanted to keep it in Unicode, what would my parameter look like? Thanks again.

You have to format a char array that contains the parameters. One way to do that is with sprintf().

For UNICODE you create an array of wchar_t* , for others the array is of type char*.

tchar.h is the header file that has macros illustrated below which make the source code correct whether compiled for UNICODE or not. If you think you want UNICODE then use the macros so that you can switch back and forth without changing the source code. If you google for "msdn scanf" (or for any other function) it will tell you what macro you need for the function, such as _stprintf() replaces sprintf().

Example:

int number = 123;
TCHAR args[255] = {0};
_stprintf(argc,_TEXT("%d"),number);

Read the description for the first parameter of CreateProcess() carefully so that you know what it is supposed to contain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.