Last active
June 27, 2025 19:05
-
-
Save ashgti/1649fde8ef28783ace1e414f87a6b680 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <wtypes.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <io.h> | |
#include <windows.h> | |
#include <winternl.h> | |
#include <process.h> | |
#include <iostream> | |
typedef enum { | |
FileModeInformation = 16, | |
FilePipeLocalInformation = 24, | |
} MY_FILE_INFORMATION_CLASS; | |
typedef NTSTATUS(NTAPI* _NtQueryInformationFile_fn)( | |
HANDLE FileHandle, PIO_STATUS_BLOCK IoStatusBlock, PVOID FileInformation, | |
ULONG Length, MY_FILE_INFORMATION_CLASS FileInformationClass); | |
static _NtQueryInformationFile_fn pNtQueryInformationFile; | |
typedef struct _FILE_MODE_INFORMATION { | |
ULONG Mode; | |
} FILE_MODE_INFORMATION, * PFILE_MODE_INFORMATION; | |
typedef struct _FILE_PIPE_LOCAL_INFORMATION { | |
ULONG NamedPipeType; | |
ULONG NamedPipeConfiguration; | |
ULONG MaximumInstances; | |
ULONG CurrentInstances; | |
ULONG InboundQuota; | |
ULONG ReadDataAvailable; | |
ULONG OutboundQuota; | |
ULONG WriteQuotaAvailable; | |
ULONG NamedPipeState; | |
ULONG NamedPipeEnd; | |
} FILE_PIPE_LOCAL_INFORMATION, * PFILE_PIPE_LOCAL_INFORMATION; | |
namespace { | |
DWORD inspect_pipe(const HANDLE& hPipe) { | |
static std::once_flag once_flag; | |
std::call_once(once_flag, []() { | |
HMODULE ntdll = LoadLibraryW(L"ntdll.dll"); | |
// Not found. | |
if (!ntdll) return; | |
// Lookup 'NtQueryInformationFile' from ntdll.dll. This should be included | |
// in Windows 2000 and newer. NOTE: Not an official API, so its parameters | |
// are may change in a future release. However, this has been stable for | |
// years at this point and is used by multiple libraries and runtimes. | |
pNtQueryInformationFile = (_NtQueryInformationFile_fn)GetProcAddress( | |
ntdll, "NtQueryInformationFile"); | |
}); | |
NTSTATUS nt_status; | |
IO_STATUS_BLOCK io_status; | |
FILE_MODE_INFORMATION mode_info; | |
pNtQueryInformationFile(hPipe, &io_status, &mode_info, sizeof(mode_info), FileModeInformation); | |
std::cout << "pipe mode: " << mode_info.Mode << std::endl; | |
FILE_PIPE_LOCAL_INFORMATION pipe_info; | |
pNtQueryInformationFile(hPipe, &io_status, &pipe_info, sizeof(pipe_info), FilePipeLocalInformation); | |
std::cout << "pipe info: " << pipe_info.NamedPipeType << std::endl; | |
return 0; | |
} | |
} | |
int main() | |
{ | |
HANDLE hStdIn = (HANDLE)_get_osfhandle(_fileno(stdin)); | |
if (GetFileType(hStdIn) == FILE_TYPE_PIPE) | |
inspect_pipe(hStdIn); | |
else | |
std::cout << "not a pipe\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment