I'm trying to use CreateProcess
to start a child process, however I keep getting error 2
, which, according to the documentation is file not found.
My code looks like this:
if (!(CreateProcess(LPCTSTR("test.exe") ,NULL ,NULL,NULL,FALSE ,0 ,NULL ,NULL ,&producer_si
,&producer)))
{
printf("Create process failed!(%d)\n", GetLastError());
}
Where test.exe
is an executable program which I created earlier. The child process is very simple, with the code looking like this:
void _tmain (int argc, TCHAR* argv[])
{
printf("%s\n", "hello!");
}
test.exe
is also found in the same folder as the parent process. I'm not understanding why I'm always getting an error code of 2.
Error 2 is ERROR_FILE_NOT_FOUND
. As others have told you, you are relying on a relative path when you need to use an absolute path instead.
Also, LPCTSTR("test.exe")
is not valid code. If UNICODE
is defined, CreateFile()
maps to CreateFileW()
, and LPCTSTR
maps to LPCWSTR
ie const wchar_t*
. You cannot typecast a char*
to a wchar_t*
and end up with meaningful data. If you want to use TCHAR
-sensitive literals, use the TEXT()
macro instead, eg:
if (!CreateProcess(TEXT("full path to\\test.exe"), ...))
Otherwise, forget using TCHAR
and just write Ansi-specific or Unicode-specific code instead, depending on your needs:
if (!CreateProcessA("full path to\\test.exe", ...))
if (!CreateProcessW(L"full path to\\test.exe", ...))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments