Discussion:
Any possible fix for tmpfile() on Windows Vista ?
(too old to reply)
Timothy Madden
2010-09-03 09:09:24 UTC
Permalink
Hello

tmpfile() from <stdio.h> requires administrative privileges to run on
Windows Vista (but not on Windows XP or Windows 7) and will fail if
invoked in a non-elevated process.

Is there a Visual Studio library patch (like dinkumware patches, or STL
port, or a Microsoft VS CRT patch ...) to correct this problem ?

tmpfile() is important for my application because it has this property
that the file returned is automatically deleted when closed. Also it
opens a temporary files without the user having to worry about TEMP/TMP
environment varibles, or GetTemporaryDirectory() or temporary name
clashes / concurrency ...

Thank you,
Timothy Madden
aslan
2010-09-20 08:51:24 UTC
Permalink
I had the same prolem and created my tmpfile like this if you prefer:

FILE* mytmpfile()
{
char* name = _tempnam( NULL, NULL );

if( !name )
return 0;
FILE* fp = fopen(name, "wb+TD");

LOG("mytmpfile: name=%s\n", name);

// When name is no longer needed :
if(name)
free(name);

return fp;
}
Post by Timothy Madden
Hello
tmpfile() from <stdio.h> requires administrative privileges to run on
Windows Vista (but not on Windows XP or Windows 7) and will fail if
invoked in a non-elevated process.
Is there a Visual Studio library patch (like dinkumware patches, or STL
port, or a Microsoft VS CRT patch ...) to correct this problem ?
tmpfile() is important for my application because it has this property
that the file returned is automatically deleted when closed. Also it opens
a temporary files without the user having to worry about TEMP/TMP
environment varibles, or GetTemporaryDirectory() or temporary name clashes
/ concurrency ...
Thank you,
Timothy Madden
Timothy Madden
2010-09-24 11:42:29 UTC
Permalink
Post by aslan
FILE* mytmpfile()
{
char* name = _tempnam( NULL, NULL );
if( !name )
return 0;
FILE* fp = fopen(name, "wb+TD");
LOG("mytmpfile: name=%s\n", name);
if(name)
free(name);
return fp;
}
Post by Timothy Madden
Hello
tmpfile() from <stdio.h> requires administrative privileges to run on
Windows Vista (but not on Windows XP or Windows 7) and will fail if
invoked in a non-elevated process.
Is there a Visual Studio library patch (like dinkumware patches, or
STL port, or a Microsoft VS CRT patch ...) to correct this problem ?
Thank you for the "wb+TD" ideea, I think it is a good one !

I think mytmpfile() still runs the risk of temporary file names clashes
so I would also need a way to open and create the file only if it does
not exist, probably with _wopen with O_CREAT | O_EXCLUSIVE, and invoke
tmpnam() again if the file exists.

Thank you,
Timothy Madden

Loading...