[quoted text, click to view] anil_goel wrote:
>Hi,
>
>I have this sample program
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <process.h>
>
>void main()
>{
> char *args[2];
> int ret = 0;
> args[0] = "D:\\tmp\\Test.exe";
> args[1] = NULL;
>
> _fileinfo = 0;
>
> FILE *fp = fopen("D:\\tmp\\abc.txt","w");
>
> ret = _spawnv (_P_NOWAIT, "D:\\tmp\\Test.exe", args);
>
> fprintf (fp, "******** TEST ********\n");
> fclose(fp);
>
> if (! DeleteFile ("D:\\tmp\\abc.txt")) {
> printf ("\nError : %d\n", GetLastError());
> }
>}
>
>
>Here DeleteFile is failing with the sharing violation error. The file handles are getting inherited to the child process irrespective of the flag _fileinfo 's value. The documentation says that if _fileinfo is set to 0 then the open file handles will not be inherited, but in my case it is not working.
After grepping the CRT source, it appears to me that _fileinfo isn't used
for anything, nor is the _C_FILE_INFO environment variable it used to
control used. I think your best bet is to open the file with
_sopen(_O_NOINHERIT) and attach a FILE to it with _fdopen.
There are really two things going on. First, there is a mapping between
OS-level file HANDLEs and C-level file descriptors ("fd"). The
_sopen(_O_NOINHERIT) affects HANDLE (and thus fd) inheritance for a given
file, which is what your program needs to suppress. The second thing
concerns communication of the HANDLE/fd mapping between the parent and child
process, and that's what _fileinfo and _C_FILE_INFO used to control.
Nowadays, the spawn functions communicate this mapping through the
STARTUPINFO lpReserved2 member, and you can find the code for that in
dospawn.c. So even if _fileinfo worked, it wouldn't do what you want it to
do, which is prevent inheritance; it only ever controlled communication of
the mapping information.
--
Doug Harrison