Skip to content

Add RAIIFree annotations for HidD_GetPreparsedData and related producers - #2376

Merged
Jevan Saks (jevansaks) merged 1 commit into
mainfrom
jevansaks-hid-raiifree-annotations
Oct 19, 2026
Merged

Jevan Saks (jevansaks) merged 1 commit into
mainfrom
jevansaks-hid-raiifree-annotations

Conversation

@jevansaks

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

Copy link
Copy Markdown
Member

Fixes #2375

Adds RAIIFree to 25 producer out-parameters so projections can wrap each returned handle in an owning type. Adds InvalidHandleValue(0) to the 8 handle typedefs that had no invalid value.

The annotations go on the producers. A typedef such as PHIDP_PREPARSED_DATA also describes caller-allocated buffers, such as the one GetRawInputDeviceInfo(RIDI_PREPARSEDDATA) fills, and HidD_FreePreparsedData must not free those. The typedefs carry no RAIIFree, and the HidP_* functions that consume the handle keep plain parameters.

A projection needs an invalid value to tell an empty owner from a live one. The 7 typedefs that already had -1, 0 are unchanged.

Handle typedef Producer (out-parameter) Cleanup Invalid values
PHIDP_PREPARSED_DATA HidD_GetPreparsedData (PreparsedData) HidD_FreePreparsedData adds 0
HDEVQUERY DevCreateObjectQuery, DevCreateObjectQueryEx, DevCreateObjectQueryFromId, DevCreateObjectQueryFromIdEx, DevCreateObjectQueryFromIds, DevCreateObjectQueryFromIdsEx (phDevQuery) DevCloseObjectQuery adds 0
HAMSICONTEXT AmsiInitialize (amsiContext) AmsiUninitialize already -1, 0
NCRYPT_DESCRIPTOR_HANDLE NCryptCreateProtectionDescriptor (phDescriptor) NCryptCloseProtectionDescriptor already -1, 0
NCRYPT_STREAM_HANDLE NCryptStreamOpenToProtect, NCryptStreamOpenToUnprotect, NCryptStreamOpenToUnprotectEx (phStream) NCryptStreamClose already -1, 0
SAFER_LEVEL_HANDLE SaferCreateLevel, SaferIdentifyLevel (pLevelHandle) SaferCloseLevel already -1, 0
PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE CreatePackageVirtualizationContext (context), DuplicatePackageVirtualizationContext (destContext) ReleasePackageVirtualizationContext adds 0
PACKAGEDEPENDENCY_CONTEXT AddPackageDependency, AddPackageDependency2 (packageDependencyContext) RemovePackageDependency adds 0
PRJ_NAMESPACE_VIRTUALIZATION_CONTEXT PrjStartVirtualizing (namespaceVirtualizationContext) PrjStopVirtualizing already -1, 0
HTTP_PUSH_WAIT_HANDLE HttpPushEnable (phWait) HttpPushClose already -1, 0
PSC_NOTIFICATION_REGISTRATION SubscribeServiceChangeNotifications (pSubscription) UnsubscribeServiceChangeNotifications adds 0
PAPPSTATE_REGISTRATION RegisterAppStateChangeNotification (Registration) UnregisterAppStateChangeNotification adds 0
PAPPCONSTRAIN_REGISTRATION RegisterAppConstrainedChangeNotification (Registration) UnregisterAppConstrainedChangeNotification adds 0
RO_REGISTRATION_COOKIE RoRegisterActivationFactories (cookie) RoRevokeActivationFactories adds 0
APARTMENT_SHUTDOWN_REGISTRATION_COOKIE RoRegisterForApartmentShutdown (regCookie) RoUnregisterForApartmentShutdown already -1, 0

The handles in the "Left out" section of #2375 are unchanged.

CsWin32

HidD_FreePreparsedData returns BOOLEAN. CsWin32 needs support for BOOLEAN cleanup results before it generates a SafeHandle for the HID pair. That support is in microsoft/CsWin32#1859.

