Still not working

Hello everyvone I was used createprocess () function to run a processes on my OS (windows). Here is my source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char *argv [])
{
    //declaration for first process
    PROCESS_INFORMATION pi;
    STARTUPINFO info;
    memset(&pi, 0, sizeof pi);
    memset(&info, 0, sizeof info);
    info.cb = sizeof STARTUPINFO;
    info.dwFlags = STARTF_USESHOWWINDOW;
    info.wShowWindow = SW_SHOWNORMAL;
    //declaration for second process
    PROCESS_INFORMATION pi2;
    STARTUPINFO info2;
    memset(&pi2, 0, sizeof pi2);
    memset(&info2, 0, sizeof info2);
    info2.cb = sizeof STARTUPINFO;
    info.dwFlags = STARTF_USESHOWWINDOW;
    info.wShowWindow = SW_SHOWNORMAL;
    int choose;
    char er; //exit or restart a program 
    replay:
    cout << "Which program vish run - 1/2?" << endl;
    cin >> choose;
    if (choose==1)
    { 
        CreateProcess(NULL,                     //run first program
        "d:C:\Folder1\\Folder2\\program1.exe", 
        NULL,
        NULL,
        FALSE,
        0,
        NULL,
        NULL,
        &info,
        &pi);
    }
    if (choose==2)
    {
        CreateProcess(NULL,                     //run second program 
        "d:C:\\Folder3\\Folder4\\program2", 
        NULL,
        NULL,
        FALSE,
        0,
        NULL,
        NULL,
        &info,
        &pi);
    }
    else 
    {
        while (1)
        {
            cout << "Wrong number!" << endl; 
            cout << "Waiting 5 seconds:" << endl;
            for (int wrong=5; wrong > 0; wrong--)
            {
                cout << wrong;
                Sleep (1000);
            }
            goto replay;
        }
    }
    cout  << "Press Q - quit program" << endl; 
    cin >> er;
    if ((er=='Q')||(er=='q'))
    {
        for (int bay=5; bay > 0; bay--)
            {
                cout << bay;
                Sleep (1000);
            }
    }
    else 
    {
        cout << "Error!!!" << endl;
        Sleep (2000);
        return -1;
    }
    return 0;
} 


But there are two errors and one warning:
11: expected primary-expression before ';' token
19: expected primary-expression before ';' token
30:9: [Warning] unknown escape sequence '\F'

Can anyone tell me true code of 11,19 and 30 lines?

And I wish to close this two process leter in this program. But I don´t wish to use Terminate Process (); becouse this function finish program very unclean! I think that Terminate Process (); leave DLLs open. Please fix me if I distrub in this think.

If you can please tell me yet how:
I reached switching betwen this two programs (If one running process start closing automatically run another AND if I start process - craateprocess () automatically close another program)
how can I got more properties about running proceses (Size of program, how much demanding is my running process for my PC or what similar)

Tnx for all post, Anon 777
Last edited on
I can't help you with the WinAPI related problems, but your syntactic errors are:
line 11 and 19: the sizeof operator needs parenthesis around it's operand : sizeof(something);
line 30: you meant "d:C:\\Folder1\\Folder2\\program1.exe"
sizeof(something);
- >this helps problem with source code which is up is saved. -> tnx ROmai
And I discovered mastake of 30 line: "d:C:\\Folder1\\Folder2\\program1.exe" I was written this line witout d:
And I was discovered one big mastake which my compilker did not:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 else 
    {
        while (1)
        {
            cout << "Wrong number!" << endl; 
            cout << "Waiting 5 seconds:" << endl;
            for (int wrong=5; wrong > 0; wrong--)
            {
                cout << wrong;
                Sleep (1000);
            }
            goto replay;
        }
    }

must be if I wish that program working true:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 if ((choose!=1)&&(choose != 2)) 
    {
        while (1)
        {
            cout << "Wrong number!" << endl; 
            cout << "Waiting 5 seconds:" << endl;
            for (int wrong=5; wrong > 0; wrong--)
            {
                cout << wrong;
                Sleep (1000);
            }
            goto replay;
        }
    }

I also can use switch instead of if in this example!
This errors are saved now can anyone answer on this questions:

And I wish to close this two process leter in this program. But I don´t wish to use Terminate Process (); becouse this function finish program very unclean! I think that Terminate Process (); leave DLLs open. Please fix me if I distrub in this think.

If you can please tell me yet how:
I reached switching betwen this two programs (If one running process start closing automatically run another AND if I start process - craateprocess () automatically close another program)
how can I got more properties about running proceses (Size of program, how much demanding is my running process for my PC or what similar)

Tnx for all next replies and again thank you Romai!
The following demonstrates how you can stop a program (supposing it's a windows application) started with CreateProcess, by sending a WM_CLOSE message to each window of its main execution thread.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

BOOL CALLBACK CloseThatWindowPlease(HWND hwnd,LPARAM lParam)
{
    SendMessage(hwnd,WM_CLOSE,0,0);
    return TRUE;
}

int main()
{
    PROCESS_INFORMATION pi;
    STARTUPINFO si;

    memset(&pi, 0, sizeof(pi));
    memset(&si, 0, sizeof(si));

    si.cb = sizeof(STARTUPINFO);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOWNORMAL;

    string program_name;
    cout << "enter the program name:\n";
    getline(cin,program_name);

    CreateProcess(program_name.c_str(),NULL,
        NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);

    cout << "\nhit enter to close the process and quit";
    cin.get();

    EnumThreadWindows(pi.dwThreadId,CloseThatWindowPlease,0);

    //Don't forget these... I did in the original post...
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

    return 0;
}
Last edited on
tnx it helps me but not in whole becouse it must start another program AUTOMATICALLY when one of this two start closing by user. And when It run and I run anoter program start closing
the first one
I was trying but It was not work!
I know somthing what I need function wich we call CALLBACK becouse this function allowing communication bettwen OS and my program

and I also need a switch...
1
2
3
4
5
6
7
8
MSG message;
switch (&message)
{
case WM_CLOSE: //must controling only a window of specific program
CreateProcess(program_name.c_str(),NULL,
        NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
} 
break;


and ...
1
2
3
4
5
6
7
8
MSG message;
switch (&message)
{
case WM_ACTIVATE: //must controling only a window of specific program
SendMessage(hwnd,WM_CLOSE,0,0); // send message to specific program 

} 
break;

Last edited on
Topic archived. No new replies allowed.