Skip to content

Commit a9c8921

Browse files
committed
Avoid calling GetTempFileName in BitmapDownload
Replace the Win32 GetTempFileName P/Invoke with Path.GetTempFileName, whose .NET 8+ implementation avoids the 65k file limit by generating random filenames internally. Fixes #259
1 parent 3eeab6e commit a9c8921

2 files changed

Lines changed: 5 additions & 22 deletions

File tree

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDownload.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using MS.Internal;
1010
using System.Net;
1111
using System.Net.Cache;
12-
using System.Text;
1312
using MS.Win32;
1413
using Microsoft.Win32.SafeHandles;
1514

@@ -111,16 +110,14 @@ Stream stream
111110
entry.inputUri = uri;
112111
entry.inputStream = stream;
113112

114-
string cacheFolder = MS.Win32.WinInet.InternetCacheFolder.LocalPath;
115113
bool passed = false;
116114

117-
// Get the file path
118-
StringBuilder tmpFileName = new StringBuilder(NativeMethods.MAX_PATH);
119-
MS.Win32.UnsafeNativeMethods.GetTempFileName(cacheFolder, "WPF", 0, tmpFileName);
120-
115+
// Use Path.GetTempFileName whose .NET 8+ implementation avoids the
116+
// Win32 GetTempFileName 65k file limit by generating random filenames.
117+
string pathToUse = Path.GetTempFileName();
118+
121119
try
122120
{
123-
string pathToUse = tmpFileName.ToString();
124121
SafeFileHandle fileHandle = MS.Win32.UnsafeNativeMethods.CreateFile(
125122
pathToUse,
126123
dwDesiredAccess: NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE,
@@ -135,7 +132,7 @@ Stream stream
135132
{
136133
throw new Win32Exception();
137134
}
138-
135+
139136
entry.outputStream = new FileStream(fileHandle, FileAccess.ReadWrite);
140137
entry.streamPath = pathToUse;
141138
passed = true;

src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsOther.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@ namespace MS.Win32
1414
{
1515
internal partial class UnsafeNativeMethods
1616
{
17-
[DllImport(ExternDll.Kernel32, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "GetTempFileName")]
18-
internal static extern uint _GetTempFileName(string tmpPath, string prefix, uint uniqueIdOrZero, StringBuilder tmpFileName);
19-
20-
internal static uint GetTempFileName(string tmpPath, string prefix, uint uniqueIdOrZero, StringBuilder tmpFileName)
21-
{
22-
uint result = _GetTempFileName(tmpPath, prefix, uniqueIdOrZero, tmpFileName);
23-
if (result == 0)
24-
{
25-
throw new Win32Exception();
26-
}
27-
28-
return result;
29-
}
30-
3117
[DllImport(ExternDll.Shell32, CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
3218
internal static extern int ExtractIconEx(
3319
string szExeFileName,

0 commit comments

Comments
 (0)