46. I can't make CreateProcess launch my process or handle its parameters. Why?
There are two main things that can go wrong when
using CreateProcess.
A. Unqualified App Names
The first problem is a failure to realise how
CreateProcess behaves when you give an application name. Let's say I call
CreateProcess like this :
BOOL bRet ;
STARTUPINFO sui ;
PROCESS_INFORMATION pi ;
sui.cb = sizeof (STARTUPINFO);
GetStartupInfo (&sui);
sui.dwFlags = STARTF_USESHOWWINDOW ;
sui.wShowWindow = SW_SHOW ;
bRet = CreateProcess ("myapp.exe",
NULL,
NULL, NULL, FALSE,
0, NULL, NULL,
&sui,
&pi);
Chances are that will fail. Why ? Because the
application name is not fully qualified. If you simply use the EXE name, in the
first parameter, CreateProcess requires that the EXE be fully qualified, i.e. it
contain the full path to the EXE file. If, on the other hand, you called it like
this :
bRet = CreateProcess (NULL,
"myapp.exe",
NULL, NULL,
FALSE,
0, NULL, NULL,
&sui,
&pi);
That would probably work, because if the EXE name
is given as part of the command line Windows will apply the same searching rules
as if you had simply typed the name at the command prompt, i.e. it will search
in the following places :
- The directory from which the application
loaded.
- The current directory for the parent process.
- Windows 95/98: The Windows system
directory.
Windows NT/2000: The 32-bit Windows
system directory (usual name System32).
- Windows NT/2000: The 16-bit Windows
system directory (usual name System).
- The Windows directory.
- The directories that are listed in the PATH
environment variable
If you give a command line which
contains spaces, you'll need to use quotes to make the actual command line
unambiguous, because Windows takes the first space-delimited token on the
command line to be the EXE name.
| NOTE: |
|
If you're launching a 16-bit
application under NT, you should always set the first parameter to NULL
and put the EXE name in the command line. |
B. Faulty Command Line
The second thing that commonly goes wrong is when
dealing with parameters. People forget that the second parameter to
CreateProcess is the full command line, not just the parameters to the EXE. Lets
take two examples :
CreateProcess ("c:\\notepad.exe",
"c:\\notepad.exe c:\\wibble.txt",
...);
will work fine (if there is a copy of
notepad.exe and a file called wibble.txt in the root of C:), whereas
CreateProcess ("c:\\notepad.exe",
"c:\\wibble.txt",
...);
will launch the EXE but fail to open the file. What this means is that when the
help systems calls the second parameter the command line, it ain't lying - it wants the whole command line. The annoying thing is that the help says
"Note that it is a common practice to repeat the module name as the first token in the command
line".
I didn't notice a "required" anywhere in there....
