Skip to content

Mark borrowed handle returns as DoNotRelease - #2380

Draft
Jevan Saks (jevansaks) wants to merge 1 commit into
mainfrom
user/jevansaks/fix-borrowed-handle-ownership
Draft

Jevan Saks (jevansaks) wants to merge 1 commit into
mainfrom
user/jevansaks/fix-borrowed-handle-ownership

Conversation

@jevansaks

@jevansaks Jevan Saks (jevansaks) commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Fixes #2379

Adds [DoNotRelease] to the return value of APIs that hand back a handle the caller doesn't own, or one whose release function depends on runtime data. Also removes the incorrect RAIIFree annotations on CreateEnhMetaFileA/W and CreateMetaFileA/W. #2379 has the per-API rationale.

For CopyImage, LoadImageA/W, RemovePropA/W, and OleDuplicateData, the caller usually owns the result, but the release function depends on what the result is (image type, property data, or clipboard format). [DoNotRelease] is the only way to override the HANDLE typedef's default CloseHandle. Here it means projections must not release the value automatically; the caller still frees it with the matching function. microsoft/CsWin32#1863 tracks projecting [DoNotRelease] returns as the raw ABI type so callers make that choice.

DoNotRelease added: CopyImage, FindExecutableA/W, GetColorSpace, GetCurrentObject, GetCurrentThread, GetCursor, GetDriverModuleHandle, GetKeyboardLayout, GetMenu, GetProcessWindowStation, GetPropA/W, GetStockObject, GetSubMenu, GetSysColorBrush, GetSystemMenu, GetThreadDesktop, GetWindowTheme, LoadIconA/W, LoadImageA/W, OleDuplicateData, RemovePropA/W, SelectObject, SelectPalette, SetClipboardData, SetColorSpace, SetCursor

RAIIFree removed: CreateEnhMetaFileA/W (DeleteEnhMetaFile), CreateMetaFileA/W (DeleteMetaFile)

Validation

  • The baseline API diff contains only these attribute changes; no signatures change.
  • I validated these by calling each real API through CsWin32's generated SafeHandle overloads. The tests check that the SafeHandle doesn't own (and so won't release) the returned handle and, where it's observable, that disposing it leaves the underlying menu, bitmap, or similar object intact. Against the current metadata package all 21 fail; against a Windows.Win32.winmd built from this branch all 21 pass, along with the rest of CsWin32's sandbox suite. They aren't checked in to CsWin32 yet, because they would fail there until it consumes a metadata package that includes this change.

Several APIs return a handle the caller doesn't own (pseudo-handles, system or
shared objects, objects owned by a window or DC, or an error code typed as a
handle), or one whose release function depends on runtime data. Without
[DoNotRelease], projections treat the result as caller-owned and destroy it on
dispose. Also remove the RAIIFree annotations on CreateEnhMetaFile and
CreateMetaFile, which named functions that don't accept the returned HDC.

Fixes #2379

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 61981950-206c-4b2c-844e-c87ed4af9432
@github-actions

Copy link
Copy Markdown

📋 API Surface Diff

+31 additions / -4 deletions vs main branch (full validation run)

