Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
GetFinalPathNameByHandle Behaviour
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 #include <windows.h> #include <iostream> #include <sstream> #include <memory> #include <tchar.h> #include <io.h> #include <string> std::string GetLastErrorStdStr() { DWORD error = GetLastError(); if (error) { LPVOID lpMsgBuf; DWORD bufLen = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); if (bufLen) { LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf; std::string result(lpMsgStr, lpMsgStr+bufLen); std::ostringstream ostr; ostr << error << " " << result; LocalFree(lpMsgBuf); return ostr.str(); } } return std::string(); } inline bool isMsysPty(int fd) noexcept { #if defined(UNICODE) || defined(_UNICODE) LPCSTR functionName = "GetFinalPathNameByHandleW"; #else LPCSTR functionName = "GetFinalPathNameByHandleA"; #endif const auto ptrGetFinalPathNameByHandle = reinterpret_cast<decltype(&GetFinalPathNameByHandle)>( GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), functionName)); if (!ptrGetFinalPathNameByHandle) { return false; } using STDTString = std::basic_string<TCHAR>; HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); if (h == INVALID_HANDLE_VALUE) { return false; } if (GetFileType(h) != FILE_TYPE_PIPE) { return false; } TCHAR Path[MAX_PATH] = { _T('\0') }; const DWORD retSize = ptrGetFinalPathNameByHandle(h, Path, MAX_PATH, VOLUME_NAME_NONE | FILE_NAME_OPENED); if (!retSize || retSize >= MAX_PATH) { // DEBUG OUTPUT: std::cout << "ERROR GetFinalPathNameByHandle: " << GetLastErrorStdStr() << std::endl; // Next try to use GetFinalPathNameByHandleEx: const size_t sizeNameInfo = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH; auto pNameInfo = std::unique_ptr<FILE_NAME_INFO, decltype(&std::free)>( static_cast<FILE_NAME_INFO*>(std::malloc(sizeNameInfo)), &std::free); // But GetFileInformationByHandleEx returns no error if(GetFileInformationByHandleEx(h, FileNameInfo, pNameInfo.get(), sizeNameInfo)) { std::wstring name(pNameInfo->FileName, pNameInfo->FileNameLength / sizeof(WCHAR)); std::wcout << "GetFileInformationByHandleEx No errors, and path is: " << name << std::endl; } return false; } STDTString name(Path,retSize); if ((name.find(STDTString(TEXT("msys-"))) == STDTString::npos && name.find(STDTString(TEXT("cygwin-"))) == STDTString::npos) || name.find(STDTString(TEXT("-pty"))) == STDTString::npos) { return false; } // DEBUG OUTPUT: #if defined(UNICODE) || defined(_UNICODE) std::wcout << name << std::endl; #else std::cout << name << std::endl; #endif return true; } int main() { isMsysPty(0); isMsysPty(1); isMsysPty(2); std::wcout << L"Hello, world!\n"; }
run
|
edit
|
history
|
help
0
variable template not supported
#32
Visual C++ template instantiation
C++ standard violation: [templates][explicit instantiation][access checking]
Dices by GOOSE
lab1
Matrix_1
ambiguity does not count as ambiguity
#30.3
SFINAE with std::enable_if