-
Feb 12th, 2025, 11:19 AM
#1
Thread Starter
New Member
Error on GetNumberOfConsoleInputEvents
Hello,
in a console application i want to check whether a character is present in the input buffer (but dont read it) and to continue if the buffer is empty.
This should also work if the input stream has been redirected (e.g. echo some text | application.exe, or type somefile.txt | application.exe)
I use the following code to do this:
<DllImport("kernel32.dll", SetLastError:=True)>
Private Function GetStdHandle(ByVal nStdHandle As UInt32) As IntPtr
End Function
Private STD_INPUT_HANDLE As UInt32 = 4294967286
<DllImport("Kernel32.dll", EntryPoint:="GetNumberOfConsoleInputEvents", SetLastError:=True)>
Private Function GetNumberOfConsoleInputEvents(hConsoleInput As IntPtr, lpcNumberOfEvents As Int32) As Boolean
End Function
Private hStdIn As IntPtr
Private iEvt As Int32
sub Main()
hStdIn = GetStdHandle(STD_INPUT_HANDLE)
GetNumberOfConsoleInputEvents(hStdIn, iEvt)
...
when i call GetNumberOfConsoleInputEvents(hStdIn, iEvt) i get this ERROR:
System.AccessViolationException: 'An attempt was made to read or write in the protected memory. This is often an indication that other memory is corrupted.'
can you Help please?
-
Feb 12th, 2025, 02:01 PM
#2
Re: Error on GetNumberOfConsoleInputEvents
You might need to change your target cpu to x86
Project-->Properties-->Compile-->Platform
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 13th, 2025, 03:15 AM
#3
Thread Starter
New Member
Re: Error on GetNumberOfConsoleInputEvents
thanks but i have the same error
-
Feb 13th, 2025, 08:49 AM
#4
Re: Error on GetNumberOfConsoleInputEvents
I suspect that since GetNumberOfConsoleInputEvents has an _Out_ LPDWORD lpcNumberOfEvents, you need a byref.
Try:
Private Function GetNumberOfConsoleInputEvents(hConsoleInput As IntPtr, ByRef lpcNumberOfEvents As Int32) As Boolean
End Function
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 13th, 2025, 02:11 PM
#5
Re: Error on GetNumberOfConsoleInputEvents
Sapator is correct. Your import of GetNumberOfConsoleInputEvents is wrong. The second argument needs to be passed by reference as it expects a pointer. This is how it returns it's value.
One other thing to note is that in my testing of it, GetNumberOfConsoleInputEvents never reports 0 input events. 1 seems to be the lowest value it will ever return for some reason.
-
Feb 14th, 2025, 03:46 AM
#6
Thread Starter
New Member
Re: Error on GetNumberOfConsoleInputEvents
 Originally Posted by Niya
Sapator is correct. Your import of GetNumberOfConsoleInputEvents is wrong. The second argument needs to be passed by reference as it expects a pointer. This is how it returns it's value.
Yes you are right. i should have read better. It works now.
 Originally Posted by Niya
One other thing to note is that in my testing of it, GetNumberOfConsoleInputEvents never reports 0 input events. 1 seems to be the lowest value it will ever return for some reason.
Yes, i just realized that too. But sometimes i get 0 returned and then immediately 1 after that.
Perhaps because mouse and other events are also returned:
https://learn.microsoft.com/de-de/wi...oleinputevents
The GetNumberOfConsoleInputEvents function reports the total number of unread input records in the input buffer, including keyboard, mouse, and window-resizing input records.
i try ReadConsoleInput to get all events...
-
Feb 14th, 2025, 08:48 AM
#7
Re: Error on GetNumberOfConsoleInputEvents
You may want to take a look at PeekConsoleInput if you're interested in what input is in the buffer.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|