Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

ReadConsole signature has wrong parameter mode #523

Closed
Entomy opened this issue Aug 6, 2020 · 0 comments · Fixed by #525
Closed

ReadConsole signature has wrong parameter mode #523

Entomy opened this issue Aug 6, 2020 · 0 comments · Fixed by #525
Assignees
Labels

Comments

@Entomy
Copy link

Entomy commented Aug 6, 2020

ReadConsole() has an incorrect parameter mode, specifically on int lpNumberOfCharsRead, which is defined as an normal input parameter, but is supposed to be an output parameter, per the WinAPI docs.

Despite the clear intentions to do this (the [Friendly(FriendlyFlags.Out)] attribute), the actual parameter is not defined this way. This causes the following exception:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at PInvoke.Kernel32.ReadConsole(IntPtr, Void*, Int32, Int32, IntPtr)
   at PInvoke.Kernel32.ReadConsole(IntPtr, Void*, Int32, Int32, IntPtr)
   at Consolator.Console.Read()
   at Tester.Program.Main()

Creating my own import as follows:

[DllImport(nameof(Kernel32), CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static unsafe extern Boolean ReadConsole(IntPtr hConsoleInput, void* lpBuffer, Int32 nNumberOfCharsToRead, out Int32 lpNumberOfCharsRead, IntPtr lpReserved);

Does not throw an exception, and operates as expected.

@AArnott AArnott added the bug label Aug 6, 2020
@AArnott AArnott self-assigned this Aug 6, 2020
AArnott added a commit that referenced this issue Aug 6, 2020
It was missing the `out` modifier on the `lpNumberOfCharsRead` parameter.
While fixing it, I went ahead and renovated the entire signature to make it easier to use.

Fixes #523
@AArnott AArnott closed this as completed Aug 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants