System command to open program freezes program

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
#include <tchar.h>
#include <urlmon.h>
#include <dos.h>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
using namespace std;
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "user32.lib")

int main()
{
	cout << "1" << endl;
	system("c:\\windows\\notepad.exe");
	cout << "2" << endl;
	Sleep (20000);
	cout << "3" << endl;

	keybd_event('a',0x45,0,0);
	keybd_event('a',0x45,2,0);
	cout << "4" << endl;
	Sleep (1000);
	return 0;
}


When running this, it only shows the 1 and the program never finishes nor puts the letter "a" in the notepad window. I used the numbers to track where it was stopping and it only shows a "1" and never goes to "2". I even changed "system" to, I think it was, "CreateProcess" and the compiler complained that that command didn't take only 1 parameter. What am I missing?
Your program is certainly starting notepad, however to continue to the next line it requires an exit statement from notepad. This means that it will sit at line 18 until notepad exits. If you do that, then you'll see the rest of the program.

Check out DOS commands for a solution, but I think what you are looking for is the "start" command. This will run notepad in a separate window meaning that it will return successfully as soon as notepad has started (instead of killed).

system("start c:\\windows\\notepad.exe");
Topic archived. No new replies allowed.