Repository navigation
Mark borrowed handle returns as DoNotRelease - #2380
Draft
Jevan Saks (jevansaks) wants to merge 1 commit into
Draft
Jevan Saks (jevansaks) wants to merge 1 commit into
Jevan Saks (jevansaks) wants to merge 1 commit into
Conversation
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
📋 API Surface Diff+31 additions / -4 deletions vs main branch (full validation run) Click to expand API diffdiff --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)]
|
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 incorrectRAIIFreeannotations onCreateEnhMetaFileA/WandCreateMetaFileA/W. #2379 has the per-API rationale.For
CopyImage,LoadImageA/W,RemovePropA/W, andOleDuplicateData, 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 theHANDLEtypedef's defaultCloseHandle. 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,SetCursorRAIIFree removed:
CreateEnhMetaFileA/W(DeleteEnhMetaFile),CreateMetaFileA/W(DeleteMetaFile)Validation
Windows.Win32.winmdbuilt 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.