Comments (1)
I'm afraid .NET's BCL defines more SafeHandle-derived types than are justified by the native functions that release them. SafeFileHandle
and SafeAccessTokenHandle
both release their handles using the win32 CloseHandle
function.
The metadata that CsWin32 is based on discloses the function used to release the handle, and CsWin32 either generates a new SafeHandle for that function or reuses one that already exists in .NET. But an ambiguity arises when .NET has many types that do the same thing, and cswin32 cannot predict for a given API which option would be best suited to match the APIs you intend to use it with.
So... we don't have a great solution here. The only solution I can imagine is a special hard-coded list of win32 APIs and the specific pre-existing SafeHandle-derived type that should be used for that particular win32 function, which is a maintenance burden.
It's unfortunate that RunImpersonatedAsync
doesn't accept any SafeHandle
object.
Converting between these two types is riddled with dangers. Drafting out one idea, I think this would be safe **but only if no one else has a reference to the original SafeFileHandle
:
SafeFileHandle sfh = new SafeFileHandle((nint)123, true);
bool success = false;
sfh.DangerousAddRef(ref success);
sfh.Dispose();
SafeAccessTokenHandle sath = new(sfh.DangerousGetHandle());
This deliberately changes the SafeFileHandle
's internal state to never release the handle. The new SafeHandle will release it once it gets finalized. So if others have a reference to the original SafeFileHandle
, that handle could be released while they are still using it.
Another complicating factor is that SafeAccessTokenHandle
does not offer a constructor that takes an unowned handle, so you can't just pass the handle value into the constructor for sake of calling an API but keep the ref count in the original SafeFileHandle
either. :(
from cswin32.
Related Issues (20)
- Code Quality: Use `WinMdRoot` instead of `winmdroot` to supress CS8981 (type name is ASCII lowercase only) HOT 5
- `PCWSTR` is not marked with `[GeneratedCode]`
- Many Win32 structs (HDC, HMONITOR, etc.) lack the IntPtr conversion operator.
- "Pointers cannot reference marshaled structures. Use ByRef instead" caused by VariableLengthInlineArray
- Latest WDK metadata fails to create DEVPKEY field
- Daily nuget feed not updating HOT 1
- MFTEnumEx signature incorrect
- `VirtualFreeEx` missing HOT 1
- Missing NtCreateThread and NtCreateThreadEx functions HOT 1
- Add an analyzer to catch inadvertent ephemeral delegates HOT 4
- Missing `CryptCATCDFEnumMembersByCDFTagEx` and `CryptCATCDFEnumAttributesWithCDFTag` HOT 1
- `ReadFileEx` incorrectly takes a `Span<byte>`
- WINMM `PlaySound` does not allow passing byte arrays. HOT 1
- CsWIn32 generates DllImports with legacy com marshalling even when BuiltInComInteropSupport is set to false (and it's not supported) HOT 1
- Properly terminate `<see href="">` tags in generated XML documentation. HOT 1
- VariableLengthInlineArray - VS2019 - CS8170 and CS8347
- AddVectoredExceptionHandler handler callback causes ExecutionEngineException
- Class modifiers would conflict when different two packages have different modifier HOT 1
- Add option to generate ExcludeFromCodeCoverage on source generated objects
- SystemParametersInfo cannot read the path of the desktop background image
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cswin32.