Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetStdHandle shouldn't return a CloseHandleSafeHandle #91

Closed
sfiruch opened this issue Feb 14, 2021 · 5 comments · Fixed by #654
Closed

GetStdHandle shouldn't return a CloseHandleSafeHandle #91

sfiruch opened this issue Feb 14, 2021 · 5 comments · Fixed by #654
Labels
blocked This issue cannot be fixed without first a change to a dependency enhancement New feature or request

Comments

@sfiruch
Copy link

sfiruch commented Feb 14, 2021

Standard output handles should rarely be closed. Here's Raymond Chen's explanation. Thus, the proper way to use GetStdHandle is by combining it with DangerousAddRef:

var handle = PInvoke.GetStdHandle(STD_HANDLE_TYPE.STD_OUTPUT_HANDLE);
bool ignore=default;
handle.DangerousAddRef(ref ignore);

This could be simplified if GetStdHandle wouldn't return a CloseHandleSafeHandle.

@AArnott AArnott added blocked This issue cannot be fixed without first a change to a dependency enhancement New feature or request labels Feb 16, 2021
@AArnott
Copy link
Member

AArnott commented Feb 16, 2021

Great point. I filed microsoft/win32metadata#244 against the metadata so we can do this along with other projections.

@AArnott
Copy link
Member

AArnott commented Feb 23, 2021

Now that #143 is done, we actually can create SafeHandle instances with ownsHandle: false set on them.

AArnott pushed a commit that referenced this issue May 6, 2022
Notice .NET SDK install failure earlier and workaround broken script
@AArnott AArnott linked a pull request Aug 19, 2022 that will close this issue
@xPaw
Copy link

xPaw commented Oct 14, 2024

I'm curious, how are you expected to pass a handle from GetStdHandle into functions such as GetConsoleMode which expect SafeHandle?

Use SafeFileHandle or is there a better way of doing it?

var @out = PInvoke.GetStdHandle(Windows.Win32.System.Console.STD_HANDLE.STD_OUTPUT_HANDLE);
var safeHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle(@out, ownsHandle: false);
PInvoke.GetConsoleMode(safeHandle, out var mode);

@AArnott
Copy link
Member

AArnott commented Oct 15, 2024

@xPaw: it's trivial if you call the GetStdHandle method that returns a SafeHandle:

SafeHandle safeHandle = PInvoke.GetStdHandle_SafeHandle(default);
PInvoke.GetConsoleMode(safeHandle, out var mode);

@xPaw
Copy link

xPaw commented Oct 15, 2024

Ah that's the trick, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked This issue cannot be fixed without first a change to a dependency enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants