[C++]CreateProcess() nie otwiera aplikacji, czemu?

Witam.

Mam dosyć prosty problem (tak mi się wydaje) dla osób które trochę kodu w swoim życiu napisały. Czytając opis funkcji CreateProcess w dokumentacji MSDN nie za bardzo rozumiem o co tam chodzi (być może ze względu na średnią znajomość angielskiego). Chciałbym uruchomić aplikacje “file.exe” znajdującą się w takim katalogu: “D:\Moje\file.exe”. Używam takiego kodu:

BOOL utworzono;

STARTUPINFO si;

PROCESS_INFORMATION pi;


ZeroMemory( &si, sizeof(si) );

si.cb = sizeof(si);

ZeroMemory( π, sizeof(pi) );

utworzono = CreateProcess( L"file.exe", L"D:\\Moje", NULL, NULL, FALSE, 0, NULL, NULL, &si, π ) ;

Oczywiście to nie działa. Funkcja mi zwraca kod błędu “2”, cokolwiek on oznacza. Obstawiam, że coś nie tak jest z pierwszym lub drugim parametrem funkcji. Ktoś mi może podpowiedzieć co i jak poprawić?

Przykład użycia masz na MSDN, zastosuj się do niego(hint: scal dwa pierwsze parametry w jeden, obojętnie który) :wink:

Kod błędu 2 to, wg MSDN, ERROR_FILE_NOT_FOUND, więc oznacza, że nie odnaleziono wskazanego przez Ciebie pliku.

Dokumentacja wyraźnie zaznacza, że parametr lpCommandLine nie może wskazywać na pamięć tylko do odczytu, a to co podałeś jest read-only :wink:

CreateProcess(L"D:\\Moje\\file.exe", NULL, /* ... */);

BOOL WINAPI CreateProcess(

__in_opt LPCTSTR lpApplicationName,

__inout_opt LPTSTR lpCommandLine,

lpApplicationName [in, optional]

The name of the module to be executed. This module can be a Windows-based application. It can be some other type of module (for example, MS-DOS or OS/2) if the appropriate subsystem is available on the local computer.

The string can specify the full path and file name of the module to execute or it can specify a partial name. In the case of a partial name, the function uses the current drive and current directory to complete the specification. The function will not use the search path. This parameter must include the file name extension; no default extension is assumed.

The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space–delimited token in the lpCommandLine string. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin; otherwise, the file name is ambiguous. For example, consider the string “c:\program files\sub dir\program name”. This string can be interpreted in a number of ways. The system tries to interpret the possibilities in the following order:

c:\program.exe files\sub dir\program name

c:\program files\sub.exe dir\program name

c:\program files\sub dir\program.exe name

c:\program files\sub dir\program name.exe

If the executable module is a 16-bit application, lpApplicationName should be NULL, and the string pointed to by lpCommandLine should specify the executable module as well as its arguments.

To run a batch file, you must start the command interpreter; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.

CreateProcess( L"D:\Moje\file.exe", NULL, …

Dzięki wielkie za podpowiedzi. Teraz sprawa wydaje się bardziej oczywista i jasna. Oraz oczywiście programik się włącza i całość działa tak jak się należy :slight_smile: Temat właściwie do zamknięcia.