External program access

I'm trying to create a security suite program, one central program that allows me access to all my programs from one file. I can't execute the programs for one of two problems:
1. Full file paths have spaces in the filename. The program refuses to go beyond the space, "C:\Program is not recognized as an internal or external command", from
1
2
3
void firewall(){
	system("C:\\Program Files (x86)\\Zone Labs\\ZoneAlarm\\zlclient.exe");
}

and the same when I use Process::Start.

2. If I try to get around by using CreateProcess, I get all kinds of trouble. I'm learning this one as I go along:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void backup(){
	STARTUPINFO si;
    PROCESS_INFORMATION pi;
	LPWSTR cmd = (LPWSTR) "start stxmanager.exe";

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

	CreateProcessW( NULL, cmd, NULL, NULL, FALSE, 0 , NULL, NULL, &si, &pi ) ;
	
	 // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
	//system("start C:\Program Files (x86)\Seagate\SeagateManager\ManagerApp\\stxmanager.exe");
}


This is currently causing a "Attempted to read or write protected memory." error during debuggin', so I'm over the size of one of the structure elements?
I've also had what looked like type conversion errors with regards to the pi pointer, such as "cannot convert 'LP#!^@ *' to 'LP#1^@'", although I can't recreate that one now.

Which approach is better? What do I need to fix to get this to work?

This is just for personal use, but this has been a bugger of a problem with me for a while, and I appreciate the help.
A little more about the program: Win32 Console app, menu-driven interface that controls a switch statement that calls these functions independently. I'm writing in VC Express 2008 on a Windows 7 (64-bit) computer. (Is that part of the difficulty?)
Thanks.
You must put quotes around your system call string, by escaping them.

system("C:\\Program Files (x86)\\Zone Labs\\ZoneAlarm\\zlclient.exe");
should be

system("\"C:\\Program Files (x86)\\Zone Labs\\ZoneAlarm\\zlclient.exe\"");
But you really shouldn't be using System calls.

Try the ShellExecute() function: http://msdn.microsoft.com/en-us/library/bb762153(v=vs.85).aspx

The rest, not sure.
Drop the start command in the CreateProcess() call, and do as KnuttyD says: Add quotation marks around filenames with spaces.
In your backup function you cast a const char* to LPWSTR, which is extremely bad. Use "L" in front instead and the compiler will use wchar_t automatically. Or just use TCHARs if you want to compile in both ANSI and UNICODE mode.

Also, CreateProcessW() DOES NOT use const wchar_t* as second argument, he can write to that memory buffer, unlike CreateProcessA().

This is WRONG:
1
2
3
4
5
6
7
8
9
10
11
STARTUPINFO si;
    PROCESS_INFORMATION pi;
	LPWSTR cmd = (LPWSTR) "start stxmanager.exe";

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

	CreateProcessW( NULL, cmd, NULL, NULL, FALSE, 0 , NULL, NULL, &si, &pi ) ;

.......................



Use this instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
STARTUPINFOW si;
    PROCESS_INFORMATION pi;
	LPWSTR cmd = new wchar_t[128 * sizeof(wchar_t)];
              wcscpy(cmd, L"start stxmanager.exe");


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

	CreateProcessW( NULL, cmd, NULL, NULL, FALSE, 0 , NULL, NULL, &si, &pi ) ;


         delete [] cmd; // free memory
..............................
Last edited on
Thanks. That works.
Fixed code below (the string in the function call is for another change I'm trying to make):
1
2
3
void ccleaner(System::String^ path){
	Process::Start("\"C:\\Program Files (x86)\\CCleaner\\CCleaner64.exe\"");//
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void backup(System::String^ path){
    STARTUPINFOW si;
    PROCESS_INFORMATION pi;
    LPWSTR cmd = new wchar_t[128 * sizeof(wchar_t)];
    wcscpy(cmd, L"\"C:\\Program Files (x86)\\Seagate\\SeagateManager\\ManagerApp\\stxmanager.exe\"");

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

    CreateProcessW( NULL, cmd, NULL, NULL, FALSE, 0 , NULL, NULL, &si, &pi ) ;


    delete [] cmd; // free memory
	
    // Wait until child process exits. //I don't want to wait!
    //WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    //Process::Start("\"C:\\Program Files (x86)\\Seagate\\SeagateManager\\ManagerApp\\stxmanager.exe\"");
}
Topic archived. No new replies allowed.