Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
define own struct
#define _WIN32_WINNT _WIN32_WINNT_VISTA #include <windows.h> #include <io.h> #include <memory> #include <string> #include <iostream> #include <iomanip> inline bool isMsysPty(int fd) noexcept { // Dynamic load for binary compability with old Windows const auto ptrGetFileInformationByHandleEx = reinterpret_cast<decltype(&GetFileInformationByHandleEx)>( GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetFileInformationByHandleEx")); if (!ptrGetFileInformationByHandleEx) { return false; } HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); if (h == INVALID_HANDLE_VALUE) { return false; } // Check that it's a pipe: if (GetFileType(h) != FILE_TYPE_PIPE) { return false; } // POD type is binary compatible with FILE_NAME_INFO from WinBase.h // It have the same alignment and used to avoid UB in caller code struct MY_FILE_NAME_INFO { DWORD FileNameLength; WCHAR FileName[MAX_PATH]; }; auto pNameInfo = std::unique_ptr<MY_FILE_NAME_INFO>( new (std::nothrow) MY_FILE_NAME_INFO()); if (!pNameInfo) { return false; } // Check pipe name is template of // {"cygwin-","msys-"}XXXXXXXXXXXXXXX-ptyX-XX if (!ptrGetFileInformationByHandleEx(h, FileNameInfo, pNameInfo.get(), sizeof(MY_FILE_NAME_INFO))) { return false; } std::wstring name(pNameInfo->FileName, pNameInfo->FileNameLength / sizeof(WCHAR)); if ((name.find(L"msys-") == std::wstring::npos && name.find(L"cygwin-") == std::wstring::npos) || name.find(L"-pty") == std::wstring::npos) { return false; } return true; } int main() { std::cout << " stdin msys: " << isMsysPty(_fileno(stdin)) << '\n' << "stdout msys: " << isMsysPty(_fileno(stdout)) << '\n' << "stderr msys: " << isMsysPty(_fileno(stderr)) << std::endl; }
run
|
edit
|
history
|
help
0
Enum Class Comparisons
#30.1
Templatized Visitor Pattern Example
zero size std::array parameter
hex manip
algorithm_1
Waiting for Multiple Objects
lab1
#22.1
DCapSurfaceDesc