Results 1 to 7 of 7

Thread: Error on GetNumberOfConsoleInputEvents

  1. #1

    Thread Starter
    New Member Matzeee's Avatar
    Join Date
    Dec 2023
    Posts
    4

    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?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,942

    Re: Error on GetNumberOfConsoleInputEvents

    You might need to change your target cpu to x86

    Project-->Properties-->Compile-->Platform

  3. #3

    Thread Starter
    New Member Matzeee's Avatar
    Join Date
    Dec 2023
    Posts
    4

    Re: Error on GetNumberOfConsoleInputEvents

    thanks but i have the same error

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,733

    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
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,779

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6

    Thread Starter
    New Member Matzeee's Avatar
    Join Date
    Dec 2023
    Posts
    4

    Re: Error on GetNumberOfConsoleInputEvents

    Quote Originally Posted by Niya View Post
    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.

    Quote Originally Posted by Niya View Post
    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...

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,779

    Re: Error on GetNumberOfConsoleInputEvents

    You may want to take a look at PeekConsoleInput if you're interested in what input is in the buffer.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width