├── .gitattributes ├── .gitignore ├── Directories.props ├── Library.props ├── LogLevel.props ├── README.md ├── StaticRuntime.props ├── StdAfx.props ├── Tests ├── BufferTest.cpp ├── ChecksumTest.cpp ├── ComTest.cpp ├── CommandLineTest.cpp ├── DisasmTest.cpp ├── GuidTest.cpp ├── JsonTest.cpp ├── PathTest.cpp ├── Sha1Test.cpp ├── ShellTest.cpp ├── SignalsTest.cpp ├── SqliteTest.cpp ├── StatementDecoratorsTest.cpp ├── StringTest.cpp ├── Tests.vcxproj ├── Tests.vcxproj.filters ├── TimeTest.cpp ├── UrlTest.cpp ├── VersionTest.cpp ├── Wow64EmulatorTest.cpp ├── XmlDomSerializerTest.cpp ├── XmlDomTest.cpp ├── stdafx.cpp ├── stdafx.h └── targetver.h ├── WinToolsLib.props ├── WinToolsLib.sln └── WinToolsLib ├── Application.cpp ├── Application.h ├── Bool.h ├── Buffer.cpp ├── Buffer.h ├── BufferStream.cpp ├── BufferStream.h ├── Cached.h ├── Checksum ├── Crc.cpp ├── Crc.h ├── Fletcher.cpp └── Fletcher.h ├── ChunkCollection.cpp ├── ChunkCollection.h ├── Com ├── BStr.cpp ├── BStr.h ├── Initialize.cpp ├── Initialize.h ├── Ptr.h ├── SafeArray.cpp ├── SafeArray.h ├── TaskMem.cpp ├── TaskMem.h ├── Variant.cpp └── Variant.h ├── CommandLine ├── ArgumentList.cpp ├── ArgumentList.h ├── CommandList.cpp └── CommandList.h ├── CommonMemoryStream.cpp ├── CommonMemoryStream.h ├── Concurrency ├── ConcurrentQueue.h ├── WorkQueue.cpp └── WorkQueue.h ├── Cryptography ├── Hash.cpp ├── Hash.h ├── IHash.h ├── Sha.cpp ├── Sha.h ├── SymmetricCipher.cpp └── SymmetricCipher.h ├── Data ├── Json │ ├── error_handler.hpp │ ├── json.hpp │ ├── json1.hpp │ ├── json2.hpp │ ├── json_char_traits.hpp │ ├── json_deserializer.hpp │ ├── json_exception.hpp │ ├── json_filter.hpp │ ├── json_input_handler.hpp │ ├── json_output_handler.hpp │ ├── json_reader.hpp │ ├── json_serializer.hpp │ ├── json_structures.hpp │ ├── jsoncons_config.hpp │ └── output_format.hpp └── Sqlite │ ├── Database.cpp │ ├── Database.h │ ├── Details │ ├── sqlite3.c │ └── sqlite3.h │ ├── IStatement.h │ ├── Query.cpp │ ├── Query.h │ ├── SqliteException.cpp │ ├── SqliteException.h │ ├── Statement.cpp │ ├── Statement.h │ ├── StatementDecorators │ ├── ExtendableBase.cpp │ ├── ExtendableBase.h │ ├── ExtendableInsert.cpp │ ├── ExtendableInsert.h │ ├── ExtendableUpdate.cpp │ ├── ExtendableUpdate.h │ └── TemplateExtendableStatement.h │ ├── TemplateQuery.h │ ├── TemplateStatement.h │ ├── Transaction.cpp │ └── Transaction.h ├── Debug.cpp ├── Debug.h ├── DebugOutput.cpp ├── DebugOutput.h ├── Disasm.cpp ├── Disasm.h ├── DynamicFunction.h ├── Environment.cpp ├── Environment.h ├── Exception.cpp ├── Exception.h ├── Executable.cpp ├── Executable.h ├── ExecutableChunk.cpp ├── ExecutableChunk.h ├── FileCacheStream.cpp ├── FileCacheStream.h ├── FileSystem ├── File.cpp ├── File.h ├── Folder.cpp ├── Folder.h ├── Path.cpp ├── Path.h ├── Shortcut.cpp └── Shortcut.h ├── Gui ├── Credentials.cpp ├── Credentials.h ├── FileDialog.cpp ├── FileDialog.h ├── FolderDialog.cpp ├── FolderDialog.h ├── MessageBox.cpp └── MessageBox.h ├── Guid.cpp ├── Guid.h ├── Handles ├── FindHandle.cpp ├── FindHandle.h ├── Handle.cpp ├── Handle.h ├── HandleBase.h ├── InternetHandle.cpp ├── InternetHandle.h ├── RegistryHandle.cpp ├── RegistryHandle.h ├── ServiceHandle.cpp └── ServiceHandle.h ├── IParsable.h ├── IStream.h ├── License.cpp ├── License.h ├── Log.h ├── Module.cpp ├── Module.h ├── Monitor.h ├── Net ├── Http.cpp ├── Http.h ├── Mac.cpp ├── Mac.h ├── Url.cpp └── Url.h ├── Os ├── Authorization │ ├── Impersonate.cpp │ ├── Impersonate.h │ ├── Sid.cpp │ └── Sid.h ├── Explorer │ ├── StartupApproved.cpp │ └── StartupApproved.h ├── Firewall │ ├── FirewallPolicy.cpp │ ├── FirewallPolicy.h │ ├── Rule.cpp │ ├── Rule.h │ ├── RuleList.cpp │ └── RuleList.h ├── InternetShortcut.cpp ├── InternetShortcut.h ├── Memory │ ├── Local.cpp │ └── Local.h ├── MsConfig │ ├── StartupFolder.cpp │ ├── StartupFolder.h │ ├── StartupRegistry.cpp │ └── StartupRegistry.h ├── RemoteDesktop │ ├── Session.cpp │ └── Session.h ├── RestartManager.cpp ├── RestartManager.h ├── Shell.cpp ├── Shell.h ├── StartupLocation.h ├── Win32Service │ ├── Factory.cpp │ ├── Factory.h │ ├── Service.cpp │ └── Service.h └── Wmi │ ├── WbemClassObject.cpp │ ├── WbemClassObject.h │ ├── WbemServices.cpp │ └── WbemServices.h ├── Privilege.cpp ├── Privilege.h ├── Process ├── Event.cpp ├── Event.h ├── IWaitable.h ├── Mutex.cpp ├── Mutex.h ├── Process.cpp ├── Process.h ├── Thread.cpp └── Thread.h ├── ProcessMemoryStream.cpp ├── ProcessMemoryStream.h ├── Registry ├── Key.cpp ├── Key.h ├── RegEdit.cpp └── RegEdit.h ├── Scheduler ├── Vista │ ├── Action.cpp │ ├── Action.h │ ├── DailyTrigger.cpp │ ├── DailyTrigger.h │ ├── ExecAction.cpp │ ├── ExecAction.h │ ├── Principal.cpp │ ├── Principal.h │ ├── RegisteredTask.cpp │ ├── RegisteredTask.h │ ├── TaskDefinition.cpp │ ├── TaskDefinition.h │ ├── TaskFolder.cpp │ ├── TaskFolder.h │ ├── TaskService.cpp │ ├── TaskService.h │ ├── TaskSettings.cpp │ ├── TaskSettings.h │ ├── Trigger.cpp │ ├── Trigger.h │ ├── WeeklyTrigger.cpp │ └── WeeklyTrigger.h └── Xp │ ├── Task.cpp │ ├── Task.h │ ├── TaskScheduler.cpp │ ├── TaskScheduler.h │ ├── TaskTrigger.cpp │ └── TaskTrigger.h ├── ScopeGuard.h ├── Serialization └── ISerializer.h ├── Signals ├── AnonymousConnection.cpp ├── AnonymousConnection.h ├── ClassConnection.h ├── ConnectionBase.cpp ├── ConnectionBase.h ├── MultiThreadModes.h ├── SignalBase.h ├── Signals.h └── Trackable.h ├── Signature.cpp ├── Signature.h ├── Singleton.h ├── Stream.cpp ├── Stream.h ├── String.cpp ├── String.h ├── System.cpp ├── System.h ├── Time.cpp ├── Time.h ├── Trace.h ├── Types.h ├── Update.cpp ├── Update.h ├── Version.cpp ├── Version.h ├── WinToolsLib.vcxproj ├── WinToolsLib.vcxproj.filters ├── Wow64 ├── Emulator.cpp ├── Emulator.h ├── FsRedirection.cpp └── FsRedirection.h ├── XmlDom ├── Serializer.cpp ├── Serializer.h ├── XmlDom.cpp └── XmlDom.h ├── libudis86 ├── decode.c ├── decode.h ├── extern.h ├── input.c ├── input.h ├── itab.c ├── itab.h ├── types.h └── udis86.c ├── stdafx.cpp └── stdafx.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.opensdf 2 | *.sdf 3 | Out/* 4 | Temp/* 5 | ipch/* 6 | *.suo 7 | *.user -------------------------------------------------------------------------------- /Directories.props: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | $(SolutionDir)Out\$(Platform)\ 5 | $(SolutionDir)Temp\$(ProjectName)\$(Configuration)\$(Platform)\ 6 | 7 | 8 | 9 | $(OutDir);%(AdditionalLibraryDirectories) 10 | 11 | 12 | -------------------------------------------------------------------------------- /Library.props: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | true 6 | 7 | 8 | -------------------------------------------------------------------------------- /LogLevel.props: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | /D LOG_LEVEL=$(LogLevel) %(AdditionalOptions) 6 | 7 | 8 | 9 | 10 | /D LOG_LEVEL=0 %(AdditionalOptions) 11 | 12 | 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WinToolsLib 2 | Suite of classes for Windows programming 3 | -------------------------------------------------------------------------------- /StaticRuntime.props: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | MultiThreadedDebug 6 | 7 | 8 | 9 | 10 | MultiThreaded 11 | 12 | 13 | -------------------------------------------------------------------------------- /StdAfx.props: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | StdAfx.h 6 | Use 7 | 8 | 9 | -------------------------------------------------------------------------------- /Tests/BufferTest.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include 3 | 4 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 5 | using namespace WinToolsLib; 6 | 7 | namespace Tests 8 | { 9 | TEST_CLASS(BufferTest) 10 | { 11 | public: 12 | TEST_METHOD(BufferToString) 13 | { 14 | Buffer source; 15 | String result = Buffer::ToString(source); 16 | 17 | Assert::AreEqual(True, result.IsEmpty()); 18 | 19 | Byte code[] = { 0x0F, 0xAE, 0x01 }; 20 | SizeT codeSize = sizeof(code); 21 | 22 | source = Buffer(code, codeSize); 23 | result = Buffer::ToString(source); 24 | 25 | Assert::AreEqual(6, (Int32)result.GetLength()); 26 | 27 | Assert::AreEqual(Text("0FAE01"), result.GetBuffer()); 28 | } 29 | 30 | TEST_METHOD(BufferFromString) 31 | { 32 | String source; 33 | Buffer result = Buffer::FromString(source); 34 | 35 | Assert::AreEqual(True, result.IsEmpty()); 36 | 37 | source = String(Text("0FAE01")); 38 | result = Buffer::FromString(source); 39 | 40 | Assert::AreEqual(3, (Int32)result.GetSize()); 41 | 42 | Assert::AreEqual(0x0F, (Int32)result.GetBuffer()[0]); 43 | Assert::AreEqual(0xAE, (Int32)result.GetBuffer()[1]); 44 | Assert::AreEqual(0x01, (Int32)result.GetBuffer()[2]); 45 | } 46 | }; 47 | } -------------------------------------------------------------------------------- /Tests/ChecksumTest.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include 3 | #include 4 | 5 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 6 | using namespace WinToolsLib; 7 | using namespace WinToolsLib::Checksum; 8 | 9 | namespace Tests 10 | { 11 | TEST_CLASS(ChecksumTest) 12 | { 13 | public: 14 | TEST_METHOD(Crc32Calc) 15 | { 16 | []() 17 | { 18 | auto input = ""; 19 | auto hash = Crc32::Calc((PCByte)input, strlen(input)); 20 | 21 | Assert::IsTrue(0 == hash); 22 | }(); 23 | 24 | []() 25 | { 26 | auto input = "The quick brown fox jumps over the lazy dog"; 27 | auto hash = Crc32::Calc((PCByte)input, strlen(input)); 28 | 29 | Assert::IsTrue(0x414fa339 == hash); 30 | }(); 31 | 32 | []() 33 | { 34 | auto input = L"The quick brown fox jumps over the lazy dog"; 35 | auto hash = Crc32::Calc((PCByte)input, wcslen(input) * sizeof(WChar)); 36 | 37 | Assert::IsTrue(0x38282c38 == hash); 38 | }(); 39 | } 40 | 41 | TEST_METHOD(Fletcher16Calc) 42 | { 43 | []() 44 | { 45 | auto input = "Wikipedia"; 46 | auto hash = Fletcher16::Calc((PCByte)input, strlen(input)); 47 | 48 | Assert::IsTrue(0xEE9A == hash); 49 | }(); 50 | 51 | []() 52 | { 53 | auto input = L"Wikipedia"; 54 | auto hash = Fletcher16::Calc((PCByte)input, wcslen(input) * sizeof(WChar)); 55 | 56 | Assert::IsTrue(0xDD9A == hash); 57 | }(); 58 | } 59 | }; 60 | } -------------------------------------------------------------------------------- /Tests/Sha1Test.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include 3 | 4 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 5 | using namespace WinToolsLib; 6 | 7 | namespace Tests 8 | { 9 | TEST_CLASS(Sha1Test) 10 | { 11 | public: 12 | TEST_METHOD(Sha1ComputeHash) 13 | { 14 | []() 15 | { 16 | Cryptography::Sha1 sha1; 17 | 18 | auto input = ""; 19 | sha1.ComputeHash((PCByte)input, strlen(input)); 20 | 21 | auto digest = sha1.GetDigest(); 22 | auto output = Buffer::ToString(digest); 23 | 24 | Assert::AreEqual(Text("DA39A3EE5E6B4B0D3255BFEF95601890AFD80709"), output.GetBuffer()); 25 | }(); 26 | 27 | []() 28 | { 29 | Cryptography::Sha1 sha1; 30 | 31 | auto input = "The quick brown fox jumps over the lazy dog"; 32 | sha1.ComputeHash((PCByte)input, strlen(input)); 33 | 34 | auto digest = sha1.GetDigest(); 35 | auto output = Buffer::ToString(digest); 36 | 37 | Assert::AreEqual(Text("2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"), output.GetBuffer()); 38 | }(); 39 | 40 | []() 41 | { 42 | Cryptography::Sha1 sha1; 43 | 44 | auto input = "The quick brown fox jumps over the lazy cog"; 45 | sha1.ComputeHash((PCByte)input, strlen(input)); 46 | 47 | auto digest = sha1.GetDigest(); 48 | auto output = Buffer::ToString(digest); 49 | 50 | Assert::AreEqual(Text("DE9F2C7FD25E1B3AFAD3E85A0BD17D9B100DB4B3"), output.GetBuffer()); 51 | }(); 52 | 53 | []() // Hash of StringA 54 | { 55 | Cryptography::Sha1 sha1; 56 | 57 | StringA input("The quick brown fox jumps over the lazy dog"); 58 | sha1.ComputeHash(input); 59 | 60 | auto digest = sha1.GetDigest(); 61 | auto output = Buffer::ToString(digest); 62 | 63 | Assert::AreEqual(Text("2FD4E1C67A2D28FCED849EE1BB76E7391B93EB12"), output.GetBuffer()); 64 | }(); 65 | 66 | []() // Hash of StringW 67 | { 68 | Cryptography::Sha1 sha1; 69 | 70 | StringW input(L"The quick brown fox jumps over the lazy dog"); 71 | sha1.ComputeHash(input); 72 | 73 | auto digest = sha1.GetDigest(); 74 | auto output = Buffer::ToString(digest); 75 | 76 | Assert::AreEqual(Text("BD136CB58899C93173C33A90DDE95EAD0D0CF6DF"), output.GetBuffer()); 77 | }(); 78 | } 79 | }; 80 | } -------------------------------------------------------------------------------- /Tests/ShellTest.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include 3 | 4 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 5 | using namespace WinToolsLib; 6 | using namespace WinToolsLib::Os; 7 | 8 | namespace WinToolsLib { namespace Os 9 | { 10 | extern Bool KnownFolderRegistryIsConsistent(); 11 | 12 | } } 13 | 14 | namespace Tests 15 | { 16 | TEST_CLASS(ShellTest) 17 | { 18 | public: 19 | TEST_METHOD(ShellGetKnownFolder) 20 | { 21 | Assert::IsTrue(KnownFolderRegistryIsConsistent()); 22 | } 23 | }; 24 | } -------------------------------------------------------------------------------- /Tests/StringTest.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deeonis-ru/WinToolsLib/f3e0883c6e64c43b3669fdafd7d32d37c46322da/Tests/StringTest.cpp -------------------------------------------------------------------------------- /Tests/Tests.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Tests/UrlTest.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include 3 | 4 | using namespace Microsoft::VisualStudio::CppUnitTestFramework; 5 | using namespace WinToolsLib; 6 | 7 | namespace Tests 8 | { 9 | TEST_CLASS(UrlTest) 10 | { 11 | public: 12 | TEST_METHOD(UrlEncode) 13 | { 14 | { 15 | auto input = Text(""); 16 | auto output = Net::Url::Encode(input); 17 | 18 | Assert::AreEqual(Text(""), output); 19 | } 20 | { 21 | auto input = Text("text with spaces"); 22 | auto output = Net::Url::Encode(input); 23 | 24 | Assert::AreEqual(Text("text%20with%20spaces"), output); 25 | } 26 | { 27 | auto input = Text("first\r\nsecond\r\n"); 28 | auto output = Net::Url::Encode(input); 29 | 30 | Assert::AreEqual(Text("first%0D%0Asecond%0D%0A"), output); 31 | } 32 | } 33 | 34 | TEST_METHOD(UrlDecode) 35 | { 36 | { 37 | auto input = Text(""); 38 | auto output = Net::Url::Decode(input); 39 | 40 | Assert::AreEqual(Text(""), output); 41 | } 42 | { 43 | auto input = Text("text%20with%20spaces"); 44 | auto output = Net::Url::Decode(input); 45 | 46 | Assert::AreEqual(Text("text with spaces"), output); 47 | } 48 | { 49 | auto input = Text("first%0D%0Asecond%0D%0A"); 50 | auto output = Net::Url::Decode(input); 51 | 52 | Assert::AreEqual(Text("first\r\nsecond\r\n"), output); 53 | } 54 | } 55 | }; 56 | } -------------------------------------------------------------------------------- /Tests/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Tests.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | #include 7 | 8 | // TODO: reference any additional headers you need in STDAFX.H 9 | // and not in this file 10 | 11 | TEST_MODULE_CLEANUP(OnCleanup) 12 | { 13 | // Destructor of static object must be trivial. 14 | // With this call we are making TraceQueue dectructor kinda trivial 15 | // to avoid an exception in std::mutex::lock() 16 | // called from ConcurrentQueue::PushBack 17 | // called from WorkQueue::~WorkQueue() 18 | // called from TraceQueue::~TraceQueue() 19 | // being called from DllMain with DLL_PROCESS_DETACH. 20 | WinToolsLib::TraceQueue::Instance().Exit(); 21 | } -------------------------------------------------------------------------------- /Tests/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | // 5 | 6 | #pragma once 7 | 8 | #include "targetver.h" 9 | 10 | // Headers for CppUnitTest 11 | #include "CppUnitTest.h" 12 | 13 | // TODO: reference additional headers your program requires here 14 | #include 15 | 16 | namespace Microsoft 17 | { 18 | namespace VisualStudio 19 | { 20 | namespace CppUnitTestFramework 21 | { 22 | template<> static std::wstring ToString(const WinToolsLib::Bool& value) 23 | { 24 | if (WinToolsLib::True == value) 25 | { 26 | return std::wstring(L"True"); 27 | } 28 | return std::wstring(L"False"); 29 | } 30 | 31 | template<> static std::wstring ToString(const WinToolsLib::Int64& value) 32 | { 33 | WinToolsLib::WChar sz[128]; 34 | swprintf_s(sz, L"%I64d", value); 35 | return std::wstring(sz); 36 | } 37 | 38 | template<> static std::wstring ToString(const WinToolsLib::UInt16& value) 39 | { 40 | return ToString((WinToolsLib::UInt32)value); 41 | } 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /Tests/targetver.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Including SDKDDKVer.h defines the highest available Windows platform. 4 | 5 | // If you wish to build your application for a previous Windows platform, include WinSDKVer.h and 6 | // set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. 7 | 8 | #include 9 | -------------------------------------------------------------------------------- /WinToolsLib.props: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | ..;%(AdditionalIncludeDirectories) 6 | 7 | 8 | 9 | 10 | WinToolsLibDebug.lib;%(AdditionalDependencies) 11 | 12 | 13 | 14 | 15 | WinToolsLibRelease.lib;%(AdditionalDependencies) 16 | 17 | 18 | -------------------------------------------------------------------------------- /WinToolsLib/Application.cpp: -------------------------------------------------------------------------------- 1 | #include "Application.h" 2 | #include "Exception.h" 3 | #include "FileSystem\File.h" 4 | #include 5 | 6 | namespace WinToolsLib 7 | { 8 | using namespace FileSystem; 9 | 10 | Buffer Application::LoadResource(const TChar* resName, const TChar* resType) 11 | { 12 | HMODULE hModule = ::GetModuleHandle(NULL); 13 | 14 | HRSRC hResInfo = ::FindResource(hModule, resName, resType); 15 | if (!hResInfo) 16 | { 17 | THROW_LAST_ERROR_EXCEPTION(); 18 | } 19 | 20 | HGLOBAL hResData = ::LoadResource(hModule, hResInfo); 21 | if (!hResData) 22 | { 23 | THROW_LAST_ERROR_EXCEPTION(); 24 | } 25 | 26 | DWORD resSize = ::SizeofResource(hModule, hResInfo); 27 | LPBYTE pResRaw = (LPBYTE)::LockResource(hResData); 28 | 29 | return Buffer(pResRaw, resSize); 30 | } 31 | 32 | Void Application::ExtractResource(const TChar* path, const TChar* resName, const TChar* resType) 33 | { 34 | auto file = File::Create(path); 35 | auto resource = Application::LoadResource(resName, resType); 36 | file.Write(resource); 37 | } 38 | 39 | String Application::LoadStringResource(UInt32 id) 40 | { 41 | HMODULE hModule = ::GetModuleHandle(NULL); 42 | PTChar pBuffer = NULL; 43 | 44 | auto size = ::LoadString(hModule, id, (PTChar)&pBuffer, 0); 45 | if (!size) 46 | { 47 | THROW_LAST_ERROR_EXCEPTION(); 48 | } 49 | 50 | return String(pBuffer, size); 51 | } 52 | } -------------------------------------------------------------------------------- /WinToolsLib/Application.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "Buffer.h" 4 | #include "String.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | class Application 9 | { 10 | public: 11 | static Buffer LoadResource(const TChar* resName, const TChar* resType); 12 | static Void ExtractResource(const TChar* path, const TChar* resName, const TChar* resType); 13 | static String LoadStringResource(UInt32 id); 14 | }; 15 | } 16 | -------------------------------------------------------------------------------- /WinToolsLib/Bool.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | namespace WinToolsLib 4 | { 5 | class Bool 6 | { 7 | public: 8 | Bool(bool value = false) : m_value(value) {} 9 | 10 | operator bool() const { return m_value; } 11 | 12 | private: 13 | bool m_value; 14 | }; 15 | 16 | #define True Bool(true) 17 | #define False Bool(false) 18 | } -------------------------------------------------------------------------------- /WinToolsLib/Buffer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "String.h" 4 | #include "Exception.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | class Buffer 9 | { 10 | public: 11 | Buffer(SizeT size = 0); 12 | Buffer(PCByte buffer, SizeT size); 13 | Buffer(const Buffer& other); 14 | Buffer(Buffer&& other); 15 | virtual ~Buffer(); 16 | 17 | operator PCByte() const; 18 | operator PByte(); 19 | 20 | const Byte& operator[](SizeT index) const; 21 | Byte& operator[](SizeT index); 22 | 23 | PCByte GetBuffer() const; 24 | PByte GetBuffer(); 25 | 26 | SizeT GetSize() const; 27 | Bool IsEmpty() const; 28 | 29 | Buffer& operator=(Buffer&& other); 30 | Buffer operator+(const Buffer& other) const; 31 | Buffer& operator+=(const Buffer& other); 32 | 33 | Buffer& Append(PCByte buffer, SizeT size); 34 | 35 | static String ToString(const Buffer& source); 36 | static Buffer FromString(const String& source); 37 | 38 | protected: 39 | PByte Allocate(SizeT size); 40 | Void Free(); 41 | 42 | private: 43 | PByte m_buffer; 44 | SizeT m_size; 45 | }; 46 | 47 | inline Buffer::operator PCByte() const 48 | { 49 | return m_buffer; 50 | } 51 | 52 | inline Buffer::operator PByte() 53 | { 54 | return m_buffer; 55 | } 56 | 57 | inline const Byte& Buffer::operator[](SizeT index) const 58 | { 59 | if (index < m_size) 60 | { 61 | return m_buffer[index]; 62 | } 63 | THROW_OUT_OF_RANGE_EXCEPTION(); 64 | } 65 | 66 | inline Byte& Buffer::operator[](SizeT index) 67 | { 68 | if (index < m_size) 69 | { 70 | return m_buffer[index]; 71 | } 72 | THROW_OUT_OF_RANGE_EXCEPTION(); 73 | } 74 | 75 | inline PCByte Buffer::GetBuffer() const 76 | { 77 | return m_buffer; 78 | } 79 | 80 | inline PByte Buffer::GetBuffer() 81 | { 82 | return m_buffer; 83 | } 84 | 85 | inline SizeT Buffer::GetSize() const 86 | { 87 | return m_size; 88 | } 89 | 90 | inline Bool Buffer::IsEmpty() const 91 | { 92 | return 0 == m_size; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /WinToolsLib/BufferStream.cpp: -------------------------------------------------------------------------------- 1 | #include "BufferStream.h" 2 | 3 | #pragma warning(disable:4355) 4 | 5 | namespace WinToolsLib 6 | { 7 | BufferStream::BufferStream(Buffer&& buffer) : 8 | Buffer(std::move(buffer)), 9 | Stream((Buffer&)*this) 10 | { 11 | } 12 | } -------------------------------------------------------------------------------- /WinToolsLib/BufferStream.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "Stream.h" 4 | #include "Buffer.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | class BufferStream : public Buffer, public Stream 9 | { 10 | public: 11 | BufferStream(Buffer&& buffer); 12 | }; 13 | } 14 | -------------------------------------------------------------------------------- /WinToolsLib/Cached.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | 4 | #include 5 | #include 6 | 7 | namespace WinToolsLib 8 | { 9 | template 10 | class Cached 11 | { 12 | public: 13 | typedef std::function Initializer; 14 | 15 | explicit Cached(Initializer&& init) : 16 | Cached(T(), std::move(init)) 17 | { 18 | } 19 | 20 | explicit Cached(T&& t, Initializer&& init) : 21 | m_t(std::move(t)), 22 | m_init(std::move(init)) 23 | { 24 | } 25 | 26 | Cached(Cached&& other) 27 | { 28 | MoveFrom(other); 29 | } 30 | 31 | Cached(const Cached& other) 32 | { 33 | CopyFrom(other); 34 | } 35 | 36 | Cached& operator=(Cached&& other) 37 | { 38 | MoveFrom(other); 39 | return *this; 40 | } 41 | 42 | Cached& operator=(const Cached& other) 43 | { 44 | CopyFrom(other); 45 | return *this; 46 | } 47 | 48 | operator const T&() const 49 | { 50 | if (m_init) 51 | { 52 | Initializer init; 53 | init.swap(m_init); 54 | m_init = nullptr; 55 | m_t = init(); 56 | } 57 | return m_t; 58 | } 59 | 60 | protected: 61 | Void MoveFrom(Cached& other) 62 | { 63 | m_t = std::move(other.m_t); 64 | m_init = std::move(other.m_init); 65 | } 66 | 67 | Void CopyFrom(const Cached& other) 68 | { 69 | m_t = other.m_t; 70 | m_init = other.m_init; 71 | } 72 | 73 | private: 74 | mutable T m_t; 75 | mutable Initializer m_init; 76 | }; 77 | } 78 | -------------------------------------------------------------------------------- /WinToolsLib/Checksum/Crc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | 4 | namespace WinToolsLib { namespace Checksum 5 | { 6 | class Crc32 7 | { 8 | public: 9 | static UInt32 Calc(PCByte data, SizeT size); 10 | 11 | private: 12 | static UInt32 Update(UInt32 init, PCByte data, SizeT size); 13 | }; 14 | 15 | } } -------------------------------------------------------------------------------- /WinToolsLib/Checksum/Fletcher.cpp: -------------------------------------------------------------------------------- 1 | #include "Fletcher.h" 2 | 3 | namespace WinToolsLib { namespace Checksum 4 | { 5 | UInt16 Fletcher16::Calc(PCByte data, SizeT size) 6 | { 7 | UInt16 sum1 = 0xff; 8 | UInt16 sum2 = 0xff; 9 | 10 | while (size) 11 | { 12 | SizeT tlen = size > 20 ? 20 : size; 13 | size -= tlen; 14 | do 15 | { 16 | sum1 += *data++; 17 | sum2 += sum1; 18 | } 19 | while (--tlen); 20 | sum1 = (sum1 & 0xff) + (sum1 >> 8); 21 | sum2 = (sum2 & 0xff) + (sum2 >> 8); 22 | } 23 | 24 | // Second reduction step to reduce sums to 8 bits 25 | sum1 = (sum1 & 0xff) + (sum1 >> 8); 26 | sum2 = (sum2 & 0xff) + (sum2 >> 8); 27 | return sum2 << 8 | sum1; 28 | } 29 | 30 | } } -------------------------------------------------------------------------------- /WinToolsLib/Checksum/Fletcher.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | 4 | namespace WinToolsLib { namespace Checksum 5 | { 6 | class Fletcher16 7 | { 8 | public: 9 | static UInt16 Calc(PCByte data, SizeT size); 10 | }; 11 | 12 | } } -------------------------------------------------------------------------------- /WinToolsLib/ChunkCollection.cpp: -------------------------------------------------------------------------------- 1 | #include "ChunkCollection.h" 2 | #include 3 | 4 | namespace WinToolsLib 5 | { 6 | ChunkBase::ChunkBase(SizeT begin, SizeT end) : 7 | m_begin(begin), 8 | m_end(end) 9 | { 10 | } 11 | 12 | ChunkBase::ChunkBase(const ChunkBase& other) : 13 | m_begin(other.m_begin), 14 | m_end(other.m_end) 15 | { 16 | } 17 | 18 | SizeT ChunkBase::GetBegin() const 19 | { 20 | return m_begin; 21 | } 22 | 23 | SizeT ChunkBase::GetEnd() const 24 | { 25 | return m_end; 26 | } 27 | 28 | Void ChunkBase::SetBegin(SizeT begin) 29 | { 30 | m_begin = begin; 31 | } 32 | 33 | Void ChunkBase::SetEnd(SizeT end) 34 | { 35 | m_end = end; 36 | } 37 | 38 | Bool ChunkBase::IsSameType(const ChunkBase& other) const 39 | { 40 | return True; 41 | } 42 | 43 | Bool ChunkBase::operator<(const ChunkBase& other) const 44 | { 45 | return (Bool)(m_begin < other.m_begin); 46 | } 47 | } -------------------------------------------------------------------------------- /WinToolsLib/Com/BStr.cpp: -------------------------------------------------------------------------------- 1 | #include "BStr.h" 2 | 3 | #include 4 | #pragma comment(lib, "OleAut32.lib") 5 | 6 | #include 7 | 8 | namespace WinToolsLib { namespace Com 9 | { 10 | BStr::BStr(const OLECHAR* bstr) : 11 | m_bstr(nullptr) 12 | { 13 | m_bstr = ::SysAllocString(bstr); 14 | } 15 | 16 | BStr::BStr(UInt32 length) : 17 | m_bstr(nullptr) 18 | { 19 | m_bstr = ::SysAllocStringLen(nullptr, length); 20 | } 21 | 22 | BStr::BStr(BStr&& other) : 23 | m_bstr(nullptr) 24 | { 25 | MoveFrom(other); 26 | } 27 | 28 | BStr::BStr(const BStr& other) : 29 | m_bstr(nullptr) 30 | { 31 | CopyFrom(other); 32 | } 33 | 34 | BStr::~BStr() 35 | { 36 | Free(); 37 | } 38 | 39 | BStr BStr::Alloc(UInt32 length) 40 | { 41 | return BStr(length); 42 | } 43 | 44 | Void BStr::Free() 45 | { 46 | ::SysFreeString(Detach()); 47 | } 48 | 49 | Void BStr::Attach(BSTR bstr) 50 | { 51 | Free(); 52 | m_bstr = bstr; 53 | } 54 | 55 | BSTR BStr::Detach() 56 | { 57 | auto bstr = m_bstr; 58 | m_bstr = nullptr; 59 | return bstr; 60 | } 61 | 62 | UInt32 BStr::GetLength() const 63 | { 64 | return ::SysStringLen(m_bstr); 65 | } 66 | 67 | BStr& BStr::operator=(BStr&& other) 68 | { 69 | MoveFrom(other); 70 | return *this; 71 | } 72 | 73 | BStr& BStr::operator=(const BStr& other) 74 | { 75 | CopyFrom(other); 76 | return *this; 77 | } 78 | 79 | Void BStr::MoveFrom(BStr& other) 80 | { 81 | if (&m_bstr != &other.m_bstr) 82 | { 83 | Attach(other.Detach()); 84 | } 85 | } 86 | 87 | Void BStr::CopyFrom(const BStr& other) 88 | { 89 | if (&m_bstr != &other.m_bstr) 90 | { 91 | ::SysReAllocString(&m_bstr, other); 92 | } 93 | } 94 | 95 | } } -------------------------------------------------------------------------------- /WinToolsLib/Com/BStr.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | #include 6 | 7 | namespace WinToolsLib { namespace Com 8 | { 9 | class BStr 10 | { 11 | public: 12 | BStr(const OLECHAR* bstr = nullptr); 13 | BStr(UInt32 length); 14 | BStr(BStr&& other); 15 | BStr(const BStr& other); 16 | ~BStr(); 17 | 18 | static BStr Alloc(UInt32 length); 19 | Void Free(); 20 | 21 | Void Attach(BSTR bstr); 22 | BSTR Detach(); 23 | 24 | UInt32 GetLength() const; 25 | BSTR GetBuffer() const; 26 | 27 | operator BSTR() const; 28 | BSTR* operator&(); 29 | 30 | BStr& operator=(BStr&& other); 31 | BStr& operator=(const BStr& other); 32 | 33 | protected: 34 | Void MoveFrom(BStr& other); 35 | Void CopyFrom(const BStr& other); 36 | 37 | private: 38 | BSTR m_bstr; 39 | }; 40 | 41 | inline BSTR BStr::GetBuffer() const 42 | { 43 | return m_bstr; 44 | } 45 | 46 | inline BStr::operator BSTR() const 47 | { 48 | return m_bstr; 49 | } 50 | 51 | inline BSTR* BStr::operator&() 52 | { 53 | return &m_bstr; 54 | } 55 | 56 | } } 57 | 58 | -------------------------------------------------------------------------------- /WinToolsLib/Com/Initialize.cpp: -------------------------------------------------------------------------------- 1 | #include "Initialize.h" 2 | 3 | #include 4 | #pragma comment(lib, "Ole32.lib") 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Com 9 | { 10 | Initialize::Initialize(Model model) : 11 | m_successful(False) 12 | { 13 | HRESULT hr = E_NOTIMPL; 14 | switch (model) 15 | { 16 | case Model::SingleThreaded: 17 | hr = ::CoInitialize(nullptr); 18 | break; 19 | 20 | case Model::ApartmentThreaded: 21 | hr = ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); 22 | break; 23 | 24 | case Model::Multithreaded: 25 | hr = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED); 26 | break; 27 | } 28 | if (S_OK == hr || S_FALSE == hr) 29 | { 30 | m_successful = True; 31 | } 32 | } 33 | 34 | Initialize::~Initialize() 35 | { 36 | if (m_successful) 37 | { 38 | ::CoUninitialize(); 39 | } 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /WinToolsLib/Com/Initialize.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | namespace Com 7 | { 8 | enum class Model 9 | { 10 | SingleThreaded, 11 | ApartmentThreaded, 12 | Multithreaded 13 | }; 14 | 15 | class Initialize 16 | { 17 | public: 18 | Initialize(Model model); 19 | ~Initialize(); 20 | 21 | private: 22 | Bool m_successful; 23 | }; 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /WinToolsLib/Com/SafeArray.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | #include 6 | 7 | namespace WinToolsLib { namespace Com 8 | { 9 | class SafeArray 10 | { 11 | public: 12 | SafeArray(SAFEARRAY* safeArray); 13 | SafeArray(SafeArray&& other); 14 | SafeArray(const SafeArray& other); 15 | ~SafeArray(); 16 | 17 | static SafeArray Create(VARTYPE vt, UInt32 size); 18 | Void Destroy(); 19 | 20 | Void Attach(SAFEARRAY* safeArray); 21 | SAFEARRAY* Detach(); 22 | 23 | UInt32 GetSize() const; 24 | operator const SAFEARRAY*() const; 25 | 26 | Void GetElement(Int32 index, PVoid value); 27 | Void SetElement(Int32 index, PVoid value); 28 | 29 | SafeArray& operator=(SafeArray&& other); 30 | SafeArray& operator=(const SafeArray& other); 31 | 32 | protected: 33 | Void MoveFrom(SafeArray& other); 34 | Void CopyFrom(const SafeArray& other); 35 | 36 | private: 37 | SAFEARRAY* m_safeArray; 38 | }; 39 | 40 | inline SafeArray::operator const SAFEARRAY*() const 41 | { 42 | return m_safeArray; 43 | } 44 | 45 | } } 46 | 47 | -------------------------------------------------------------------------------- /WinToolsLib/Com/TaskMem.cpp: -------------------------------------------------------------------------------- 1 | #include "TaskMem.h" 2 | 3 | #include 4 | #pragma comment(lib, "Ole32.lib") 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Com 9 | { 10 | namespace Details 11 | { 12 | PVoid Alloc(SizeT bytes) 13 | { 14 | return ::CoTaskMemAlloc(bytes); 15 | } 16 | 17 | Void Free(PVoid p) 18 | { 19 | ::CoTaskMemFree(p); 20 | } 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /WinToolsLib/Com/TaskMem.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace Com 8 | { 9 | template 10 | class TaskMem 11 | { 12 | TaskMem(const TaskMem& other); // Non-copyable 13 | TaskMem& operator=(const TaskMem& other); // Non-copyable 14 | 15 | public: 16 | TaskMem(T* p = nullptr); 17 | TaskMem(TaskMem&& other); 18 | ~TaskMem(); 19 | 20 | TaskMem& operator=(TaskMem&& other); 21 | 22 | Void Free(); 23 | static TaskMem Alloc(SizeT bytes); 24 | 25 | operator T*() const; 26 | T& operator *() const; 27 | T* operator ->() const; 28 | T** operator &(); 29 | 30 | private: 31 | T* m_p; 32 | }; 33 | 34 | namespace Details 35 | { 36 | PVoid Alloc(SizeT bytes); 37 | Void Free(PVoid p); 38 | } 39 | 40 | template 41 | TaskMem::TaskMem(T* p) : 42 | m_p(p) 43 | { 44 | } 45 | 46 | template 47 | TaskMem::TaskMem(TaskMem&& other) : 48 | m_p(other.m_p) 49 | { 50 | other.m_p = nullptr; 51 | } 52 | 53 | template 54 | TaskMem::~TaskMem() 55 | { 56 | Details::Free(m_p); 57 | } 58 | 59 | template 60 | TaskMem& TaskMem::operator=(TaskMem&& other) 61 | { 62 | Free(); 63 | m_p = other.m_p; 64 | other.m_p = nullptr; 65 | return *this; 66 | } 67 | 68 | template 69 | Void TaskMem::Free() 70 | { 71 | T* p = m_p; 72 | if (p) 73 | { 74 | m_p = nullptr; 75 | Details::Free(p); 76 | } 77 | } 78 | 79 | template 80 | TaskMem TaskMem::Alloc(SizeT bytes) 81 | { 82 | return TaskMem((T*)Details::Alloc(bytes)); 83 | } 84 | 85 | template 86 | TaskMem::operator T*() const 87 | { 88 | return m_p; 89 | } 90 | 91 | template 92 | T& TaskMem::operator *() const 93 | { 94 | return *m_p; 95 | } 96 | 97 | template 98 | T* TaskMem::operator ->() const 99 | { 100 | return m_p; 101 | } 102 | 103 | template 104 | T** TaskMem::operator &() 105 | { 106 | return &m_p; 107 | } 108 | } 109 | } 110 | 111 | -------------------------------------------------------------------------------- /WinToolsLib/Com/Variant.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | #include "..\Exception.h" 5 | 6 | #include 7 | 8 | #include 9 | 10 | namespace WinToolsLib { namespace Com 11 | { 12 | class Variant 13 | { 14 | public: 15 | Variant(VARIANT* pvar = nullptr); 16 | Variant(Variant&& other); 17 | Variant(const Variant& other); 18 | ~Variant(); 19 | 20 | Void Clear(); 21 | 22 | operator const VARIANT&() const; 23 | 24 | VARIANT* operator&(); 25 | VARIANT* operator->(); 26 | 27 | Variant& operator=(Variant&& other); 28 | Variant& operator=(const Variant& other); 29 | 30 | typedef std::vector StringArray; 31 | 32 | StringArray ToStringArray() const; 33 | static Variant FromStringArray(const StringArray& stringArray); 34 | 35 | IDispatch* ToIDispatch() const; 36 | 37 | protected: 38 | Void MoveFrom(Variant& other); 39 | Void CopyFrom(const Variant& other); 40 | 41 | private: 42 | VARIANT m_var; 43 | }; 44 | 45 | inline Variant::operator const VARIANT&() const 46 | { 47 | return m_var; 48 | } 49 | 50 | inline VARIANT* Variant::operator&() 51 | { 52 | return &m_var; 53 | } 54 | 55 | inline VARIANT* Variant::operator->() 56 | { 57 | return &m_var; 58 | } 59 | 60 | class VariantWrongTypeException : public Exception 61 | { 62 | public: 63 | VariantWrongTypeException(PCChar function, UInt32 line, PCChar file, VARTYPE expectedType) : 64 | Exception(function, line, file, CreateErrorMessage(expectedType)), 65 | m_expectedType(expectedType) 66 | { 67 | } 68 | 69 | protected: 70 | static String CreateErrorMessage(VARTYPE expectedType) 71 | { 72 | return String::FormatS(Text("Variant has wrong type, was expected: %hu"), expectedType); 73 | } 74 | 75 | private: 76 | VARTYPE m_expectedType; 77 | }; 78 | 79 | } } 80 | 81 | -------------------------------------------------------------------------------- /WinToolsLib/CommandLine/ArgumentList.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | #include "CommandList.h" 5 | 6 | #include 7 | 8 | namespace WinToolsLib 9 | { 10 | namespace CommandLine 11 | { 12 | class ArgumentList 13 | { 14 | public: 15 | ArgumentList(const TChar* commandLine = nullptr); 16 | 17 | Void PushBack(const TChar* argument); 18 | Void PushFront(const TChar* argument); 19 | String PopFront(); 20 | 21 | Void Append(const CommandList& list); 22 | 23 | const String& operator[](SizeT index) const; 24 | String& operator[](SizeT index); 25 | 26 | SizeT GetSize() const; 27 | String ToCommandLine() const; 28 | CommandList ToCommandList() const; 29 | 30 | private: 31 | std::deque m_arguments; 32 | }; 33 | 34 | inline Void ArgumentList::PushBack(const TChar* argument) 35 | { 36 | m_arguments.push_back(argument); 37 | } 38 | 39 | inline Void ArgumentList::PushFront(const TChar* argument) 40 | { 41 | m_arguments.push_front(argument); 42 | } 43 | 44 | inline String ArgumentList::PopFront() 45 | { 46 | String front = std::move(m_arguments.front()); 47 | m_arguments.pop_front(); 48 | return front; 49 | } 50 | 51 | inline const String& ArgumentList::operator[](SizeT index) const 52 | { 53 | return m_arguments[index]; 54 | } 55 | 56 | inline String& ArgumentList::operator[](SizeT index) 57 | { 58 | return m_arguments[index]; 59 | } 60 | 61 | inline SizeT ArgumentList::GetSize() const 62 | { 63 | return m_arguments.size(); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /WinToolsLib/CommandLine/CommandList.cpp: -------------------------------------------------------------------------------- 1 | #include "CommandList.h" 2 | #include "..\Exception.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | namespace CommandLine 7 | { 8 | Void Command::MoveFrom(Command& other) 9 | { 10 | if (this != &other) 11 | { 12 | m_name = std::move(other.m_name); 13 | m_parameters = std::move(other.m_parameters); 14 | } 15 | } 16 | 17 | Void Command::CopyFrom(const Command& other) 18 | { 19 | if (this != &other) 20 | { 21 | m_name = other.m_name; 22 | m_parameters = other.m_parameters; 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /WinToolsLib/CommandLine/CommandList.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | #include 6 | #include 7 | 8 | namespace WinToolsLib 9 | { 10 | namespace CommandLine 11 | { 12 | class Command; 13 | 14 | typedef std::list CommandList; 15 | typedef std::vector ParameterList; 16 | 17 | class Command 18 | { 19 | public: 20 | Command(const TChar* name); 21 | Command(Command&& other); 22 | Command(const Command& other); 23 | 24 | Command& operator=(Command&& other); 25 | Command& operator=(const Command& other); 26 | Bool operator==(const Command& other) const; 27 | 28 | const String& GetName() const; 29 | 30 | Void AddParameter(const TChar* parameter); 31 | const ParameterList& GetParameters() const; 32 | 33 | protected: 34 | Void MoveFrom(Command& other); 35 | Void CopyFrom(const Command& other); 36 | 37 | private: 38 | String m_name; 39 | std::vector m_parameters; 40 | }; 41 | 42 | inline Command::Command(const TChar* name) : 43 | m_name(name) 44 | { 45 | } 46 | 47 | inline Command::Command(Command&& other) 48 | { 49 | MoveFrom(other); 50 | } 51 | 52 | inline Command::Command(const Command& other) 53 | { 54 | CopyFrom(other); 55 | } 56 | 57 | inline Command& Command::operator=(Command&& other) 58 | { 59 | MoveFrom(other); 60 | return *this; 61 | } 62 | 63 | inline Command& Command::operator=(const Command& other) 64 | { 65 | CopyFrom(other); 66 | return *this; 67 | } 68 | 69 | inline Bool Command::operator==(const Command& other) const 70 | { 71 | return m_name == other.m_name; 72 | } 73 | 74 | inline const String& Command::GetName() const 75 | { 76 | return m_name; 77 | } 78 | 79 | inline Void Command::AddParameter(const TChar* parameter) 80 | { 81 | m_parameters.push_back(parameter); 82 | } 83 | 84 | inline const ParameterList& Command::GetParameters() const 85 | { 86 | return m_parameters; 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /WinToolsLib/CommonMemoryStream.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "IStream.h" 4 | #include "Handles\Handle.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | class CommonMemoryStream : public IStream 9 | { 10 | public: 11 | SizeT GetPosition() const override; 12 | Void SetPosition(SizeT position) override; 13 | 14 | Void Reset() override; 15 | Void Skip(SizeT bytes) override; 16 | 17 | IStream& operator>>(Int8& value) override; 18 | IStream& operator>>(Int16& value) override; 19 | IStream& operator>>(Int32& value) override; 20 | IStream& operator>>(Int64& value) override; 21 | 22 | IStream& operator>>(UInt8& value) override; 23 | IStream& operator>>(UInt16& value) override; 24 | IStream& operator>>(UInt32& value) override; 25 | IStream& operator>>(UInt64& value) override; 26 | 27 | IStream& operator<<(Int8 value) override; 28 | IStream& operator<<(Int16 value) override; 29 | IStream& operator<<(Int32 value) override; 30 | IStream& operator<<(Int64 value) override; 31 | 32 | IStream& operator<<(UInt8 value) override; 33 | IStream& operator<<(UInt16 value) override; 34 | IStream& operator<<(UInt32 value) override; 35 | IStream& operator<<(UInt64 value) override; 36 | 37 | protected: 38 | SizeT m_position; 39 | }; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /WinToolsLib/Concurrency/ConcurrentQueue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Process\Event.h" 4 | 5 | #include 6 | #include 7 | 8 | namespace WinToolsLib { namespace Concurrency 9 | { 10 | using namespace Process; 11 | template 12 | class ConcurrentQueue 13 | { 14 | public: 15 | ConcurrentQueue(); 16 | 17 | Void PushBack(T&& t); 18 | Void PushFront(T&& t); 19 | T Pop(); 20 | 21 | Void Clear(); 22 | Bool IsEmpty() const; 23 | 24 | private: 25 | mutable std::mutex m_lock; 26 | std::list m_list; 27 | Event m_ready; 28 | }; 29 | 30 | template 31 | ConcurrentQueue::ConcurrentQueue() 32 | { 33 | m_ready.Create(False, False); 34 | } 35 | 36 | template 37 | Void ConcurrentQueue::PushBack(T&& t) 38 | { 39 | std::lock_guard guard(m_lock); 40 | m_list.push_back(std::forward(t)); 41 | m_ready.Set(); 42 | } 43 | 44 | template 45 | Void ConcurrentQueue::PushFront(T&& t) 46 | { 47 | std::lock_guard guard(m_lock); 48 | m_list.push_front(std::forward(t)); 49 | m_ready.Set(); 50 | } 51 | 52 | template 53 | T ConcurrentQueue::Pop() 54 | { 55 | Bool haveToWait = False; 56 | while (True) 57 | { 58 | if (haveToWait) 59 | { 60 | m_ready.Wait(); 61 | } 62 | 63 | std::lock_guard guard(m_lock); 64 | if (m_list.empty()) 65 | { 66 | haveToWait = True; 67 | continue; 68 | } 69 | 70 | auto t = m_list.front(); 71 | m_list.pop_front(); 72 | 73 | if (m_list.empty()) 74 | { 75 | m_ready.Reset(); 76 | } 77 | return t; 78 | } 79 | 80 | return T(); 81 | } 82 | 83 | template 84 | Void ConcurrentQueue::Clear() 85 | { 86 | std::lock_guard guard(m_lock); 87 | m_list.clear(); 88 | m_ready.Reset(); 89 | } 90 | 91 | template 92 | Bool ConcurrentQueue::IsEmpty() const 93 | { 94 | std::lock_guard guard(m_lock); 95 | if (m_list.empty()) 96 | { 97 | return True; 98 | } 99 | return False; 100 | } 101 | 102 | } } -------------------------------------------------------------------------------- /WinToolsLib/Concurrency/WorkQueue.cpp: -------------------------------------------------------------------------------- 1 | #include "WorkQueue.h" 2 | 3 | namespace WinToolsLib { namespace Concurrency 4 | { 5 | WorkQueue::WorkQueue() : 6 | m_done(False), 7 | m_thread([=]() 8 | { 9 | while (!m_done) 10 | { 11 | try 12 | { 13 | m_queue.Pop()(); 14 | } 15 | catch (...) 16 | { 17 | //HANDLE_EXCEPTION(); 18 | } 19 | } 20 | 21 | // Without ExitThread() the WorkQueue class is not usable 22 | // as static (Singleton) due to the bug in Visual C++: 23 | // "std::thread::join() hangs if called after main() exits". 24 | ::ExitThread(0); 25 | }) 26 | { 27 | } 28 | 29 | WorkQueue::~WorkQueue() 30 | { 31 | Exit(); 32 | } 33 | } } -------------------------------------------------------------------------------- /WinToolsLib/Concurrency/WorkQueue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "ConcurrentQueue.h" 4 | 5 | #include 6 | #include 7 | 8 | namespace WinToolsLib { namespace Concurrency 9 | { 10 | class WorkQueue 11 | { 12 | public: 13 | WorkQueue(); 14 | ~WorkQueue(); 15 | 16 | typedef std::function WorkItem; 17 | 18 | Void Clear(); 19 | Void Exit(); 20 | 21 | Void PushBack(WorkItem workItem); 22 | Void PushFront(WorkItem workItem); 23 | 24 | protected: 25 | ConcurrentQueue m_queue; 26 | Bool m_done; // NOTE: m_done MUST be declared before m_thread 27 | std::thread m_thread; 28 | }; 29 | 30 | inline Void WorkQueue::Clear() 31 | { 32 | m_queue.PushFront([=]() 33 | { 34 | m_queue.Clear(); 35 | }); 36 | } 37 | 38 | inline Void WorkQueue::Exit() 39 | { 40 | if (m_thread.joinable()) 41 | { 42 | m_queue.PushBack([=]() 43 | { 44 | m_done = True; 45 | }); 46 | m_thread.join(); 47 | } 48 | } 49 | 50 | inline Void WorkQueue::PushBack(WorkItem workItem) 51 | { 52 | m_queue.PushBack(std::move(workItem)); 53 | } 54 | 55 | inline Void WorkQueue::PushFront(WorkItem workItem) 56 | { 57 | m_queue.PushBack(std::move(workItem)); 58 | } 59 | 60 | } } -------------------------------------------------------------------------------- /WinToolsLib/Cryptography/Hash.cpp: -------------------------------------------------------------------------------- 1 | #include "Hash.h" 2 | #include "..\Exception.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | namespace Cryptography 7 | { 8 | Void Hash::ComputeHash(const Byte* data, SizeT size) 9 | { 10 | Update(data, size); 11 | Finalize(); 12 | } 13 | 14 | Void Hash::ComputeHash(const Buffer& buffer) 15 | { 16 | ComputeHash(buffer.GetBuffer(), buffer.GetSize()); 17 | } 18 | 19 | Void Hash::ComputeHash(const StringA& string) 20 | { 21 | ComputeHash((PCByte)string.GetBuffer(), string.GetSize() * sizeof(Char)); 22 | } 23 | 24 | Void Hash::ComputeHash(const StringW& string) 25 | { 26 | ComputeHash((PCByte)string.GetBuffer(), string.GetSize() * sizeof(WChar)); 27 | } 28 | 29 | Void Hash::ComputeHash(const FileSystem::File& file, SizeT chunkSize) 30 | { 31 | auto data = Buffer(chunkSize); 32 | auto bytes = file.Read(data); 33 | while (bytes > 0) 34 | { 35 | Update(data.GetBuffer(), bytes); 36 | bytes = file.Read(data); 37 | } 38 | Finalize(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /WinToolsLib/Cryptography/Hash.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "IHash.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace Cryptography 8 | { 9 | class Hash : public IHash 10 | { 11 | public: 12 | Void ComputeHash(const Byte* data, SizeT size) override; 13 | Void ComputeHash(const Buffer& buffer) override; 14 | Void ComputeHash(const StringA& string) override; 15 | Void ComputeHash(const StringW& string) override; 16 | Void ComputeHash(const FileSystem::File& file, SizeT chunkSize = 16384) override; 17 | }; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /WinToolsLib/Cryptography/IHash.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Buffer.h" 4 | #include "..\String.h" 5 | #include "..\FileSystem\File.h" 6 | 7 | namespace WinToolsLib 8 | { 9 | namespace Cryptography 10 | { 11 | class IHash 12 | { 13 | public: 14 | virtual ~IHash() {} 15 | 16 | virtual Void ComputeHash(const Byte* data, SizeT size) = 0; 17 | virtual Void ComputeHash(const Buffer& buffer) = 0; 18 | virtual Void ComputeHash(const StringA& string) = 0; 19 | virtual Void ComputeHash(const StringW& string) = 0; 20 | virtual Void ComputeHash(const FileSystem::File& file, SizeT chunkSize = 0) = 0; 21 | 22 | virtual Void Update(const Byte* data, SizeT size) = 0; 23 | virtual Void Finalize() = 0; 24 | virtual Void Reset() = 0; 25 | 26 | virtual Buffer GetDigest() const = 0; 27 | }; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /WinToolsLib/Cryptography/Sha.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Buffer.h" 4 | #include "Hash.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Cryptography 9 | { 10 | class Sha1 : public Hash 11 | { 12 | public: 13 | Sha1(); 14 | 15 | Void Update(const Byte* data, SizeT size) override; 16 | Void Finalize() override; 17 | Void Reset() override; 18 | 19 | Buffer GetDigest() const override; 20 | 21 | private: 22 | Void ProcessMessageBlock(); 23 | Void PadMessage(); 24 | 25 | private: 26 | UInt32 m_digest[5]; 27 | UInt32 m_lengthLow; 28 | UInt32 m_lengthHigh; 29 | 30 | Byte m_block[64]; 31 | UInt32 m_index; 32 | 33 | Bool m_isComputed; 34 | Bool m_isCorrupted; 35 | }; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /WinToolsLib/Cryptography/SymmetricCipher.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "SymmetricCipher.h" 3 | 4 | namespace WinToolsLib { namespace Cryptography 5 | { 6 | Void SymmetricCipher::Compute(const Byte* data, SizeT size) 7 | { 8 | Update(data, size); 9 | Finalize(); 10 | } 11 | 12 | Void SymmetricCipher::Compute(const Buffer& buffer) 13 | { 14 | Compute(buffer.GetBuffer(), buffer.GetSize()); 15 | } 16 | 17 | Void SymmetricCipher::Compute(const StringA& string) 18 | { 19 | Compute((PCByte)string.GetBuffer(), string.GetSize() * sizeof(Char)); 20 | } 21 | 22 | Void SymmetricCipher::Compute(const StringW& string) 23 | { 24 | Compute((PCByte)string.GetBuffer(), string.GetSize() * sizeof(WChar)); 25 | } 26 | 27 | Void SymmetricCipher::Compute(const FileSystem::File& file, SizeT chunkSize) 28 | { 29 | auto data = Buffer(chunkSize); 30 | auto bytes = file.Read(data); 31 | while (bytes > 0) 32 | { 33 | Update(data.GetBuffer(), bytes); 34 | bytes = file.Read(data); 35 | } 36 | Finalize(); 37 | } 38 | } } 39 | -------------------------------------------------------------------------------- /WinToolsLib/Cryptography/SymmetricCipher.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Buffer.h" 4 | #include "..\FileSystem\File.h" 5 | 6 | namespace WinToolsLib { namespace Cryptography 7 | { 8 | class SymmetricCipher 9 | { 10 | public: 11 | Void Compute(const Byte* data, SizeT size); 12 | Void Compute(const Buffer& buffer); 13 | Void Compute(const StringA& string); 14 | Void Compute(const StringW& string); 15 | Void Compute(const FileSystem::File& file, SizeT chunkSize = 16384); 16 | 17 | virtual Buffer GetOutput(); 18 | virtual Void Reset() = 0; 19 | 20 | protected: 21 | virtual Void Update(PCByte data, SizeT size) = 0; 22 | virtual Void Finalize() = 0; 23 | 24 | protected: 25 | Buffer m_out; 26 | }; 27 | 28 | inline Buffer SymmetricCipher::GetOutput() 29 | { 30 | return std::move(m_out); 31 | } 32 | } } 33 | -------------------------------------------------------------------------------- /WinToolsLib/Data/Json/json.hpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Daniel Parker 2 | // Distributed under the Boost license, Version 1.0. 3 | // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 4 | 5 | // See https://sourceforge.net/projects/jsoncons/files/ for latest version 6 | // See https://sourceforge.net/p/jsoncons/wiki/Home/ for documentation. 7 | 8 | #ifndef JSONCONS_JSON_HPP 9 | #define JSONCONS_JSON_HPP 10 | 11 | #include "json1.hpp" 12 | #include "json2.hpp" 13 | 14 | namespace jsoncons { 15 | 16 | typedef basic_json json; 17 | typedef basic_json wjson; 18 | 19 | } 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /WinToolsLib/Data/Json/json_exception.hpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Daniel Parker 2 | // Distributed under the Boost license, Version 1.0. 3 | // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 4 | 5 | // See https://sourceforge.net/projects/jsoncons/files/ for latest version 6 | // See https://sourceforge.net/p/jsoncons/wiki/Home/ for documentation. 7 | 8 | #ifndef JSONCONS_JSON_EXCEPTION_HPP 9 | #define JSONCONS_JSON_EXCEPTION_HPP 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include "json_char_traits.hpp" 19 | #include "jsoncons_config.hpp" 20 | 21 | namespace jsoncons { 22 | 23 | class json_exception : public std::exception 24 | { 25 | public: 26 | json_exception() 27 | { 28 | } 29 | }; 30 | 31 | class json_exception_0 : public json_exception 32 | { 33 | public: 34 | json_exception_0(std::string s) 35 | : message_(s) 36 | { 37 | } 38 | const char* what() const 39 | { 40 | return message_.c_str(); 41 | } 42 | private: 43 | std::string message_; 44 | }; 45 | 46 | template 47 | class json_exception_1 : public json_exception 48 | { 49 | public: 50 | json_exception_1(const std::string& format, const std::basic_string& arg1) 51 | : format_(format), arg1_(arg1) 52 | { 53 | } 54 | const char* what() const 55 | { 56 | c99_snprintf(const_cast(message_),255, format_.c_str(),arg1_.c_str()); 57 | return message_; 58 | } 59 | private: 60 | std::string format_; 61 | std::basic_string arg1_; 62 | char message_[255]; 63 | }; 64 | 65 | #define JSONCONS_THROW_EXCEPTION(x) throw jsoncons::json_exception_0((x)) 66 | #define JSONCONS_THROW_EXCEPTION_1(fmt,arg1) throw jsoncons::json_exception_1((fmt),(arg1)) 67 | #define JSONCONS_ASSERT(x) if (!(x)) {std::cerr << #x; abort();} 68 | 69 | } 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /WinToolsLib/Data/Json/json_input_handler.hpp: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Daniel Parker 2 | // Distributed under the Boost license, Version 1.0. 3 | // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 4 | 5 | // See https://sourceforge.net/projects/jsoncons/files/ for latest version 6 | // See https://sourceforge.net/p/jsoncons/wiki/Home/ for documentation. 7 | 8 | #ifndef JSONCONS_JSON_INPUT_HANDLER_HPP 9 | #define JSONCONS_JSON_INPUT_HANDLER_HPP 10 | 11 | #include 12 | #include "jsoncons_config.hpp" 13 | 14 | namespace jsoncons { 15 | 16 | template 17 | class basic_parsing_context; 18 | 19 | template 20 | class basic_json_input_handler 21 | { 22 | public: 23 | virtual void begin_json() = 0; 24 | 25 | virtual void end_json() = 0; 26 | 27 | virtual void begin_object(const basic_parsing_context& context) = 0; 28 | 29 | virtual void end_object(const basic_parsing_context& context) = 0; 30 | 31 | virtual void begin_array(const basic_parsing_context& context) = 0; 32 | 33 | virtual void end_array(const basic_parsing_context& context) = 0; 34 | 35 | virtual void name(const std::basic_string& name, const basic_parsing_context& context) = 0; 36 | 37 | virtual void value(const std::basic_string& value, const basic_parsing_context& context) = 0; 38 | 39 | virtual void value(double value, const basic_parsing_context& context) = 0; 40 | 41 | virtual void value(long long value, const basic_parsing_context& context) = 0; 42 | 43 | virtual void value(unsigned long long value, const basic_parsing_context& context) = 0; 44 | 45 | virtual void value(bool value, const basic_parsing_context& context) = 0; 46 | 47 | virtual void null_value(const basic_parsing_context& context) = 0; 48 | }; 49 | 50 | typedef basic_json_input_handler json_input_handler; 51 | 52 | } 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/Database.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | 4 | #include "Query.h" 5 | #include "Statement.h" 6 | 7 | namespace WinToolsLib 8 | { 9 | namespace Data 10 | { 11 | namespace Sqlite 12 | { 13 | namespace Details 14 | { 15 | #include "Details\sqlite3.h" 16 | } 17 | 18 | class Database 19 | { 20 | Database(const Database& other); // Non-copyable 21 | Database& operator=(const Database& other); // Non-copyable 22 | 23 | Database(Details::sqlite3* db); 24 | 25 | public: 26 | Database(); 27 | ~Database(); 28 | 29 | Database(Database&& other); 30 | Database& operator=(Database&& other); 31 | 32 | static Database Open(const WChar* path); 33 | Void Close(); 34 | 35 | StatementPtr CompileStatement(const WChar* sql); 36 | Void CompileStatement(const WChar* sql, Statement* statement); 37 | QueryPtr ExecuteQuery(const WChar* sql); 38 | SizeT ExecuteScalar(const WChar* sql); 39 | Void Execute(const WChar* sql); 40 | 41 | private: 42 | Details::sqlite3_stmt* Compile(const WChar* sql); 43 | 44 | private: 45 | Details::sqlite3* m_db; 46 | }; 47 | 48 | class DatabaseNotOpen : public WinToolsLib::Exception 49 | { 50 | public: 51 | DatabaseNotOpen(PCChar function, UInt32 line, PCChar file) : 52 | Exception(function, line, file, Text("Database not open")) 53 | { 54 | } 55 | }; 56 | 57 | class DatabaseNotEqual : public WinToolsLib::Exception 58 | { 59 | public: 60 | DatabaseNotEqual(PCChar function, UInt32 line, PCChar file) : 61 | Exception(function, line, file, Text("Database not equal")) 62 | { 63 | } 64 | }; 65 | } 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/IStatement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Serialization\ISerializer.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | namespace Data 7 | { 8 | namespace Sqlite 9 | { 10 | class IStatement : public Serialization::IStoreProperty 11 | { 12 | public: 13 | virtual ~IStatement() {} 14 | 15 | virtual Void Finalize() = 0; 16 | virtual Int64 Execute() = 0; 17 | 18 | virtual Void StoreNull(const TChar* name) = 0; 19 | 20 | virtual Int64 GetLastId() const = 0; 21 | virtual Int32 GetRowsModified() const = 0; 22 | }; 23 | 24 | typedef std::unique_ptr StatementPtr; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/SqliteException.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Exception.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Data 9 | { 10 | namespace Sqlite 11 | { 12 | class SqliteException : public ExceptionWithCode 13 | { 14 | public: 15 | SqliteException( 16 | PCChar function, 17 | UInt32 line, 18 | PCChar file, 19 | UInt32 errorCode, 20 | PCChar errorMessage = nullptr); 21 | 22 | protected: 23 | static String CreateErrorMessage(UInt32 errorCode, PCChar errorMessage); 24 | static const TChar* ErrorCodeToMessage(UInt32 errorCode); 25 | }; 26 | 27 | #define THROW_SQLITE_EXCEPTION(errorCode) \ 28 | throw SqliteException(__FUNCTION__, __LINE__, __FILE__, errorCode) 29 | 30 | #define THROW_SQLITE_EXCEPTION2(errorCode, errorMessage) \ 31 | throw SqliteException(__FUNCTION__, __LINE__, __FILE__, errorCode, errorMessage) 32 | 33 | class ColumnNotFoundException : public WinToolsLib::Exception 34 | { 35 | public: 36 | ColumnNotFoundException( 37 | PCChar function, 38 | UInt32 line, 39 | PCChar file, 40 | PCWChar name); 41 | 42 | protected: 43 | static String CreateErrorMessage(PCWChar name); 44 | }; 45 | 46 | #define THROW_COLUMN_NOT_FOUND_EXCEPTION(name) \ 47 | throw ColumnNotFoundException(__FUNCTION__, __LINE__, __FILE__, name) 48 | } 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/StatementDecorators/ExtendableInsert.cpp: -------------------------------------------------------------------------------- 1 | #include "ExtendableInsert.h" 2 | 3 | #include "..\Database.h" 4 | 5 | namespace WinToolsLib { namespace Data { namespace Sqlite { namespace StatementDecorators 6 | { 7 | using namespace Details; 8 | 9 | ExtendableInsert::ExtendableInsert(Database* db, StatementPtr&& statement, const TChar* sql) : 10 | ExtendableBase(db, std::move(statement), sql) 11 | { 12 | } 13 | 14 | ExtendableInsert::~ExtendableInsert() 15 | { 16 | } 17 | 18 | ExtendableInsert::ExtendableInsert(ExtendableInsert&& other) : 19 | ExtendableBase(std::move(other)) 20 | { 21 | } 22 | 23 | ExtendableInsert& ExtendableInsert::operator=(ExtendableInsert&& other) 24 | { 25 | MoveFrom(other); 26 | 27 | return *this; 28 | } 29 | 30 | Void ExtendableInsert::ModifySql(const TChar* name, const TChar* type) 31 | { 32 | auto field = SearchByRegExp(Text("\\[\\w{1,100}\\]\\s{0,}\\)")); 33 | ReplaceInSql(field, Text("%s, [%s]"), name); 34 | 35 | auto value = SearchByRegExp(Text("VALUES\\s{0,}\\(.*\\)")); 36 | ReplaceInSql(value, Text("%s, @%s"), name); 37 | 38 | auto table = SearchByRegExp(Text("\\[\\w{1,100}\\]")); 39 | String tableName(table.GetBuffer() + 1, table.GetSize() - 2); 40 | StringW sql = StringW::FormatS(L"ALTER TABLE [%s] ADD [%s] %s;", tableName.GetBuffer(), name, type); 41 | try 42 | { 43 | m_db->Execute(sql); 44 | } 45 | catch (SqliteException& ex) 46 | { 47 | if (ex.GetErrorCode() != SQLITE_ERROR) 48 | throw; 49 | } 50 | } 51 | } } } } -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/StatementDecorators/ExtendableInsert.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ExtendableBase.h" 3 | 4 | namespace WinToolsLib { namespace Data { namespace Sqlite { namespace StatementDecorators 5 | { 6 | using namespace Serialization; 7 | 8 | class ExtendableInsert : public ExtendableBase 9 | { 10 | ExtendableInsert(const ExtendableInsert& other); // Non-copyable 11 | ExtendableInsert& operator=(const ExtendableInsert& other); // Non-copyable 12 | 13 | public: 14 | ExtendableInsert(Database* db, StatementPtr&& statement, const TChar* sql); 15 | ~ExtendableInsert(); 16 | 17 | ExtendableInsert(ExtendableInsert&& other); 18 | ExtendableInsert& operator=(ExtendableInsert&& other); 19 | 20 | protected: 21 | Void ModifySql(const TChar* name, const TChar* type) override; 22 | 23 | }; 24 | } } } } 25 | 26 | -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/StatementDecorators/ExtendableUpdate.cpp: -------------------------------------------------------------------------------- 1 | #include "ExtendableUpdate.h" 2 | 3 | #include "..\Database.h" 4 | 5 | namespace WinToolsLib { namespace Data { namespace Sqlite { namespace StatementDecorators 6 | { 7 | using namespace Details; 8 | 9 | ExtendableUpdate::ExtendableUpdate(Database* db, StatementPtr&& statement, const TChar* sql) : 10 | ExtendableBase(db, std::move(statement), sql) 11 | { 12 | } 13 | 14 | ExtendableUpdate::~ExtendableUpdate() 15 | { 16 | } 17 | 18 | ExtendableUpdate::ExtendableUpdate(ExtendableUpdate&& other) : 19 | ExtendableBase(std::move(other)) 20 | { 21 | } 22 | 23 | ExtendableUpdate& ExtendableUpdate::operator=(ExtendableUpdate&& other) 24 | { 25 | MoveFrom(other); 26 | 27 | return *this; 28 | } 29 | 30 | Void ExtendableUpdate::ModifySql(const TChar* name, const TChar* type) 31 | { 32 | auto fieldValue = String::FormatS(Text("[%s] = @%s, "), name, name); 33 | auto set = SearchByRegExp(Text("SET\\s{1,}")); 34 | ReplaceInSql(set, Text("%s %s"), fieldValue); 35 | } 36 | } } } } -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/StatementDecorators/ExtendableUpdate.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "ExtendableBase.h" 3 | 4 | namespace WinToolsLib { namespace Data { namespace Sqlite { namespace StatementDecorators 5 | { 6 | using namespace Serialization; 7 | 8 | class ExtendableUpdate : public ExtendableBase 9 | { 10 | ExtendableUpdate(const ExtendableUpdate& other); // Non-copyable 11 | ExtendableUpdate& operator=(const ExtendableUpdate& other); // Non-copyable 12 | 13 | public: 14 | ExtendableUpdate(Database* db, StatementPtr&& statement, const TChar* sql); 15 | ~ExtendableUpdate(); 16 | 17 | ExtendableUpdate(ExtendableUpdate&& other); 18 | ExtendableUpdate& operator=(ExtendableUpdate&& other); 19 | 20 | protected: 21 | Void ModifySql(const TChar* name, const TChar* type) override; 22 | 23 | }; 24 | } } } } 25 | 26 | -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/StatementDecorators/TemplateExtendableStatement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\..\Types.h" 3 | #include "..\Statement.h" 4 | 5 | namespace WinToolsLib { namespace Data { namespace Sqlite { namespace StatementDecorators 6 | { 7 | template 8 | class TemplateExtendableStatement : public Statement 9 | { 10 | public: 11 | TemplateExtendableStatement(Statement&& base); 12 | 13 | TemplateExtendableStatement(TemplateExtendableStatement&& other); 14 | TemplateExtendableStatement& operator=(TemplateExtendableStatement&& other); 15 | 16 | virtual IStorePropertyPtr StoreChild(const TChar* name) override; 17 | 18 | private: 19 | static const StringW m_sql; 20 | }; 21 | 22 | template 23 | TemplateExtendableStatement::TemplateExtendableStatement(Statement&& base) : 24 | Statement(std::move(base)) 25 | {} 26 | 27 | template 28 | TemplateExtendableStatement::TemplateExtendableStatement(TemplateExtendableStatement&& other) : 29 | Statement(std::move(other)) 30 | {} 31 | 32 | template 33 | TemplateExtendableStatement& TemplateExtendableStatement::operator=(TemplateExtendableStatement&& other) 34 | { 35 | __super::operator=(std::move(other)); 36 | return *this; 37 | } 38 | 39 | template 40 | IStorePropertyPtr TemplateExtendableStatement::StoreChild(const TChar* name) 41 | { 42 | if (m_sql.IsEmpty()) 43 | return IStorePropertyPtr(); 44 | 45 | auto sql = String::FormatS(m_sql.GetBuffer(), name, m_lastId); 46 | 47 | auto statement = m_db->CompileStatement(sql); 48 | auto rawPtr = static_cast(statement.get()); 49 | auto statementPtr = StatementPtr(new T1(std::move(*rawPtr))); 50 | 51 | return IStorePropertyPtr(new T2(m_db, std::move(statementPtr), sql)); 52 | } 53 | 54 | #define BEGIN_TEMPLATE_EXTENDABLE_STATEMENT_SQL namespace WinToolsLib { namespace Data { namespace Sqlite { namespace StatementDecorators { 55 | #define END_TEMPLATE_EXTENDABLE_STATEMENT_SQL } } } } 56 | } } } } -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/TemplateStatement.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "Statement.h" 4 | 5 | namespace WinToolsLib { namespace Data { namespace Sqlite 6 | { 7 | template 8 | class TemplateStatement : public Statement 9 | { 10 | public: 11 | TemplateStatement(Statement&& base); 12 | 13 | TemplateStatement(TemplateStatement&& other); 14 | TemplateStatement& operator=(TemplateStatement&& other); 15 | 16 | virtual IStorePropertyPtr StoreChild(const TChar* name) override; 17 | 18 | private: 19 | static const StringW m_sql; 20 | }; 21 | 22 | template 23 | TemplateStatement::TemplateStatement(Statement&& base) : 24 | Statement(std::move(base)) 25 | {} 26 | 27 | template 28 | TemplateStatement::TemplateStatement(TemplateStatement&& other) : 29 | Statement(std::move(other)) 30 | {} 31 | 32 | template 33 | TemplateStatement& TemplateStatement::operator=(TemplateStatement&& other) 34 | { 35 | __super::operator=(std::move(other)); 36 | return *this; 37 | } 38 | 39 | template 40 | IStorePropertyPtr TemplateStatement::StoreChild(const TChar* name) 41 | { 42 | if (m_sql.IsEmpty()) 43 | return IStorePropertyPtr(); 44 | 45 | auto sql = String::FormatS(m_sql.GetBuffer(), name, m_lastId); 46 | 47 | auto statement = m_db->CompileStatement(sql); 48 | auto rawPtr = static_cast(statement.get()); 49 | return IStorePropertyPtr(new T(std::move(*rawPtr))); 50 | } 51 | 52 | #define DEFINE_STATEMENT_CLASS(classname) \ 53 | class classname : public Statement \ 54 | { \ 55 | public: \ 56 | classname(Statement&& base); \ 57 | classname(classname&& other); \ 58 | classname& operator=(classname&& other); \ 59 | }; \ 60 | inline classname::classname(Statement&& base) : \ 61 | Statement(std::move(base)) \ 62 | {} \ 63 | inline classname::classname(classname&& other) : \ 64 | Statement(std::move(other)) \ 65 | {} \ 66 | inline classname& classname::operator=(classname&& other) \ 67 | { \ 68 | MoveFrom(other); \ 69 | return *this; \ 70 | } 71 | 72 | #define BEGIN_TEMPLATE_STATEMENT_SQL namespace WinToolsLib { namespace Data { namespace Sqlite { 73 | #define END_TEMPLATE_STATEMENT_SQL } } } 74 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/Transaction.cpp: -------------------------------------------------------------------------------- 1 | #include "Transaction.h" 2 | 3 | namespace WinToolsLib { namespace Data { namespace Sqlite 4 | { 5 | using namespace Details; 6 | 7 | Transaction::Transaction(Database* db) : 8 | m_db(db), 9 | m_isFinished(False) 10 | { 11 | m_db->Execute(L"BEGIN;"); 12 | } 13 | 14 | Transaction::~Transaction() 15 | { 16 | Rollback(); 17 | } 18 | 19 | Transaction::Transaction(Transaction&& other) 20 | { 21 | MoveFrom(other); 22 | } 23 | 24 | Transaction& Transaction::operator=(Transaction&& other) 25 | { 26 | MoveFrom(other); 27 | return *this; 28 | } 29 | 30 | Void Transaction::Commit() 31 | { 32 | if (!m_isFinished) 33 | { 34 | m_db->Execute(L"COMMIT;"); 35 | m_isFinished = True; 36 | } 37 | } 38 | 39 | Void Transaction::Rollback() 40 | { 41 | if (!m_isFinished) 42 | { 43 | m_db->Execute(L"ROLLBACK;"); 44 | m_isFinished = True; 45 | } 46 | } 47 | 48 | Void Transaction::MoveFrom(Transaction& other) 49 | { 50 | if (&other != this) 51 | { 52 | m_db = other.m_db; 53 | m_isFinished = std::move(other.m_isFinished); 54 | 55 | other.m_db = nullptr; 56 | other.m_isFinished = True; 57 | } 58 | } 59 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Data/Sqlite/Transaction.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "Database.h" 4 | 5 | namespace WinToolsLib { namespace Data { namespace Sqlite 6 | { 7 | class Transaction 8 | { 9 | public: 10 | Transaction(Database* db); 11 | ~Transaction(); 12 | 13 | Transaction(Transaction&& other); 14 | Transaction& operator=(Transaction&& other); 15 | 16 | Void Commit(); 17 | Void Rollback(); 18 | 19 | private: 20 | Void MoveFrom(Transaction& other); 21 | 22 | private: 23 | Database* m_db; 24 | Bool m_isFinished; 25 | }; 26 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Debug.cpp: -------------------------------------------------------------------------------- 1 | #include "Debug.h" 2 | #include "Exception.h" 3 | #include 4 | 5 | namespace WinToolsLib 6 | { 7 | Debug::Debug(UInt32 processId) : 8 | m_processId(processId) 9 | { 10 | BOOL success = ::DebugActiveProcess(m_processId); 11 | if (!success) 12 | { 13 | THROW_LAST_ERROR_EXCEPTION(); 14 | } 15 | } 16 | 17 | Debug::~Debug() 18 | { 19 | BOOL success = ::DebugActiveProcessStop(m_processId); 20 | if (!success) 21 | { 22 | THROW_LAST_ERROR_EXCEPTION(); 23 | } 24 | } 25 | 26 | Void Debug::KillProcessOnExit(Bool kill) 27 | { 28 | BOOL success = ::DebugSetProcessKillOnExit(kill ? TRUE : FALSE); 29 | if (!success) 30 | { 31 | THROW_LAST_ERROR_EXCEPTION(); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /WinToolsLib/Debug.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | class Debug 7 | { 8 | public: 9 | Debug(UInt32 processId); 10 | ~Debug(); 11 | 12 | static Void KillProcessOnExit(Bool kill); 13 | 14 | private: 15 | UInt32 m_processId; 16 | }; 17 | } -------------------------------------------------------------------------------- /WinToolsLib/DebugOutput.cpp: -------------------------------------------------------------------------------- 1 | #include "DebugOutput.h" 2 | #include "String.h" 3 | #include 4 | 5 | namespace WinToolsLib 6 | { 7 | Void DebugOutput(const TChar* format, ...) 8 | { 9 | va_list argList; 10 | va_start(argList, format); 11 | 12 | String buffer; 13 | buffer.FormatV(format, argList); 14 | 15 | if (!buffer.EndsWith(Text("\n"))) 16 | { 17 | buffer += Text("\n"); 18 | } 19 | ::OutputDebugString(buffer); 20 | } 21 | } -------------------------------------------------------------------------------- /WinToolsLib/DebugOutput.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | Void DebugOutput(const TChar* format, ...); 7 | } 8 | -------------------------------------------------------------------------------- /WinToolsLib/Disasm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "IStream.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace Details 8 | { 9 | #include "libudis86\extern.h" 10 | } 11 | 12 | class Disasm 13 | { 14 | public: 15 | Disasm(IStream* stream, UInt8 bits); 16 | 17 | Bool IsHook(UInt64 va, SizeT size); 18 | Bool IsTrampoline(UInt64 va, SizeT size); 19 | 20 | UInt64 GetHookDestination() const; 21 | SizeT GetHookSize() const; 22 | 23 | private: 24 | static Int32 InputHook(Details::ud_t* ud); 25 | 26 | static Int64 GetOperandValue(Details::ud_t* ud, SizeT i); 27 | static Int64 GetOffsetValue(Details::ud_t* ud, SizeT i); 28 | 29 | static Bool ReadMemoryOperand(Details::ud_t* ud, UInt64* value); 30 | static Void Skip(Details::ud_t* ud, SizeT bytes); 31 | 32 | private: 33 | Details::ud_t m_ud; 34 | UInt64 m_hookDestination; 35 | SizeT m_hookSize; 36 | }; 37 | 38 | inline UInt64 Disasm::GetHookDestination() const 39 | { 40 | return m_hookDestination; 41 | } 42 | 43 | inline SizeT Disasm::GetHookSize() const 44 | { 45 | return m_hookSize; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /WinToolsLib/DynamicFunction.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | template 7 | Fn DynamicFunction(const Char* module, const Char* function) 8 | { 9 | return (Fn)::GetProcAddress(::GetModuleHandleA(module), function); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /WinToolsLib/Environment.cpp: -------------------------------------------------------------------------------- 1 | #include "Environment.h" 2 | #include "Exception.h" 3 | #include "Buffer.h" 4 | #include 5 | #include 6 | #pragma comment(lib, "Userenv.lib") 7 | 8 | namespace WinToolsLib 9 | { 10 | String Environment::ExpandString(const TChar* pszString, const Handle& hToken) 11 | { 12 | UInt32 nAttempts = 6; 13 | UInt32 sizeInChars = 512; 14 | 15 | // Lets start from 512 chars buffer and increase it up to 32768 chars if necessary. 16 | while (nAttempts > 0) 17 | { 18 | UInt32 sizeInBytes = sizeInChars * sizeof(TChar); 19 | Buffer buffer(sizeInBytes); 20 | 21 | auto success = ::ExpandEnvironmentStringsForUser(hToken, pszString, (PTChar)buffer.GetBuffer(), sizeInChars - 1); 22 | if (success) 23 | { 24 | // TODO: Use String::GetBuffer(sizeInChars) when it is ready. 25 | return String((PTChar)buffer.GetBuffer()); 26 | } 27 | 28 | auto lastError = ::GetLastError(); 29 | if (lastError != ERROR_INSUFFICIENT_BUFFER) 30 | { 31 | THROW_WIN32_EXCEPTION(lastError); 32 | } 33 | 34 | sizeInChars *= 2; 35 | nAttempts--; 36 | } 37 | 38 | // We failed to expand string, lets just return the original one. 39 | return String(pszString); 40 | } 41 | 42 | String Environment::GetVariable(const TChar* name) 43 | { 44 | UInt32 nAttempts = 6; 45 | UInt32 sizeInChars = 512; 46 | 47 | // Lets start from 512 chars buffer and increase it up to 32768 chars if necessary. 48 | while (nAttempts > 0) 49 | { 50 | UInt32 sizeInBytes = sizeInChars * sizeof(TChar); 51 | Buffer buffer(sizeInBytes); 52 | 53 | auto nChars = ::GetEnvironmentVariable(name, (PTChar)buffer.GetBuffer(), sizeInChars - 1); 54 | if (nChars > 0) 55 | { 56 | // TODO: Use String::GetBuffer(sizeInChars) when it is ready. 57 | return String((PTChar)buffer.GetBuffer()); 58 | } 59 | 60 | auto lastError = ::GetLastError(); 61 | if (lastError != ERROR_INSUFFICIENT_BUFFER) 62 | { 63 | THROW_WIN32_EXCEPTION(lastError); 64 | } 65 | 66 | sizeInChars *= 2; 67 | nAttempts--; 68 | } 69 | 70 | // We failed to expand string, lets just return the original one. 71 | return String(name); 72 | } 73 | } -------------------------------------------------------------------------------- /WinToolsLib/Environment.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "String.h" 4 | #include "Handles\Handle.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | using namespace Handles; 9 | 10 | class Environment 11 | { 12 | public: 13 | static String ExpandString(const TChar* pszString, const Handle& hToken = Handle()); 14 | static String GetVariable(const TChar* name); 15 | }; 16 | } 17 | -------------------------------------------------------------------------------- /WinToolsLib/ExecutableChunk.cpp: -------------------------------------------------------------------------------- 1 | #include "ExecutableChunk.h" 2 | 3 | namespace WinToolsLib 4 | { 5 | ExecutableChunk::ExecutableChunk(SizeT begin, SizeT end, PCChar name, UInt32 rva) : 6 | ChunkBase(begin, end), 7 | m_name(name), 8 | m_rva(rva) 9 | { 10 | m_size = (UInt32)(m_end - m_begin); 11 | } 12 | 13 | ExecutableChunk::ExecutableChunk(const ExecutableChunk& other) : 14 | ChunkBase(other), 15 | m_name(other.m_name), 16 | m_rva(other.m_rva), 17 | m_size(other.m_size) 18 | { 19 | } 20 | 21 | PCChar ExecutableChunk::GetName() const 22 | { 23 | return m_name; 24 | } 25 | 26 | UInt32 ExecutableChunk::GetRva() const 27 | { 28 | return m_rva; 29 | } 30 | 31 | UInt32 ExecutableChunk::GetSize() const 32 | { 33 | return m_size; 34 | } 35 | 36 | Void ExecutableChunk::SetBegin(SizeT begin) 37 | { 38 | auto offset = (UInt32)(begin - m_begin); 39 | m_rva += offset; 40 | ChunkBase::SetBegin(begin); 41 | m_size = (UInt32)(m_end - m_begin); 42 | } 43 | 44 | Void ExecutableChunk::SetEnd(SizeT end) 45 | { 46 | ChunkBase::SetEnd(end); 47 | m_size = (UInt32)(m_end - m_begin); 48 | } 49 | 50 | Bool ExecutableChunk::IsSameType(const ChunkBase& other) const 51 | { 52 | const ExecutableChunk& otherChunk = static_cast(other); 53 | if (strcmp(m_name, otherChunk.m_name) == 0) 54 | { 55 | return True; 56 | } 57 | 58 | return False; 59 | } 60 | } -------------------------------------------------------------------------------- /WinToolsLib/ExecutableChunk.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "ChunkCollection.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | class ExecutableChunk : protected ChunkBase 8 | { 9 | public: 10 | ExecutableChunk(SizeT begin, SizeT end, PCChar name, UInt32 rva); 11 | ExecutableChunk(const ExecutableChunk& other); 12 | 13 | using ChunkBase::operator<; 14 | 15 | using ChunkBase::GetBegin; 16 | using ChunkBase::GetEnd; 17 | 18 | PCChar GetName() const; 19 | UInt32 GetRva() const; 20 | UInt32 GetSize() const; 21 | 22 | protected: 23 | Void SetBegin(SizeT begin) override; 24 | Void SetEnd(SizeT end) override; 25 | 26 | Bool IsSameType(const ChunkBase& other) const override; 27 | 28 | private: 29 | PCChar m_name; 30 | UInt32 m_rva; 31 | UInt32 m_size; 32 | 33 | friend class ChunkCollection; 34 | }; 35 | 36 | typedef ChunkCollection ExecutableChunks; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /WinToolsLib/FileCacheStream.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "FileSystem\File.h" 4 | #include "String.h" 5 | #include "IStream.h" 6 | #include "Buffer.h" 7 | #include 8 | #include 9 | 10 | namespace WinToolsLib 11 | { 12 | using namespace FileSystem; 13 | 14 | class FileCacheStream : public IStream 15 | { 16 | public: 17 | FileCacheStream(const String& path, UInt32 chunkSize); 18 | 19 | SizeT GetPosition() const override; 20 | Void SetPosition(SizeT position) override; 21 | 22 | Void Reset() override; 23 | Void Skip(SizeT bytes) override; 24 | 25 | Void Read(PByte buffer, SizeT bytes) override; 26 | Void Write(PByte buffer, SizeT bytes) override; 27 | 28 | IStream& operator>>(Int8& value) override; 29 | IStream& operator>>(Int16& value) override; 30 | IStream& operator>>(Int32& value) override; 31 | IStream& operator>>(Int64& value) override; 32 | 33 | IStream& operator>>(UInt8& value) override; 34 | IStream& operator>>(UInt16& value) override; 35 | IStream& operator>>(UInt32& value) override; 36 | IStream& operator>>(UInt64& value) override; 37 | 38 | IStream& operator<<(Int8 value) override; 39 | IStream& operator<<(Int16 value) override; 40 | IStream& operator<<(Int32 value) override; 41 | IStream& operator<<(Int64 value) override; 42 | 43 | IStream& operator<<(UInt8 value) override; 44 | IStream& operator<<(UInt16 value) override; 45 | IStream& operator<<(UInt32 value) override; 46 | IStream& operator<<(UInt64 value) override; 47 | 48 | protected: 49 | SizeT GetAccessible(); 50 | 51 | private: 52 | typedef std::unique_ptr BufferPtr; 53 | typedef std::vector Chunks; 54 | 55 | File m_file; 56 | UInt64 m_fileSize; 57 | UInt32 m_chunkSize; 58 | SizeT m_position; 59 | SizeT m_chunkBegin; 60 | SizeT m_chunkEnd; 61 | SizeT m_currentChunk; 62 | Chunks m_chunks; 63 | 64 | }; 65 | } 66 | -------------------------------------------------------------------------------- /WinToolsLib/FileSystem/Folder.cpp: -------------------------------------------------------------------------------- 1 | #include "Folder.h" 2 | #include "Path.h" 3 | 4 | #include "..\Handles\FindHandle.h" 5 | 6 | #include 7 | 8 | namespace WinToolsLib 9 | { 10 | namespace FileSystem 11 | { 12 | using namespace Handles; 13 | 14 | Void Folder::EnumFiles( 15 | const String& path, 16 | EnumCallback fileEnumCallback, 17 | EnumCallback folderEnumCallback) 18 | { 19 | Path searchDir(path); 20 | searchDir.Append(Text("*")); 21 | 22 | WIN32_FIND_DATA data; 23 | FindHandle handle = ::FindFirstFile(searchDir.ToString(), &data); 24 | if (!handle.IsValid()) 25 | { 26 | return; 27 | } 28 | 29 | do 30 | { 31 | if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 32 | { 33 | if (_tcscmp(data.cFileName, Text(".")) != 0 && 34 | _tcscmp(data.cFileName, Text("..")) != 0) 35 | { 36 | folderEnumCallback(&data); 37 | } 38 | } 39 | else 40 | { 41 | fileEnumCallback(&data); 42 | } 43 | 44 | } while(::FindNextFile(handle, &data)); 45 | } 46 | 47 | Bool Folder::Create(const TChar* path) 48 | { 49 | auto success = ::CreateDirectory(path, NULL); 50 | if (!success) 51 | { 52 | auto error = ::GetLastError(); 53 | if (ERROR_ALREADY_EXISTS != error) 54 | { 55 | return False; 56 | } 57 | } 58 | return True; 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /WinToolsLib/FileSystem/Folder.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | #include 5 | #include 6 | 7 | namespace WinToolsLib 8 | { 9 | namespace FileSystem 10 | { 11 | class Folder 12 | { 13 | public: 14 | typedef std::function EnumCallback; 15 | 16 | static Void EnumFiles( 17 | const String& path, 18 | EnumCallback fileEnumCallback, 19 | EnumCallback folderEnumCallback); 20 | 21 | static Bool Create(const TChar* path); 22 | }; 23 | } 24 | } -------------------------------------------------------------------------------- /WinToolsLib/FileSystem/Path.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace FileSystem 8 | { 9 | class Path 10 | { 11 | public: 12 | Path(); 13 | Path(String&& path); 14 | Path(const String& path); 15 | Path(Path&& other); 16 | Path(const Path& other); 17 | 18 | Path& operator=(Path&& other); 19 | Path& operator=(const Path& other); 20 | 21 | Path& operator+=(const Path& other); 22 | Path operator+(const Path& other); 23 | 24 | operator const String&() const; 25 | const String& ToString() const; 26 | 27 | Void Append(const String& path); 28 | Void Append(const Path& other); 29 | 30 | Void ReplaceName(const String& other); 31 | Void ReplaceFullName(const String& other); 32 | Void ReplaceExtension(const String& other); 33 | Void ReplaceDirectory(const String& other); 34 | 35 | String GetName() const; 36 | String GetNameWithoutExtension() const; 37 | String GetFolder() const; 38 | 39 | static Path UrlToWin32(const TChar* url); 40 | static Path GetFileName(String cmdLine); 41 | static Path NtToWin32(const TChar* ntPath, Bool kernelRules); 42 | static Path SearchFullPath(const TChar* fileName); 43 | static Path GetFullPath(const TChar* path); 44 | 45 | protected: 46 | Void MoveFrom(Path& other); 47 | Void CopyFrom(const Path& other); 48 | 49 | private: 50 | String m_path; 51 | }; 52 | } 53 | } -------------------------------------------------------------------------------- /WinToolsLib/FileSystem/Shortcut.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\String.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | namespace FileSystem 7 | { 8 | class Shortcut 9 | { 10 | public: 11 | static StringW Resolve(const TChar* shortcutPath); 12 | static Bool IsShortcut(const String& fileName); 13 | static Void Create(const WChar* shortcutPath, const WChar* targetPath); 14 | }; 15 | } 16 | } -------------------------------------------------------------------------------- /WinToolsLib/Gui/Credentials.cpp: -------------------------------------------------------------------------------- 1 | #include "Credentials.h" 2 | #include "..\Exception.h" 3 | 4 | #include 5 | #pragma comment(lib, "Credui.lib") 6 | 7 | namespace WinToolsLib { namespace Gui 8 | { 9 | Credentials::Credentials( 10 | const TChar* userName, 11 | const TChar* password) : 12 | m_userName(userName ? userName : Text("")), 13 | m_password(password ? password : Text("")) 14 | { 15 | } 16 | 17 | Credentials::Credentials(Credentials&& other) 18 | { 19 | MoveFrom(other); 20 | } 21 | 22 | Credentials::Credentials(const Credentials& other) 23 | { 24 | CopyFrom(other); 25 | } 26 | 27 | Credentials::~Credentials() 28 | { 29 | ::SecureZeroMemory(m_password.GetBuffer(), m_password.GetLength() * sizeof(TChar)); 30 | } 31 | 32 | Credentials& Credentials::operator=(Credentials&& other) 33 | { 34 | MoveFrom(other); 35 | return *this; 36 | } 37 | 38 | Credentials& Credentials::operator=(const Credentials& other) 39 | { 40 | CopyFrom(other); 41 | return *this; 42 | } 43 | 44 | Credentials Credentials::Prompt( 45 | HWND parent, 46 | const TChar* message, 47 | const TChar* caption, 48 | const TChar* target) 49 | { 50 | CREDUI_INFO info = {0}; 51 | info.cbSize = sizeof(info); 52 | info.hwndParent = parent; 53 | info.pszMessageText = message; 54 | info.pszCaptionText = caption; 55 | 56 | TChar userName[CREDUI_MAX_USERNAME_LENGTH + 1] = {0}; 57 | TChar password[CREDUI_MAX_PASSWORD_LENGTH + 1] = {0}; 58 | 59 | const auto error = ::CredUIPromptForCredentials( 60 | &info, 61 | target, 62 | nullptr, 63 | 0, 64 | userName, 65 | CREDUI_MAX_USERNAME_LENGTH, 66 | password, 67 | CREDUI_MAX_PASSWORD_LENGTH, 68 | nullptr, 69 | CREDUI_FLAGS_VALIDATE_USERNAME); 70 | 71 | if (NO_ERROR != error) 72 | { 73 | THROW_WIN32_EXCEPTION(error); 74 | } 75 | 76 | Credentials credentials(userName, password); 77 | ::SecureZeroMemory(password, sizeof(password)); 78 | return credentials; 79 | } 80 | 81 | Void Credentials::MoveFrom(Credentials& other) 82 | { 83 | if (&other != this) 84 | { 85 | m_userName = std::move(other.m_userName); 86 | m_password = std::move(other.m_password); 87 | } 88 | } 89 | 90 | Void Credentials::CopyFrom(const Credentials& other) 91 | { 92 | if (&other != this) 93 | { 94 | m_userName = other.m_userName; 95 | m_password = other.m_password; 96 | } 97 | } 98 | 99 | } } -------------------------------------------------------------------------------- /WinToolsLib/Gui/Credentials.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | #include 6 | 7 | namespace WinToolsLib { namespace Gui 8 | { 9 | class Credentials 10 | { 11 | public: 12 | Credentials( 13 | const TChar* userName, 14 | const TChar* password); 15 | 16 | Credentials(Credentials&& other); 17 | Credentials(const Credentials& other); 18 | 19 | ~Credentials(); 20 | 21 | Credentials& operator=(Credentials&& other); 22 | Credentials& operator=(const Credentials& other); 23 | 24 | const String& GetUserName() const; 25 | const String& GetPassword() const; 26 | 27 | static Credentials Prompt( 28 | HWND parent, 29 | const TChar* message, 30 | const TChar* caption, 31 | const TChar* target); 32 | 33 | protected: 34 | Void MoveFrom(Credentials& other); 35 | Void CopyFrom(const Credentials& other); 36 | 37 | private: 38 | String m_userName; 39 | String m_password; 40 | }; 41 | 42 | inline const String& Credentials::GetUserName() const 43 | { 44 | return m_userName; 45 | } 46 | 47 | inline const String& Credentials::GetPassword() const 48 | { 49 | return m_password; 50 | } 51 | 52 | } } 53 | 54 | -------------------------------------------------------------------------------- /WinToolsLib/Gui/FileDialog.cpp: -------------------------------------------------------------------------------- 1 | #include "FileDialog.h" 2 | 3 | #pragma comment(lib, "Comdlg32.lib") 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace Gui 8 | { 9 | FileDialog::FileDialog() 10 | { 11 | ZeroMemory(&m_ofn, sizeof(m_ofn)); 12 | ZeroMemory(m_path, sizeof(m_path)); 13 | 14 | m_ofn.lStructSize = sizeof(m_ofn); 15 | m_ofn.lpstrFile = m_path; 16 | m_ofn.nMaxFile = MAX_PATH; 17 | m_ofn.Flags = static_cast(FileDialogFlags::Default); 18 | } 19 | 20 | Bool FileDialog::ShowSaveDialog() 21 | { 22 | return TRUE == ::GetSaveFileName(&m_ofn); 23 | } 24 | 25 | Bool FileDialog::ShowOpenDialog() 26 | { 27 | return TRUE == ::GetOpenFileName(&m_ofn); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /WinToolsLib/Gui/FileDialog.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | namespace WinToolsLib 10 | { 11 | namespace Gui 12 | { 13 | enum class FileDialogFlags 14 | { 15 | Explorer = OFN_EXPLORER, 16 | FileMustExist = OFN_FILEMUSTEXIST, 17 | OverwritePromt = OFN_OVERWRITEPROMPT, 18 | Default = OFN_EXPLORER 19 | }; 20 | 21 | class FileDialog 22 | { 23 | public: 24 | FileDialog(); 25 | 26 | Void SetFlags(FileDialogFlags flags); 27 | Void SetOwner(HWND hWnd); 28 | 29 | Void SetFilter(const TChar* filter); 30 | Void SetTitle(const TChar* title); 31 | 32 | Void SetDefaultFileName(const TChar* fileName); 33 | Void SetDefaultExtension(const TChar* extension); 34 | 35 | Bool ShowSaveDialog(); 36 | Bool ShowOpenDialog(); 37 | 38 | PCWChar GetPath(); 39 | 40 | private: 41 | OPENFILENAME m_ofn; 42 | WChar m_path[MAX_PATH]; 43 | }; 44 | 45 | inline Void FileDialog::SetFlags(FileDialogFlags flags) 46 | { 47 | m_ofn.Flags = static_cast(flags); 48 | } 49 | 50 | inline Void FileDialog::SetOwner(HWND hWnd) 51 | { 52 | m_ofn.hwndOwner = hWnd; 53 | } 54 | 55 | inline Void FileDialog::SetFilter(const TChar* filter) 56 | { 57 | m_ofn.lpstrFilter = filter; 58 | } 59 | 60 | inline Void FileDialog::SetTitle(const TChar* title) 61 | { 62 | m_ofn.lpstrTitle = title; 63 | } 64 | 65 | inline Void FileDialog::SetDefaultFileName(const TChar* fileName) 66 | { 67 | _tcsncpy_s(m_path, fileName, MAX_PATH); 68 | } 69 | 70 | inline Void FileDialog::SetDefaultExtension(const TChar* extension) 71 | { 72 | m_ofn.lpstrDefExt = extension; 73 | } 74 | 75 | inline PCWChar FileDialog::GetPath() 76 | { 77 | return m_path; 78 | } 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /WinToolsLib/Gui/FolderDialog.cpp: -------------------------------------------------------------------------------- 1 | #include "FolderDialog.h" 2 | #include "..\Com\Initialize.h" 3 | #include "..\Com\TaskMem.h" 4 | 5 | #include 6 | #pragma comment(lib, "Shell32.lib") 7 | #pragma warning(push) 8 | #pragma warning(disable:4090) 9 | 10 | namespace WinToolsLib 11 | { 12 | namespace Gui 13 | { 14 | FolderDialog::FolderDialog(HWND hWnd) : 15 | m_hWnd(hWnd) 16 | { 17 | } 18 | 19 | Bool FolderDialog::SelectFolder() 20 | { 21 | m_path = Text(""); 22 | Com::Initialize init(Com::Model::ApartmentThreaded); 23 | 24 | BROWSEINFO bi = {0}; 25 | bi.hwndOwner = m_hWnd; 26 | bi.ulFlags = BIF_USENEWUI; 27 | 28 | Com::TaskMem pidl = ::SHBrowseForFolder(&bi); 29 | if (nullptr != pidl) 30 | { 31 | TCHAR path[MAX_PATH]; 32 | ::SHGetPathFromIDList(pidl, path); 33 | 34 | m_path = path; 35 | return True; 36 | } 37 | 38 | return False; 39 | } 40 | } 41 | } 42 | 43 | #pragma warning(pop) -------------------------------------------------------------------------------- /WinToolsLib/Gui/FolderDialog.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | #include 6 | 7 | namespace WinToolsLib 8 | { 9 | namespace Gui 10 | { 11 | class FolderDialog 12 | { 13 | public: 14 | FolderDialog(HWND hWnd); 15 | 16 | Bool SelectFolder(); 17 | const String& GetFolderPath(); 18 | 19 | private: 20 | HWND m_hWnd; 21 | String m_path; 22 | }; 23 | 24 | inline const String& FolderDialog::GetFolderPath() 25 | { 26 | return m_path; 27 | } 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /WinToolsLib/Gui/MessageBox.cpp: -------------------------------------------------------------------------------- 1 | #include "MessageBox.h" 2 | 3 | namespace WinToolsLib 4 | { 5 | namespace Gui 6 | { 7 | Void MessageBox::Show(HWND hWnd, const String& text, const String& caption, const UInt32 type) 8 | { 9 | auto style = ::GetWindowLongPtr(hWnd, GWL_STYLE); 10 | if ((style & WS_VISIBLE) == WS_VISIBLE) 11 | ::MessageBox(hWnd, text, caption, type); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /WinToolsLib/Gui/MessageBox.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | #include 6 | 7 | namespace WinToolsLib 8 | { 9 | namespace Gui 10 | { 11 | class MessageBox 12 | { 13 | public: 14 | static Void Show(HWND hWnd, const String& text, const String& caption, const UInt32 type); 15 | }; 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /WinToolsLib/Guid.cpp: -------------------------------------------------------------------------------- 1 | #include "Guid.h" 2 | #include 3 | 4 | #include "Exception.h" 5 | #pragma comment (lib, "Rpcrt4.lib") 6 | 7 | namespace WinToolsLib 8 | { 9 | const Guid Guid::Null; 10 | 11 | Guid::Guid() 12 | { 13 | ZeroMemory(&m_guid, sizeof(m_guid)); 14 | } 15 | 16 | Guid::Guid(const GUID& guid) 17 | { 18 | CopyMemory(&m_guid, &guid, sizeof(m_guid)); 19 | } 20 | 21 | Guid::Guid(const TChar* guid) 22 | { 23 | ZeroMemory(&m_guid, sizeof(m_guid)); 24 | 25 | auto error = ::UuidFromString((RPC_WSTR)guid, &m_guid); 26 | if (error != RPC_S_OK) 27 | { 28 | THROW_WIN32_EXCEPTION(error); 29 | } 30 | } 31 | 32 | Guid::Guid(Guid&& other) 33 | { 34 | MoveFrom(other); 35 | } 36 | 37 | Guid::Guid(const Guid& other) 38 | { 39 | CopyFrom(other); 40 | } 41 | 42 | Guid& Guid::operator=(Guid&& other) 43 | { 44 | MoveFrom(other); 45 | return *this; 46 | } 47 | 48 | Guid& Guid::operator=(const Guid& other) 49 | { 50 | CopyFrom(other); 51 | return *this; 52 | } 53 | 54 | String Guid::ToString() const 55 | { 56 | String string; 57 | string.Format( 58 | Text("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"), 59 | m_guid.Data1, 60 | m_guid.Data2, 61 | m_guid.Data3, 62 | m_guid.Data4[0], 63 | m_guid.Data4[1], 64 | m_guid.Data4[2], 65 | m_guid.Data4[3], 66 | m_guid.Data4[4], 67 | m_guid.Data4[5], 68 | m_guid.Data4[6], 69 | m_guid.Data4[7]); 70 | return string; 71 | } 72 | 73 | Guid Guid::Create() 74 | { 75 | Guid guid; 76 | ::CoCreateGuid(&guid.m_guid); 77 | return guid; 78 | } 79 | 80 | Void Guid::MoveFrom(Guid& other) 81 | { 82 | if (this != &other) 83 | { 84 | CopyMemory(&m_guid, &other.m_guid, sizeof(m_guid)); 85 | ZeroMemory(&other.m_guid, sizeof(m_guid)); 86 | } 87 | } 88 | 89 | Void Guid::CopyFrom(const Guid& other) 90 | { 91 | if (this != &other) 92 | { 93 | CopyMemory(&m_guid, &other.m_guid, sizeof(m_guid)); 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /WinToolsLib/Guid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "String.h" 4 | #include 5 | 6 | namespace WinToolsLib 7 | { 8 | class Guid 9 | { 10 | public: 11 | Guid(); 12 | Guid(const GUID& guid); 13 | Guid(const TChar* guid); 14 | 15 | Guid(Guid&& other); 16 | Guid(const Guid& other); 17 | 18 | Guid& operator=(Guid&& other); 19 | Guid& operator=(const Guid& other); 20 | 21 | Bool operator<(const Guid& other) const; 22 | Bool operator>(const Guid& other) const; 23 | 24 | Bool operator<=(const Guid& other) const; 25 | Bool operator>=(const Guid& other) const; 26 | 27 | Bool operator==(const Guid& other) const; 28 | Bool operator!=(const Guid& other) const; 29 | 30 | Bool IsNull() const; 31 | String ToString() const; 32 | 33 | operator const GUID&() const; 34 | 35 | static Guid Create(); 36 | 37 | public: 38 | static const Guid Null; 39 | 40 | protected: 41 | Void MoveFrom(Guid& other); 42 | Void CopyFrom(const Guid& other); 43 | 44 | private: 45 | GUID m_guid; 46 | }; 47 | 48 | inline Bool Guid::operator<(const Guid& other) const 49 | { 50 | return 0 < memcmp(&m_guid, &other.m_guid, sizeof(m_guid)); 51 | } 52 | 53 | inline Bool Guid::operator>(const Guid& other) const 54 | { 55 | return !operator<=(other); 56 | } 57 | 58 | inline Bool Guid::operator<=(const Guid& other) const 59 | { 60 | return 0 <= memcmp(&m_guid, &other.m_guid, sizeof(m_guid)); 61 | } 62 | 63 | inline Bool Guid::operator>=(const Guid& other) const 64 | { 65 | return !operator<(other); 66 | } 67 | 68 | inline Bool Guid::operator==(const Guid& other) const 69 | { 70 | return 0 == memcmp(&m_guid, &other.m_guid, sizeof(m_guid)); 71 | } 72 | 73 | inline Bool Guid::operator!=(const Guid& other) const 74 | { 75 | return !operator==(other); 76 | } 77 | 78 | inline Bool Guid::IsNull() const 79 | { 80 | static const Guid null; 81 | return 0 == memcmp(&null.m_guid, &m_guid, sizeof(m_guid)); 82 | } 83 | 84 | inline Guid::operator const GUID&() const 85 | { 86 | return m_guid; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /WinToolsLib/Handles/FindHandle.cpp: -------------------------------------------------------------------------------- 1 | #include "FindHandle.h" 2 | 3 | namespace WinToolsLib 4 | { 5 | namespace Handles 6 | { 7 | FindHandle::FindHandle() : 8 | Base(FindHandle::Finalizer) 9 | { 10 | } 11 | 12 | FindHandle::FindHandle(HANDLE handle) : 13 | Base(FindHandle::Finalizer, handle) 14 | { 15 | } 16 | 17 | FindHandle::FindHandle(FindHandle&& other) : 18 | Base(FindHandle::Finalizer) 19 | { 20 | MoveFrom(std::move(other)); 21 | } 22 | 23 | Bool FindHandle::IsValid() const 24 | { 25 | if (IsNull()) 26 | return False; 27 | 28 | if (INVALID_HANDLE_VALUE == m_handle) 29 | return False; 30 | 31 | return True; 32 | } 33 | 34 | FindHandle& FindHandle::operator=(FindHandle&& other) 35 | { 36 | MoveFrom(std::move(other)); 37 | return *this; 38 | } 39 | 40 | Void FindHandle::MoveFrom(FindHandle&& other) 41 | { 42 | if (this != &other) 43 | { 44 | Reset(other.m_handle); 45 | other.m_handle = NULL; 46 | } 47 | } 48 | 49 | Void FindHandle::Finalizer(HANDLE handle) 50 | { 51 | ::FindClose(handle); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/FindHandle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "HandleBase.h" 4 | #include 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Handles 9 | { 10 | class FindHandle : public HandleBase 11 | { 12 | typedef HandleBase Base; 13 | 14 | public: 15 | FindHandle(); 16 | FindHandle(HANDLE handle); 17 | FindHandle(FindHandle&& other); 18 | 19 | Bool IsValid() const; 20 | 21 | FindHandle& operator=(FindHandle&& other); 22 | 23 | protected: 24 | Void MoveFrom(FindHandle&& other); 25 | Void CopyFrom(const FindHandle& other); 26 | 27 | static Void Finalizer(HANDLE handle); 28 | }; 29 | } 30 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/Handle.cpp: -------------------------------------------------------------------------------- 1 | #include "Handle.h" 2 | #include "..\Exception.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | namespace Handles 7 | { 8 | Handle::Handle() : 9 | Base(Handle::Finalizer) 10 | { 11 | } 12 | 13 | Handle::Handle(HANDLE handle) : 14 | Base(Handle::Finalizer, handle) 15 | { 16 | } 17 | 18 | Handle::Handle(Handle&& other) : 19 | Base(Handle::Finalizer) 20 | { 21 | MoveFrom(std::move(other)); 22 | } 23 | 24 | Handle::Handle(const Handle& other) : 25 | Base(Handle::Finalizer) 26 | { 27 | CopyFrom(other); 28 | } 29 | 30 | Bool Handle::IsValid() const 31 | { 32 | if (IsNull()) 33 | return False; 34 | 35 | if (INVALID_HANDLE_VALUE == m_handle) 36 | return False; 37 | 38 | return True; 39 | } 40 | 41 | Void Handle::Duplicate(HANDLE handle) 42 | { 43 | Close(); 44 | BOOL success = ::DuplicateHandle( 45 | ::GetCurrentProcess(), 46 | handle, 47 | ::GetCurrentProcess(), 48 | &m_handle, 49 | 0, 50 | FALSE, 51 | DUPLICATE_SAME_ACCESS); 52 | 53 | if (!success) 54 | { 55 | THROW_LAST_ERROR_EXCEPTION(); 56 | } 57 | } 58 | 59 | Handle& Handle::operator=(Handle&& other) 60 | { 61 | MoveFrom(std::move(other)); 62 | return *this; 63 | } 64 | 65 | Handle& Handle::operator=(const Handle& other) 66 | { 67 | CopyFrom(other); 68 | return *this; 69 | } 70 | 71 | Void Handle::MoveFrom(Handle&& other) 72 | { 73 | if (this != &other) 74 | { 75 | Reset(other.m_handle); 76 | other.m_handle = NULL; 77 | } 78 | } 79 | 80 | Void Handle::CopyFrom(const Handle& other) 81 | { 82 | if (this != &other) 83 | { 84 | if (other.IsValid()) 85 | { 86 | Duplicate(other.m_handle); 87 | } 88 | } 89 | } 90 | 91 | Void Handle::Finalizer(HANDLE handle) 92 | { 93 | ::CloseHandle(handle); 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/Handle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "HandleBase.h" 4 | #include 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Handles 9 | { 10 | class Handle : public HandleBase 11 | { 12 | typedef HandleBase Base; 13 | 14 | public: 15 | Handle(); 16 | Handle(HANDLE handle); 17 | Handle(Handle&& other); 18 | Handle(const Handle& other); 19 | 20 | Bool IsValid() const; 21 | Void Duplicate(HANDLE handle); 22 | 23 | Handle& operator=(Handle&& other); 24 | Handle& operator=(const Handle& other); 25 | 26 | protected: 27 | Void MoveFrom(Handle&& other); 28 | Void CopyFrom(const Handle& other); 29 | 30 | static Void Finalizer(HANDLE handle); 31 | }; 32 | } 33 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/HandleBase.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace Handles 8 | { 9 | template 10 | class HandleBase 11 | { 12 | public: 13 | typedef std::function Finalizer; 14 | 15 | HandleBase(Finalizer finalizer); 16 | HandleBase(Finalizer finalizer, HandleType handle); 17 | ~HandleBase(); 18 | 19 | Bool IsNull() const; 20 | 21 | Void Close(); 22 | Void Reset(HandleType handle); 23 | 24 | HandleType Detach(); 25 | operator HandleType() const; 26 | 27 | protected: 28 | Finalizer m_finalizer; 29 | HandleType m_handle; 30 | }; 31 | 32 | template 33 | HandleBase::HandleBase(Finalizer finalizer) : 34 | m_finalizer(finalizer), 35 | m_handle(NULL) 36 | { 37 | } 38 | 39 | template 40 | HandleBase::HandleBase(Finalizer finalizer, HandleType handle) : 41 | m_finalizer(finalizer), 42 | m_handle(handle) 43 | { 44 | } 45 | 46 | template 47 | HandleBase::~HandleBase() 48 | { 49 | Close(); 50 | } 51 | 52 | template 53 | Bool HandleBase::IsNull() const 54 | { 55 | if (NULL == m_handle) 56 | return True; 57 | 58 | return False; 59 | } 60 | 61 | template 62 | Void HandleBase::Close() 63 | { 64 | if (!IsNull()) 65 | { 66 | m_finalizer(m_handle); 67 | m_handle = NULL; 68 | } 69 | } 70 | 71 | template 72 | Void HandleBase::Reset(HandleType handle) 73 | { 74 | Close(); 75 | m_handle = handle; 76 | } 77 | 78 | template 79 | HandleType HandleBase::Detach() 80 | { 81 | HandleType handle = m_handle; 82 | m_handle = NULL; 83 | return handle; 84 | } 85 | 86 | template 87 | HandleBase::operator HandleType() const 88 | { 89 | return m_handle; 90 | } 91 | } 92 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/InternetHandle.cpp: -------------------------------------------------------------------------------- 1 | #include "InternetHandle.h" 2 | 3 | namespace WinToolsLib 4 | { 5 | namespace Handles 6 | { 7 | InternetHandle::InternetHandle() : 8 | Base(InternetHandle::Finalizer) 9 | { 10 | } 11 | 12 | InternetHandle::InternetHandle(HINTERNET handle) : 13 | Base(InternetHandle::Finalizer, handle) 14 | { 15 | } 16 | 17 | InternetHandle::InternetHandle(InternetHandle&& other) : 18 | Base(InternetHandle::Finalizer) 19 | { 20 | MoveFrom(std::move(other)); 21 | } 22 | 23 | InternetHandle& InternetHandle::operator=(InternetHandle&& other) 24 | { 25 | MoveFrom(std::move(other)); 26 | return *this; 27 | } 28 | 29 | Void InternetHandle::MoveFrom(InternetHandle&& other) 30 | { 31 | if (this != &other) 32 | { 33 | Reset(other.m_handle); 34 | other.m_handle = NULL; 35 | } 36 | } 37 | 38 | Void InternetHandle::Finalizer(HINTERNET handle) 39 | { 40 | ::InternetCloseHandle(handle); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/InternetHandle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "HandleBase.h" 4 | #include 5 | #include 6 | 7 | namespace WinToolsLib 8 | { 9 | namespace Handles 10 | { 11 | class InternetHandle : public HandleBase 12 | { 13 | typedef HandleBase Base; 14 | 15 | public: 16 | InternetHandle(); 17 | InternetHandle(HINTERNET handle); 18 | InternetHandle(InternetHandle&& other); 19 | 20 | InternetHandle& operator=(InternetHandle&& other); 21 | 22 | protected: 23 | Void MoveFrom(InternetHandle&& other); 24 | Void CopyFrom(const InternetHandle& other); 25 | 26 | static Void Finalizer(HINTERNET handle); 27 | }; 28 | } 29 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/RegistryHandle.cpp: -------------------------------------------------------------------------------- 1 | #include "RegistryHandle.h" 2 | 3 | namespace WinToolsLib 4 | { 5 | namespace Handles 6 | { 7 | RegistryHandle::RegistryHandle() : 8 | Base(RegistryHandle::Finalizer) 9 | { 10 | } 11 | 12 | RegistryHandle::RegistryHandle(HKEY handle) : 13 | Base(RegistryHandle::Finalizer, handle) 14 | { 15 | } 16 | 17 | RegistryHandle::RegistryHandle(RegistryHandle&& other) : 18 | Base(RegistryHandle::Finalizer) 19 | { 20 | MoveFrom(std::move(other)); 21 | } 22 | 23 | RegistryHandle& RegistryHandle::operator=(RegistryHandle&& other) 24 | { 25 | MoveFrom(std::move(other)); 26 | return *this; 27 | } 28 | 29 | Void RegistryHandle::MoveFrom(RegistryHandle&& other) 30 | { 31 | if (this != &other) 32 | { 33 | Reset(other.m_handle); 34 | other.m_handle = NULL; 35 | } 36 | } 37 | 38 | Void RegistryHandle::Finalizer(HKEY handle) 39 | { 40 | ::RegCloseKey(handle); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/RegistryHandle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "HandleBase.h" 4 | #include 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Handles 9 | { 10 | class RegistryHandle : public HandleBase 11 | { 12 | typedef HandleBase Base; 13 | 14 | public: 15 | RegistryHandle(); 16 | RegistryHandle(HKEY handle); 17 | RegistryHandle(RegistryHandle&& other); 18 | 19 | RegistryHandle& operator=(RegistryHandle&& other); 20 | 21 | protected: 22 | Void MoveFrom(RegistryHandle&& other); 23 | Void CopyFrom(const RegistryHandle& other); 24 | 25 | static Void Finalizer(HKEY handle); 26 | }; 27 | } 28 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/ServiceHandle.cpp: -------------------------------------------------------------------------------- 1 | #include "ServiceHandle.h" 2 | 3 | namespace WinToolsLib 4 | { 5 | namespace Handles 6 | { 7 | ServiceHandle::ServiceHandle() : 8 | Base(ServiceHandle::Finalizer) 9 | { 10 | } 11 | 12 | ServiceHandle::ServiceHandle(SC_HANDLE handle) : 13 | Base(ServiceHandle::Finalizer, handle) 14 | { 15 | } 16 | 17 | ServiceHandle::ServiceHandle(ServiceHandle&& other) : 18 | Base(ServiceHandle::Finalizer) 19 | { 20 | MoveFrom(std::move(other)); 21 | } 22 | 23 | ServiceHandle& ServiceHandle::operator=(ServiceHandle&& other) 24 | { 25 | MoveFrom(std::move(other)); 26 | return *this; 27 | } 28 | 29 | Void ServiceHandle::MoveFrom(ServiceHandle&& other) 30 | { 31 | if (this != &other) 32 | { 33 | Reset(other.m_handle); 34 | other.m_handle = NULL; 35 | } 36 | } 37 | 38 | Void ServiceHandle::Finalizer(SC_HANDLE handle) 39 | { 40 | ::CloseServiceHandle(handle); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /WinToolsLib/Handles/ServiceHandle.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "HandleBase.h" 4 | #include 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Handles 9 | { 10 | class ServiceHandle : public HandleBase 11 | { 12 | typedef HandleBase Base; 13 | 14 | public: 15 | ServiceHandle(); 16 | ServiceHandle(SC_HANDLE handle); 17 | ServiceHandle(ServiceHandle&& other); 18 | 19 | ServiceHandle& operator=(ServiceHandle&& other); 20 | 21 | protected: 22 | Void MoveFrom(ServiceHandle&& other); 23 | Void CopyFrom(const ServiceHandle& other); 24 | 25 | static Void Finalizer(SC_HANDLE handle); 26 | }; 27 | } 28 | } -------------------------------------------------------------------------------- /WinToolsLib/IParsable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "IStream.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | class IParsable 8 | { 9 | public: 10 | virtual ~IParsable() {} 11 | 12 | virtual Void Parse(IStream& stream) = 0; 13 | }; 14 | } 15 | -------------------------------------------------------------------------------- /WinToolsLib/IStream.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include 4 | 5 | namespace WinToolsLib 6 | { 7 | class IStream 8 | { 9 | public: 10 | virtual ~IStream() {} 11 | 12 | virtual SizeT GetPosition() const = 0; 13 | virtual Void SetPosition(SizeT position) = 0; 14 | 15 | virtual Void Reset() = 0; 16 | virtual Void Skip(SizeT bytes) = 0; 17 | 18 | virtual Void Read(PByte buffer, SizeT bytes) = 0; 19 | virtual Void Write(PByte buffer, SizeT bytes) = 0; 20 | 21 | virtual IStream& operator>>(Int8& value) = 0; 22 | virtual IStream& operator>>(Int16& value) = 0; 23 | virtual IStream& operator>>(Int32& value) = 0; 24 | virtual IStream& operator>>(Int64& value) = 0; 25 | 26 | virtual IStream& operator>>(UInt8& value) = 0; 27 | virtual IStream& operator>>(UInt16& value) = 0; 28 | virtual IStream& operator>>(UInt32& value) = 0; 29 | virtual IStream& operator>>(UInt64& value) = 0; 30 | 31 | virtual IStream& operator<<(Int8 value) = 0; 32 | virtual IStream& operator<<(Int16 value) = 0; 33 | virtual IStream& operator<<(Int32 value) = 0; 34 | virtual IStream& operator<<(Int64 value) = 0; 35 | 36 | virtual IStream& operator<<(UInt8 value) = 0; 37 | virtual IStream& operator<<(UInt16 value) = 0; 38 | virtual IStream& operator<<(UInt32 value) = 0; 39 | virtual IStream& operator<<(UInt64 value) = 0; 40 | }; 41 | 42 | typedef std::unique_ptr IStreamPtr; 43 | typedef std::tr1::weak_ptr IStreamWeakPtr; 44 | typedef std::tr1::shared_ptr IStreamSharedPtr; 45 | } 46 | -------------------------------------------------------------------------------- /WinToolsLib/License.cpp: -------------------------------------------------------------------------------- 1 | #include "License.h" 2 | #include "Exception.h" 3 | #include "Registry\Key.h" 4 | #include "Checksum\Crc.h" 5 | #include "Process\Process.h" 6 | 7 | namespace WinToolsLib 8 | { 9 | String License::GetUserId() 10 | { 11 | auto processorId = GetProcessorId(); 12 | auto productId = GetProductId(); 13 | auto userId = String::FormatS(Text("%s/%s"), processorId.GetBuffer(), productId.GetBuffer()); 14 | 15 | auto data = (PCByte)userId.GetBuffer(); 16 | auto size = userId.GetLength() * sizeof(TChar); 17 | auto hash = Checksum::Crc32::Calc(data, size); 18 | 19 | return String::FormatS(Text("%010u"), hash); 20 | } 21 | 22 | String License::GetProcessorId() 23 | { 24 | struct 25 | { 26 | UInt32 eax; 27 | UInt32 ebx; 28 | UInt32 ecx; 29 | UInt32 edx; 30 | } id; 31 | 32 | __cpuid((Int32*)&id, 1); 33 | 34 | return String::FormatS(Text("%08X%08X"), id.edx, id.eax); 35 | } 36 | 37 | String License::GetProductId() 38 | { 39 | try 40 | { 41 | auto currentVersion = Registry::Key::Open( 42 | HKEY_LOCAL_MACHINE, 43 | Text("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), 44 | Process::Process::IsWow64() ? KEY_WOW64_64KEY | KEY_QUERY_VALUE : KEY_QUERY_VALUE); 45 | 46 | const String keyName(Text("ProductId")); 47 | String productId; 48 | 49 | currentVersion.EnumValue([&keyName, &productId](const TChar* name, UInt32 type, Buffer& data) 50 | { 51 | if (keyName == name && REG_SZ == type) 52 | { 53 | productId = (const TChar*)data.GetBuffer(); 54 | } 55 | }); 56 | 57 | return productId; 58 | } 59 | catch (Exception&) 60 | { 61 | } 62 | 63 | return String(); 64 | } 65 | } -------------------------------------------------------------------------------- /WinToolsLib/License.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "String.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | class License 8 | { 9 | public: 10 | static String GetUserId(); 11 | 12 | private: 13 | static String GetProcessorId(); 14 | static String GetProductId(); 15 | }; 16 | } 17 | -------------------------------------------------------------------------------- /WinToolsLib/Log.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "String.h" 4 | #include "DebugOutput.h" 5 | 6 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0100 7 | #define ENABLE_LOG_CHECKPOINT 8 | #endif 9 | 10 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0010 11 | #define ENABLE_LOG_INFO 12 | #endif 13 | 14 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0008 15 | #define ENABLE_LOG_EXCEPTION 16 | #endif 17 | 18 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0004 19 | #define ENABLE_LOG_WARNING 20 | #endif 21 | 22 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0002 23 | #define ENABLE_LOG_ERROR 24 | #endif 25 | 26 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0001 27 | #define ENABLE_LOG_FATAL 28 | #endif 29 | 30 | namespace WinToolsLib 31 | { 32 | #ifdef ENABLE_LOG_CHECKPOINT 33 | #define LOG_CHECKPOINT(...) DebugOutput(Text("

%s"), String::FormatS(__VA_ARGS__).GetBuffer()) 34 | #else 35 | #define LOG_CHECKPOINT(...) 36 | #endif 37 | 38 | #ifdef ENABLE_LOG_INFO 39 | #define LOG_INFO(...) DebugOutput(Text(" %s"), String::FormatS(__VA_ARGS__).GetBuffer()) 40 | #else 41 | #define LOG_INFO(...) 42 | #endif 43 | 44 | #ifdef ENABLE_LOG_EXCEPTION 45 | #define LOG_EXCEPTION(...) DebugOutput(Text(" %s"), String::FormatS(__VA_ARGS__).GetBuffer()) 46 | #else 47 | #define LOG_EXCEPTION(...) 48 | #endif 49 | 50 | #ifdef ENABLE_LOG_WARNING 51 | #define LOG_WARNING(...) DebugOutput(Text(" %s"), String::FormatS(__VA_ARGS__).GetBuffer()) 52 | #else 53 | #define LOG_WARNING(...) 54 | #endif 55 | 56 | #ifdef ENABLE_LOG_ERROR 57 | #define LOG_ERROR(...) DebugOutput(Text(" %s"), String::FormatS(__VA_ARGS__).GetBuffer()) 58 | #else 59 | #define LOG_ERROR(...) 60 | #endif 61 | 62 | #ifdef ENABLE_LOG_FATAL 63 | #define LOG_FATAL(...) DebugOutput(Text(" %s"), String::FormatS(__VA_ARGS__).GetBuffer()) 64 | #else 65 | #define LOG_FATAL(...) 66 | #endif 67 | } 68 | -------------------------------------------------------------------------------- /WinToolsLib/Module.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "String.h" 4 | #include 5 | 6 | namespace WinToolsLib 7 | { 8 | class Module; 9 | typedef std::list ModuleList; 10 | 11 | class Module 12 | { 13 | public: 14 | Module(String name, String path, PByte base, UInt32 size, Bool isKernel); 15 | Module(Module&& other); 16 | Module(const Module& other); 17 | ~Module(); 18 | 19 | Module& operator=(Module&& other); 20 | Module& operator=(const Module& other); 21 | 22 | const String& GetName() const; 23 | const String& GetPath() const; 24 | PByte GetBase() const; 25 | UInt32 GetSize() const; 26 | Bool IsKernel() const; 27 | 28 | static ModuleList GetKernelList(); 29 | static ModuleList GetList(UInt32 processId); 30 | 31 | protected: 32 | Void MoveFrom(Module& other); 33 | Void CopyFrom(const Module& other); 34 | 35 | protected: 36 | String m_name; 37 | String m_path; 38 | PByte m_base; 39 | UInt32 m_size; 40 | Bool m_isKernel; 41 | }; 42 | 43 | inline const String& Module::GetName() const 44 | { 45 | return m_name; 46 | } 47 | 48 | inline const String& Module::GetPath() const 49 | { 50 | return m_path; 51 | } 52 | 53 | inline PByte Module::GetBase() const 54 | { 55 | return m_base; 56 | } 57 | 58 | inline UInt32 Module::GetSize() const 59 | { 60 | return m_size; 61 | } 62 | 63 | inline Bool Module::IsKernel() const 64 | { 65 | return m_isKernel; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /WinToolsLib/Monitor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // 9 | // Based on monitor implementation by Herb Sutter 10 | // 11 | 12 | namespace WinToolsLib 13 | { 14 | template 15 | class Monitor 16 | { 17 | Monitor(const Monitor& other) = delete; // Non-copyable 18 | Monitor& operator=(const Monitor& other) = delete; // Non-copyable 19 | 20 | public: 21 | Monitor() 22 | { 23 | m_lock = std::make_unique(); 24 | } 25 | 26 | explicit Monitor(T&& t) : 27 | m_lock(std::make_unique()), 28 | m_t(std::move(t)) 29 | { 30 | } 31 | 32 | Monitor(Monitor&& other) 33 | { 34 | MoveFrom(other); 35 | } 36 | 37 | Monitor& operator=(Monitor&& other) 38 | { 39 | MoveFrom(other); 40 | return *this; 41 | } 42 | 43 | template 44 | typename std::result_of::type operator()(F f) const 45 | { 46 | std::lock_guard hold(*m_lock); 47 | return f(m_t); 48 | } 49 | 50 | protected: 51 | Void MoveFrom(Monitor& other) 52 | { 53 | m_t = std::move(other.m_t); 54 | m_lock = std::move(other.m_lock); 55 | } 56 | 57 | private: 58 | mutable T m_t; 59 | mutable std::unique_ptr m_lock; 60 | }; 61 | } 62 | -------------------------------------------------------------------------------- /WinToolsLib/Net/Http.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Buffer.h" 4 | #include "..\Handles\InternetHandle.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Net 9 | { 10 | using namespace Handles; 11 | 12 | struct Flag 13 | { 14 | enum 15 | { 16 | Reload = INTERNET_FLAG_RELOAD, 17 | Secure = INTERNET_FLAG_SECURE, 18 | NoCacheWrite = INTERNET_FLAG_NO_CACHE_WRITE, 19 | IgnoreCertInvalidName = INTERNET_FLAG_IGNORE_CERT_CN_INVALID, 20 | IgnoreCertInvalidDate = INTERNET_FLAG_IGNORE_CERT_DATE_INVALID 21 | }; 22 | }; 23 | 24 | class Http 25 | { 26 | public: 27 | Http(PCTChar userAgent); 28 | Http(Http&& other); 29 | 30 | Http& operator=(Http&& other); 31 | 32 | Void Connect(PCTChar pszServer, UInt16 port); 33 | 34 | static Bool IsOffline(InternetHandle hInternet = nullptr); 35 | static Void GoOnline(InternetHandle hInternet = nullptr); 36 | 37 | Void SendRequest( 38 | PCTChar pszVerb, 39 | PCTChar pszUrl, 40 | PCTChar pszHeaders, 41 | PCByte pBody, 42 | UInt32 bodyLength, 43 | UInt32 flags = 0); 44 | 45 | Void SendGetRequest( 46 | PCTChar pszUrl, 47 | UInt32 flags = 0); 48 | 49 | Void SendPostRequest( 50 | PCTChar pszUrl, 51 | PCTChar pszHeaders, 52 | PCByte pBody, 53 | UInt32 bodyLength, 54 | UInt32 flags = 0); 55 | 56 | UInt32 GetContentSize() const; 57 | 58 | typedef std::function ReadFileCallback; 59 | Buffer ReadFile(UInt32 chunkSize = (256 * 1024), ReadFileCallback callback = nullptr) const; 60 | 61 | StringA ReadStringA(UInt32 chunkSize = 1024) const; 62 | 63 | struct Options 64 | { 65 | enum 66 | { 67 | TryToGoOnline = 1, 68 | IgnoreUnknownCertificateAuthority 69 | }; 70 | }; 71 | Void SetOption(UInt32 option); 72 | Void SetSendTimeout(UInt32 milliseconds); 73 | 74 | protected: 75 | Void MoveFrom(Http& other); 76 | Void SetIgnoreUnknownCertificateAuthority(); 77 | Void SetSendTimeout(); 78 | 79 | private: 80 | InternetHandle m_hInternet; 81 | InternetHandle m_hConnect; 82 | InternetHandle m_hRequest; 83 | UInt32 m_options; 84 | UInt32 m_sendTimeout; 85 | }; 86 | 87 | inline Void Http::SetOption(UInt32 option) 88 | { 89 | m_options |= option; 90 | } 91 | 92 | inline Void Http::SetSendTimeout(UInt32 milliseconds) 93 | { 94 | m_sendTimeout = milliseconds; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /WinToolsLib/Net/Mac.cpp: -------------------------------------------------------------------------------- 1 | #include "Mac.h" 2 | #include "..\Exception.h" 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | #pragma comment (lib, "IPHlpApi.lib") 10 | 11 | namespace WinToolsLib 12 | { 13 | namespace Net 14 | { 15 | Buffer Mac::GetFirst() 16 | { 17 | // 18 | // The recommended method of calling the GetAdaptersAddresses function 19 | // is to pre-allocate a 15KB working buffer pointed to by the AdapterAddresses parameter. 20 | // 21 | ULONG size = 15 * 1024; 22 | while (True) 23 | { 24 | Buffer buffer(size); 25 | PIP_ADAPTER_ADDRESSES pAddresses = (PIP_ADAPTER_ADDRESSES)buffer.GetBuffer(); 26 | 27 | auto result = ::GetAdaptersAddresses(0, 0, NULL, pAddresses, &size); 28 | if (ERROR_BUFFER_OVERFLOW == result) 29 | { 30 | continue; 31 | } 32 | else if (ERROR_SUCCESS == result) 33 | { 34 | Buffer mac(pAddresses->PhysicalAddress, pAddresses->PhysicalAddressLength); 35 | return mac; 36 | } 37 | 38 | break; 39 | } 40 | 41 | return Buffer(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /WinToolsLib/Net/Mac.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Buffer.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace Net 8 | { 9 | class Mac 10 | { 11 | public: 12 | static Buffer GetFirst(); 13 | 14 | private: 15 | }; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /WinToolsLib/Net/Url.cpp: -------------------------------------------------------------------------------- 1 | #include "Url.h" 2 | #include "..\Exception.h" 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #pragma comment (lib, "Shlwapi.lib") 10 | 11 | namespace WinToolsLib 12 | { 13 | namespace Net 14 | { 15 | String Url::Encode(const TChar* url, EncodePortion flag) 16 | { 17 | const auto maxAttempts = 4; 18 | const auto maxLength = INTERNET_MAX_URL_LENGTH; 19 | const auto step = maxLength / maxAttempts; 20 | 21 | for (auto i = 1; i <= maxAttempts; i++) 22 | { 23 | DWORD length = step * i; 24 | std::vector buffer(length + 1); 25 | 26 | auto result = ::UrlEscape(url, &buffer[0], &length, static_cast(flag)); 27 | if (S_OK == result) 28 | { 29 | return String(&buffer[0]); 30 | } 31 | if (E_POINTER == result) 32 | { 33 | continue; 34 | } 35 | THROW_WIN32_EXCEPTION(result); 36 | } 37 | THROW_WIN32_EXCEPTION(E_POINTER); 38 | } 39 | 40 | String Url::Decode(const TChar* url) 41 | { 42 | const auto maxAttempts = 4; 43 | const auto maxLength = INTERNET_MAX_URL_LENGTH; 44 | const auto step = maxLength / maxAttempts; 45 | 46 | for (auto i = 1; i <= maxAttempts; i++) 47 | { 48 | DWORD length = step * i; 49 | std::vector buffer(length + 1); 50 | 51 | auto result = ::UrlUnescape((PTSTR)url, &buffer[0], &length, 0); 52 | if (S_OK == result) 53 | { 54 | return String(&buffer[0]); 55 | } 56 | if (E_POINTER == result) 57 | { 58 | continue; 59 | } 60 | THROW_WIN32_EXCEPTION(result); 61 | } 62 | THROW_WIN32_EXCEPTION(E_POINTER); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /WinToolsLib/Net/Url.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace Net 8 | { 9 | enum class EncodePortion : UInt32 10 | { 11 | None = 0x00000000, 12 | SegmentOnly = 0x00002000 13 | }; 14 | 15 | class Url 16 | { 17 | public: 18 | static String Encode(const TChar* url, EncodePortion flag = EncodePortion::None); 19 | static String Decode(const TChar* url); 20 | }; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /WinToolsLib/Os/Authorization/Impersonate.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | 4 | namespace WinToolsLib { namespace Os { namespace Authorization 5 | { 6 | class Impersonate 7 | { 8 | public: 9 | Impersonate(); 10 | ~Impersonate(); 11 | 12 | static Void LoggedOnUser(); 13 | static Void CreateProcessAsLoggedOnUser( 14 | const TChar* application, 15 | const TChar* parameters = nullptr); 16 | 17 | private: 18 | Bool m_revertToSelf; 19 | }; 20 | 21 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Authorization/Sid.cpp: -------------------------------------------------------------------------------- 1 | #include "Sid.h" 2 | 3 | #pragma comment(lib, "Advapi32.lib") 4 | 5 | namespace WinToolsLib { namespace Os { namespace Authorization 6 | { 7 | Sid::Sid(PSID psid) : 8 | m_psid(psid) 9 | { 10 | } 11 | 12 | Sid::Sid(Sid&& other) : 13 | m_psid(nullptr) 14 | { 15 | MoveFrom(other); 16 | } 17 | 18 | Sid::~Sid() 19 | { 20 | Free(); 21 | } 22 | 23 | Sid& Sid::operator=(Sid&& other) 24 | { 25 | MoveFrom(other); 26 | return *this; 27 | } 28 | 29 | Void Sid::Free() 30 | { 31 | if (m_psid) 32 | { 33 | auto psid = m_psid; 34 | m_psid = nullptr; 35 | ::FreeSid(psid); 36 | } 37 | } 38 | 39 | Void Sid::Attach(PSID psid) 40 | { 41 | Free(); 42 | m_psid = psid; 43 | } 44 | 45 | PSID Sid::Detach() 46 | { 47 | auto psid = m_psid; 48 | m_psid = nullptr; 49 | return psid; 50 | } 51 | 52 | Bool Sid::IsAdmin() 53 | { 54 | SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY; 55 | Sid adminGroup; 56 | 57 | auto success = ::AllocateAndInitializeSid( 58 | &ntAuthority, 59 | 2, 60 | SECURITY_BUILTIN_DOMAIN_RID, 61 | DOMAIN_ALIAS_RID_ADMINS, 62 | 0, 0, 0, 0, 0, 0, 63 | adminGroup.GetAddress()); 64 | 65 | if (!success) 66 | { 67 | return False; 68 | } 69 | 70 | BOOL isMember = FALSE; 71 | 72 | success = ::CheckTokenMembership(NULL, adminGroup, &isMember); 73 | if (!success) 74 | { 75 | return False; 76 | } 77 | 78 | return TRUE == isMember; 79 | } 80 | 81 | Bool Sid::IsSystem() 82 | { 83 | SID_IDENTIFIER_AUTHORITY ntAuthority = SECURITY_NT_AUTHORITY; 84 | Sid system; 85 | 86 | auto success = ::AllocateAndInitializeSid( 87 | &ntAuthority, 88 | 1, 89 | SECURITY_LOCAL_SYSTEM_RID, 90 | 0, 0, 0, 0, 0, 0, 0, 91 | system.GetAddress()); 92 | 93 | if (!success) 94 | { 95 | return False; 96 | } 97 | 98 | BOOL isSystem = FALSE; 99 | 100 | success = ::CheckTokenMembership(NULL, system, &isSystem); 101 | if (!success) 102 | { 103 | return False; 104 | } 105 | 106 | return TRUE == isSystem; 107 | } 108 | 109 | Void Sid::MoveFrom(Sid& other) 110 | { 111 | if (this != &other) 112 | { 113 | Attach(other.Detach()); 114 | } 115 | } 116 | 117 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Authorization/Sid.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | 4 | #include 5 | 6 | namespace WinToolsLib { namespace Os { namespace Authorization 7 | { 8 | class Sid 9 | { 10 | Sid(const Sid& other); // Non-copyable 11 | Sid& operator=(const Sid& other); // Non-copyable 12 | 13 | public: 14 | Sid(PSID psid = nullptr); 15 | Sid(Sid&& other); 16 | ~Sid(); 17 | 18 | Sid& operator=(Sid&& other); 19 | 20 | Void Free(); 21 | Void Attach(PSID psid); 22 | PSID Detach(); 23 | 24 | operator PSID() const; 25 | PSID* GetAddress(); 26 | 27 | static Bool IsAdmin(); 28 | static Bool IsSystem(); 29 | 30 | protected: 31 | Void MoveFrom(Sid& other); 32 | 33 | private: 34 | PSID m_psid; 35 | }; 36 | 37 | inline Sid::operator PSID() const 38 | { 39 | return m_psid; 40 | } 41 | 42 | inline PSID* Sid::GetAddress() 43 | { 44 | return &m_psid; 45 | } 46 | 47 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Firewall/FirewallPolicy.cpp: -------------------------------------------------------------------------------- 1 | #include "FirewallPolicy.h" 2 | #include "..\..\Exception.h" 3 | 4 | namespace WinToolsLib { namespace Os { namespace Firewall 5 | { 6 | FirewallPolicy::FirewallPolicy() 7 | { 8 | auto hr = ::CoCreateInstance( 9 | __uuidof(NetFwPolicy2), 10 | nullptr, 11 | CLSCTX_INPROC_SERVER, 12 | __uuidof(INetFwPolicy2), 13 | (PVoid*)&m_fwPolicy); 14 | 15 | if (FAILED(hr)) 16 | { 17 | THROW_WIN32_EXCEPTION(hr); 18 | } 19 | } 20 | 21 | FirewallPolicy::~FirewallPolicy() 22 | { 23 | } 24 | 25 | RuleList FirewallPolicy::GetRuleList() 26 | { 27 | Com::Ptr ruleList; 28 | auto hr = m_fwPolicy->get_Rules(&ruleList); 29 | if (FAILED(hr)) 30 | { 31 | THROW_WIN32_EXCEPTION(hr); 32 | } 33 | 34 | return RuleList(std::move(ruleList)); 35 | } 36 | 37 | ProfileList FirewallPolicy::GetCurrentProfileTypes() const 38 | { 39 | long profileBitmask = 0; 40 | auto hr = m_fwPolicy->get_CurrentProfileTypes(&profileBitmask); 41 | if (FAILED(hr)) 42 | { 43 | THROW_WIN32_EXCEPTION(hr); 44 | } 45 | 46 | if ((profileBitmask & (UInt32)Profile::All) == (UInt32)Profile::All) 47 | { 48 | return ProfileList({ Profile::All }); 49 | } 50 | 51 | ProfileList list; 52 | if ((profileBitmask & (UInt32)Profile::Domain) == (UInt32)Profile::Domain) list.push_back(Profile::Domain); 53 | if ((profileBitmask & (UInt32)Profile::Public) == (UInt32)Profile::Public) list.push_back(Profile::Public); 54 | if ((profileBitmask & (UInt32)Profile::Private) == (UInt32)Profile::Private) list.push_back(Profile::Private); 55 | return list; 56 | } 57 | 58 | Void FirewallPolicy::SetDefaultOutboundAction(Profile profile, Action action) 59 | { 60 | auto hr = m_fwPolicy->put_DefaultOutboundAction(NET_FW_PROFILE_TYPE2(profile), NET_FW_ACTION(action)); 61 | if (FAILED(hr)) 62 | { 63 | THROW_WIN32_EXCEPTION(hr); 64 | } 65 | } 66 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Firewall/FirewallPolicy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\Com\Ptr.h" 4 | 5 | #include 6 | #include 7 | 8 | #include "RuleList.h" 9 | 10 | namespace WinToolsLib { namespace Os { namespace Firewall 11 | { 12 | enum class Profile 13 | { 14 | Domain = NET_FW_PROFILE2_DOMAIN, 15 | Private = NET_FW_PROFILE2_PRIVATE, 16 | Public = NET_FW_PROFILE2_PUBLIC, 17 | All = NET_FW_PROFILE2_ALL 18 | }; 19 | typedef std::vector ProfileList; 20 | 21 | class FirewallPolicy 22 | { 23 | public: 24 | FirewallPolicy(); 25 | ~FirewallPolicy(); 26 | 27 | RuleList GetRuleList(); 28 | ProfileList GetCurrentProfileTypes() const; 29 | 30 | Void SetDefaultOutboundAction(Profile profile, Action action); 31 | 32 | private: 33 | Com::Ptr m_fwPolicy; 34 | }; 35 | 36 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Firewall/Rule.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | 8 | namespace WinToolsLib { namespace Os { namespace Firewall 9 | { 10 | enum class Action 11 | { 12 | Block = NET_FW_ACTION_BLOCK, 13 | Allow = NET_FW_ACTION_ALLOW 14 | }; 15 | 16 | enum class Direction 17 | { 18 | In = NET_FW_RULE_DIR_IN, 19 | Out = NET_FW_RULE_DIR_OUT 20 | }; 21 | 22 | class RuleList; 23 | 24 | class Rule 25 | { 26 | Rule(const Rule& other) = delete; 27 | Rule& operator=(const Rule& other) = delete; 28 | public: 29 | Rule(); 30 | Rule(Com::Ptr&& rule); 31 | Rule(Rule&& other); 32 | ~Rule(); 33 | 34 | Rule& operator=(Rule&& other); 35 | 36 | const Action GetAction() const; 37 | const Direction GetDirection() const; 38 | const Bool IsEnabled() const; 39 | const WChar* GetLocalAddresses() const; 40 | const WChar* GetName() const; 41 | const WChar* GetRemoteAddresses() const; 42 | 43 | Void SetAction(Action value); 44 | Void SetDirection(Direction value); 45 | Void SetEnabled(Bool value); 46 | Void SetLocalAddresses(const WChar* value); 47 | Void SetName(const WChar* value); 48 | Void SetRemoteAddresses(const WChar* value); 49 | 50 | protected: 51 | Void MoveFrom(Rule& other); 52 | 53 | private: 54 | operator const INetFwRule*() const; 55 | operator INetFwRule*(); 56 | 57 | private: 58 | Com::Ptr m_rule; 59 | 60 | friend class RuleList; 61 | }; 62 | 63 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Firewall/RuleList.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\Com\Ptr.h" 4 | 5 | #include 6 | 7 | #include 8 | 9 | #include "Rule.h" 10 | 11 | namespace WinToolsLib { namespace Os { namespace Firewall 12 | { 13 | class RuleList 14 | { 15 | RuleList(const RuleList& other) = delete; 16 | RuleList& operator=(const RuleList& other) = delete; 17 | public: 18 | RuleList(Com::Ptr&& rules); 19 | RuleList(RuleList&& other); 20 | ~RuleList(); 21 | 22 | RuleList& operator=(RuleList&& other); 23 | 24 | Bool IsRuleExist(const WChar* name) const; 25 | Void Add(Rule& rule); 26 | Rule Item(const WChar* name) const; 27 | 28 | typedef std::function EnumCallback; 29 | Void EnumRules(EnumCallback callback); 30 | 31 | protected: 32 | Void MoveFrom(RuleList& other); 33 | 34 | private: 35 | Com::Ptr m_rules; 36 | }; 37 | 38 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/InternetShortcut.cpp: -------------------------------------------------------------------------------- 1 | #include "InternetShortcut.h" 2 | 3 | #include 4 | #include 5 | 6 | #include "..\Exception.h" 7 | #include "..\Com\Initialize.h" 8 | #include "..\Com\TaskMem.h" 9 | #include "..\Com\Ptr.h" 10 | 11 | namespace WinToolsLib { namespace Os 12 | { 13 | Void InternetShortcut::Create(const WChar* path, const WChar* url) 14 | { 15 | Com::Initialize init(Com::Model::SingleThreaded); 16 | Com::Ptr locator; 17 | 18 | auto hr = ::CoCreateInstance( 19 | CLSID_InternetShortcut, 20 | NULL, 21 | CLSCTX_INPROC_SERVER, 22 | IID_IUniformResourceLocator, 23 | (PVoid*)&locator); 24 | 25 | if (FAILED(hr)) 26 | { 27 | return THROW_WIN32_EXCEPTION(hr); 28 | } 29 | 30 | hr = locator->SetURL(url, TRANSLATEURL_FL_USE_DEFAULT_PROTOCOL); 31 | if (FAILED(hr)) 32 | { 33 | return THROW_WIN32_EXCEPTION(hr); 34 | } 35 | 36 | Com::Ptr persistFile; 37 | 38 | hr = locator->QueryInterface(IID_IPersistFile, (PVoid*)&persistFile); 39 | if (FAILED(hr)) 40 | { 41 | return THROW_WIN32_EXCEPTION(hr); 42 | } 43 | 44 | hr = persistFile->Save(path, FALSE); 45 | if (FAILED(hr)) 46 | { 47 | return THROW_WIN32_EXCEPTION(hr); 48 | } 49 | } 50 | 51 | } } -------------------------------------------------------------------------------- /WinToolsLib/Os/InternetShortcut.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | namespace Os 7 | { 8 | class InternetShortcut 9 | { 10 | public: 11 | static Void Create(const WChar* path, const WChar* url); 12 | }; 13 | } 14 | } -------------------------------------------------------------------------------- /WinToolsLib/Os/Memory/Local.cpp: -------------------------------------------------------------------------------- 1 | #include "Local.h" 2 | #include "..\..\Exception.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | namespace Memory 7 | { 8 | Local::Local(HLOCAL hLocal) : 9 | m_hLocal(hLocal), 10 | m_pointer(nullptr) 11 | { 12 | } 13 | 14 | Local::~Local() 15 | { 16 | Free(); 17 | } 18 | 19 | Void Local::Free() 20 | { 21 | Unlock(); 22 | if (m_hLocal) 23 | { 24 | ::LocalFree(m_hLocal); 25 | m_hLocal = nullptr; 26 | } 27 | } 28 | 29 | PVoid Local::Lock() 30 | { 31 | if (!m_pointer && m_hLocal) 32 | { 33 | m_pointer = ::LocalLock(m_hLocal); 34 | } 35 | return m_pointer; 36 | } 37 | 38 | Void Local::Unlock() 39 | { 40 | if (m_pointer && m_hLocal) 41 | { 42 | ::LocalUnlock(m_hLocal); 43 | m_pointer = nullptr; 44 | } 45 | } 46 | 47 | Local Local::Alloc(UInt32 flags, SizeT bytes) 48 | { 49 | Local local(::LocalAlloc(flags, bytes)); 50 | if (!local.m_hLocal) 51 | { 52 | THROW_LAST_ERROR_EXCEPTION(); 53 | } 54 | return local; 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /WinToolsLib/Os/Memory/Local.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | 4 | #include 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Memory 9 | { 10 | class Local 11 | { 12 | public: 13 | Local(HLOCAL hLocal); 14 | ~Local(); 15 | 16 | Void Free(); 17 | 18 | PVoid Lock(); 19 | Void Unlock(); 20 | 21 | static Local Alloc(UInt32 flags, SizeT bytes); 22 | 23 | private: 24 | HLOCAL m_hLocal; 25 | PVoid m_pointer; 26 | }; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /WinToolsLib/Os/RemoteDesktop/Session.cpp: -------------------------------------------------------------------------------- 1 | #include "Session.h" 2 | #include "..\..\Exception.h" 3 | 4 | #include 5 | #include 6 | #pragma comment(lib, "WtsApi32.lib") 7 | 8 | namespace WinToolsLib { namespace Os { namespace RemoteDesktop 9 | { 10 | Handles::Handle Session::GetActiveUserToken() 11 | { 12 | auto sessionId = ::WTSGetActiveConsoleSessionId(); 13 | HANDLE handle = nullptr; 14 | 15 | auto success = ::WTSQueryUserToken(sessionId, &handle); 16 | if (!success) 17 | { 18 | THROW_LAST_ERROR_EXCEPTION(); 19 | } 20 | 21 | return Handles::Handle(handle); 22 | } 23 | 24 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/RemoteDesktop/Session.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\Handles\Handle.h" 4 | 5 | namespace WinToolsLib { namespace Os { namespace RemoteDesktop 6 | { 7 | class Session 8 | { 9 | public: 10 | static Handles::Handle GetActiveUserToken(); 11 | }; 12 | 13 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/RestartManager.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Process\Process.h" 4 | 5 | namespace WinToolsLib { namespace Os 6 | { 7 | class RestartManager 8 | { 9 | public: 10 | RestartManager(); 11 | ~RestartManager(); 12 | 13 | std::vector GetResourceUsedProcessList(const TChar* path); 14 | 15 | private: 16 | DWORD m_session; 17 | }; 18 | } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Shell.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | #include "..\Exception.h" 5 | #include "..\Handles\Handle.h" 6 | 7 | namespace WinToolsLib { namespace Os 8 | { 9 | enum class KnownFolder 10 | { 11 | Unknown = 0, 12 | CommonPrograms, 13 | CommonStartMenu, 14 | CommonStartup, 15 | CommonTemplates, 16 | ComputerFolder, 17 | ConnectionsFolder, 18 | ControlPanelFolder, 19 | Cookies, 20 | Desktop, 21 | Documents, 22 | DocumentsLibrary, 23 | Downloads, 24 | Favorites, 25 | Fonts, 26 | History, 27 | InternetCache, 28 | InternetFolder, 29 | Libraries, 30 | Links, 31 | LocalAppData, 32 | LocalAppDataLow, 33 | NetworkFolder, 34 | Profile, 35 | ProgramData, 36 | ProgramFiles, 37 | ProgramFilesX86, 38 | ProgramFilesCommon, 39 | ProgramFilesCommonX86, 40 | Programs, 41 | Public, 42 | PublicDesktop, 43 | PublicDocuments, 44 | PublicDownloads, 45 | QuickLaunch, 46 | Recent, 47 | RecycleBinFolder, 48 | RoamingAppData, 49 | SendTo, 50 | StartMenu, 51 | Startup, 52 | System, 53 | SystemX86, 54 | Windows, 55 | _End 56 | }; 57 | 58 | class Shell 59 | { 60 | public: 61 | static const TChar* KnownFolderToString(KnownFolder folder); 62 | static KnownFolder KnownFolderFromString(const TChar* string); 63 | 64 | static String GetKnownFolder(KnownFolder folder, const Handles::Handle& token = Handles::Handle()); 65 | }; 66 | 67 | class KnownFolderNotSupported : public Exception 68 | { 69 | public: 70 | KnownFolderNotSupported(PCChar function, UInt32 line, PCChar file, KnownFolder folder) : 71 | Exception(function, line, file, CreateErrorMessage(folder)) 72 | { 73 | } 74 | 75 | KnownFolderNotSupported(PCChar function, UInt32 line, PCChar file, const TChar* string) : 76 | Exception(function, line, file, CreateErrorMessage(string)) 77 | { 78 | } 79 | 80 | protected: 81 | static String CreateErrorMessage(KnownFolder folder) 82 | { 83 | return String::FormatS(Text("Specified known folder %u is not supported"), folder); 84 | } 85 | 86 | static String CreateErrorMessage(const TChar* string) 87 | { 88 | return String::FormatS(Text("Specified known folder %s is not supported"), string); 89 | } 90 | }; 91 | 92 | } } -------------------------------------------------------------------------------- /WinToolsLib/Os/StartupLocation.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Exception.h" 4 | 5 | namespace WinToolsLib { namespace Os 6 | { 7 | class Startup 8 | { 9 | public: 10 | enum class FileLocation 11 | { 12 | Unknown = 0, 13 | UserStartup, 14 | CommonStartup 15 | }; 16 | enum class RegistryLocation 17 | { 18 | Unknown = 0, 19 | CurrentUserRun, 20 | CurrentUserRunOnce, 21 | LocalMachineRun, 22 | LocalMachineRun32, 23 | LocalMachineRunOnce, 24 | LocalMachineRunOnce32 25 | }; 26 | }; 27 | 28 | class UnsupportedLocation : public Exception 29 | { 30 | public: 31 | UnsupportedLocation(PCChar function, UInt32 line, PCChar file); 32 | }; 33 | 34 | inline UnsupportedLocation::UnsupportedLocation(PCChar function, UInt32 line, PCChar file) : 35 | Exception(function, line, file, Text("Unsupported location")) 36 | { 37 | } 38 | 39 | #define THROW_UNSUPPORTED_LOCATION() \ 40 | throw UnsupportedLocation(__FUNCTION__, __LINE__, __FILE__) 41 | 42 | } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Win32Service/Factory.cpp: -------------------------------------------------------------------------------- 1 | #include "Factory.h" 2 | 3 | #include "..\..\Exception.h" 4 | 5 | namespace WinToolsLib { namespace Os { namespace Win32Service 6 | { 7 | std::vector Factory::m_serviceList; 8 | 9 | UInt32 Factory::RunRegisteredServices() 10 | { 11 | if (m_serviceList.size() > 1) 12 | { 13 | Details::ServiceType = SERVICE_WIN32_SHARE_PROCESS; 14 | } 15 | m_serviceList.push_back(SERVICE_TABLE_ENTRY({ NULL, NULL })); 16 | 17 | auto table = (SERVICE_TABLE_ENTRY*)m_serviceList.data(); 18 | if (!::StartServiceCtrlDispatcher(table)) 19 | { 20 | const auto& entry = m_serviceList.front(); 21 | const auto error = ::GetLastError(); 22 | Details::ReportEvent(entry.lpServiceName, Text("StartServiceCtrlDispatcher"), error); 23 | return error; 24 | } 25 | 26 | return ERROR_SUCCESS; 27 | } 28 | }}} -------------------------------------------------------------------------------- /WinToolsLib/Os/Win32Service/Factory.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #include 5 | 6 | #include "..\..\Types.h" 7 | #include "Service.h" 8 | 9 | namespace WinToolsLib { namespace Os { namespace Win32Service 10 | { 11 | class Factory 12 | { 13 | public: 14 | template 15 | static Void RegisterService(); 16 | 17 | static UInt32 RunRegisteredServices(); 18 | 19 | private: 20 | static std::vector m_serviceList; 21 | }; 22 | 23 | template 24 | Void Factory::RegisterService() 25 | { 26 | SERVICE_TABLE_ENTRY entry = { Instance::GetName(), ServiceT::Main }; 27 | m_serviceList.push_back(entry); 28 | } 29 | }}} -------------------------------------------------------------------------------- /WinToolsLib/Os/Win32Service/Service.cpp: -------------------------------------------------------------------------------- 1 | #include "Service.h" 2 | 3 | #include "..\..\Exception.h" 4 | 5 | namespace WinToolsLib { namespace Os { namespace Win32Service { namespace Details 6 | { 7 | Void ReportEvent(PCTChar serviceName, PCTChar function, UInt32 error) 8 | { 9 | auto eventSource = ::RegisterEventSource(NULL, serviceName); 10 | 11 | if (NULL != eventSource) 12 | { 13 | auto msg = String::FormatS(Text("%s failed with %d"), function, error); 14 | 15 | PCTChar strings[2]; 16 | strings[0] = serviceName; 17 | strings[1] = msg.GetBuffer(); 18 | 19 | ::ReportEvent(eventSource, // event log handle 20 | EVENTLOG_ERROR_TYPE, // event type 21 | 0, // event category 22 | 0, // event identifier 23 | NULL, // no security identifier 24 | 2, // size of lpszStrings array 25 | 0, // no binary data 26 | strings, // array of strings 27 | NULL); // no binary data 28 | 29 | ::DeregisterEventSource(eventSource); 30 | } 31 | } 32 | }}}} -------------------------------------------------------------------------------- /WinToolsLib/Os/Wmi/WbemClassObject.cpp: -------------------------------------------------------------------------------- 1 | #include "WbemClassObject.h" 2 | #include "..\..\Exception.h" 3 | 4 | namespace WinToolsLib { namespace Os { namespace Wmi 5 | { 6 | WbemClassObject::WbemClassObject(Com::Ptr object) : 7 | m_object(std::move(object)) 8 | { 9 | } 10 | 11 | WbemClassObject::~WbemClassObject() 12 | { 13 | } 14 | 15 | WbemClassObject::WbemClassObject(WbemClassObject&& other) 16 | { 17 | MoveFrom(other); 18 | } 19 | 20 | WbemClassObject& WbemClassObject::operator=(WbemClassObject&& other) 21 | { 22 | MoveFrom(other); 23 | return *this; 24 | } 25 | 26 | Com::Variant WbemClassObject::Get(const WChar* name) 27 | { 28 | Com::Variant value; 29 | 30 | auto hr = m_object->Get(name, 0, &value, nullptr, nullptr); 31 | if (FAILED(hr)) 32 | { 33 | THROW_WIN32_EXCEPTION(hr); 34 | } 35 | 36 | return value; 37 | } 38 | 39 | Void WbemClassObject::Set(const WChar* name, Com::Variant& value) 40 | { 41 | auto hr = m_object->Put(name, 0, &value, 0); 42 | if (FAILED(hr)) 43 | { 44 | THROW_WIN32_EXCEPTION(hr); 45 | } 46 | } 47 | 48 | WbemClassObject WbemClassObject::GetMethodInSignature(const WChar* name) 49 | { 50 | Com::Ptr inSignature; 51 | 52 | auto hr = m_object->GetMethod(name, 0, &inSignature, nullptr); 53 | if (FAILED(hr)) 54 | { 55 | THROW_WIN32_EXCEPTION(hr); 56 | } 57 | 58 | return WbemClassObject(std::move(inSignature)); 59 | } 60 | 61 | WbemClassObject WbemClassObject::SpawnInstance() 62 | { 63 | Com::Ptr instance; 64 | 65 | auto hr = m_object->SpawnInstance(0, &instance); 66 | if (FAILED(hr)) 67 | { 68 | THROW_WIN32_EXCEPTION(hr); 69 | } 70 | 71 | return WbemClassObject(std::move(instance)); 72 | } 73 | 74 | Void WbemClassObject::MoveFrom(WbemClassObject& other) 75 | { 76 | if (this != &other) 77 | { 78 | m_object = std::move(other.m_object); 79 | } 80 | } 81 | 82 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Wmi/WbemClassObject.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\Com\Ptr.h" 4 | #include "..\..\Com\Variant.h" 5 | 6 | #include 7 | #include 8 | #pragma comment(lib, "Wbemuuid.lib") 9 | 10 | namespace WinToolsLib { namespace Os { namespace Wmi 11 | { 12 | class WbemClassObject 13 | { 14 | WbemClassObject(const WbemClassObject& other); // Non-copyable 15 | WbemClassObject& operator=(const WbemClassObject& other); // Non-copyable 16 | 17 | public: 18 | WbemClassObject(Com::Ptr object); 19 | ~WbemClassObject(); 20 | 21 | WbemClassObject(WbemClassObject&& other); 22 | WbemClassObject& operator=(WbemClassObject&& other); 23 | 24 | operator IWbemClassObject*() const; 25 | 26 | Com::Variant Get(const WChar* name); 27 | Void Set(const WChar* name, Com::Variant& value); 28 | 29 | WbemClassObject GetMethodInSignature(const WChar* name); 30 | WbemClassObject SpawnInstance(); 31 | 32 | protected: 33 | Void MoveFrom(WbemClassObject& other); 34 | 35 | private: 36 | Com::Ptr m_object; 37 | }; 38 | 39 | inline WbemClassObject::operator IWbemClassObject*() const 40 | { 41 | return m_object; 42 | } 43 | 44 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Os/Wmi/WbemServices.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\Com\Ptr.h" 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | #pragma comment(lib, "Wbemuuid.lib") 10 | 11 | #include "WbemClassObject.h" 12 | 13 | namespace WinToolsLib { namespace Os { namespace Wmi 14 | { 15 | class WbemServices 16 | { 17 | WbemServices(const WbemServices& other); // Non-copyable 18 | WbemServices& operator=(const WbemServices& other); // Non-copyable 19 | 20 | public: 21 | WbemServices(); 22 | ~WbemServices(); 23 | 24 | WbemServices(WbemServices&& other); 25 | WbemServices& operator=(WbemServices&& other); 26 | 27 | WbemClassObject GetObject(const TChar* objectPath); 28 | 29 | WbemClassObject ExecMethod( 30 | const TChar* objectPath, 31 | const TChar* methodName, 32 | const WbemClassObject& inParams); 33 | 34 | typedef std::function EnumCallback; 35 | Void ExecQuery(const TChar* query, EnumCallback callback); 36 | 37 | protected: 38 | Void MoveFrom(WbemServices& other); 39 | 40 | private: 41 | Com::Ptr m_services; 42 | }; 43 | 44 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Privilege.cpp: -------------------------------------------------------------------------------- 1 | #include "Privilege.h" 2 | #include "Handles\Handle.h" 3 | #include "Exception.h" 4 | #include 5 | 6 | namespace WinToolsLib 7 | { 8 | using namespace Handles; 9 | 10 | Void Privilege::Set(PTChar pszPrivilege, Bool enable) 11 | { 12 | HANDLE tokenHandle = NULL; 13 | BOOL success = ::OpenProcessToken( 14 | ::GetCurrentProcess(), 15 | TOKEN_ADJUST_PRIVILEGES, 16 | &tokenHandle); 17 | 18 | Handle handle = tokenHandle; 19 | if (!success) 20 | { 21 | THROW_LAST_ERROR_EXCEPTION(); 22 | } 23 | 24 | LUID luid = {0}; 25 | success = ::LookupPrivilegeValue( 26 | NULL, 27 | pszPrivilege, 28 | &luid); 29 | 30 | if (!success) 31 | { 32 | THROW_LAST_ERROR_EXCEPTION(); 33 | } 34 | 35 | TOKEN_PRIVILEGES tp = {0}; 36 | tp.PrivilegeCount = 1; 37 | tp.Privileges[0].Luid = luid; 38 | tp.Privileges[0].Attributes = (enable) ? SE_PRIVILEGE_ENABLED : 0; 39 | 40 | success = ::AdjustTokenPrivileges( 41 | handle, 42 | FALSE, 43 | &tp, 44 | sizeof(tp), 45 | NULL, 46 | NULL); 47 | 48 | if (!success) 49 | { 50 | THROW_LAST_ERROR_EXCEPTION(); 51 | } 52 | } 53 | 54 | Void DebugPrivilege::Enable() 55 | { 56 | Privilege::Set(SE_DEBUG_NAME, True); 57 | } 58 | 59 | Void DebugPrivilege::Disable() 60 | { 61 | Privilege::Set(SE_DEBUG_NAME, False); 62 | } 63 | } -------------------------------------------------------------------------------- /WinToolsLib/Privilege.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | class Privilege 7 | { 8 | public: 9 | static Void Set(PTChar pszPrivilege, Bool enable); 10 | }; 11 | 12 | class DebugPrivilege : protected Privilege 13 | { 14 | public: 15 | static Void Enable(); 16 | static Void Disable(); 17 | }; 18 | } 19 | -------------------------------------------------------------------------------- /WinToolsLib/Process/Event.cpp: -------------------------------------------------------------------------------- 1 | #include "Event.h" 2 | #include "..\Exception.h" 3 | #include 4 | 5 | namespace WinToolsLib { namespace Process 6 | { 7 | Event::Event() 8 | { 9 | } 10 | 11 | Event::Event(Event&& other) 12 | : m_handle(std::move(other.m_handle)) 13 | { 14 | } 15 | 16 | Event::~Event() 17 | { 18 | } 19 | 20 | Void Event::Create(Bool manualReset, Bool initialState, const TChar* name) 21 | { 22 | m_handle = ::CreateEvent( 23 | NULL, 24 | manualReset, 25 | initialState, 26 | name); 27 | 28 | if (!m_handle.IsValid()) 29 | { 30 | THROW_LAST_ERROR_EXCEPTION(); 31 | } 32 | } 33 | 34 | Void Event::Open(Bool modifyState, Bool inheritHandle, const TChar* name) 35 | { 36 | m_handle = ::OpenEvent( 37 | modifyState ? EVENT_MODIFY_STATE : SYNCHRONIZE, 38 | inheritHandle, 39 | name); 40 | 41 | if (!m_handle.IsValid()) 42 | { 43 | THROW_LAST_ERROR_EXCEPTION(); 44 | } 45 | } 46 | 47 | Bool Event::IsValid() const 48 | { 49 | return m_handle.IsValid(); 50 | } 51 | 52 | Void Event::Set() 53 | { 54 | if (!m_handle.IsValid()) 55 | { 56 | THROW_INVALID_HANDLE_EXCEPTION(); 57 | } 58 | 59 | BOOL success = ::SetEvent(m_handle); 60 | if (!success) 61 | { 62 | THROW_LAST_ERROR_EXCEPTION(); 63 | } 64 | } 65 | 66 | Void Event::Reset() 67 | { 68 | if (!m_handle.IsValid()) 69 | { 70 | THROW_INVALID_HANDLE_EXCEPTION(); 71 | } 72 | 73 | BOOL success = ::ResetEvent(m_handle); 74 | if (!success) 75 | { 76 | THROW_LAST_ERROR_EXCEPTION(); 77 | } 78 | } 79 | 80 | Void Event::Wait() const 81 | { 82 | Wait(INFINITE); 83 | } 84 | 85 | Void Event::Wait(UInt32 milliseconds) const 86 | { 87 | if (!m_handle.IsValid()) 88 | { 89 | THROW_INVALID_HANDLE_EXCEPTION(); 90 | } 91 | 92 | DWORD result = ::WaitForSingleObject(m_handle, milliseconds); 93 | if (WAIT_FAILED == result) 94 | { 95 | THROW_LAST_ERROR_EXCEPTION(); 96 | } 97 | } 98 | } } -------------------------------------------------------------------------------- /WinToolsLib/Process/Event.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Handles\Handle.h" 4 | #include "IWaitable.h" 5 | 6 | namespace WinToolsLib { namespace Process 7 | { 8 | using namespace Handles; 9 | 10 | class Event : public IWaitable 11 | { 12 | public: 13 | Event(); 14 | Event(Event&& other); 15 | ~Event(); 16 | 17 | Void Create(Bool manualReset, Bool initialState, const TChar* name = nullptr); 18 | Void Open(Bool modifyState, Bool inheritHandle, const TChar* name); 19 | 20 | Bool IsValid() const; 21 | Void Set(); 22 | Void Reset(); 23 | 24 | Void Wait() const override; 25 | Void Wait(UInt32 milliseconds) const override; 26 | 27 | operator HANDLE() const; 28 | 29 | private: 30 | Handle m_handle; 31 | }; 32 | 33 | inline Event::operator HANDLE() const 34 | { 35 | return m_handle; 36 | } 37 | }} 38 | -------------------------------------------------------------------------------- /WinToolsLib/Process/IWaitable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | 4 | namespace WinToolsLib { namespace Process 5 | { 6 | class IWaitable 7 | { 8 | public: 9 | virtual ~IWaitable() {} 10 | 11 | virtual Void Wait() const = 0; 12 | virtual Void Wait(UInt32 milliseconds) const = 0; 13 | }; 14 | }} 15 | -------------------------------------------------------------------------------- /WinToolsLib/Process/Mutex.cpp: -------------------------------------------------------------------------------- 1 | #include "Mutex.h" 2 | #include "..\Exception.h" 3 | #include 4 | 5 | namespace WinToolsLib { namespace Process 6 | { 7 | Mutex::Mutex() 8 | : m_isExist(False) 9 | { 10 | } 11 | 12 | Mutex::Mutex(Mutex&& other) 13 | : m_handle(std::move(other.m_handle)) 14 | , m_isExist(std::move(other.m_isExist)) 15 | { 16 | } 17 | 18 | Mutex::~Mutex() 19 | { 20 | } 21 | 22 | Void Mutex::Create(Bool initialState, const TChar* name) 23 | { 24 | m_handle = ::CreateMutex( 25 | NULL, 26 | initialState, 27 | name); 28 | 29 | if (!m_handle.IsValid()) 30 | { 31 | THROW_LAST_ERROR_EXCEPTION(); 32 | } 33 | 34 | m_isExist = ::GetLastError() == ERROR_ALREADY_EXISTS; 35 | } 36 | 37 | Void Mutex::Open(Bool modifyState, Bool inheritHandle, const TChar* name) 38 | { 39 | m_handle = ::OpenMutex( 40 | modifyState ? MUTEX_MODIFY_STATE : SYNCHRONIZE, 41 | inheritHandle, 42 | name); 43 | 44 | if (!m_handle.IsValid()) 45 | { 46 | THROW_LAST_ERROR_EXCEPTION(); 47 | } 48 | m_isExist = True; 49 | } 50 | 51 | Bool Mutex::IsValid() const 52 | { 53 | return m_handle.IsValid(); 54 | } 55 | 56 | Void Mutex::Set() 57 | { 58 | if (!m_handle.IsValid()) 59 | { 60 | THROW_INVALID_HANDLE_EXCEPTION(); 61 | } 62 | 63 | const auto res = ::WaitForSingleObject(m_handle, INFINITE); 64 | if (res != WAIT_OBJECT_0) 65 | { 66 | THROW_LAST_ERROR_EXCEPTION(); 67 | } 68 | } 69 | 70 | Void Mutex::Release() 71 | { 72 | if (!m_handle.IsValid()) 73 | { 74 | THROW_INVALID_HANDLE_EXCEPTION(); 75 | } 76 | 77 | BOOL success = ::ReleaseMutex(m_handle); 78 | if (!success) 79 | { 80 | THROW_LAST_ERROR_EXCEPTION(); 81 | } 82 | } 83 | 84 | Void Mutex::Wait() const 85 | { 86 | Wait(INFINITE); 87 | } 88 | 89 | Void Mutex::Wait(UInt32 milliseconds) const 90 | { 91 | if (!m_handle.IsValid()) 92 | { 93 | THROW_INVALID_HANDLE_EXCEPTION(); 94 | } 95 | 96 | DWORD result = ::WaitForSingleObject(m_handle, milliseconds); 97 | if (WAIT_FAILED == result) 98 | { 99 | THROW_LAST_ERROR_EXCEPTION(); 100 | } 101 | } 102 | }} -------------------------------------------------------------------------------- /WinToolsLib/Process/Mutex.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Handles\Handle.h" 4 | #include "IWaitable.h" 5 | 6 | namespace WinToolsLib { namespace Process 7 | { 8 | using namespace Handles; 9 | 10 | class Mutex : public IWaitable 11 | { 12 | public: 13 | Mutex(); 14 | Mutex(Mutex&& other); 15 | ~Mutex(); 16 | 17 | Void Create(Bool initialState, const TChar* name = nullptr); 18 | Void Open(Bool modifyState, Bool inheritHandle, const TChar* name); 19 | 20 | inline Bool IsExistBefore() const; 21 | Bool IsValid() const; 22 | Void Set(); 23 | Void Release(); 24 | 25 | Void Wait() const override; 26 | Void Wait(UInt32 milliseconds) const override; 27 | 28 | operator HANDLE() const; 29 | 30 | private: 31 | Handle m_handle; 32 | Bool m_isExist; 33 | }; 34 | 35 | inline Mutex::operator HANDLE() const 36 | { 37 | return m_handle; 38 | } 39 | 40 | Bool Mutex::IsExistBefore() const 41 | { 42 | return m_isExist; 43 | } 44 | }} 45 | 46 | 47 | -------------------------------------------------------------------------------- /WinToolsLib/Process/Thread.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\Exception.h" 4 | #include "..\Handles\Handle.h" 5 | #include "IWaitable.h" 6 | #include 7 | #include 8 | 9 | namespace WinToolsLib { namespace Process 10 | { 11 | class Thread; 12 | typedef std::list ThreadList; 13 | 14 | using namespace Handles; 15 | 16 | class Thread : public IWaitable 17 | { 18 | public: 19 | explicit Thread(UInt32 id); 20 | Thread(UInt32 id, Handle&& handle); 21 | Thread(UInt32 id, const Handle& handle); 22 | Thread(Handle&& handle); 23 | Thread(const Handle& handle); 24 | Thread(Thread&& other); 25 | Thread(const Thread& other); 26 | 27 | Thread& operator=(Thread&& other); 28 | Thread& operator=(const Thread& other); 29 | 30 | Void Wait() const override; 31 | Void Wait(UInt32 milliseconds) const override; 32 | Void Suspend(); 33 | Void Resume(); 34 | 35 | template 36 | static Thread Create(Callback callback, Bool suspended = False); 37 | 38 | static Void Sleep(UInt32 milliseconds); 39 | 40 | static ThreadList GetList(UInt32 processId); 41 | 42 | protected: 43 | Void MoveFrom(Thread& other); 44 | Void CopyFrom(const Thread& other); 45 | 46 | private: 47 | UInt32 m_id; 48 | Handle m_handle; 49 | 50 | static const UInt32 m_invalidId = -1; 51 | }; 52 | 53 | template 54 | Thread Thread::Create(Callback callback, Bool suspended) 55 | { 56 | struct ThreadImpl 57 | { 58 | static DWORD WINAPI EntryPoint(LPVOID pParam) 59 | { 60 | StructuredException::EnableInThisThread(); 61 | std::unique_ptr callbackPtr((Callback*)pParam); 62 | (*callbackPtr)(); 63 | return 0; 64 | } 65 | }; 66 | 67 | Handle handle = ::CreateThread( 68 | NULL, 69 | 0, 70 | ThreadImpl::EntryPoint, 71 | new Callback(callback), 72 | (suspended) ? CREATE_SUSPENDED : 0, 73 | NULL); 74 | 75 | if (handle.IsNull()) 76 | { 77 | THROW_LAST_ERROR_EXCEPTION(); 78 | } 79 | 80 | return Thread(std::move(handle)); 81 | } 82 | }} 83 | -------------------------------------------------------------------------------- /WinToolsLib/ProcessMemoryStream.cpp: -------------------------------------------------------------------------------- 1 | #include "ProcessMemoryStream.h" 2 | #include "CommonMemoryStream.h" 3 | #include "Exception.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | ProcessMemoryStream::ProcessMemoryStream(UInt32 processId) 8 | { 9 | m_hProcess = ::OpenProcess( 10 | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, 11 | FALSE, 12 | processId); 13 | 14 | if (!m_hProcess.IsValid()) 15 | { 16 | THROW_LAST_ERROR_EXCEPTION(); 17 | } 18 | } 19 | 20 | Void ProcessMemoryStream::Read(PByte buffer, SizeT bytes) 21 | { 22 | SIZE_T bytesRead = 0; 23 | 24 | BOOL success = ::ReadProcessMemory( 25 | m_hProcess, 26 | (LPCVOID)m_position, 27 | buffer, 28 | bytes, 29 | &bytesRead); 30 | 31 | if (!success) 32 | { 33 | THROW_LAST_ERROR_EXCEPTION(); 34 | } 35 | 36 | m_position += bytes; 37 | } 38 | 39 | Void ProcessMemoryStream::Write(PByte buffer, SizeT bytes) 40 | { 41 | DWORD oldProtect = 0; 42 | 43 | BOOL success = ::VirtualProtectEx( 44 | m_hProcess, 45 | (LPVOID)m_position, 46 | bytes, 47 | PAGE_READWRITE, 48 | &oldProtect); 49 | 50 | if (!success) 51 | { 52 | THROW_LAST_ERROR_EXCEPTION(); 53 | } 54 | 55 | SIZE_T bytesWritten = 0; 56 | 57 | success = ::WriteProcessMemory( 58 | m_hProcess, 59 | (LPVOID)m_position, 60 | buffer, 61 | bytes, 62 | &bytesWritten); 63 | 64 | if (!success) 65 | { 66 | THROW_LAST_ERROR_EXCEPTION(); 67 | } 68 | 69 | m_position += bytes; 70 | 71 | success = ::VirtualProtectEx( 72 | m_hProcess, 73 | (LPVOID)m_position, 74 | bytes, 75 | oldProtect, 76 | &oldProtect); 77 | 78 | if (!success) 79 | { 80 | THROW_LAST_ERROR_EXCEPTION(); 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /WinToolsLib/ProcessMemoryStream.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "IStream.h" 4 | #include "Handles\Handle.h" 5 | #include "CommonMemoryStream.h" 6 | 7 | namespace WinToolsLib 8 | { 9 | using namespace Handles; 10 | 11 | class ProcessMemoryStream : public CommonMemoryStream 12 | { 13 | public: 14 | explicit ProcessMemoryStream(UInt32 processId); 15 | 16 | Void Read(PByte buffer, SizeT bytes) override; 17 | Void Write(PByte buffer, SizeT bytes) override; 18 | 19 | protected: 20 | Handle m_hProcess; 21 | }; 22 | } 23 | -------------------------------------------------------------------------------- /WinToolsLib/Registry/Key.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #include "..\Types.h" 9 | #include "..\String.h" 10 | #include "..\Buffer.h" 11 | #include "..\Handles\RegistryHandle.h" 12 | #include "..\Process\Event.h" 13 | 14 | namespace WinToolsLib 15 | { 16 | namespace Registry 17 | { 18 | using namespace Handles; 19 | using namespace Process; 20 | 21 | enum class ValueType 22 | { 23 | String = REG_SZ, 24 | MultiString = REG_MULTI_SZ, 25 | ExpandString = REG_EXPAND_SZ, 26 | DoubleWord = REG_DWORD, 27 | QuadWord = REG_QWORD, 28 | Binary = REG_BINARY 29 | }; 30 | 31 | class Key 32 | { 33 | Key(const Key& other); 34 | Key& operator=(const Key& other); 35 | 36 | public: 37 | Key(HKEY hKey); 38 | Key(Key&& other); 39 | 40 | Key& operator=(Key&& other); 41 | 42 | typedef std::function EnumNameCallback; 43 | typedef std::function EnumValueCallback; 44 | typedef std::function EnumSubKeyCallback; 45 | 46 | Void EnumName(const EnumNameCallback& callback); 47 | Void EnumValue(const EnumValueCallback& callback); 48 | Void EnumSubKey(const EnumSubKeyCallback& callback, UInt32 accessRights); 49 | 50 | Bool ValueExist(const TChar* valueName) const; 51 | Bool SetValue(const TChar* valueName, ValueType valueType, const Byte* data, UInt32 size); 52 | Bool GetValue(const TChar* valueName, ValueType valueType, Buffer& data) const; 53 | Bool DeleteValue(const TChar* valueName); 54 | 55 | operator HKEY() const; 56 | 57 | static PWChar RootToString(HKEY hRoot); 58 | static PWChar RootToFullString(HKEY hRoot); 59 | static HKEY StringToRootHkey(const String& rootString); 60 | 61 | static Key Open(HKEY hKey, const TChar* subKey, UInt32 accessRights); 62 | static Key Create(HKEY hKey, const TChar* subKey, UInt32 accessRights); 63 | static Bool Delete(HKEY hKey, const TChar* subKey, Bool recursive = False); 64 | static Bool Exists(HKEY hKey, const TChar* subKey); 65 | 66 | Event CreateChangeEvent(UInt32 filter); 67 | 68 | private: 69 | RegistryHandle m_handle; 70 | }; 71 | 72 | inline Key::operator HKEY() const 73 | { 74 | return m_handle; 75 | } 76 | } 77 | } 78 | 79 | -------------------------------------------------------------------------------- /WinToolsLib/Registry/RegEdit.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #include "..\Types.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | namespace Registry 9 | { 10 | class RegEdit 11 | { 12 | public: 13 | static Bool JumpTo(HKEY root, UInt32 flags, const TChar* path); 14 | }; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/Action.cpp: -------------------------------------------------------------------------------- 1 | #include "Action.h" 2 | 3 | #include "..\..\Exception.h" 4 | 5 | #include 6 | #pragma comment(lib, "comsuppw.lib") 7 | 8 | namespace WinToolsLib { namespace Scheduler { namespace Vista 9 | { 10 | Action::Action() 11 | { 12 | } 13 | 14 | Action::Action(Com::Ptr&& action) 15 | { 16 | m_action = std::move(action); 17 | } 18 | 19 | Action::Action(Action&& other) 20 | { 21 | MoveFrom(other); 22 | } 23 | 24 | Action::~Action() 25 | { 26 | } 27 | 28 | Action& Action::operator=(Action&& other) 29 | { 30 | MoveFrom(other); 31 | return *this; 32 | } 33 | 34 | ActionType Action::GetType() const 35 | { 36 | TASK_ACTION_TYPE type; 37 | 38 | auto hr = m_action->get_Type(&type); 39 | if (FAILED(hr)) 40 | { 41 | THROW_WIN32_EXCEPTION(hr); 42 | } 43 | 44 | return static_cast(type); 45 | } 46 | 47 | ExecAction Action::QueryExecAction() const 48 | { 49 | Com::Ptr action; 50 | 51 | auto hr = m_action->QueryInterface(&action); 52 | if (FAILED(hr)) 53 | { 54 | THROW_WIN32_EXCEPTION(hr); 55 | } 56 | 57 | return ExecAction(std::move(action)); 58 | } 59 | 60 | Void Action::MoveFrom(Action& other) 61 | { 62 | if (this != &other) 63 | { 64 | m_action = std::move(other.m_action); 65 | } 66 | } 67 | 68 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/Action.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "taskschd.lib") 8 | 9 | #include "ExecAction.h" 10 | 11 | namespace WinToolsLib { namespace Scheduler { namespace Vista 12 | { 13 | enum class ActionType 14 | { 15 | Exec = TASK_ACTION_EXEC 16 | }; 17 | 18 | class Action 19 | { 20 | Action(const Action& other); // Non-copyable 21 | Action& operator=(const Action& other); // Non-copyable 22 | 23 | public: 24 | Action(); 25 | Action(Com::Ptr&& action); 26 | Action(Action&& other); 27 | ~Action(); 28 | 29 | Action& operator=(Action&& other); 30 | 31 | ActionType GetType() const; 32 | ExecAction QueryExecAction() const; 33 | 34 | protected: 35 | Void MoveFrom(Action& other); 36 | 37 | private: 38 | Com::Ptr m_action; 39 | }; 40 | 41 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/DailyTrigger.cpp: -------------------------------------------------------------------------------- 1 | #include "DailyTrigger.h" 2 | 3 | #include "..\..\Exception.h" 4 | 5 | #include 6 | #pragma comment(lib, "comsuppw.lib") 7 | 8 | namespace WinToolsLib { namespace Scheduler { namespace Vista 9 | { 10 | DailyTrigger::DailyTrigger(Com::Ptr&& trigger) 11 | { 12 | m_trigger = std::move(trigger); 13 | } 14 | 15 | DailyTrigger::DailyTrigger(DailyTrigger&& other) 16 | { 17 | MoveFrom(other); 18 | } 19 | 20 | DailyTrigger::~DailyTrigger() 21 | { 22 | } 23 | 24 | DailyTrigger& DailyTrigger::operator=(DailyTrigger&& other) 25 | { 26 | MoveFrom(other); 27 | return *this; 28 | } 29 | 30 | Int16 DailyTrigger::GetDaysInterval() const 31 | { 32 | Int16 days = 0; 33 | 34 | auto hr = m_trigger->get_DaysInterval(&days); 35 | if (FAILED(hr)) 36 | { 37 | THROW_WIN32_EXCEPTION(hr); 38 | } 39 | 40 | return days; 41 | } 42 | 43 | Void DailyTrigger::SetDaysInterval(Int16 days) 44 | { 45 | auto hr = m_trigger->put_DaysInterval(days); 46 | if (FAILED(hr)) 47 | { 48 | THROW_WIN32_EXCEPTION(hr); 49 | } 50 | } 51 | 52 | Void DailyTrigger::MoveFrom(DailyTrigger& other) 53 | { 54 | if (this != &other) 55 | { 56 | m_trigger = std::move(other.m_trigger); 57 | } 58 | } 59 | 60 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/DailyTrigger.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "taskschd.lib") 8 | 9 | namespace WinToolsLib { namespace Scheduler { namespace Vista 10 | { 11 | class DailyTrigger 12 | { 13 | DailyTrigger(const DailyTrigger& other); // Non-copyable 14 | DailyTrigger& operator=(const DailyTrigger& other); // Non-copyable 15 | 16 | public: 17 | DailyTrigger(Com::Ptr&& trigger); 18 | DailyTrigger(DailyTrigger&& other); 19 | ~DailyTrigger(); 20 | 21 | DailyTrigger& operator=(DailyTrigger&& other); 22 | 23 | Int16 GetDaysInterval() const; 24 | Void SetDaysInterval(Int16 days); 25 | 26 | protected: 27 | Void MoveFrom(DailyTrigger& other); 28 | 29 | private: 30 | Com::Ptr m_trigger; 31 | }; 32 | 33 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/ExecAction.cpp: -------------------------------------------------------------------------------- 1 | #include "ExecAction.h" 2 | 3 | #include "..\..\Exception.h" 4 | 5 | #include 6 | #pragma comment(lib, "comsuppw.lib") 7 | 8 | namespace WinToolsLib { namespace Scheduler { namespace Vista 9 | { 10 | ExecAction::ExecAction(Com::Ptr&& action) 11 | { 12 | m_action = std::move(action); 13 | } 14 | 15 | ExecAction::ExecAction(ExecAction&& other) 16 | { 17 | MoveFrom(other); 18 | } 19 | 20 | ExecAction::~ExecAction() 21 | { 22 | } 23 | 24 | ExecAction& ExecAction::operator=(ExecAction&& other) 25 | { 26 | MoveFrom(other); 27 | return *this; 28 | } 29 | 30 | StringW ExecAction::GetPath() const 31 | { 32 | _bstr_t path; 33 | 34 | auto hr = m_action->get_Path(path.GetAddress()); 35 | if (FAILED(hr)) 36 | { 37 | THROW_WIN32_EXCEPTION(hr); 38 | } 39 | 40 | return StringW(path); 41 | } 42 | 43 | Void ExecAction::SetPath(const WChar* path) 44 | { 45 | auto hr = m_action->put_Path(_bstr_t(path)); 46 | if (FAILED(hr)) 47 | { 48 | THROW_WIN32_EXCEPTION(hr); 49 | } 50 | } 51 | 52 | StringW ExecAction::GetArguments() const 53 | { 54 | _bstr_t arguments; 55 | 56 | auto hr = m_action->get_Arguments(arguments.GetAddress()); 57 | if (FAILED(hr)) 58 | { 59 | THROW_WIN32_EXCEPTION(hr); 60 | } 61 | 62 | return StringW(arguments); 63 | } 64 | 65 | Void ExecAction::SetArguments(const WChar* arguments) 66 | { 67 | auto hr = m_action->put_Arguments(_bstr_t(arguments)); 68 | if (FAILED(hr)) 69 | { 70 | THROW_WIN32_EXCEPTION(hr); 71 | } 72 | } 73 | 74 | Void ExecAction::MoveFrom(ExecAction& other) 75 | { 76 | if (this != &other) 77 | { 78 | m_action = std::move(other.m_action); 79 | } 80 | } 81 | 82 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/ExecAction.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "taskschd.lib") 8 | 9 | namespace WinToolsLib { namespace Scheduler { namespace Vista 10 | { 11 | class ExecAction 12 | { 13 | ExecAction(const ExecAction& other); // Non-copyable 14 | ExecAction& operator=(const ExecAction& other); // Non-copyable 15 | 16 | public: 17 | ExecAction(Com::Ptr&& action); 18 | ExecAction(ExecAction&& other); 19 | ~ExecAction(); 20 | 21 | ExecAction& operator=(ExecAction&& other); 22 | 23 | StringW GetPath() const; 24 | Void SetPath(const WChar* path); 25 | 26 | StringW GetArguments() const; 27 | Void SetArguments(const WChar* arguments); 28 | 29 | protected: 30 | Void MoveFrom(ExecAction& other); 31 | 32 | private: 33 | Com::Ptr m_action; 34 | }; 35 | 36 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/Principal.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "taskschd.lib") 8 | 9 | namespace WinToolsLib { namespace Scheduler { namespace Vista 10 | { 11 | enum class RunLevel 12 | { 13 | LeastPrivileges = TASK_RUNLEVEL_LUA, 14 | HighestPrivileges = TASK_RUNLEVEL_HIGHEST 15 | }; 16 | 17 | enum class LogonType 18 | { 19 | None = TASK_LOGON_NONE, 20 | Password = TASK_LOGON_PASSWORD, 21 | ServiceForUser = TASK_LOGON_S4U, 22 | Interactive = TASK_LOGON_INTERACTIVE_TOKEN, 23 | Group = TASK_LOGON_GROUP, 24 | Service = TASK_LOGON_SERVICE_ACCOUNT, 25 | InteractiveOrPassword = TASK_LOGON_INTERACTIVE_TOKEN_OR_PASSWORD 26 | }; 27 | 28 | class Principal 29 | { 30 | Principal(const Principal& other); // Non-copyable 31 | Principal& operator=(const Principal& other); // Non-copyable 32 | 33 | public: 34 | Principal(); 35 | Principal(Com::Ptr&& principal); 36 | Principal(Principal&& other); 37 | ~Principal(); 38 | 39 | Principal& operator=(Principal&& other); 40 | operator IPrincipal*() const; 41 | 42 | RunLevel GetRunLevel() const; 43 | Void SetRunLevel(RunLevel runLevel); 44 | 45 | LogonType GetLogonType() const; 46 | Void SetLogonType(LogonType logonType); 47 | 48 | StringW GetUserId() const; 49 | Void SetUserId(const WChar* userId); 50 | 51 | StringW GetGroupId() const; 52 | Void SetGroupId(const WChar* groupId); 53 | 54 | StringW GetDisplayName() const; 55 | Void SetDisplayName(const WChar* displayName); 56 | 57 | protected: 58 | Void MoveFrom(Principal& other); 59 | 60 | private: 61 | Com::Ptr m_principal; 62 | }; 63 | 64 | inline Principal::operator IPrincipal*() const 65 | { 66 | return m_principal.operator IPrincipal*(); 67 | } 68 | 69 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/RegisteredTask.cpp: -------------------------------------------------------------------------------- 1 | #include "RegisteredTask.h" 2 | 3 | #include "..\..\Exception.h" 4 | 5 | #include 6 | #pragma comment(lib, "comsuppw.lib") 7 | 8 | namespace WinToolsLib { namespace Scheduler { namespace Vista 9 | { 10 | RegisteredTask::RegisteredTask(Com::Ptr&& task) 11 | { 12 | m_task = std::move(task); 13 | } 14 | 15 | RegisteredTask::RegisteredTask(RegisteredTask&& other) 16 | { 17 | MoveFrom(other); 18 | } 19 | 20 | RegisteredTask::~RegisteredTask() 21 | { 22 | } 23 | 24 | RegisteredTask& RegisteredTask::operator=(RegisteredTask&& other) 25 | { 26 | MoveFrom(other); 27 | return *this; 28 | } 29 | 30 | TaskDefinition RegisteredTask::GetDefinition() const 31 | { 32 | Com::Ptr definition; 33 | 34 | auto hr = m_task->get_Definition(&definition); 35 | if (FAILED(hr)) 36 | { 37 | THROW_WIN32_EXCEPTION(hr); 38 | } 39 | 40 | return TaskDefinition(std::move(definition)); 41 | } 42 | 43 | Void RegisteredTask::Run() const 44 | { 45 | auto hr = m_task->Run(_variant_t(), nullptr); 46 | if (FAILED(hr)) 47 | { 48 | THROW_WIN32_EXCEPTION(hr); 49 | } 50 | } 51 | 52 | Void RegisteredTask::Run(const WChar* param) const 53 | { 54 | auto hr = m_task->Run(_variant_t(_bstr_t(param)), nullptr); 55 | if (FAILED(hr)) 56 | { 57 | THROW_WIN32_EXCEPTION(hr); 58 | } 59 | } 60 | 61 | Void RegisteredTask::MoveFrom(RegisteredTask& other) 62 | { 63 | if (this != &other) 64 | { 65 | m_task = std::move(other.m_task); 66 | } 67 | } 68 | 69 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/RegisteredTask.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\Com\Ptr.h" 4 | 5 | #include 6 | #pragma comment(lib, "taskschd.lib") 7 | 8 | #include "TaskDefinition.h" 9 | 10 | namespace WinToolsLib { namespace Scheduler { namespace Vista 11 | { 12 | class RegisteredTask 13 | { 14 | RegisteredTask(const RegisteredTask& other); // Non-copyable 15 | RegisteredTask& operator=(const RegisteredTask& other); // Non-copyable 16 | 17 | public: 18 | RegisteredTask(Com::Ptr&& task); 19 | RegisteredTask(RegisteredTask&& other); 20 | ~RegisteredTask(); 21 | 22 | RegisteredTask& operator=(RegisteredTask&& other); 23 | TaskDefinition GetDefinition() const; 24 | 25 | Void Run() const; 26 | Void Run(const WChar* param) const; 27 | 28 | protected: 29 | Void MoveFrom(RegisteredTask& other); 30 | 31 | private: 32 | Com::Ptr m_task; 33 | }; 34 | 35 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/TaskDefinition.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "taskschd.lib") 8 | 9 | #include "Action.h" 10 | #include "Principal.h" 11 | #include "Trigger.h" 12 | #include "TaskSettings.h" 13 | 14 | namespace WinToolsLib { namespace Scheduler { namespace Vista 15 | { 16 | class TaskDefinition 17 | { 18 | TaskDefinition(const TaskDefinition& other); // Non-copyable 19 | TaskDefinition& operator=(const TaskDefinition& other); // Non-copyable 20 | 21 | public: 22 | TaskDefinition(Com::Ptr&& task); 23 | TaskDefinition(TaskDefinition&& other); 24 | ~TaskDefinition(); 25 | 26 | TaskDefinition& operator=(TaskDefinition&& other); 27 | operator ITaskDefinition*() const; 28 | 29 | StringW GetAuthor() const; 30 | Void SetAuthor(const WChar* author); 31 | 32 | Action GetAction() const; 33 | Action CreateAction(ActionType type); 34 | 35 | Trigger GetTrigger() const; 36 | Trigger CreateTrigger(TriggerType type); 37 | 38 | TaskSettings GetSettings() const; 39 | Void SetSettings(ITaskSettings* settings); 40 | 41 | Principal GetPrincipal() const; 42 | Void SetPrincipal(IPrincipal* principal); 43 | 44 | protected: 45 | Void MoveFrom(TaskDefinition& other); 46 | 47 | private: 48 | Com::Ptr m_task; 49 | }; 50 | 51 | inline TaskDefinition::operator ITaskDefinition*() const 52 | { 53 | return m_task.operator ITaskDefinition*(); 54 | } 55 | 56 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/TaskFolder.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\Com\Ptr.h" 4 | 5 | #include 6 | #pragma comment(lib, "taskschd.lib") 7 | 8 | #include 9 | 10 | #include "RegisteredTask.h" 11 | 12 | namespace WinToolsLib { namespace Scheduler { namespace Vista 13 | { 14 | class TaskFolder 15 | { 16 | TaskFolder(const TaskFolder& other); // Non-copyable 17 | TaskFolder& operator=(const TaskFolder& other); // Non-copyable 18 | 19 | public: 20 | TaskFolder(Com::Ptr&& folder); 21 | TaskFolder(TaskFolder&& other); 22 | ~TaskFolder(); 23 | 24 | TaskFolder& operator=(TaskFolder&& other); 25 | 26 | TaskFolder GetFolder(const WChar* path, Bool createIfNotExist = False); 27 | 28 | RegisteredTask GetTask(const WChar* path); 29 | inline RegisteredTask RegisterTask(const WChar* path, ITaskDefinition* definition); 30 | inline RegisteredTask UpdateTask(const WChar* path, ITaskDefinition* definition); 31 | 32 | Void DeleteTask(const WChar* name); 33 | Void DeleteFolder(const WChar* path); 34 | 35 | typedef std::function EnumCallback; 36 | Void EnumTasks(EnumCallback callback); 37 | 38 | protected: 39 | Void MoveFrom(TaskFolder& other); 40 | 41 | private: 42 | RegisteredTask RegisterTaskImpl(const WChar* path, ITaskDefinition* definition, LONG flags); 43 | 44 | private: 45 | Com::Ptr m_folder; 46 | }; 47 | 48 | RegisteredTask TaskFolder::RegisterTask(const WChar* path, ITaskDefinition* definition) 49 | { 50 | return RegisterTaskImpl(path, definition, TASK_CREATE); 51 | } 52 | 53 | RegisteredTask TaskFolder::UpdateTask(const WChar* path, ITaskDefinition* definition) 54 | { 55 | return RegisterTaskImpl(path, definition, TASK_UPDATE); 56 | } 57 | 58 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/TaskService.cpp: -------------------------------------------------------------------------------- 1 | #include "TaskService.h" 2 | 3 | #include "..\..\Exception.h" 4 | #include "..\..\Com\Variant.h" 5 | 6 | #include 7 | #pragma comment(lib, "comsuppw.lib") 8 | 9 | namespace WinToolsLib { namespace Scheduler { namespace Vista 10 | { 11 | TaskService::TaskService() 12 | { 13 | auto hr = ::CoCreateInstance( 14 | CLSID_TaskScheduler, 15 | nullptr, 16 | CLSCTX_INPROC_SERVER, 17 | IID_ITaskService, 18 | (PVoid*)&m_service); 19 | 20 | if (FAILED(hr)) 21 | { 22 | THROW_WIN32_EXCEPTION(hr); 23 | } 24 | 25 | hr = m_service->Connect( 26 | Com::Variant(), 27 | Com::Variant(), 28 | Com::Variant(), 29 | Com::Variant()); 30 | 31 | if (FAILED(hr)) 32 | { 33 | THROW_WIN32_EXCEPTION(hr); 34 | } 35 | } 36 | 37 | TaskService::~TaskService() 38 | { 39 | } 40 | 41 | TaskDefinition TaskService::CreateTask() 42 | { 43 | Com::Ptr task; 44 | 45 | auto hr = m_service->NewTask(0, &task); 46 | if (FAILED(hr)) 47 | { 48 | THROW_WIN32_EXCEPTION(hr); 49 | } 50 | 51 | return TaskDefinition(std::move(task)); 52 | } 53 | 54 | TaskFolder TaskService::GetFolder(const WChar* path) const 55 | { 56 | Com::Ptr folder; 57 | 58 | auto hr = m_service->GetFolder(_bstr_t(path), &folder); 59 | if (FAILED(hr)) 60 | { 61 | THROW_WIN32_EXCEPTION(hr); 62 | } 63 | 64 | return TaskFolder(std::move(folder)); 65 | } 66 | 67 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/TaskService.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\Com\Ptr.h" 4 | 5 | #include 6 | #pragma comment(lib, "taskschd.lib") 7 | 8 | #include "TaskDefinition.h" 9 | #include "TaskFolder.h" 10 | 11 | namespace WinToolsLib { namespace Scheduler { namespace Vista 12 | { 13 | class TaskService 14 | { 15 | public: 16 | TaskService(); 17 | ~TaskService(); 18 | 19 | TaskDefinition CreateTask(); 20 | TaskFolder GetFolder(const WChar* path) const; 21 | 22 | private: 23 | Com::Ptr m_service; 24 | }; 25 | 26 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/TaskSettings.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "taskschd.lib") 8 | 9 | namespace WinToolsLib { namespace Scheduler { namespace Vista 10 | { 11 | enum class InstancesPolicy 12 | { 13 | RunInParallel = TASK_INSTANCES_PARALLEL, 14 | Queue = TASK_INSTANCES_QUEUE, 15 | IgnoreNew = TASK_INSTANCES_IGNORE_NEW, 16 | StopExisting = TASK_INSTANCES_STOP_EXISTING 17 | }; 18 | 19 | class TaskSettings 20 | { 21 | TaskSettings(const TaskSettings& other); // Non-copyable 22 | TaskSettings& operator=(const TaskSettings& other); // Non-copyable 23 | 24 | public: 25 | TaskSettings(Com::Ptr&& settings); 26 | TaskSettings(TaskSettings&& other); 27 | ~TaskSettings(); 28 | 29 | TaskSettings& operator=(TaskSettings&& other); 30 | operator ITaskSettings*() const; 31 | 32 | Bool GetDisallowStartIfOnBatteries() const; 33 | Void SetDisallowStartIfOnBatteries(Bool value); 34 | 35 | Bool GetStopIfGoingOnBatteries() const; 36 | Void SetStopIfGoingOnBatteries(Bool value); 37 | 38 | Bool GetRunOnlyIfIdle() const; 39 | Void SetRunOnlyIfIdle(Bool value); 40 | 41 | Bool GetStopOnIdleEnd() const; 42 | Void SetStopOnIdleEnd(Bool value); 43 | 44 | Bool GetStartWhenAvailable() const; 45 | Void SetStartWhenAvailable(Bool value); 46 | 47 | InstancesPolicy GetInstancesPolicy() const; 48 | Void SetInstancesPolicy(InstancesPolicy policy); 49 | 50 | StringW GetExecutionTimeLimit() const; 51 | Void SetExecutionTimeLimit(const WChar* limit); 52 | 53 | Bool GetEnabled() const; 54 | Void SetEnabled(Bool value); 55 | 56 | protected: 57 | Void MoveFrom(TaskSettings& other); 58 | 59 | private: 60 | Com::Ptr m_settings; 61 | }; 62 | 63 | inline TaskSettings::operator ITaskSettings*() const 64 | { 65 | return m_settings.operator ITaskSettings*(); 66 | } 67 | 68 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/Trigger.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "taskschd.lib") 8 | 9 | #include "DailyTrigger.h" 10 | #include "WeeklyTrigger.h" 11 | 12 | namespace WinToolsLib { namespace Scheduler { namespace Vista 13 | { 14 | enum class TriggerType 15 | { 16 | Time = TASK_TRIGGER_TIME, 17 | Daily = TASK_TRIGGER_DAILY, 18 | Weekly = TASK_TRIGGER_WEEKLY, 19 | Logon = TASK_TRIGGER_LOGON 20 | }; 21 | 22 | class Trigger 23 | { 24 | Trigger(const Trigger& other); // Non-copyable 25 | Trigger& operator=(const Trigger& other); // Non-copyable 26 | 27 | public: 28 | Trigger(); 29 | Trigger(Com::Ptr&& trigger); 30 | Trigger(Trigger&& other); 31 | ~Trigger(); 32 | 33 | Trigger& operator=(Trigger&& other); 34 | 35 | TriggerType GetType() const; 36 | DailyTrigger QueryDailyTrigger() const; 37 | WeeklyTrigger QueryWeeklyTrigger() const; 38 | 39 | StringW GetStartBoundary() const; 40 | Void SetStartBoundary(const WChar* startBoundary); 41 | 42 | StringW GetRepetitionDuration() const; 43 | Void SetRepetitionDuration(const WChar* duration); 44 | 45 | StringW GetRepetitionInterval() const; 46 | Void SetRepetitionInterval(const WChar* interval); 47 | 48 | protected: 49 | Void MoveFrom(Trigger& other); 50 | 51 | private: 52 | Com::Ptr m_trigger; 53 | }; 54 | 55 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/WeeklyTrigger.cpp: -------------------------------------------------------------------------------- 1 | #include "WeeklyTrigger.h" 2 | 3 | #include "..\..\Exception.h" 4 | 5 | #include 6 | #pragma comment(lib, "comsuppw.lib") 7 | 8 | namespace WinToolsLib { namespace Scheduler { namespace Vista 9 | { 10 | WeeklyTrigger::WeeklyTrigger(Com::Ptr&& trigger) 11 | { 12 | m_trigger = std::move(trigger); 13 | } 14 | 15 | WeeklyTrigger::WeeklyTrigger(WeeklyTrigger&& other) 16 | { 17 | MoveFrom(other); 18 | } 19 | 20 | WeeklyTrigger::~WeeklyTrigger() 21 | { 22 | } 23 | 24 | WeeklyTrigger& WeeklyTrigger::operator=(WeeklyTrigger&& other) 25 | { 26 | MoveFrom(other); 27 | return *this; 28 | } 29 | 30 | Int16 WeeklyTrigger::GetWeeksInterval() const 31 | { 32 | Int16 weeks = 0; 33 | 34 | auto hr = m_trigger->get_WeeksInterval(&weeks); 35 | if (FAILED(hr)) 36 | { 37 | THROW_WIN32_EXCEPTION(hr); 38 | } 39 | 40 | return weeks; 41 | } 42 | 43 | Void WeeklyTrigger::SetWeeksInterval(Int16 weeks) 44 | { 45 | auto hr = m_trigger->put_WeeksInterval(weeks); 46 | if (FAILED(hr)) 47 | { 48 | THROW_WIN32_EXCEPTION(hr); 49 | } 50 | } 51 | 52 | Int16 WeeklyTrigger::GetDaysOfWeek() const 53 | { 54 | Int16 days = 0; 55 | 56 | auto hr = m_trigger->get_DaysOfWeek(&days); 57 | if (FAILED(hr)) 58 | { 59 | THROW_WIN32_EXCEPTION(hr); 60 | } 61 | 62 | return days; 63 | } 64 | 65 | Void WeeklyTrigger::SetDaysOfWeek(Int16 days) 66 | { 67 | auto hr = m_trigger->put_DaysOfWeek(days); 68 | if (FAILED(hr)) 69 | { 70 | THROW_WIN32_EXCEPTION(hr); 71 | } 72 | } 73 | 74 | Void WeeklyTrigger::MoveFrom(WeeklyTrigger& other) 75 | { 76 | if (this != &other) 77 | { 78 | m_trigger = std::move(other.m_trigger); 79 | } 80 | } 81 | 82 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Vista/WeeklyTrigger.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "taskschd.lib") 8 | 9 | namespace WinToolsLib { namespace Scheduler { namespace Vista 10 | { 11 | enum class DaysOfWeek : Int16 12 | { 13 | Sunday = 0x01, 14 | Monday = 0x02, 15 | Tuesday = 0x04, 16 | Wednesday = 0x08, 17 | Thursday = 0x10, 18 | Friday = 0x20, 19 | Saturday = 0x40 20 | }; 21 | 22 | class WeeklyTrigger 23 | { 24 | WeeklyTrigger(const WeeklyTrigger& other); // Non-copyable 25 | WeeklyTrigger& operator=(const WeeklyTrigger& other); // Non-copyable 26 | 27 | public: 28 | WeeklyTrigger(Com::Ptr&& trigger); 29 | WeeklyTrigger(WeeklyTrigger&& other); 30 | ~WeeklyTrigger(); 31 | 32 | WeeklyTrigger& operator=(WeeklyTrigger&& other); 33 | 34 | Int16 GetWeeksInterval() const; 35 | Void SetWeeksInterval(Int16 weeks); 36 | 37 | Int16 GetDaysOfWeek() const; 38 | Void SetDaysOfWeek(Int16 days); 39 | 40 | protected: 41 | Void MoveFrom(WeeklyTrigger& other); 42 | 43 | private: 44 | Com::Ptr m_trigger; 45 | }; 46 | 47 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Xp/Task.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "MSTask.lib") 8 | 9 | #include "TaskTrigger.h" 10 | 11 | namespace WinToolsLib { namespace Scheduler { namespace Xp 12 | { 13 | struct Flags 14 | { 15 | enum 16 | { 17 | StartOnlyIfIdle = TASK_FLAG_START_ONLY_IF_IDLE, 18 | KillOnIdleEnd = TASK_FLAG_KILL_ON_IDLE_END, 19 | RestartOnIdleResume = TASK_FLAG_RESTART_ON_IDLE_RESUME, 20 | DontStartIfOnBatteries = TASK_FLAG_DONT_START_IF_ON_BATTERIES, 21 | KillIfGoingOnBatteries = TASK_FLAG_KILL_IF_GOING_ON_BATTERIES 22 | }; 23 | }; 24 | 25 | class Task 26 | { 27 | Task(const Task& other); // Non-copyable 28 | Task& operator=(const Task& other); // Non-copyable 29 | 30 | public: 31 | Task(Com::Ptr&& task); 32 | Task(Task&& other); 33 | ~Task(); 34 | 35 | Task& operator=(Task&& other); 36 | 37 | StringW GetApplication() const; 38 | Void SetApplication(const WChar* application); 39 | 40 | StringW GetCreator() const; 41 | Void SetCreator(const WChar* creator); 42 | 43 | StringW GetParameters() const; 44 | Void SetParameters(const WChar* parameters); 45 | 46 | UInt32 GetFlags() const; 47 | Void SetFlags(UInt32 flags); 48 | 49 | Void GetTrigger(TaskTrigger& trigger) const; 50 | Void SetTrigger(const TaskTrigger& trigger); 51 | Void DeleteTrigger(); 52 | 53 | Void GetAccountInformation(StringW& account) const; 54 | Void SetAccountInformation(const WChar* account, const WChar* password); 55 | 56 | Void Save() const; 57 | Void Run() const; 58 | 59 | protected: 60 | Void MoveFrom(Task& other); 61 | 62 | private: 63 | Com::Ptr m_task; 64 | }; 65 | 66 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Xp/TaskScheduler.cpp: -------------------------------------------------------------------------------- 1 | #include "TaskScheduler.h" 2 | 3 | #include "..\..\Exception.h" 4 | 5 | namespace WinToolsLib { namespace Scheduler { namespace Xp 6 | { 7 | TaskScheduler::TaskScheduler() 8 | { 9 | auto hr = ::CoCreateInstance( 10 | CLSID_CTaskScheduler, 11 | nullptr, 12 | CLSCTX_INPROC_SERVER, 13 | IID_ITaskScheduler, 14 | (PVoid*)&m_scheduler); 15 | 16 | if (FAILED(hr)) 17 | { 18 | THROW_WIN32_EXCEPTION(hr); 19 | } 20 | } 21 | 22 | TaskScheduler::~TaskScheduler() 23 | { 24 | } 25 | 26 | Task TaskScheduler::GetTask(const WChar* name) const 27 | { 28 | Com::Ptr task; 29 | 30 | auto hr = m_scheduler->Activate( 31 | name, 32 | IID_ITask, 33 | (IUnknown**)&task); 34 | 35 | if (FAILED(hr)) 36 | { 37 | THROW_WIN32_EXCEPTION(hr); 38 | } 39 | 40 | return Task(std::move(task)); 41 | } 42 | 43 | Task TaskScheduler::CreateTask(const WChar* name) 44 | { 45 | Com::Ptr task; 46 | 47 | auto hr = m_scheduler->NewWorkItem( 48 | name, 49 | CLSID_CTask, 50 | IID_ITask, 51 | (IUnknown**)&task); 52 | 53 | if (FAILED(hr)) 54 | { 55 | THROW_WIN32_EXCEPTION(hr); 56 | } 57 | 58 | return Task(std::move(task)); 59 | } 60 | 61 | Void TaskScheduler::DeleteTask(const WChar* name) 62 | { 63 | auto hr = m_scheduler->Delete(name); 64 | if (FAILED(hr)) 65 | { 66 | THROW_WIN32_EXCEPTION(hr); 67 | } 68 | } 69 | 70 | Void TaskScheduler::EnumTasks(EnumCallback callback) 71 | { 72 | Com::Ptr enumItems; 73 | 74 | auto hr = m_scheduler->Enum(&enumItems); 75 | if (FAILED(hr)) 76 | { 77 | THROW_WIN32_EXCEPTION(hr); 78 | } 79 | 80 | while (True) 81 | { 82 | const auto fetchBy = 5ul; 83 | auto fetchedTasks = 0ul; 84 | PWChar* names = nullptr; 85 | 86 | hr = enumItems->Next(fetchBy, &names, &fetchedTasks); 87 | if (FAILED(hr)) 88 | { 89 | THROW_WIN32_EXCEPTION(hr); 90 | } 91 | 92 | for (auto next = 0ul; next < fetchedTasks; next++) 93 | { 94 | callback(names[next]); 95 | ::CoTaskMemFree(names[next]); 96 | } 97 | ::CoTaskMemFree(names); 98 | 99 | if (S_FALSE == hr) 100 | { 101 | break; 102 | } 103 | } 104 | } 105 | 106 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Xp/TaskScheduler.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\Com\Ptr.h" 4 | 5 | #include 6 | #pragma comment(lib, "MSTask.lib") 7 | 8 | #include 9 | 10 | #include "Task.h" 11 | 12 | namespace WinToolsLib { namespace Scheduler { namespace Xp 13 | { 14 | class TaskScheduler 15 | { 16 | public: 17 | TaskScheduler(); 18 | ~TaskScheduler(); 19 | 20 | Task GetTask(const WChar* name) const; 21 | Task CreateTask(const WChar* name); 22 | Void DeleteTask(const WChar* name); 23 | 24 | typedef std::function EnumCallback; 25 | Void EnumTasks(EnumCallback callback); 26 | 27 | private: 28 | Com::Ptr m_scheduler; 29 | }; 30 | 31 | } } } -------------------------------------------------------------------------------- /WinToolsLib/Scheduler/Xp/TaskTrigger.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\..\Types.h" 3 | #include "..\..\String.h" 4 | #include "..\..\Com\Ptr.h" 5 | 6 | #include 7 | #pragma comment(lib, "MSTask.lib") 8 | 9 | namespace WinToolsLib { namespace Scheduler { namespace Xp 10 | { 11 | enum class TriggerType 12 | { 13 | Once = TASK_TIME_TRIGGER_ONCE, 14 | Daily = TASK_TIME_TRIGGER_DAILY, 15 | Weekly = TASK_TIME_TRIGGER_WEEKLY 16 | }; 17 | 18 | enum class DaysOfWeek : UInt16 19 | { 20 | Sunday = TASK_SUNDAY, 21 | Monday = TASK_MONDAY, 22 | Tuesday = TASK_TUESDAY, 23 | Wednesday = TASK_WEDNESDAY, 24 | Thursday = TASK_THURSDAY, 25 | Friday = TASK_FRIDAY, 26 | Saturday = TASK_SATURDAY 27 | }; 28 | 29 | class TaskTrigger 30 | { 31 | public: 32 | TaskTrigger(); 33 | 34 | const TASK_TRIGGER* GetAddress() const; 35 | TASK_TRIGGER* GetAddress(); 36 | 37 | TriggerType GetType() const; 38 | Void SetType(TriggerType type); 39 | 40 | StringW GetStartTime() const; 41 | Void SetStartTime(const WChar* startTime); 42 | 43 | UInt32 GetRepetitionDuration() const; 44 | Void SetRepetitionDuration(UInt32 minutes); 45 | 46 | UInt32 GetRepetitionInterval() const; 47 | Void SetRepetitionInterval(UInt32 minutes); 48 | 49 | UInt16 GetDaysInterval() const; 50 | Void SetDaysInterval(UInt16 days); 51 | 52 | UInt16 GetWeeksInterval() const; 53 | Void SetWeeksInterval(UInt16 weeks); 54 | 55 | UInt16 GetDaysOfWeek() const; 56 | Void SetDaysOfWeek(UInt16 days); 57 | 58 | private: 59 | TASK_TRIGGER m_trigger; 60 | }; 61 | 62 | inline const TASK_TRIGGER* TaskTrigger::GetAddress() const 63 | { 64 | return &m_trigger; 65 | } 66 | 67 | inline TASK_TRIGGER* TaskTrigger::GetAddress() 68 | { 69 | return &m_trigger; 70 | } 71 | 72 | } } } -------------------------------------------------------------------------------- /WinToolsLib/ScopeGuard.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | 4 | // 5 | // Based on ScopeGuard implementation by 6 | // Andrei Alexandrescu and Petru Marginean 7 | // 8 | 9 | namespace WinToolsLib 10 | { 11 | template 12 | class ScopeGuard 13 | { 14 | ScopeGuard(const ScopeGuard& other); // Non-copyable 15 | ScopeGuard& operator=(const ScopeGuard& other); // Non-copyable 16 | 17 | public: 18 | ScopeGuard(Func function); 19 | ScopeGuard(ScopeGuard&& other); 20 | ~ScopeGuard(); 21 | 22 | Void Dismiss(); 23 | Void Enforce(); 24 | 25 | private: 26 | Func m_function; 27 | Bool m_active; 28 | }; 29 | 30 | template 31 | ScopeGuard::ScopeGuard(Func function) : 32 | m_function(std::move(function)), 33 | m_active(True) 34 | { 35 | } 36 | 37 | template 38 | ScopeGuard::ScopeGuard(ScopeGuard&& other) : 39 | m_function(std::move(other.m_function)), 40 | m_active(other.m_active) 41 | { 42 | other.Dismiss(); 43 | } 44 | 45 | template 46 | ScopeGuard::~ScopeGuard() 47 | { 48 | Enforce(); 49 | } 50 | 51 | template 52 | Void ScopeGuard::Dismiss() 53 | { 54 | m_active = False; 55 | } 56 | 57 | template 58 | Void ScopeGuard::Enforce() 59 | { 60 | if (m_active) 61 | { 62 | m_active = False; 63 | m_function(); 64 | } 65 | } 66 | 67 | template 68 | ScopeGuard OnExit(Func function) 69 | { 70 | return ScopeGuard(std::move(function)); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /WinToolsLib/Signals/AnonymousConnection.cpp: -------------------------------------------------------------------------------- 1 | #include "AnonymousConnection.h" 2 | 3 | namespace WinToolsLib { namespace Signals 4 | { 5 | AnonymousConnection0::AnonymousConnection0(SlotId id, AnonymousFunction& function) : 6 | m_function(function), 7 | ConnectionBase0(id) 8 | { 9 | } 10 | } } -------------------------------------------------------------------------------- /WinToolsLib/Signals/ConnectionBase.cpp: -------------------------------------------------------------------------------- 1 | #include "ConnectionBase.h" 2 | 3 | namespace WinToolsLib { namespace Signals 4 | { 5 | ConnectionBase::ConnectionBase(SlotId id) : 6 | m_slotId(id) 7 | { 8 | } 9 | 10 | ConnectionBase::~ConnectionBase() 11 | { 12 | } 13 | 14 | ConnectionBase0::ConnectionBase0(SlotId id) : 15 | ConnectionBase(id) 16 | { 17 | } 18 | 19 | ConnectionBase0::~ConnectionBase0() 20 | { 21 | } 22 | } } -------------------------------------------------------------------------------- /WinToolsLib/Signals/MultiThreadModes.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "../Types.h" 3 | 4 | #include 5 | 6 | #define OSHI_SIGNALS_MTL 7 | 8 | #if defined OSHI_SIGNALS_MTG 9 | # define OSHI_SIGNALS_DEFAULT_THREAD_MODE MultiThreadGlobal 10 | #elif defined OSHI_SIGNALS_MTL 11 | # define OSHI_SIGNALS_DEFAULT_THREAD_MODE MultiThreadLocal 12 | #else 13 | # define OSHI_SIGNALS_DEFAULT_THREAD_MODE SingleThread 14 | #endif 15 | 16 | namespace WinToolsLib { namespace Signals 17 | { 18 | class SingleThread 19 | { 20 | public: 21 | SingleThread() {} 22 | 23 | void lock() {} 24 | bool try_lock() {return true;} 25 | void unlock() {} 26 | }; 27 | 28 | class MultiThreadGlobal 29 | { 30 | public: 31 | MultiThreadGlobal() {} 32 | 33 | void lock(); 34 | bool try_lock(); 35 | void unlock(); 36 | 37 | private: 38 | static std::mutex m_mutex; 39 | }; 40 | 41 | inline void MultiThreadGlobal::lock() 42 | { 43 | m_mutex.lock(); 44 | } 45 | 46 | inline bool MultiThreadGlobal::try_lock() 47 | { 48 | return m_mutex.try_lock(); 49 | } 50 | 51 | inline void MultiThreadGlobal::unlock() 52 | { 53 | m_mutex.unlock(); 54 | } 55 | 56 | typedef std::mutex MultiThreadLocal; 57 | } } -------------------------------------------------------------------------------- /WinToolsLib/Signals/SignalBase.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | 4 | #include 5 | 6 | namespace WinToolsLib { namespace Signals 7 | { 8 | class Track; 9 | typedef std::shared_ptr SignalExistFlag; 10 | typedef UInt64 SlotId; 11 | 12 | class SignalBase 13 | { 14 | public: 15 | SignalBase(); 16 | virtual ~SignalBase(); 17 | 18 | virtual Void Disconnect(SlotId id) {}; 19 | virtual Void DisconnectAll() {}; 20 | 21 | const SignalExistFlag& GetExistFlag() const; 22 | 23 | protected: 24 | SlotId m_lastId; 25 | SignalExistFlag m_existFlag; 26 | }; 27 | 28 | inline SignalBase::SignalBase() : 29 | m_lastId(0) 30 | { 31 | } 32 | 33 | inline SignalBase::~SignalBase() 34 | { 35 | DisconnectAll(); 36 | } 37 | 38 | inline const SignalExistFlag& SignalBase::GetExistFlag() const 39 | { 40 | return m_existFlag; 41 | } 42 | 43 | } } -------------------------------------------------------------------------------- /WinToolsLib/Signature.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "Types.h" 8 | #include "Buffer.h" 9 | #include "Handles\Handle.h" 10 | 11 | namespace WinToolsLib 12 | { 13 | using namespace Handles; 14 | 15 | class Signature 16 | { 17 | public: 18 | enum class State 19 | { 20 | Unknown, 21 | IsTrusted, 22 | SubjectNotTrusted, 23 | UntrustedRoot, 24 | ProviderUnknown, 25 | ActionUnknown, 26 | SubjectFromUnknown, 27 | CertificateRevoked, 28 | CertificateExpired, 29 | BadDigest, 30 | NoSignature, 31 | FileNotFound 32 | }; 33 | 34 | Signature(const String& filePath); 35 | 36 | Bool IsValid(); 37 | State Check(); 38 | 39 | StringW GetSigner(); 40 | CATALOG_INFO& GetCatalogInfo(); 41 | 42 | private: 43 | String m_filePath; 44 | CATALOG_INFO m_catalogInfo; 45 | 46 | State WinErrorToState(const Int32 err); 47 | Int32 IsTrusted(WINTRUST_DATA* pWintrustData); 48 | Int32 IsTrusted(const Buffer& fileHash, const HCATADMIN& adminContext); 49 | Int32 IsTrusted(); 50 | Buffer CalcFileHash(const Handle& fileHandle, const HCATADMIN& adminContext); 51 | Buffer CalcFileHash(const TChar* filePath, const HCATADMIN& adminContext); 52 | }; 53 | 54 | inline CATALOG_INFO& Signature::GetCatalogInfo() 55 | { 56 | return m_catalogInfo; 57 | } 58 | } -------------------------------------------------------------------------------- /WinToolsLib/Singleton.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | namespace WinToolsLib 5 | { 6 | template 7 | class Singleton 8 | { 9 | Singleton(const Singleton& other); // Non-copyable 10 | Singleton& operator=(const Singleton& other); // Non-copyable 11 | 12 | Singleton(Singleton&& other); // Non-movable 13 | Singleton& operator=(Singleton&& other); // Non-movable 14 | 15 | public: 16 | static T& Instance(); 17 | 18 | protected: 19 | Singleton() {} 20 | 21 | private: 22 | static std::once_flag m_flag; 23 | static T* m_instancePtr; 24 | }; 25 | 26 | template 27 | std::once_flag Singleton::m_flag; 28 | 29 | template 30 | T* Singleton::m_instancePtr = nullptr; 31 | 32 | template 33 | T& Singleton::Instance() 34 | { 35 | std::call_once(m_flag, []() 36 | { 37 | static T instance; 38 | Singleton::m_instancePtr = &instance; 39 | }); 40 | return *m_instancePtr; 41 | } 42 | } -------------------------------------------------------------------------------- /WinToolsLib/Stream.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "IStream.h" 4 | #include "Buffer.h" 5 | 6 | namespace WinToolsLib 7 | { 8 | class Stream : public IStream 9 | { 10 | public: 11 | Stream(Buffer& buffer); 12 | Stream(Buffer& buffer, SizeT offset); 13 | 14 | SizeT GetPosition() const override; 15 | Void SetPosition(SizeT position) override; 16 | 17 | Void Reset() override; 18 | Void Skip(SizeT bytes) override; 19 | 20 | Void Read(PByte buffer, SizeT bytes) override; 21 | Void Write(PByte buffer, SizeT bytes) override; 22 | 23 | IStream& operator>>(Int8& value) override; 24 | IStream& operator>>(Int16& value) override; 25 | IStream& operator>>(Int32& value) override; 26 | IStream& operator>>(Int64& value) override; 27 | 28 | IStream& operator>>(UInt8& value) override; 29 | IStream& operator>>(UInt16& value) override; 30 | IStream& operator>>(UInt32& value) override; 31 | IStream& operator>>(UInt64& value) override; 32 | 33 | IStream& operator<<(Int8 value) override; 34 | IStream& operator<<(Int16 value) override; 35 | IStream& operator<<(Int32 value) override; 36 | IStream& operator<<(Int64 value) override; 37 | 38 | IStream& operator<<(UInt8 value) override; 39 | IStream& operator<<(UInt16 value) override; 40 | IStream& operator<<(UInt32 value) override; 41 | IStream& operator<<(UInt64 value) override; 42 | 43 | private: 44 | Buffer& m_buffer; 45 | SizeT m_position; 46 | }; 47 | } 48 | -------------------------------------------------------------------------------- /WinToolsLib/System.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "String.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | class System 8 | { 9 | public: 10 | static Bool IsXpOrLater(); 11 | static Bool IsVistaOrLater(); 12 | static Bool IsSevenOrLater(); 13 | static Bool IsEightOrLater(); 14 | static Void GetVersion(UInt32& major, UInt32& minor); 15 | static String GetFullVersion(); 16 | static String GetSysDir(); 17 | static String GetWinDir(); 18 | static String GetSysWinDir(); 19 | static String GetSysWowDir(); 20 | }; 21 | } 22 | -------------------------------------------------------------------------------- /WinToolsLib/Trace.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "Log.h" 4 | #include "Singleton.h" 5 | #include "Concurrency\WorkQueue.h" 6 | 7 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0010 8 | #define ENABLE_TRACE_INFO 9 | #endif 10 | 11 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0008 12 | #define ENABLE_TRACE_EXCEPTION 13 | #endif 14 | 15 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0004 16 | #define ENABLE_TRACE_WARNING 17 | #endif 18 | 19 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0002 20 | #define ENABLE_TRACE_ERROR 21 | #endif 22 | 23 | #if _WINTOOLSLIB_LOG_LEVEL & 0x0001 24 | #define ENABLE_TRACE_FATAL 25 | #endif 26 | 27 | namespace WinToolsLib 28 | { 29 | class TraceQueue : public Concurrency::WorkQueue, public Singleton 30 | { 31 | protected: 32 | TraceQueue() {} 33 | ~TraceQueue() {} 34 | 35 | friend class Singleton; 36 | }; 37 | 38 | #ifdef ENABLE_TRACE_INFO 39 | #define TRACE_INFO(...) \ 40 | TraceQueue::Instance().PushBack([=] \ 41 | { \ 42 | LOG_INFO(__VA_ARGS__); \ 43 | }); 44 | #else 45 | #define TRACE_INFO(...) 46 | #endif 47 | 48 | #ifdef ENABLE_TRACE_WARNING 49 | #define TRACE_WARNING(...) \ 50 | TraceQueue::Instance().PushBack([=] \ 51 | { \ 52 | LOG_WARNING(__VA_ARGS__); \ 53 | }); 54 | #else 55 | #define TRACE_WARNING(...) 56 | #endif 57 | 58 | #ifdef ENABLE_TRACE_ERROR 59 | #define TRACE_ERROR(...) \ 60 | TraceQueue::Instance().PushBack([=] \ 61 | { \ 62 | LOG_ERROR(__VA_ARGS__); \ 63 | }); 64 | #else 65 | #define TRACE_ERROR(...) 66 | #endif 67 | 68 | #ifdef ENABLE_TRACE_FATAL 69 | #define TRACE_FATAL(...) \ 70 | TraceQueue::Instance().PushBack([=] \ 71 | { \ 72 | LOG_FATAL(__VA_ARGS__); \ 73 | }); 74 | #else 75 | #define TRACE_FATAL(...) 76 | #endif 77 | 78 | } 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /WinToolsLib/Types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Bool.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | typedef void Void, *PVoid; 7 | typedef unsigned char Byte, *PByte; 8 | 9 | typedef const void *PCVoid; 10 | typedef const unsigned char *PCByte; 11 | 12 | typedef char Char, *PChar; 13 | typedef wchar_t WChar, *PWChar; 14 | 15 | typedef const char *PCChar; 16 | typedef const wchar_t *PCWChar; 17 | 18 | #ifdef UNICODE 19 | typedef wchar_t TChar, *PTChar; 20 | typedef const wchar_t *PCTChar; 21 | #else 22 | typedef char TChar, *PTChar; 23 | typedef const char *PCTChar; 24 | #endif 25 | 26 | #ifdef UNICODE 27 | #define _Text(quote) L##quote 28 | #else 29 | #define _Text(quote) quote 30 | #endif 31 | 32 | #define Text(quote) _Text(quote) 33 | 34 | typedef signed char Int8, *PInt8; 35 | typedef signed short Int16, *PInt16; 36 | typedef signed int Int32, *PInt32; 37 | typedef signed __int64 Int64, *PInt64; 38 | 39 | typedef unsigned char UInt8, *PUInt8; 40 | typedef unsigned short UInt16, *PUInt16; 41 | typedef unsigned int UInt32, *PUInt32; 42 | typedef unsigned __int64 UInt64, *PUInt64; 43 | 44 | #ifdef _WIN64 45 | typedef unsigned __int64 SizeT, *PSizeT; 46 | #else 47 | typedef unsigned int SizeT, *PSizeT; 48 | #endif 49 | 50 | typedef float Float, *PFloat; 51 | typedef double Double, *PDouble; 52 | } -------------------------------------------------------------------------------- /WinToolsLib/Version.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Types.h" 3 | #include "String.h" 4 | 5 | #include 6 | 7 | namespace WinToolsLib 8 | { 9 | class Version 10 | { 11 | public: 12 | Version(); 13 | Version(Version&& other); 14 | Version(const Version& other); 15 | Version(const String& string); 16 | 17 | Version& operator=(Version&& other); 18 | Version& operator=(const Version& other); 19 | 20 | Bool operator<(const Version& other) const; 21 | Bool operator>(const Version& other) const; 22 | 23 | Bool operator<=(const Version& other) const; 24 | Bool operator>=(const Version& other) const; 25 | 26 | Bool operator==(const Version& other) const; 27 | Bool operator!=(const Version& other) const; 28 | 29 | const SizeT& operator[](SizeT index) const; 30 | SizeT& operator[](SizeT index); 31 | 32 | SizeT GetPartsCount() const; 33 | 34 | String ToString() const; 35 | Int32 ToInt() const; 36 | 37 | protected: 38 | Void MoveFrom(Version& other); 39 | Void CopyFrom(const Version& other); 40 | 41 | private: 42 | std::vector m_parts; 43 | }; 44 | 45 | inline const SizeT& Version::operator[](SizeT index) const 46 | { 47 | return m_parts[index]; 48 | } 49 | 50 | inline SizeT& Version::operator[](SizeT index) 51 | { 52 | return m_parts[index]; 53 | } 54 | 55 | inline SizeT Version::GetPartsCount() const 56 | { 57 | return m_parts.size(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /WinToolsLib/Wow64/Emulator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | #include "..\String.h" 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace Wow64 8 | { 9 | class Emulator 10 | { 11 | Emulator() {} // Singleton, use GetInstance() 12 | 13 | Emulator(const Emulator& other); // Non-copyable 14 | Emulator& operator=(const Emulator& other); // Non-copyable 15 | 16 | Emulator(Emulator&& other); // Non-movable 17 | Emulator& operator=(Emulator&& other); // Non-movable 18 | 19 | public: 20 | static Emulator& GetInstance(); 21 | 22 | String EmulateFsRedirection(const TChar* path); 23 | }; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /WinToolsLib/Wow64/FsRedirection.cpp: -------------------------------------------------------------------------------- 1 | #include "FsRedirection.h" 2 | #include "..\DynamicFunction.h" 3 | #include 4 | 5 | namespace WinToolsLib 6 | { 7 | namespace Wow64 8 | { 9 | FsRedirection::FsRedirection() : 10 | m_shouldRevert(False), 11 | m_oldValue(nullptr) 12 | { 13 | } 14 | 15 | FsRedirection::~FsRedirection() 16 | { 17 | Revert(); 18 | } 19 | 20 | FsRedirection::FsRedirection(FsRedirection&& other) : 21 | m_shouldRevert(other.m_shouldRevert), 22 | m_oldValue(other.m_oldValue) 23 | { 24 | other.m_shouldRevert = False; 25 | other.m_oldValue = nullptr; 26 | } 27 | 28 | FsRedirection& FsRedirection::operator=(FsRedirection&& other) 29 | { 30 | Revert(); 31 | 32 | m_shouldRevert = other.m_shouldRevert; 33 | m_oldValue = other.m_oldValue; 34 | 35 | other.m_shouldRevert = False; 36 | other.m_oldValue = nullptr; 37 | 38 | return *this; 39 | } 40 | 41 | Void FsRedirection::Disable() 42 | { 43 | typedef BOOL (WINAPI *Fn)(PVOID* OldValue); 44 | 45 | static const auto fn = DynamicFunction("kernel32", "Wow64DisableWow64FsRedirection"); 46 | if (fn) 47 | { 48 | fn(&m_oldValue); 49 | m_shouldRevert = True; 50 | } 51 | } 52 | 53 | Void FsRedirection::Revert() 54 | { 55 | typedef BOOL (WINAPI *Fn)(PVOID OldValue); 56 | 57 | if (!m_shouldRevert) 58 | { 59 | return; 60 | } 61 | 62 | static const auto fn = DynamicFunction("kernel32", "Wow64RevertWow64FsRedirection"); 63 | if (fn) 64 | { 65 | fn(m_oldValue); 66 | m_shouldRevert = False; 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /WinToolsLib/Wow64/FsRedirection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "..\Types.h" 3 | 4 | namespace WinToolsLib 5 | { 6 | namespace Wow64 7 | { 8 | class FsRedirection 9 | { 10 | FsRedirection(const FsRedirection& other); // Non-copyable 11 | FsRedirection& operator=(const FsRedirection& other); // Non-copyable 12 | 13 | public: 14 | FsRedirection(); 15 | ~FsRedirection(); 16 | 17 | FsRedirection(FsRedirection&& other); 18 | FsRedirection& operator=(FsRedirection&& other); 19 | 20 | Void Disable(); 21 | Void Revert(); 22 | 23 | private: 24 | Bool m_shouldRevert; 25 | PVoid m_oldValue; 26 | }; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /WinToolsLib/libudis86/extern.h: -------------------------------------------------------------------------------- 1 | /* ----------------------------------------------------------------------------- 2 | * extern.h 3 | * 4 | * Copyright (c) 2004, 2005, 2006, Vivek Mohan 5 | * All rights reserved. See LICENSE 6 | * ----------------------------------------------------------------------------- 7 | */ 8 | #ifndef UD_EXTERN_H 9 | #define UD_EXTERN_H 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | #include 16 | #include "types.h" 17 | 18 | /* ============================= PUBLIC API ================================= */ 19 | 20 | extern void ud_init(struct ud*); 21 | 22 | extern void ud_set_mode(struct ud*, uint8_t); 23 | 24 | extern void ud_set_pc(struct ud*, uint64_t); 25 | 26 | extern void ud_set_input_hook(struct ud*, int (*)(struct ud*)); 27 | 28 | extern void ud_set_input_buffer(struct ud*, uint8_t*, size_t); 29 | 30 | #ifndef __UD_STANDALONE__ 31 | extern void ud_set_input_file(struct ud*, FILE*); 32 | #endif /* __UD_STANDALONE__ */ 33 | 34 | extern void ud_set_vendor(struct ud*, unsigned); 35 | 36 | extern void ud_set_syntax(struct ud*, void (*)(struct ud*)); 37 | 38 | extern void ud_input_skip(struct ud*, size_t); 39 | 40 | extern int ud_input_end(struct ud*); 41 | 42 | extern unsigned int ud_decode(struct ud*); 43 | 44 | extern unsigned int ud_disassemble(struct ud*); 45 | 46 | extern void ud_translate_intel(struct ud*); 47 | 48 | extern void ud_translate_att(struct ud*); 49 | 50 | extern char* ud_insn_asm(struct ud* u); 51 | 52 | extern uint8_t* ud_insn_ptr(struct ud* u); 53 | 54 | extern uint64_t ud_insn_off(struct ud*); 55 | 56 | extern char* ud_insn_hex(struct ud*); 57 | 58 | extern unsigned int ud_insn_len(struct ud* u); 59 | 60 | extern const char* ud_lookup_mnemonic(enum ud_mnemonic_code c); 61 | 62 | /* ========================================================================== */ 63 | 64 | #ifdef __cplusplus 65 | } 66 | #endif 67 | #endif 68 | -------------------------------------------------------------------------------- /WinToolsLib/libudis86/input.h: -------------------------------------------------------------------------------- 1 | /* ----------------------------------------------------------------------------- 2 | * input.h 3 | * 4 | * Copyright (c) 2006, Vivek Mohan 5 | * All rights reserved. See LICENSE 6 | * ----------------------------------------------------------------------------- 7 | */ 8 | #ifndef UD_INPUT_H 9 | #define UD_INPUT_H 10 | 11 | #include "types.h" 12 | 13 | uint8_t inp_next(struct ud*); 14 | uint8_t inp_peek(struct ud*); 15 | uint8_t inp_uint8(struct ud*); 16 | uint16_t inp_uint16(struct ud*); 17 | uint32_t inp_uint32(struct ud*); 18 | uint64_t inp_uint64(struct ud*); 19 | void inp_move(struct ud*, size_t); 20 | void inp_back(struct ud*); 21 | 22 | /* inp_init() - Initializes the input system. */ 23 | #define inp_init(u) \ 24 | do { \ 25 | u->inp_curr = 0; \ 26 | u->inp_fill = 0; \ 27 | u->inp_ctr = 0; \ 28 | u->inp_end = 0; \ 29 | } while (0) 30 | 31 | /* inp_start() - Should be called before each de-code operation. */ 32 | #define inp_start(u) u->inp_ctr = 0 33 | 34 | /* inp_back() - Resets the current pointer to its position before the current 35 | * instruction disassembly was started. 36 | */ 37 | #define inp_reset(u) \ 38 | do { \ 39 | u->inp_curr -= u->inp_ctr; \ 40 | u->inp_ctr = 0; \ 41 | } while (0) 42 | 43 | /* inp_sess() - Returns the pointer to current session. */ 44 | #define inp_sess(u) (u->inp_sess) 45 | 46 | /* inp_cur() - Returns the current input byte. */ 47 | #define inp_curr(u) ((u)->inp_cache[(u)->inp_curr]) 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /WinToolsLib/stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // Console.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | 7 | // TODO: reference any additional headers you need in STDAFX.H 8 | // and not in this file 9 | -------------------------------------------------------------------------------- /WinToolsLib/stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, but 3 | // are changed infrequently 4 | #pragma once 5 | 6 | // C RunTime Header Files 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | // Windows Header Files 24 | #define WIN32_LEAN_AND_MEAN 25 | #include 26 | #undef WIN32_LEAN_AND_MEAN 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | 52 | // STL Header Files 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | #include 60 | #include 61 | #include 62 | #include 63 | #include 64 | #include 65 | #include 66 | #include 67 | #include 68 | #include 69 | #include 70 | #include 71 | #include 72 | #include 73 | #include 74 | #include 75 | #include 76 | #include 77 | #include 78 | #include 79 | #include 80 | #include 81 | #include 82 | #include 83 | #include 84 | --------------------------------------------------------------------------------