Validation

  • scripts/DoTests.ps1 after a full scrape and emit for x86, x64, and arm64: MetadataUtils.Tests 17/17, Win32MetadataScraperTests 50/50, Windows.Win32.Tests 13/13. The local build ran on an ARM64 host, so its scraper used -target x86_64-pc-windows-msvc to match the target that the x64 CI hosts use.
  • The built winmd has RAIIFree with the expected cleanup function on all 25 parameters and InvalidHandleValue(0) on all 8 typedefs.
  • A declaration diff against 71.0.30-preview contains only these 25 parameters, these 8 typedefs, and the ContainsInteriorPointers changes from Annotate 77 more caller-owned buffers containing interior pointers #2356.
  • With Generate SafeHandle owners for cleanup functions that return BOOLEAN CsWin32#1859 and this winmd, CsWin32 generates an owner for all 15 handle types. Each owner's ReleaseHandle calls the cleanup function, IsInvalid treats 0 as invalid, and every producer has an overload with an out owner parameter. With the 71.0.30-preview winmd, all 15 of these checks fail.
  • On real HID hardware, HidP_GetCaps succeeded through both DangerousValue and Lease() on 32 of 33 interfaces, and ReleaseHandle returned true. Over 20,000 get-and-dispose cycles, private bytes grew 996 KB. The same loop without a free grew 9,520 KB.

Annotate 25 producer out-parameters with the cleanup function that releases
the handle they return, and mark 0 as the invalid value for the 8 handle
typedefs that had no invalid value.

The annotations go on the producers. Typedefs such as PHIDP_PREPARSED_DATA
also describe caller-allocated buffers that the cleanup function must not
free, so the typedefs carry no RAIIFree.

Fixes #2375

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 62a90e5b-2b4b-46ac-b2e3-4a42da4989fc
@github-actions

Copy link
Copy Markdown

📋 API Surface Diff

