Execute .bat in windows service

I have the code.
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
void CSampleService::ServiceWorkerThread(void)
{
    // Periodically check if the service is stopping.
    while (1)
    {
        // Perform main service function here...
		ifstream input;		//input configuration file
		ofstream log;		//ouptut log file
		ofstream bat;		//batch program to backup

		input.open("config.txt");	//contains the sql dump statement and time
		log.open("log.txt",ios::app);	//logging file
		bat.open("batch.bat");			// create batch file in working director

		string dump;	//sqldump
		int time;		//period

		getline(input,dump);		//get mysqldump statement
		input>>time;				//get time
		
		bat<<dump<<endl;			//edit .bat
		bat<<"exit";

		bat.close();				//close bat
		input.close();				//close sql input file

		/// SEGMENT TO CREATE PROCESS
		STARTUPINFO si;
		PROCESS_INFORMATION pi;

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

		// Start the child process. 
		if( !CreateProcess( NULL,   // No module name (use command line)
        L"C:\\Windows\\System32\\cmd.exe /C batch.bat",  // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        CREATE_NO_WINDOW,              // 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
		) 
 
    // Wait until child process exits.
		WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
		CloseHandle( pi.hProcess );
		CloseHandle( pi.hThread );
				
		log<<"Database updated: "<<__DATE__<<" "<<__TIME__<<endl;		//log..

		log.close();		//close log

        ::Sleep(time*3600);  //wait this long before next backup
    }

    // Signal the stopped event.
    SetEvent(m_hStoppedEvent);
}


And there are several things wrong.

With line 6 to 57 commented out, the service starts. But it does not start with error "1607 starting service on local syste" as the code is.
-The service installs with "servicename.exe -install".
-The files in line 12 and line 13 are NOT created.
-Hence the batch is not run.

Any help?
Topic archived. No new replies allowed.