How to check if there is enough space before WriteFile in c in windows?

  hPipe = CreateNamedPipe( 
     lpszPipename,             // pipe name 
     PIPE_ACCESS_DUPLEX,       // read/write access 
     PIPE_TYPE_MESSAGE |       // message type pipe 
     PIPE_READMODE_MESSAGE |   // message-read mode 
     PIPE_WAIT,                // blocking mode 
     PIPE_UNLIMITED_INSTANCES, // max. instances  
     100,                  // output buffer size 
     100,                  // input buffer size 
     0,                        // client time-out 
     NULL);                    // default security attribute 

  DWORD totalBytesAvailable; 
  PeekNamedPipe( 
    hPipe ,                // __in       HANDLE hNamedPipe, 
    NULL,                  // __out_opt  LPVOID lpBuffer, 
    0,                     // __in       DWORD nBufferSize, 
    NULL,                  // __out_opt  LPDWORD lpBytesRead, 
    &totalBytesAvailable,  // __out_opt  LPDWORD lpTotalBytesAvail, 
    NULL                   // __out_opt  LPDWORD lpBytesLeftThisMessage 
  ); 
    if(totalBytesAvailable allows)
    WriteFile( tmp_pipe, pBuffer, BufferLen, &dwWritten, NULL );

As you can see, I used PeekNamedPipeto get the available space, but it turns out that totalBytesAvailablealways 0, how to do it right?

+3
source share
6 answers

IMHO, this approach of checking the free space before performing the actual recording is erroneous.

It may happen that by the time the actual recording starts, some other process running in parallel fills the last bits of free disk space, as a result of which your WriteFile fails.

I would only rely on what WriteFile returns.

+4
source

, lpTotalBytesAvail, - , , . , .

- NT Krenel - WriteFile() .

, , * * *... * , .

, . , , WriteFile(), . , WriteFile() - , .

, , , Windows ( - Windows) - "" "" . , .

, , WriteFile() .

-Foredecker

+2

PIPE_NOWAIT PIPE_WAIT. WriteFile , .
100 -! ?

+1

, , , , ?

0

.

PIPE_NOWAIT Microsoft.

/. WriteFile() FALSE ERROR_IO_PENDING, . CancelIo(), WriteFile(). , CancelIo() GetOverlappedResult(), WriteFile() - - , OVERLAPPED , .

, . , , !

0

Source: https://habr.com/ru/post/1764273/


All Articles