all groups > visual c libraries > december 2003 >
You're in the

visual c libraries

group:

_fileinfo (file handles are getting inherited by child process created via _spwanv)


_fileinfo (file handles are getting inherited by child process created via _spwanv) anil_goel
12/12/2003 2:06:04 AM
visual c libraries:
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.

Can somebody help with this regards,
thanks
Anil Goel
Re: _fileinfo (file handles are getting inherited by child process created via _spwanv) Doug Harrison [MVP]
12/12/2003 10:56:05 AM
[quoted text, click to view]

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
AddThis Social Bookmark Button