#pragma once #include "../Include/Winheaders.h" #include #include #include namespace blackbone { class Utils { public: /// /// Convert UTF-8 string to wide char one /// /// UTF-8 string /// wide char string BLACKBONE_API static std::wstring UTF8ToWstring( const std::string& str ); /// /// Convert wide string to UTF-8 /// /// UTF-8 string /// wide char string BLACKBONE_API static std::string WstringToUTF8( const std::wstring& str ); /// /// Convert ANSI string to wide char one /// /// ANSI string. /// String locale /// wide char string BLACKBONE_API static std::wstring AnsiToWstring( const std::string& input, DWORD locale = CP_ACP ); /// /// Convert wide char string to ANSI one /// /// wide char string. /// String locale /// ANSI string BLACKBONE_API static std::string WstringToAnsi( const std::wstring& input, DWORD locale = CP_ACP ); /// /// Format string /// /// Format specifier /// Arguments /// Formatted string BLACKBONE_API static std::wstring FormatString( const wchar_t* fmt, ... ); /// /// Get filename from full-qualified path /// /// File path /// Filename BLACKBONE_API static std::wstring StripPath( const std::wstring& path ); /// /// Get parent directory /// /// File path /// Parent directory BLACKBONE_API static std::wstring GetParent( const std::wstring& path ); /// /// Get current process exe file directory /// /// Exe directory BLACKBONE_API static std::wstring GetExeDirectory(); /// /// Cast string characters to lower case /// /// Source string. /// Result string BLACKBONE_API static std::wstring ToLower( std::wstring str ); /// /// Generate random alpha-numeric string /// /// Desired length. 0 - random length from 5 to 15 /// Generated string BLACKBONE_API static std::wstring RandomANString( int length = 0 ); /// /// Get system error description /// /// The code. /// Error message BLACKBONE_API static std::wstring GetErrorDescription( NTSTATUS code ); /// /// Check if file exists /// /// Full-qualified file path /// true if exists BLACKBONE_API static bool FileExists( const std::wstring& path ); }; /// /// std::mutex alternative /// class CriticalSection { public: BLACKBONE_API CriticalSection() { InitializeCriticalSection( &_native ); } BLACKBONE_API ~CriticalSection() { DeleteCriticalSection( &_native ); } BLACKBONE_API void lock() { EnterCriticalSection( &_native ); } BLACKBONE_API void unlock() { LeaveCriticalSection( &_native ); } private: CRITICAL_SECTION _native; }; /// /// std::lock_guard alternative /// class CSLock { public: BLACKBONE_API CSLock( CriticalSection& cs ) : _cs( cs ) { cs.lock(); } BLACKBONE_API ~CSLock() { _cs.unlock(); } private: CSLock( const CSLock& ) = delete; CSLock& operator = ( const CSLock& ) = delete; private: CriticalSection& _cs; }; /// /// System32 helper /// class FsRedirector { public: FsRedirector( bool wow64 ) : _wow64( wow64 ) { #ifndef XP_BUILD if (wow64) Wow64DisableWow64FsRedirection( &_fsRedirection ); #endif } ~FsRedirector() { #ifndef XP_BUILD if (_wow64) Wow64RevertWow64FsRedirection( _fsRedirection ); #endif } private: PVOID _fsRedirection = nullptr; bool _wow64; }; #if _MSC_VER >= 1900 namespace tuple_detail { template void visit_each( T&& t, F f, std::index_sequence ) { auto l = { (f( std::get( t ) ), 0)... }; } template void copyTuple( std::tuple const& from, std::vector& to ) { auto func = [&to]( auto& v ) { auto ptr = to.size(); to.resize( ptr + sizeof( v ) ); memcpy( to.data() + ptr, &v, sizeof( v ) ); return 0; }; visit_each( from, func, std::index_sequence_for() ); } } #endif }