Click to expand API diff
diff --git "a/bin\\baseline.apidump.cs" "b/bin\\current.apidump.cs"
index bc6e749a..985e2723 100644
--- "a/bin\\baseline.apidump.cs"
+++ "b/bin\\current.apidump.cs"
@@ -201476,14 +201476,12 @@ public static class Apis
 	[SupportedOSPlatform ("windows5.0")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-createmetafilea")]
-	[return: RAIIFree ("DeleteMetaFile")]
 	public static extern HDC CreateMetaFileA ([Optional][In][Const] PSTR pszFile);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-createmetafilew")]
-	[return: RAIIFree ("DeleteMetaFile")]
 	public static extern HDC CreateMetaFileW ([Optional][In][Const] PWSTR pszFile);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -201773,6 +201771,7 @@ public static class Apis
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-getcurrentobject")]
+	[return: DoNotRelease]
 	public static extern HGDIOBJ GetCurrentObject ([In] HDC hdc, [In][AssociatedEnum ("OBJ_TYPE")] uint type);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -201897,6 +201896,7 @@ public static class Apis
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-getstockobject")]
+	[return: DoNotRelease]
 	public static extern HGDIOBJ GetStockObject ([In] GET_STOCK_OBJECT_FLAGS i);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -202242,11 +202242,13 @@ public static class Apis
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-selectobject")]
+	[return: DoNotRelease]
 	public static extern HGDIOBJ SelectObject ([In] HDC hdc, [In] HGDIOBJ h);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-selectpalette")]
+	[return: DoNotRelease]
 	public static extern HPALETTE SelectPalette ([In] HDC hdc, [In] HPALETTE hPal, [In] BOOL bForceBkgd);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -202455,14 +202457,12 @@ public static class Apis
 	[SupportedOSPlatform ("windows5.0")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-createenhmetafilea")]
-	[return: RAIIFree ("DeleteEnhMetaFile")]
 	public unsafe static extern HDC CreateEnhMetaFileA ([Optional][In] HDC hdc, [Optional][In][Const] PSTR lpFilename, [Optional][In][Const] RECT* lprc, [Optional][In][Const] PSTR lpDesc);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-createenhmetafilew")]
-	[return: RAIIFree ("DeleteEnhMetaFile")]
 	public unsafe static extern HDC CreateEnhMetaFileW ([Optional][In] HDC hdc, [Optional][In][Const] PWSTR lpFilename, [Optional][In][Const] RECT* lprc, [Optional][In][Const] PWSTR lpDesc);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -203198,6 +203198,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getsyscolorbrush")]
+	[return: DoNotRelease]
 	public static extern HBRUSH GetSysColorBrush ([In] SYS_COLOR_INDEX nIndex);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
@@ -345835,6 +345836,7 @@ public static class Apis
 	[DllImport ("WINMM.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/mmiscapi/nf-mmiscapi-getdrivermodulehandle")]
+	[return: DoNotRelease]
 	public static extern HMODULE GetDriverModuleHandle ([In] HDRVR hDriver);
 
 	[DllImport ("WINMM.dll", ExactSpelling = true, PreserveSig = false)]
@@ -655909,6 +655911,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-setclipboarddata")]
+	[return: DoNotRelease]
 	public static extern HANDLE SetClipboardData ([In] uint uFormat, [Optional][In] HANDLE hMem);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
@@ -745500,6 +745503,7 @@ public static class Apis
 	[DllImport ("OLE32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/ole2/nf-ole2-oleduplicatedata")]
+	[return: DoNotRelease]
 	public static extern HANDLE OleDuplicateData ([In] HANDLE hSrc, [In] CLIPBOARD_FORMAT cfFormat, [In] GLOBAL_ALLOC_FLAGS uiFlags);
 
 	[DllImport ("OLE32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -802867,6 +802871,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getthreaddesktop")]
+	[return: DoNotRelease]
 	public static extern HDESK GetThreadDesktop ([In] uint dwThreadId);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
@@ -802918,6 +802923,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getprocesswindowstation")]
+	[return: DoNotRelease]
 	public static extern HWINSTA GetProcessWindowStation ();
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
@@ -818138,6 +818144,7 @@ public static class Apis
 	[DllImport ("KERNEL32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentthread")]
+	[return: DoNotRelease]
 	public static extern HANDLE GetCurrentThread ();
 
 	[DllImport ("KERNEL32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -856342,6 +856349,7 @@ public static class Apis
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-getcolorspace")]
+	[return: DoNotRelease]
 	public static extern HCOLORSPACE GetColorSpace ([In] HDC hdc);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -856371,6 +856379,7 @@ public static class Apis
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/wingdi/nf-wingdi-setcolorspace")]
+	[return: DoNotRelease]
 	public static extern HCOLORSPACE SetColorSpace ([In] HDC hdc, [In] HCOLORSPACE hcs);
 
 	[DllImport ("GDI32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -864169,6 +864178,7 @@ public static class Apis
 	[DllImport ("UXTHEME.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows6.0.6000")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/uxtheme/nf-uxtheme-getwindowtheme")]
+	[return: DoNotRelease]
 	public static extern HTHEME GetWindowTheme ([In] HWND hwnd);
 
 	[DllImport ("UxTheme.dll", ExactSpelling = true, PreserveSig = false)]
@@ -888491,6 +888501,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getkeyboardlayout")]
+	[return: DoNotRelease]
 	public static extern HKL GetKeyboardLayout ([In] uint idThread);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
@@ -898372,12 +898383,14 @@ public static class Apis
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/shellapi/nf-shellapi-findexecutablea")]
+	[return: DoNotRelease]
 	public static extern HINSTANCE FindExecutableA ([In][Const] PSTR lpFile, [Optional][In][Const] PSTR lpDirectory, [Out][NativeArrayInfo (CountConst = 260)] PSTR lpResult);
 
 	[DllImport ("SHELL32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/shellapi/nf-shellapi-findexecutablew")]
+	[return: DoNotRelease]
 	public static extern HINSTANCE FindExecutableW ([In][Const] PWSTR lpFile, [Optional][In][Const] PWSTR lpDirectory, [Out][NativeArrayInfo (CountConst = 260)] PWSTR lpResult);
 
 	[DllImport ("SHELL32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -939412,6 +939425,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getmenu")]
+	[return: DoNotRelease]
 	public static extern HMENU GetMenu ([In] HWND hWnd);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
@@ -939457,6 +939471,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getsystemmenu")]
+	[return: DoNotRelease]
 	public static extern HMENU GetSystemMenu ([In] HWND hWnd, [In] BOOL bRevert);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
@@ -939487,6 +939502,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getsubmenu")]
+	[return: DoNotRelease]
 	public static extern HMENU GetSubMenu ([In] HMENU hMenu, [In] int nPos);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -939717,24 +939733,28 @@ public static class Apis
 	[SupportedOSPlatform ("windows5.0")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getpropa")]
+	[return: DoNotRelease]
 	public static extern HANDLE GetPropA ([In] HWND hWnd, [In][Const] PSTR lpString);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getpropw")]
+	[return: DoNotRelease]
 	public static extern HANDLE GetPropW ([In] HWND hWnd, [In][Const] PWSTR lpString);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-removepropa")]
+	[return: DoNotRelease]
 	public static extern HANDLE RemovePropA ([In] HWND hWnd, [In][Const] PSTR lpString);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-removepropw")]
+	[return: DoNotRelease]
 	public static extern HANDLE RemovePropW ([In] HWND hWnd, [In][Const] PWSTR lpString);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -939871,6 +939891,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-setcursor")]
+	[return: DoNotRelease]
 	public static extern HCURSOR SetCursor ([Optional][In] HCURSOR hCursor);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
@@ -939891,6 +939912,7 @@ public static class Apis
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getcursor")]
+	[return: DoNotRelease]
 	public static extern HCURSOR GetCursor ();
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
@@ -940233,12 +940255,14 @@ public static class Apis
 	[SupportedOSPlatform ("windows5.0")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-loadicona")]
+	[return: DoNotRelease]
 	public static extern HICON LoadIconA ([Optional][In] HINSTANCE hInstance, [In][Const] PSTR lpIconName);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-loadiconw")]
+	[return: DoNotRelease]
 	public static extern HICON LoadIconW ([Optional][In] HINSTANCE hInstance, [In][Const] PWSTR lpIconName);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false)]
@@ -940287,17 +940311,20 @@ public static class Apis
 	[SupportedOSPlatform ("windows5.0")]
 	[Ansi]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-loadimagea")]
+	[return: DoNotRelease]
 	public static extern HANDLE LoadImageA ([Optional][In] HINSTANCE hInst, [In][Const] PSTR name, [In] GDI_IMAGE_TYPE type, [In] int cx, [In] int cy, [In] IMAGE_FLAGS fuLoad);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Unicode]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-loadimagew")]
+	[return: DoNotRelease]
 	public static extern HANDLE LoadImageW ([Optional][In] HINSTANCE hInst, [In][Const] PWSTR name, [In] GDI_IMAGE_TYPE type, [In] int cx, [In] int cy, [In] IMAGE_FLAGS fuLoad);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-copyimage")]
+	[return: DoNotRelease]
 	public static extern HANDLE CopyImage ([In] HANDLE h, [In] GDI_IMAGE_TYPE type, [In] int cx, [In] int cy, [In] IMAGE_FLAGS flags);
 
 	[DllImport ("USER32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]

This comment is automatically updated on each push.

Jevan Saks (jevansaks) added a commit to microsoft/CsWin32 that referenced this pull request Oct 21, 2026
Remove the 21 HandleOwnershipTests that were skipped until CsWin32 consumes
the annotations from microsoft/win32metadata#2380. Each would have needed a
CsWin32 change once the metadata is fixed. The remaining tests cover APIs
whose ownership is already correct, and pass against both the current
metadata package and a winmd built with #2380.

Drop the NativeMethods.txt entries and helpers that only those tests used.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 61981950-206c-4b2c-844e-c87ed4af9432
Jevan Saks (jevansaks) added a commit to microsoft/CsWin32 that referenced this pull request Oct 22, 2026
…ions (#1864)

* Fix OverflowException for sign-extended handles in SafeHandle conversions

#1844 converted typedef handles to IntPtr with an explicit `(IntPtr)ptr`
cast, and leased SafeHandles back to `(void*)nint`. Under
CheckForOverflowUnderflow those casts throw for handles with the high bit
set: INVALID_HANDLE_VALUE, pseudo-handles such as GetCurrentProcess(), and
sign-extended GDI/USER handles. Before #1844 the conversion went through the
typedef's own IntPtr operator and never threw. Emit both conversions in an
unchecked context.

Add sandbox tests that call real APIs (pseudo-handles, failure handles, and
thousands of GDI bitmaps) through the SafeHandle overloads and leases.

Add HandleOwnershipTests, which call real APIs to verify whether each
returned SafeHandle owns its handle. Tests for the annotations added by
microsoft/win32metadata#2380 are skipped until CsWin32 consumes a metadata
package that contains them.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 61981950-206c-4b2c-844e-c87ed4af9432

* Keep only ownership tests that pass with current and corrected metadata

Remove the 21 HandleOwnershipTests that were skipped until CsWin32 consumes
the annotations from microsoft/win32metadata#2380. Each would have needed a
CsWin32 change once the metadata is fixed. The remaining tests cover APIs
whose ownership is already correct, and pass against both the current
metadata package and a winmd built with #2380.

Drop the NativeMethods.txt entries and helpers that only those tests used.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 61981950-206c-4b2c-844e-c87ed4af9432

---------

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 61981950-206c-4b2c-844e-c87ed4af9432

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Several APIs that return borrowed handles are missing [DoNotRelease]

1 participant