1

I am not getting concept regarding NT processes as I am using native APIs.Since they are returning values like STATUS_TIMEOUT, STATUS_PENDING etc.What does STATUS_PENDING really mean?

My code goes like this.

NTSTATUS rc;
rc = NtReadFile(
                            Keybrds[iLoop].hKeyboard,
                            Keybrds[iLoop].hEvent,
                            NULL,
                            NULL,
                            &IoStatusBlock,
                            &KbData,
                            sizeof(KEYBOARD_INPUT_DATA),
                            &ByteOffset,
                            NULL
                            );

Here,NtReadFile() is returning STATUS_PENDING, what does that means?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
sonu gupta
  • 473
  • 8
  • 21
  • 2
    Hmmmm, a program using the native APIs to read keyboard data. What might this be? – Jonathon Reinhart Jan 06 '15 at 07:36
  • @JonathonReinhart: for example an application similar to `autochk.exe` or UltraDefrag ... just to name two completely harmless applications which would be doing just that. – 0xC0000022L Oct 26 '18 at 22:37
  • @JonathonReinhart Well, i was working with “Defragmentation” application. Which was needed to be invoked during boot time. And if user wants to abort defragmentation, he needs to press ESC to abort “Defragmentation” – sonu gupta Oct 26 '18 at 22:54

2 Answers2

6

It means the I/O operation is pending, and you should wait on the handle for its completion. It also probably means the file handle is opened in async mode; if you want synchrony you should open the handle in synchronous mode instead to avoid STATUS_PENDING in the first place.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thank you.But,as i am a beginner, i dont know how to open the handle in synchronous mode to avoid STATUS_PENDING. – sonu gupta Jan 06 '15 at 08:51
  • @sonugupta: you need to specify `FILE_SYNCHRONOUS_IO_ALERT` or `FILE_SYNCHRONOUS_IO_NONALERT` along with `SYNCHRONIZE` when you call `NtOpenFile` or `NtCreateFile`. See here: http://msdn.microsoft.com/en-us/library/windows/hardware/ff566424%28v=vs.85%29.aspx – John Zwinck Jan 06 '15 at 08:57
  • Actually what i want is,user should press ESC key within 10 seconds. I have tried using FILE_SYNCHRONOUS_IO_ALERT or FILE_SYNCHRONOUS_IO_NONALERT along with SYNCHRONIZE while creating file using NtcreateFile. But at boot time,screen stucks for user input for unlimited time. – sonu gupta Jan 06 '15 at 13:21
5

NtReadFile [and ZwReadFile] are two versions of the same Windows Native System Services routine. As per the documentation

Return value

ZwReadFile returns either STATUS_SUCCESS or the appropriate NTSTATUS error code.

Yes, STATUS_PENDING is a return error code. It is documented as

0x00000103 STATUS_PENDING The operation that was requested is pending completion.

This message indicates that some of the I/O operation using the same handle is already pending. The handle is probably opened in asynchronous mode and thus by having the return value of STATUS_PENDING, the caller will wait for the completion of the ongoing operation on that handle.

In case of the file handle was opened in async mode, the NtReadFile () will not wait itself on the handle. OTOH, for handles with sync mode, NtReadFile () will wait [block] until the read operation is complete.

Now, as for details of NTSTATUS error code part, you can have a look at here.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • But, i do not want to block read operation.I have specified some time interval say of 10 seconds and if it does not get any input till that time,some another function is called. – sonu gupta Jan 06 '15 at 13:23
  • @sonugupta well, then, upon receiving `STATUS_PENDING` you have to keep watch on the handle itself, up to the timeout. – Sourav Ghosh Jan 06 '15 at 13:34
  • @sonugupta also _The caller can safely wait on the file handle only if all three of the following conditions are met: The handle was opened for asynchronous access (that is, neither FILE_SYNCHRONOUS_IO_XXX flag was specified). The handle is being used for only one I/O operation at a time. ZwReadFile returned STATUS_PENDING._ – Sourav Ghosh Jan 06 '15 at 13:35
  • Yes but, since i am waiting for multiple handles and hence i have used **rc = NtWaitForMultipleObjects(iDest, hTempEvents, WaitAnyObject, TRUE, &TimeOut);** where hTempEvents is array of handles. The value returned by NtWaitForMultipleObjects is STATUS_PENDING.It never returns STATUS_SUCCESS. – sonu gupta Jan 07 '15 at 08:42