+33 additions / -25 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 6b9b9523..d73bad8d 100644
--- "a/bin\\baseline.apidump.cs"
+++ "b/bin\\current.apidump.cs"
@@ -34010,22 +34010,22 @@ namespace Windows.Win32.Devices.DeviceQuery;
 public static class Apis
 {
 	[DllImport ("api-ms-win-devices-query-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern HRESULT DevCreateObjectQuery ([In] DEV_OBJECT_TYPE ObjectType, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 2)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 4)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out] HDEVQUERY* phDevQuery);
+	public unsafe static extern HRESULT DevCreateObjectQuery ([In] DEV_OBJECT_TYPE ObjectType, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 2)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 4)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out][RAIIFree ("DevCloseObjectQuery")] HDEVQUERY* phDevQuery);
 
 	[DllImport ("api-ms-win-devices-query-l1-1-1.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern HRESULT DevCreateObjectQueryEx ([In] DEV_OBJECT_TYPE ObjectType, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 2)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 4)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] uint cExtendedParameterCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 6)] DEV_QUERY_PARAMETER* pExtendedParameters, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out] HDEVQUERY* phDevQuery);
+	public unsafe static extern HRESULT DevCreateObjectQueryEx ([In] DEV_OBJECT_TYPE ObjectType, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 2)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 4)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] uint cExtendedParameterCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 6)] DEV_QUERY_PARAMETER* pExtendedParameters, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out][RAIIFree ("DevCloseObjectQuery")] HDEVQUERY* phDevQuery);
 
 	[DllImport ("api-ms-win-devices-query-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern HRESULT DevCreateObjectQueryFromId ([In] DEV_OBJECT_TYPE ObjectType, [In][Const] PWSTR pszObjectId, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 3)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 5)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out] HDEVQUERY* phDevQuery);
+	public unsafe static extern HRESULT DevCreateObjectQueryFromId ([In] DEV_OBJECT_TYPE ObjectType, [In][Const] PWSTR pszObjectId, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 3)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 5)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out][RAIIFree ("DevCloseObjectQuery")] HDEVQUERY* phDevQuery);
 
 	[DllImport ("api-ms-win-devices-query-l1-1-1.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern HRESULT DevCreateObjectQueryFromIdEx ([In] DEV_OBJECT_TYPE ObjectType, [In][Const] PWSTR pszObjectId, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 3)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 5)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] uint cExtendedParameterCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 7)] DEV_QUERY_PARAMETER* pExtendedParameters, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out] HDEVQUERY* phDevQuery);
+	public unsafe static extern HRESULT DevCreateObjectQueryFromIdEx ([In] DEV_OBJECT_TYPE ObjectType, [In][Const] PWSTR pszObjectId, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 3)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 5)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] uint cExtendedParameterCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 7)] DEV_QUERY_PARAMETER* pExtendedParameters, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out][RAIIFree ("DevCloseObjectQuery")] HDEVQUERY* phDevQuery);
 
 	[DllImport ("api-ms-win-devices-query-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern HRESULT DevCreateObjectQueryFromIds ([In] DEV_OBJECT_TYPE ObjectType, [In][Const][NotNullTerminated][NullNullTerminated] PWSTR pszzObjectIds, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 3)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 5)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out] HDEVQUERY* phDevQuery);
+	public unsafe static extern HRESULT DevCreateObjectQueryFromIds ([In] DEV_OBJECT_TYPE ObjectType, [In][Const][NotNullTerminated][NullNullTerminated] PWSTR pszzObjectIds, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 3)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 5)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out][RAIIFree ("DevCloseObjectQuery")] HDEVQUERY* phDevQuery);
 
 	[DllImport ("api-ms-win-devices-query-l1-1-1.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern HRESULT DevCreateObjectQueryFromIdsEx ([In] DEV_OBJECT_TYPE ObjectType, [In][Const][NotNullTerminated][NullNullTerminated] PWSTR pszzObjectIds, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 3)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 5)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] uint cExtendedParameterCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 7)] DEV_QUERY_PARAMETER* pExtendedParameters, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out] HDEVQUERY* phDevQuery);
+	public unsafe static extern HRESULT DevCreateObjectQueryFromIdsEx ([In] DEV_OBJECT_TYPE ObjectType, [In][Const][NotNullTerminated][NullNullTerminated] PWSTR pszzObjectIds, [In] uint QueryFlags, [In] uint cRequestedProperties, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 3)] DEVPROPCOMPKEY* pRequestedProperties, [In] uint cFilterExpressionCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 5)] DEVPROP_FILTER_EXPRESSION* pFilter, [In] uint cExtendedParameterCount, [Optional][In][Const][NativeArrayInfo (CountParamIndex = 7)] DEV_QUERY_PARAMETER* pExtendedParameters, [In] PDEV_QUERY_RESULT_CALLBACK pCallback, [Optional][In] void* pContext, [Out][RAIIFree ("DevCloseObjectQuery")] HDEVQUERY* phDevQuery);
 
 	[DllImport ("api-ms-win-devices-query-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
 	public static extern void DevCloseObjectQuery ([In] HDEVQUERY hDevQuery);
@@ -34225,6 +34225,7 @@ using Windows.Win32.Foundation.Metadata;
 
 namespace Windows.Win32.Devices.DeviceQuery;
 
+[InvalidHandleValue (0L)]
 [NativeTypedef]
 public struct HDEVQUERY
 {
@@ -63482,7 +63483,7 @@ public static class Apis
 	public unsafe static extern void HidD_GetHidGuid ([Out] Guid* HidGuid);
 
 	[DllImport ("HID.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern BOOLEAN HidD_GetPreparsedData ([In] HANDLE HidDeviceObject, [Out] PHIDP_PREPARSED_DATA* PreparsedData);
+	public unsafe static extern BOOLEAN HidD_GetPreparsedData ([In] HANDLE HidDeviceObject, [Out][RAIIFree ("HidD_FreePreparsedData")] PHIDP_PREPARSED_DATA* PreparsedData);
 
 	[DllImport ("HID.dll", ExactSpelling = true, PreserveSig = false)]
 	public static extern BOOLEAN HidD_FreePreparsedData ([In] PHIDP_PREPARSED_DATA PreparsedData);
@@ -66481,6 +66482,7 @@ using Windows.Win32.Foundation.Metadata;
 
 namespace Windows.Win32.Devices.HumanInterfaceDevice;
 
+[InvalidHandleValue (0L)]
 [NativeTypedef]
 public struct PHIDP_PREPARSED_DATA
 {
@@ -472507,7 +472509,7 @@ public static class Apis
 	public unsafe static extern uint HttpGetServerCredentials ([In] PWSTR pwszUrl, [Out] PWSTR* ppwszUserName, [Out] PWSTR* ppwszPassword);
 
 	[DllImport ("WININET.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern uint HttpPushEnable ([In] void* hRequest, [In] HTTP_PUSH_TRANSPORT_SETTING* pTransportSetting, [Out] HTTP_PUSH_WAIT_HANDLE* phWait);
+	public unsafe static extern uint HttpPushEnable ([In] void* hRequest, [In] HTTP_PUSH_TRANSPORT_SETTING* pTransportSetting, [Out][RAIIFree ("HttpPushClose")] HTTP_PUSH_WAIT_HANDLE* phWait);
 
 	[DllImport ("WININET.dll", ExactSpelling = true, PreserveSig = false)]
 	public unsafe static extern uint HttpPushWait ([In] HTTP_PUSH_WAIT_HANDLE hWait, [In] HTTP_PUSH_WAIT_TYPE eType, [Optional][Out] HTTP_PUSH_NOTIFICATION_STATUS* pNotificationStatus);
@@ -494024,7 +494026,7 @@ public static class Apis
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winsafer/nf-winsafer-safercreatelevel")]
-	public unsafe static extern BOOL SaferCreateLevel ([In] uint dwScopeId, [In] uint dwLevelId, [In] uint OpenFlags, [Out] SAFER_LEVEL_HANDLE* pLevelHandle, [Optional][Reserved] void* lpReserved);
+	public unsafe static extern BOOL SaferCreateLevel ([In] uint dwScopeId, [In] uint dwLevelId, [In] uint OpenFlags, [Out][RAIIFree ("SaferCloseLevel")] SAFER_LEVEL_HANDLE* pLevelHandle, [Optional][Reserved] void* lpReserved);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
@@ -494034,7 +494036,7 @@ public static class Apis
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/winsafer/nf-winsafer-saferidentifylevel")]
-	public unsafe static extern BOOL SaferIdentifyLevel ([In] uint dwNumProperties, [Optional][In][NativeArrayInfo (CountParamIndex = 0)] SAFER_CODE_PROPERTIES_V2* pCodeProperties, [Out] SAFER_LEVEL_HANDLE* pLevelHandle, [Optional][In] void* lpReserved);
+	public unsafe static extern BOOL SaferIdentifyLevel ([In] uint dwNumProperties, [Optional][In][NativeArrayInfo (CountParamIndex = 0)] SAFER_CODE_PROPERTIES_V2* pCodeProperties, [Out][RAIIFree ("SaferCloseLevel")] SAFER_LEVEL_HANDLE* pLevelHandle, [Optional][In] void* lpReserved);
 
 	[DllImport ("ADVAPI32.dll", ExactSpelling = true, PreserveSig = false, SetLastError = true)]
 	[SupportedOSPlatform ("windows5.1.2600")]
@@ -526823,7 +526825,7 @@ public static class Apis
 	[DllImport ("ncrypt.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows8.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/ncryptprotect/nf-ncryptprotect-ncryptcreateprotectiondescriptor")]
-	public unsafe static extern HRESULT NCryptCreateProtectionDescriptor ([In][Const] PWSTR pwszDescriptorString, [In] uint dwFlags, [Out] NCRYPT_DESCRIPTOR_HANDLE* phDescriptor);
+	public unsafe static extern HRESULT NCryptCreateProtectionDescriptor ([In][Const] PWSTR pwszDescriptorString, [In] uint dwFlags, [Out][RAIIFree ("NCryptCloseProtectionDescriptor")] NCRYPT_DESCRIPTOR_HANDLE* phDescriptor);
 
 	[DllImport ("ncrypt.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows8.0")]
@@ -526848,16 +526850,16 @@ public static class Apis
 	[DllImport ("ncrypt.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows8.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/ncryptprotect/nf-ncryptprotect-ncryptstreamopentoprotect")]
-	public unsafe static extern HRESULT NCryptStreamOpenToProtect ([In] NCRYPT_DESCRIPTOR_HANDLE hDescriptor, [In] uint dwFlags, [Optional][In] HWND hWnd, [In] NCRYPT_PROTECT_STREAM_INFO* pStreamInfo, [Out] NCRYPT_STREAM_HANDLE* phStream);
+	public unsafe static extern HRESULT NCryptStreamOpenToProtect ([In] NCRYPT_DESCRIPTOR_HANDLE hDescriptor, [In] uint dwFlags, [Optional][In] HWND hWnd, [In] NCRYPT_PROTECT_STREAM_INFO* pStreamInfo, [Out][RAIIFree ("NCryptStreamClose")] NCRYPT_STREAM_HANDLE* phStream);
 
 	[DllImport ("ncrypt.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows8.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/ncryptprotect/nf-ncryptprotect-ncryptstreamopentounprotect")]
-	public unsafe static extern HRESULT NCryptStreamOpenToUnprotect ([In] NCRYPT_PROTECT_STREAM_INFO* pStreamInfo, [In] uint dwFlags, [Optional][In] HWND hWnd, [Out] NCRYPT_STREAM_HANDLE* phStream);
+	public unsafe static extern HRESULT NCryptStreamOpenToUnprotect ([In] NCRYPT_PROTECT_STREAM_INFO* pStreamInfo, [In] uint dwFlags, [Optional][In] HWND hWnd, [Out][RAIIFree ("NCryptStreamClose")] NCRYPT_STREAM_HANDLE* phStream);
 
 	[DllImport ("ncrypt.dll", ExactSpelling = true, PreserveSig = false)]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/ncryptprotect/nf-ncryptprotect-ncryptstreamopentounprotectex")]
-	public unsafe static extern HRESULT NCryptStreamOpenToUnprotectEx ([In] NCRYPT_PROTECT_STREAM_INFO_EX* pStreamInfo, [In] uint dwFlags, [Optional][In] HWND hWnd, [Out] NCRYPT_STREAM_HANDLE* phStream);
+	public unsafe static extern HRESULT NCryptStreamOpenToUnprotectEx ([In] NCRYPT_PROTECT_STREAM_INFO_EX* pStreamInfo, [In] uint dwFlags, [Optional][In] HWND hWnd, [Out][RAIIFree ("NCryptStreamClose")] NCRYPT_STREAM_HANDLE* phStream);
 
 	[DllImport ("ncrypt.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows8.0")]
@@ -606395,10 +606397,10 @@ public static class Apis
 
 	[DllImport ("KERNELBASE.dll", ExactSpelling = true, PreserveSig = false)]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/appmodel/nf-appmodel-addpackagedependency")]
-	public unsafe static extern HRESULT AddPackageDependency ([In][Const] PWSTR packageDependencyId, [In] int rank, [In] AddPackageDependencyOptions options, [Out] PACKAGEDEPENDENCY_CONTEXT* packageDependencyContext, [Optional][Out] PWSTR* packageFullName);
+	public unsafe static extern HRESULT AddPackageDependency ([In][Const] PWSTR packageDependencyId, [In] int rank, [In] AddPackageDependencyOptions options, [Out][RAIIFree ("RemovePackageDependency")] PACKAGEDEPENDENCY_CONTEXT* packageDependencyContext, [Optional][Out] PWSTR* packageFullName);
 
 	[DllImport ("api-ms-win-appmodel-runtime-l1-1-7.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern HRESULT AddPackageDependency2 ([In][Const] PWSTR packageDependencyId, [In] int rank, [In] AddPackageDependencyOptions2 options, [Out] PACKAGEDEPENDENCY_CONTEXT* packageDependencyContext, [Optional][Out] PWSTR* packageFullName);
+	public unsafe static extern HRESULT AddPackageDependency2 ([In][Const] PWSTR packageDependencyId, [In] int rank, [In] AddPackageDependencyOptions2 options, [Out][RAIIFree ("RemovePackageDependency")] PACKAGEDEPENDENCY_CONTEXT* packageDependencyContext, [Optional][Out] PWSTR* packageFullName);
 
 	[DllImport ("KERNELBASE.dll", ExactSpelling = true, PreserveSig = false)]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/appmodel/nf-appmodel-removepackagedependency")]
@@ -606460,7 +606462,7 @@ public static class Apis
 	public unsafe static extern WIN32_ERROR AppPolicyGetCreateFileAccess ([In] HANDLE processToken, [Out] AppPolicyCreateFileAccess* policy);
 
 	[DllImport ("KERNEL32.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern HRESULT CreatePackageVirtualizationContext ([Optional][In][Const] PWSTR packageFamilyName, [Out] PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE* context);
+	public unsafe static extern HRESULT CreatePackageVirtualizationContext ([Optional][In][Const] PWSTR packageFamilyName, [Out][RAIIFree ("ReleasePackageVirtualizationContext")] PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE* context);
 
 	[DllImport ("KERNEL32.dll", ExactSpelling = true, PreserveSig = false)]
 	public unsafe static extern HRESULT ActivatePackageVirtualizationContext ([In] PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE context, [Out] UIntPtr* cookie);
@@ -606472,7 +606474,7 @@ public static class Apis
 	public static extern void DeactivatePackageVirtualizationContext ([In] UIntPtr cookie);
 
 	[DllImport ("KERNEL32.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern HRESULT DuplicatePackageVirtualizationContext ([In] PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE sourceContext, [Out] PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE* destContext);
+	public unsafe static extern HRESULT DuplicatePackageVirtualizationContext ([In] PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE sourceContext, [Out][RAIIFree ("ReleasePackageVirtualizationContext")] PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE* destContext);
 
 	[DllImport ("KERNEL32.dll", ExactSpelling = true, PreserveSig = false)]
 	public static extern PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE GetCurrentPackageVirtualizationContext ();
@@ -608444,6 +608446,7 @@ using Windows.Win32.Foundation.Metadata;
 
 namespace Windows.Win32.Storage.Packaging.Appx;
 
+[InvalidHandleValue (0L)]
 [NativeTypedef]
 public struct PACKAGEDEPENDENCY_CONTEXT
 {
@@ -608578,6 +608581,7 @@ using Windows.Win32.Foundation.Metadata;
 
 namespace Windows.Win32.Storage.Packaging.Appx;
 
+[InvalidHandleValue (0L)]
 [NativeTypedef]
 public struct PACKAGE_VIRTUALIZATION_CONTEXT_HANDLE
 {
@@ -609901,7 +609905,7 @@ public static class Apis
 	[DllImport ("PROJECTEDFSLIB.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows10.0.17763")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/projectedfslib/nf-projectedfslib-prjstartvirtualizing")]
-	public unsafe static extern HRESULT PrjStartVirtualizing ([In][Const] PWSTR virtualizationRootPath, [In][Const] PRJ_CALLBACKS* callbacks, [Optional][In][Const] void* instanceContext, [Optional][In][Const] PRJ_STARTVIRTUALIZING_OPTIONS* options, [Out] PRJ_NAMESPACE_VIRTUALIZATION_CONTEXT* namespaceVirtualizationContext);
+	public unsafe static extern HRESULT PrjStartVirtualizing ([In][Const] PWSTR virtualizationRootPath, [In][Const] PRJ_CALLBACKS* callbacks, [Optional][In][Const] void* instanceContext, [Optional][In][Const] PRJ_STARTVIRTUALIZING_OPTIONS* options, [Out][RAIIFree ("PrjStopVirtualizing")] PRJ_NAMESPACE_VIRTUALIZATION_CONTEXT* namespaceVirtualizationContext);
 
 	[DllImport ("PROJECTEDFSLIB.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows10.0.17763")]
@@ -626809,7 +626813,7 @@ public static class Apis
 	[DllImport ("Amsi.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows10.0.10240")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/amsi/nf-amsi-amsiinitialize")]
-	public unsafe static extern HRESULT AmsiInitialize ([In][Const] PWSTR appName, [Out] HAMSICONTEXT* amsiContext);
+	public unsafe static extern HRESULT AmsiInitialize ([In][Const] PWSTR appName, [Out][RAIIFree ("AmsiUninitialize")] HAMSICONTEXT* amsiContext);
 
 	[DllImport ("Amsi.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows10.0.10240")]
@@ -793544,7 +793548,7 @@ public static class Apis
 
 	[DllImport ("SecHost.dll", ExactSpelling = true, PreserveSig = false)]
 	[Documentation ("https://learn.microsoft.com/windows/win32/Services/subscribeservicechangenotifications")]
-	public unsafe static extern uint SubscribeServiceChangeNotifications ([In] SC_HANDLE hService, [In] SC_EVENT_TYPE eEventType, [In] PSC_NOTIFICATION_CALLBACK pCallback, [Optional][In] void* pCallbackContext, [Out] PSC_NOTIFICATION_REGISTRATION* pSubscription);
+	public unsafe static extern uint SubscribeServiceChangeNotifications ([In] SC_HANDLE hService, [In] SC_EVENT_TYPE eEventType, [In] PSC_NOTIFICATION_CALLBACK pCallback, [Optional][In] void* pCallbackContext, [Out][RAIIFree ("UnsubscribeServiceChangeNotifications")] PSC_NOTIFICATION_REGISTRATION* pSubscription);
 
 	[DllImport ("SecHost.dll", ExactSpelling = true, PreserveSig = false)]
 	[Documentation ("https://learn.microsoft.com/windows/win32/Services/unsubscribeservicechangenotifications")]
@@ -793741,6 +793745,7 @@ using Windows.Win32.Foundation.Metadata;
 
 namespace Windows.Win32.System.Services;
 
+[InvalidHandleValue (0L)]
 [NativeTypedef]
 public struct PSC_NOTIFICATION_REGISTRATION
 {
@@ -820004,7 +820009,7 @@ public static class Apis
 	[DllImport ("api-ms-win-core-winrt-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows8.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/roapi/nf-roapi-roregisteractivationfactories")]
-	public unsafe static extern HRESULT RoRegisterActivationFactories ([In][NativeArrayInfo (CountParamIndex = 2)] HSTRING* activatableClassIds, [In][NativeArrayInfo (CountParamIndex = 2)] PFNGETACTIVATIONFACTORY* activationFactoryCallbacks, [In] uint count, [Out] RO_REGISTRATION_COOKIE* cookie);
+	public unsafe static extern HRESULT RoRegisterActivationFactories ([In][NativeArrayInfo (CountParamIndex = 2)] HSTRING* activatableClassIds, [In][NativeArrayInfo (CountParamIndex = 2)] PFNGETACTIVATIONFACTORY* activationFactoryCallbacks, [In] uint count, [Out][RAIIFree ("RoRevokeActivationFactories")] RO_REGISTRATION_COOKIE* cookie);
 
 	[DllImport ("api-ms-win-core-winrt-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows8.0")]
@@ -820019,7 +820024,7 @@ public static class Apis
 	[DllImport ("api-ms-win-core-winrt-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows8.0")]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/roapi/nf-roapi-roregisterforapartmentshutdown")]
-	public unsafe static extern HRESULT RoRegisterForApartmentShutdown ([In] IApartmentShutdown callbackObject, [Out] ulong* apartmentIdentifier, [Out] APARTMENT_SHUTDOWN_REGISTRATION_COOKIE* regCookie);
+	public unsafe static extern HRESULT RoRegisterForApartmentShutdown ([In] IApartmentShutdown callbackObject, [Out] ulong* apartmentIdentifier, [Out][RAIIFree ("RoUnregisterForApartmentShutdown")] APARTMENT_SHUTDOWN_REGISTRATION_COOKIE* regCookie);
 
 	[DllImport ("api-ms-win-core-winrt-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
 	[SupportedOSPlatform ("windows8.0")]
@@ -824657,6 +824662,7 @@ using Windows.Win32.Foundation.Metadata;
 
 namespace Windows.Win32.System.WinRT;
 
+[InvalidHandleValue (0L)]
 [NativeTypedef]
 [Documentation ("https://learn.microsoft.com/windows/win32/WinRT/ro-registration-cookie")]
 public struct RO_REGISTRATION_COOKIE
@@ -893548,14 +893554,14 @@ public static class Apis
 
 	[DllImport ("api-ms-win-core-psm-appnotify-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/appnotify/nf-appnotify-registerappstatechangenotification")]
-	public unsafe static extern uint RegisterAppStateChangeNotification ([In] PAPPSTATE_CHANGE_ROUTINE Routine, [Optional][In] void* Context, [Out] PAPPSTATE_REGISTRATION* Registration);
+	public unsafe static extern uint RegisterAppStateChangeNotification ([In] PAPPSTATE_CHANGE_ROUTINE Routine, [Optional][In] void* Context, [Out][RAIIFree ("UnregisterAppStateChangeNotification")] PAPPSTATE_REGISTRATION* Registration);
 
 	[DllImport ("api-ms-win-core-psm-appnotify-l1-1-0.dll", ExactSpelling = true, PreserveSig = false)]
 	[Documentation ("https://learn.microsoft.com/windows/win32/api/appnotify/nf-appnotify-unregisterappstatechangenotification")]
 	public static extern void UnregisterAppStateChangeNotification ([In] PAPPSTATE_REGISTRATION Registration);
 
 	[DllImport ("api-ms-win-core-psm-appnotify-l1-1-1.dll", ExactSpelling = true, PreserveSig = false)]
-	public unsafe static extern uint RegisterAppConstrainedChangeNotification ([In] PAPPCONSTRAIN_CHANGE_ROUTINE Routine, [Optional][In] void* Context, [Out] PAPPCONSTRAIN_REGISTRATION* Registration);
+	public unsafe static extern uint RegisterAppConstrainedChangeNotification ([In] PAPPCONSTRAIN_CHANGE_ROUTINE Routine, [Optional][In] void* Context, [Out][RAIIFree ("UnregisterAppConstrainedChangeNotification")] PAPPCONSTRAIN_REGISTRATION* Registration);
 
 	[DllImport ("api-ms-win-core-psm-appnotify-l1-1-1.dll", ExactSpelling = true, PreserveSig = false)]
 	public static extern void UnregisterAppConstrainedChangeNotification ([In] PAPPCONSTRAIN_REGISTRATION Registration);
@@ -908802,6 +908808,7 @@ using Windows.Win32.Foundation.Metadata;
 
 namespace Windows.Win32.UI.Shell;
 
+[InvalidHandleValue (0L)]
 [NativeTypedef]
 public struct PAPPCONSTRAIN_REGISTRATION
 {
@@ -908822,6 +908829,7 @@ using Windows.Win32.Foundation.Metadata;
 
 namespace Windows.Win32.UI.Shell;
 
+[InvalidHandleValue (0L)]
 [NativeTypedef]
 public struct PAPPSTATE_REGISTRATION
 {

This comment is automatically updated on each push.

@jevansaks
Jevan Saks (jevansaks) enabled auto-merge (squash) October 19, 2026 00:08
@jevansaks
Jevan Saks (jevansaks) merged commit 825a73d into main Oct 19, 2026
7 checks passed
@jevansaks
Jevan Saks (jevansaks) deleted the jevansaks-hid-raiifree-annotations branch October 19, 2026 01:36
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.

HidD_GetPreparsedData and related producers are missing RAIIFree annotations

2 participants