From 63d989cab40f17a8503a4d597923bf6dfa446323 Mon Sep 17 00:00:00 2001 From: Tomas Agustin Raies Date: Thu, 23 Apr 2020 17:11:02 -0700 Subject: [PATCH] Deprecate ATL based IPC wrapper library (#2248) * Deprecate ATL based IPC wrapper library * C# projects now use named pipe server implementations from two_way_pipe_message through the interop C++/Cli library. * Added Unit testing to interop library --- PowerToys.sln | 23 +- .../common-md-flag/common-md-flag.vcxproj | 3 + .../common-md-flag.vcxproj.filters | 9 + src/common/common.vcxproj | 4 +- src/common/common.vcxproj.filters | 12 +- .../interop-tests/Properties/AssemblyInfo.cs | 20 + src/common/interop-tests/UnitTest1.cs | 55 ++ src/common/interop-tests/interop-tests.csproj | 77 +++ src/common/interop-tests/packages.config | 5 + src/common/interop/interop.h | 76 ++- src/common/two_way_pipe_message_ipc.cpp | 498 ++++++++++++++++++ src/common/two_way_pipe_message_ipc.h | 489 +---------------- src/common/two_way_pipe_message_ipc_impl.h | 43 ++ ...ft.PowerToys.Settings.IPCWrapperPS.vcxproj | 210 -------- ...Toys.Settings.IPCWrapperPS.vcxproj.filters | 37 -- .../TwoWayIPCLib.cpp | 72 --- .../TwoWayIPCLib.def | 10 - .../TwoWayIPCLib.idl | 48 -- .../TwoWayIPCLib.rc | Bin 6080 -> 0 bytes .../TwoWayIPCLib.rgs | 3 - .../TwoWayIPCLib.vcxproj | 296 ----------- .../TwoWayIPCLib.vcxproj.filters | 85 --- .../TwoWayIPCLibps.def | 8 - .../TwoWayIPCManager.cpp | 54 -- .../TwoWayIPCManager.h | 59 --- .../TwoWayIPCManager.rgs | 26 - .../_ITwoWayIPCManagerEvents_CP.h | 9 - .../dllmain.cpp | 16 - .../dllmain.h | 10 - .../framework.h | 21 - .../pch.cpp | 5 - .../pch.h | 13 - .../resource.h | 18 - .../targetver.h | 8 - .../MainWindow.xaml.cs | 2 +- ...rosoft.PowerToys.Settings.UI.Runner.csproj | 1 + .../Program.cs | 9 +- src/settings/main.cpp | 1 + 38 files changed, 820 insertions(+), 1515 deletions(-) create mode 100644 src/common/interop-tests/Properties/AssemblyInfo.cs create mode 100644 src/common/interop-tests/UnitTest1.cs create mode 100644 src/common/interop-tests/interop-tests.csproj create mode 100644 src/common/interop-tests/packages.config create mode 100644 src/common/two_way_pipe_message_ipc.cpp create mode 100644 src/common/two_way_pipe_message_ipc_impl.h delete mode 100644 src/core/Microsoft.PowerToys.Settings.IPCWrapperPS/Microsoft.PowerToys.Settings.IPCWrapperPS.vcxproj delete mode 100644 src/core/Microsoft.PowerToys.Settings.IPCWrapperPS/Microsoft.PowerToys.Settings.IPCWrapperPS.vcxproj.filters delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.cpp delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.def delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.idl delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.rc delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.rgs delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.vcxproj delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.vcxproj.filters delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLibps.def delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.cpp delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.h delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.rgs delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/_ITwoWayIPCManagerEvents_CP.h delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/dllmain.cpp delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/dllmain.h delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/framework.h delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/pch.cpp delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/pch.h delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/resource.h delete mode 100644 src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/targetver.h diff --git a/PowerToys.sln b/PowerToys.sln index 684cad7af5..c0c95b9f3a 100644 --- a/PowerToys.sln +++ b/PowerToys.sln @@ -189,16 +189,9 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core", "core", "{C3081D9A-1586-441A-B5F4-ED815B3719C1}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.PowerToys.Settings.UI.Runner", "src\core\Microsoft.PowerToys.Settings.UI.Runner\Microsoft.PowerToys.Settings.UI.Runner.csproj", "{E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F}" - ProjectSection(ProjectDependencies) = postProject - {F88B6FD1-14BD-48DB-85C2-6C51B8045121} = {F88B6FD1-14BD-48DB-85C2-6C51B8045121} - EndProjectSection -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Microsoft.PowerToys.Settings.TwoWayIPCLib", "src\core\Microsoft.PowerToys.Settings.TwoWayIPCLib\TwoWayIPCLib.vcxproj", "{F88B6FD1-14BD-48DB-85C2-6C51B8045121}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.PowerToys.Settings.UI", "src\core\Microsoft.PowerToys.Settings.UI\Microsoft.PowerToys.Settings.UI.csproj", "{A7D5099E-F0FD-4BF3-8522-5A682759F915}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Microsoft.PowerToys.Settings.IPCWrapperPS", "src\core\Microsoft.PowerToys.Settings.IPCWrapperPS\Microsoft.PowerToys.Settings.IPCWrapperPS.vcxproj", "{C073B057-B157-40F0-8678-1DCD119D841C}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4981CCD1-4CD9-4A49-B240-00AA46493FF8}" ProjectSection(SolutionItems) = preProject .editorconfig = .editorconfig @@ -213,6 +206,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "interop", "src\common\inter EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "common-md-flag", "src\common\common-md-flag\common-md-flag.vcxproj", "{985B3F2F-CEED-4C0A-A249-69257E719145}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "interop-tests", "src\common\interop-tests\interop-tests.csproj", "{437AD818-3F1F-4CA5-A79B-25233A157026}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -375,16 +370,11 @@ Global {E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F}.Debug|x64.Build.0 = Debug|x64 {E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F}.Release|x64.ActiveCfg = Release|x64 {E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F}.Release|x64.Build.0 = Release|x64 - {F88B6FD1-14BD-48DB-85C2-6C51B8045121}.Debug|x64.ActiveCfg = Debug|x64 - {F88B6FD1-14BD-48DB-85C2-6C51B8045121}.Debug|x64.Build.0 = Debug|x64 - {F88B6FD1-14BD-48DB-85C2-6C51B8045121}.Release|x64.ActiveCfg = Release|x64 - {F88B6FD1-14BD-48DB-85C2-6C51B8045121}.Release|x64.Build.0 = Release|x64 {A7D5099E-F0FD-4BF3-8522-5A682759F915}.Debug|x64.ActiveCfg = Debug|x64 {A7D5099E-F0FD-4BF3-8522-5A682759F915}.Debug|x64.Build.0 = Debug|x64 {A7D5099E-F0FD-4BF3-8522-5A682759F915}.Release|x64.ActiveCfg = Release|x64 {A7D5099E-F0FD-4BF3-8522-5A682759F915}.Release|x64.Build.0 = Release|x64 - {C073B057-B157-40F0-8678-1DCD119D841C}.Debug|x64.ActiveCfg = Debug|x64 - {C073B057-B157-40F0-8678-1DCD119D841C}.Release|x64.ActiveCfg = Release|x64 + {A7D5099E-F0FD-4BF3-8522-5A682759F915}.Release|x64.Deploy.0 = Release|x64 {B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Debug|x64.ActiveCfg = Debug|x64 {B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Debug|x64.Build.0 = Debug|x64 {B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A}.Release|x64.ActiveCfg = Release|x64 @@ -401,6 +391,10 @@ Global {985B3F2F-CEED-4C0A-A249-69257E719145}.Debug|x64.Build.0 = Debug|x64 {985B3F2F-CEED-4C0A-A249-69257E719145}.Release|x64.ActiveCfg = Release|x64 {985B3F2F-CEED-4C0A-A249-69257E719145}.Release|x64.Build.0 = Release|x64 + {437AD818-3F1F-4CA5-A79B-25233A157026}.Debug|x64.ActiveCfg = Debug|x64 + {437AD818-3F1F-4CA5-A79B-25233A157026}.Debug|x64.Build.0 = Debug|x64 + {437AD818-3F1F-4CA5-A79B-25233A157026}.Release|x64.ActiveCfg = Release|x64 + {437AD818-3F1F-4CA5-A79B-25233A157026}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -449,13 +443,12 @@ Global {217DF501-135C-4E38-BFC8-99D4821032EA} = {2F305555-C296-497E-AC20-5FA1B237996A} {47310AB4-9034-4BD1-8D8B-E88AD21A171B} = {2F305555-C296-497E-AC20-5FA1B237996A} {E4E0D2AE-B17D-4BD4-8BEE-AFC8CC464C5F} = {C3081D9A-1586-441A-B5F4-ED815B3719C1} - {F88B6FD1-14BD-48DB-85C2-6C51B8045121} = {C3081D9A-1586-441A-B5F4-ED815B3719C1} {A7D5099E-F0FD-4BF3-8522-5A682759F915} = {C3081D9A-1586-441A-B5F4-ED815B3719C1} - {C073B057-B157-40F0-8678-1DCD119D841C} = {C3081D9A-1586-441A-B5F4-ED815B3719C1} {B1BCC8C6-46B5-4BFA-8F22-20F32D99EC6A} = {C3081D9A-1586-441A-B5F4-ED815B3719C1} {A80355C2-780D-4245-BD80-25B8DE698EE3} = {C3081D9A-1586-441A-B5F4-ED815B3719C1} {F055103B-F80B-4D0C-BF48-057C55620033} = {1AFB6476-670D-4E80-A464-657E01DFF482} {985B3F2F-CEED-4C0A-A249-69257E719145} = {1AFB6476-670D-4E80-A464-657E01DFF482} + {437AD818-3F1F-4CA5-A79B-25233A157026} = {1AFB6476-670D-4E80-A464-657E01DFF482} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0} diff --git a/src/common/common-md-flag/common-md-flag.vcxproj b/src/common/common-md-flag/common-md-flag.vcxproj index 4e3cac1d47..ec3785e16f 100644 --- a/src/common/common-md-flag/common-md-flag.vcxproj +++ b/src/common/common-md-flag/common-md-flag.vcxproj @@ -86,11 +86,14 @@ + + + Create Create diff --git a/src/common/common-md-flag/common-md-flag.vcxproj.filters b/src/common/common-md-flag/common-md-flag.vcxproj.filters index 8cb6132a95..7ecac76e58 100644 --- a/src/common/common-md-flag/common-md-flag.vcxproj.filters +++ b/src/common/common-md-flag/common-md-flag.vcxproj.filters @@ -27,6 +27,12 @@ Header Files + + Header Files + + + Header Files + @@ -35,5 +41,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/src/common/common.vcxproj b/src/common/common.vcxproj index 505568a82a..0a2a1d7c95 100644 --- a/src/common/common.vcxproj +++ b/src/common/common.vcxproj @@ -126,6 +126,7 @@ + @@ -140,7 +141,7 @@ - + @@ -167,6 +168,7 @@ + diff --git a/src/common/common.vcxproj.filters b/src/common/common.vcxproj.filters index ad1096cdaa..941683584f 100644 --- a/src/common/common.vcxproj.filters +++ b/src/common/common.vcxproj.filters @@ -51,9 +51,6 @@ Header Files\Telemetry - - Header Files - Header Files @@ -108,6 +105,12 @@ Header Files + + Header Files + + + Header Files + @@ -174,6 +177,9 @@ Source Files + + Source Files + diff --git a/src/common/interop-tests/Properties/AssemblyInfo.cs b/src/common/interop-tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..5d57ad7547 --- /dev/null +++ b/src/common/interop-tests/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("interop-tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("interop-tests")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("437ad818-3f1f-4ca5-a79b-25233a157026")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/common/interop-tests/UnitTest1.cs b/src/common/interop-tests/UnitTest1.cs new file mode 100644 index 0000000000..283fb2d40c --- /dev/null +++ b/src/common/interop-tests/UnitTest1.cs @@ -0,0 +1,55 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.IO.Pipes; +using interop; +using System.IO; +using System.Threading; +using System.Text; + +namespace interop_tests +{ + [TestClass] + public class UnitTest1 + { + private string SERVER_SIDE = "\\\\.\\pipe\\serverside"; + private string CLIENT_SIDE = "\\\\.\\pipe\\clientside"; + + private TwoWayPipeMessageIPCManaged clientPipe; + + [TestInitialize] + public void Initialize() + { + clientPipe = new TwoWayPipeMessageIPCManaged(CLIENT_SIDE, SERVER_SIDE, null); + } + + [TestCleanup] + public void Cleanup() + { + clientPipe.End(); + } + + [TestMethod] + public void TestSend() + { + var testString = "This string is a test\n"; + var reset = new AutoResetEvent(false); + + var serverPipe = new TwoWayPipeMessageIPCManaged( + SERVER_SIDE, + CLIENT_SIDE, + (string msg) => + { + Assert.AreEqual(testString, msg); + reset.Set(); + }); + serverPipe.Start(); + clientPipe.Start(); + + clientPipe.Send(testString); + reset.WaitOne(); + + serverPipe.End(); + } + + } +} diff --git a/src/common/interop-tests/interop-tests.csproj b/src/common/interop-tests/interop-tests.csproj new file mode 100644 index 0000000000..82ba29d322 --- /dev/null +++ b/src/common/interop-tests/interop-tests.csproj @@ -0,0 +1,77 @@ + + + + + + Debug + AnyCPU + {437AD818-3F1F-4CA5-A79B-25233A157026} + Library + Properties + interop_tests + interop-tests + v4.7.2 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + bin\x64\Debug\ + DEBUG;TRACE + full + x64 + 7.3 + prompt + MinimumRecommendedRules.ruleset + + + bin\x64\Release\ + TRACE + true + pdbonly + x64 + 7.3 + prompt + MinimumRecommendedRules.ruleset + + + + ..\..\..\packages\MSTest.TestFramework.2.1.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\..\..\packages\MSTest.TestFramework.2.1.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + {f055103b-f80b-4d0c-bf48-057c55620033} + interop + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/src/common/interop-tests/packages.config b/src/common/interop-tests/packages.config new file mode 100644 index 0000000000..e7e0134e3c --- /dev/null +++ b/src/common/interop-tests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/common/interop/interop.h b/src/common/interop/interop.h index 85f3812cec..9690d50880 100644 --- a/src/common/interop/interop.h +++ b/src/common/interop/interop.h @@ -1,12 +1,19 @@ #pragma once #include +#include +#include +#include +#include + using namespace System; +using namespace System::Runtime::InteropServices; //https://docs.microsoft.com/en-us/cpp/dotnet/how-to-wrap-native-class-for-use-by-csharp?view=vs-2019 namespace interop { -public ref class LayoutMapManaged +public + ref class LayoutMapManaged { public: LayoutMapManaged() : @@ -17,12 +24,11 @@ public ref class LayoutMapManaged delete _map; } - String ^ GetKeyName(DWORD key) - { + String ^ GetKeyName(DWORD key) { return gcnew String(_map->GetKeyName(key).c_str()); } - void Updatelayout() + void Updatelayout() { _map->UpdateLayout(); } @@ -36,5 +42,65 @@ public ref class LayoutMapManaged private: LayoutMap* _map; }; -} +public + ref class TwoWayPipeMessageIPCManaged + { + public: + delegate void ReadCallback(String ^ message); + + TwoWayPipeMessageIPCManaged(String ^ inputPipeName, String ^ outputPipeName, ReadCallback ^ callback) + { + _wrapperCallback = gcnew InternalReadCallback(this, &TwoWayPipeMessageIPCManaged::ReadCallbackHelper); + _callback = callback; + + TwoWayPipeMessageIPC::callback_function cb = nullptr; + if (callback != nullptr) + { + cb = (TwoWayPipeMessageIPC::callback_function)(void*)Marshal::GetFunctionPointerForDelegate(_wrapperCallback); + } + _pipe = new TwoWayPipeMessageIPC( + msclr::interop::marshal_as(inputPipeName), + msclr::interop::marshal_as(outputPipeName), + cb); + } + + ~TwoWayPipeMessageIPCManaged() + { + delete _pipe; + } + + void Send(String ^ msg) + { + _pipe->send(msclr::interop::marshal_as(msg)); + } + + void Start() + { + _pipe->start(nullptr); + } + + void End() + { + _pipe->end(); + } + + protected: + !TwoWayPipeMessageIPCManaged() + { + delete _pipe; + } + + private: + delegate void InternalReadCallback(const std::wstring& msg); + + TwoWayPipeMessageIPC* _pipe; + ReadCallback ^ _callback; + InternalReadCallback ^ _wrapperCallback; + + void ReadCallbackHelper(const std::wstring& msg) + { + _callback(gcnew String(msg.c_str())); + } + }; +} diff --git a/src/common/two_way_pipe_message_ipc.cpp b/src/common/two_way_pipe_message_ipc.cpp new file mode 100644 index 0000000000..f22d409312 --- /dev/null +++ b/src/common/two_way_pipe_message_ipc.cpp @@ -0,0 +1,498 @@ +#include "pch.h" +#include "two_way_pipe_message_ipc_impl.h" + +#pragma comment(lib, "advapi32.lib") + +TwoWayPipeMessageIPC::TwoWayPipeMessageIPC( + std::wstring _input_pipe_name, + std::wstring _output_pipe_name, + callback_function p_func) : + impl(new TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl( + _input_pipe_name, + _output_pipe_name, + p_func)) +{ +} + +TwoWayPipeMessageIPC::~TwoWayPipeMessageIPC() +{ + delete impl; +} + +void TwoWayPipeMessageIPC::send(std::wstring msg) +{ + impl->send(msg); +} + +void TwoWayPipeMessageIPC::start(HANDLE _restricted_pipe_token) +{ + impl->start(_restricted_pipe_token); +} + +void TwoWayPipeMessageIPC::end() +{ + impl->end(); +} + + +TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::TwoWayPipeMessageIPCImpl( + std::wstring _input_pipe_name, + std::wstring _output_pipe_name, + callback_function p_func) +{ + input_pipe_name = _input_pipe_name; + output_pipe_name = _output_pipe_name; + dispatch_inc_message_function = p_func; +} + +void TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::send(std::wstring msg) +{ + output_queue.queue_message(msg); +} + +void TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::start(HANDLE _restricted_pipe_token) +{ + output_queue_thread = std::thread(&TwoWayPipeMessageIPCImpl::consume_output_queue_thread, this); + input_queue_thread = std::thread(&TwoWayPipeMessageIPCImpl::consume_input_queue_thread, this); + input_pipe_thread = std::thread(&TwoWayPipeMessageIPCImpl::start_named_pipe_server, this, _restricted_pipe_token); +} + +void TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::end() +{ + closed = true; + input_queue.interrupt(); + input_queue_thread.join(); + output_queue.interrupt(); + output_queue_thread.join(); + pipe_connect_handle_mutex.lock(); + if (current_connect_pipe_handle != NULL) + { + //Cancels the Pipe currently waiting for a connection. + CancelIoEx(current_connect_pipe_handle, NULL); + } + pipe_connect_handle_mutex.unlock(); + input_pipe_thread.join(); +} + +void TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::send_pipe_message(std::wstring message) +{ + // Adapted from https://docs.microsoft.com/en-us/windows/win32/ipc/named-pipe-client + HANDLE output_pipe_handle; + const wchar_t* message_send = message.c_str(); + BOOL fSuccess = FALSE; + DWORD cbToWrite, cbWritten, dwMode; + const wchar_t* lpszPipename = output_pipe_name.c_str(); + + // Try to open a named pipe; wait for it, if necessary. + + while (1) + { + output_pipe_handle = CreateFile( + lpszPipename, // pipe name + GENERIC_READ | // read and write access + GENERIC_WRITE, + 0, // no sharing + NULL, // default security attributes + OPEN_EXISTING, // opens existing pipe + 0, // default attributes + NULL); // no template file + + // Break if the pipe handle is valid. + + if (output_pipe_handle != INVALID_HANDLE_VALUE) + break; + + // Exit if an error other than ERROR_PIPE_BUSY occurs. + DWORD curr_error = 0; + if ((curr_error = GetLastError()) != ERROR_PIPE_BUSY) + { + return; + } + + // All pipe instances are busy, so wait for 20 seconds. + + if (!WaitNamedPipe(lpszPipename, 20000)) + { + return; + } + } + dwMode = PIPE_READMODE_MESSAGE; + fSuccess = SetNamedPipeHandleState( + output_pipe_handle, // pipe handle + &dwMode, // new pipe mode + NULL, // don't set maximum bytes + NULL); // don't set maximum time + if (!fSuccess) + { + return; + } + + // Send a message to the pipe server. + + cbToWrite = (lstrlen(message_send)) * sizeof(WCHAR); // no need to send final '\0'. Pipe is in message mode. + + fSuccess = WriteFile( + output_pipe_handle, // pipe handle + message_send, // message + cbToWrite, // message length + &cbWritten, // bytes written + NULL); // not overlapped + if (!fSuccess) + { + return; + } + CloseHandle(output_pipe_handle); + return; +} + +void TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::consume_output_queue_thread() +{ + while (!closed) + { + std::wstring message = output_queue.pop_message(); + if (message.length() == 0) + { + break; + } + send_pipe_message(message); + } +} + +BOOL TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::GetLogonSID(HANDLE hToken, PSID* ppsid) +{ + // From https://docs.microsoft.com/en-us/previous-versions/aa446670(v=vs.85) + BOOL bSuccess = FALSE; + DWORD dwIndex; + DWORD dwLength = 0; + PTOKEN_GROUPS ptg = NULL; + + // Verify the parameter passed in is not NULL. + if (NULL == ppsid) + goto Cleanup; + + // Get required buffer size and allocate the TOKEN_GROUPS buffer. + + if (!GetTokenInformation( + hToken, // handle to the access token + TokenGroups, // get information about the token's groups + (LPVOID)ptg, // pointer to TOKEN_GROUPS buffer + 0, // size of buffer + &dwLength // receives required buffer size + )) + { + if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + goto Cleanup; + + ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(), + HEAP_ZERO_MEMORY, + dwLength); + + if (ptg == NULL) + goto Cleanup; + } + + // Get the token group information from the access token. + + if (!GetTokenInformation( + hToken, // handle to the access token + TokenGroups, // get information about the token's groups + (LPVOID)ptg, // pointer to TOKEN_GROUPS buffer + dwLength, // size of buffer + &dwLength // receives required buffer size + )) + { + goto Cleanup; + } + + // Loop through the groups to find the logon SID. + + for (dwIndex = 0; dwIndex < ptg->GroupCount; dwIndex++) + if ((ptg->Groups[dwIndex].Attributes & SE_GROUP_LOGON_ID) == SE_GROUP_LOGON_ID) + { + // Found the logon SID; make a copy of it. + + dwLength = GetLengthSid(ptg->Groups[dwIndex].Sid); + *ppsid = (PSID)HeapAlloc(GetProcessHeap(), + HEAP_ZERO_MEMORY, + dwLength); + if (*ppsid == NULL) + goto Cleanup; + if (!CopySid(dwLength, *ppsid, ptg->Groups[dwIndex].Sid)) + { + HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid); + goto Cleanup; + } + break; + } + + bSuccess = TRUE; + +Cleanup: + + // Free the buffer for the token groups. + + if (ptg != NULL) + HeapFree(GetProcessHeap(), 0, (LPVOID)ptg); + + return bSuccess; +} + +VOID TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::FreeLogonSID(PSID* ppsid) +{ + // From https://docs.microsoft.com/en-us/previous-versions/aa446670(v=vs.85) + HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid); +} + +int TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::change_pipe_security_allow_restricted_token(HANDLE handle, HANDLE token) +{ + PACL old_dacl, new_dacl; + PSECURITY_DESCRIPTOR sd; + EXPLICIT_ACCESS ea; + PSID user_restricted; + int error; + + if (!GetLogonSID(token, &user_restricted)) + { + error = 5; // No access error. + goto Ldone; + } + + if (GetSecurityInfo(handle, + SE_KERNEL_OBJECT, + DACL_SECURITY_INFORMATION, + NULL, + NULL, + &old_dacl, + NULL, + &sd)) + { + error = GetLastError(); + goto Lclean_sid; + } + + memset(&ea, 0, sizeof(EXPLICIT_ACCESS)); + ea.grfAccessPermissions |= GENERIC_READ | FILE_WRITE_ATTRIBUTES; + ea.grfAccessPermissions |= GENERIC_WRITE | FILE_READ_ATTRIBUTES; + ea.grfAccessPermissions |= SYNCHRONIZE; + ea.grfAccessMode = SET_ACCESS; + ea.grfInheritance = NO_INHERITANCE; + ea.Trustee.TrusteeForm = TRUSTEE_IS_SID; + ea.Trustee.TrusteeType = TRUSTEE_IS_USER; + ea.Trustee.ptstrName = (LPTSTR)user_restricted; + + if (SetEntriesInAcl(1, &ea, old_dacl, &new_dacl)) + { + error = GetLastError(); + goto Lclean_sd; + } + + if (SetSecurityInfo(handle, + SE_KERNEL_OBJECT, + DACL_SECURITY_INFORMATION, + NULL, + NULL, + new_dacl, + NULL)) + { + error = GetLastError(); + goto Lclean_dacl; + } + + error = 0; + +Lclean_dacl: + LocalFree((HLOCAL)new_dacl); +Lclean_sd: + LocalFree((HLOCAL)sd); +Lclean_sid: + FreeLogonSID(&user_restricted); +Ldone: + return error; +} + +HANDLE TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::create_medium_integrity_token() +{ + HANDLE restricted_token_handle; + SAFER_LEVEL_HANDLE level_handle = NULL; + DWORD sid_size = SECURITY_MAX_SID_SIZE; + BYTE medium_sid[SECURITY_MAX_SID_SIZE]; + if (!SaferCreateLevel(SAFER_SCOPEID_USER, SAFER_LEVELID_NORMALUSER, SAFER_LEVEL_OPEN, &level_handle, NULL)) + { + return NULL; + } + if (!SaferComputeTokenFromLevel(level_handle, NULL, &restricted_token_handle, 0, NULL)) + { + SaferCloseLevel(level_handle); + return NULL; + } + SaferCloseLevel(level_handle); + + if (!CreateWellKnownSid(WinMediumLabelSid, nullptr, medium_sid, &sid_size)) + { + CloseHandle(restricted_token_handle); + return NULL; + } + + TOKEN_MANDATORY_LABEL integrity_level = { 0 }; + integrity_level.Label.Attributes = SE_GROUP_INTEGRITY; + integrity_level.Label.Sid = reinterpret_cast(medium_sid); + + if (!SetTokenInformation(restricted_token_handle, TokenIntegrityLevel, &integrity_level, sizeof(integrity_level))) + { + CloseHandle(restricted_token_handle); + return NULL; + } + + return restricted_token_handle; +} + +void TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::handle_pipe_connection(HANDLE input_pipe_handle) +{ + //Adapted from https://docs.microsoft.com/en-us/windows/win32/ipc/multithreaded-pipe-server + HANDLE hHeap = GetProcessHeap(); + uint8_t* pchRequest = (uint8_t*)HeapAlloc(hHeap, 0, BUFSIZE * sizeof(uint8_t)); + + DWORD cbBytesRead = 0, cbReplyBytes = 0, cbWritten = 0; + BOOL fSuccess = FALSE; + + // Do some extra error checking since the app will keep running even if this thread fails. + std::list> message_parts; + + if (input_pipe_handle == NULL) + { + if (pchRequest != NULL) + HeapFree(hHeap, 0, pchRequest); + return; + } + + if (pchRequest == NULL) + { + return; + } + + // Loop until done reading + do + { + // Read client requests from the pipe. This simplistic code only allows messages + // up to BUFSIZE characters in length. + ZeroMemory(pchRequest, BUFSIZE * sizeof(uint8_t)); + fSuccess = ReadFile( + input_pipe_handle, // handle to pipe + pchRequest, // buffer to receive data + BUFSIZE * sizeof(uint8_t), // size of buffer + &cbBytesRead, // number of bytes read + NULL); // not overlapped I/O + + if (!fSuccess && GetLastError() != ERROR_MORE_DATA) + { + break; + } + std::vector part_vector; + part_vector.reserve(cbBytesRead); + std::copy(pchRequest, pchRequest + cbBytesRead, std::back_inserter(part_vector)); + message_parts.push_back(part_vector); + } while (!fSuccess); + + if (fSuccess) + { + // Reconstruct the total_message. + std::vector reconstructed_message; + size_t total_size = 0; + for (auto& part_vector : message_parts) + { + total_size += part_vector.size(); + } + reconstructed_message.reserve(total_size); + for (auto& part_vector : message_parts) + { + std::move(part_vector.begin(), part_vector.end(), std::back_inserter(reconstructed_message)); + } + std::wstring unicode_msg; + unicode_msg.assign(reinterpret_cast(reconstructed_message.data()), reconstructed_message.size() / sizeof(std::wstring::value_type)); + input_queue.queue_message(unicode_msg); + } + + // Flush the pipe to allow the client to read the pipe's contents + // before disconnecting. Then disconnect the pipe, and close the + // handle to this pipe instance. + + FlushFileBuffers(input_pipe_handle); + DisconnectNamedPipe(input_pipe_handle); + CloseHandle(input_pipe_handle); + + HeapFree(hHeap, 0, pchRequest); + + printf("InstanceThread exitting.\n"); +} + +void TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::start_named_pipe_server(HANDLE token) +{ + // Adapted from https://docs.microsoft.com/en-us/windows/win32/ipc/multithreaded-pipe-server + const wchar_t* pipe_name = input_pipe_name.c_str(); + BOOL connected = FALSE; + HANDLE connect_pipe_handle = INVALID_HANDLE_VALUE; + while (!closed) + { + { + std::unique_lock lock(pipe_connect_handle_mutex); + connect_pipe_handle = CreateNamedPipe( + pipe_name, + PIPE_ACCESS_DUPLEX | + WRITE_DAC, + PIPE_TYPE_MESSAGE | + PIPE_READMODE_MESSAGE | + PIPE_WAIT, + PIPE_UNLIMITED_INSTANCES, + BUFSIZE, + BUFSIZE, + 0, + NULL); + + if (connect_pipe_handle == INVALID_HANDLE_VALUE) + { + return; + } + + if (token != NULL) + { + int err = change_pipe_security_allow_restricted_token(connect_pipe_handle, token); + } + current_connect_pipe_handle = connect_pipe_handle; + } + connected = ConnectNamedPipe(connect_pipe_handle, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); + { + std::unique_lock lock(pipe_connect_handle_mutex); + current_connect_pipe_handle = NULL; + } + if (connected) + { + std::thread(&TwoWayPipeMessageIPCImpl::handle_pipe_connection, this, connect_pipe_handle).detach(); + } + else + { + // Client could not connect. + CloseHandle(connect_pipe_handle); + } + } +} + +void TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl::consume_input_queue_thread() +{ + while (!closed) + { + outgoing_message = L""; + std::wstring message = input_queue.pop_message(); + if (message.length() == 0) + { + break; + } + + // Check if callback method exists first before trying to call it. + // otherwise just store the response message in a variable. + if (dispatch_inc_message_function != nullptr) + { + dispatch_inc_message_function(message); + } + outgoing_message = message; + } +} \ No newline at end of file diff --git a/src/common/two_way_pipe_message_ipc.h b/src/common/two_way_pipe_message_ipc.h index 6387fa77a7..bcf7bd4c24 100644 --- a/src/common/two_way_pipe_message_ipc.h +++ b/src/common/two_way_pipe_message_ipc.h @@ -1,485 +1,18 @@ #pragma once -#include -#include "async_message_queue.h" -#include -#include -#include -#include -#include - class TwoWayPipeMessageIPC { public: typedef void (*callback_function)(const std::wstring&); - void send(std::wstring msg) - { - output_queue.queue_message(msg); - } - TwoWayPipeMessageIPC(std::wstring _input_pipe_name, std::wstring _output_pipe_name, callback_function p_func) - { - input_pipe_name = _input_pipe_name; - output_pipe_name = _output_pipe_name; - dispatch_inc_message_function = p_func; - } - void start(HANDLE _restricted_pipe_token) - { - output_queue_thread = std::thread(&TwoWayPipeMessageIPC::consume_output_queue_thread, this); - input_queue_thread = std::thread(&TwoWayPipeMessageIPC::consume_input_queue_thread, this); - input_pipe_thread = std::thread(&TwoWayPipeMessageIPC::start_named_pipe_server, this, _restricted_pipe_token); - } - - void end() - { - closed = true; - input_queue.interrupt(); - input_queue_thread.join(); - output_queue.interrupt(); - output_queue_thread.join(); - pipe_connect_handle_mutex.lock(); - if (current_connect_pipe_handle != NULL) - { - //Cancels the Pipe currently waiting for a connection. - CancelIoEx(current_connect_pipe_handle, NULL); - } - pipe_connect_handle_mutex.unlock(); - input_pipe_thread.join(); - } + TwoWayPipeMessageIPC( + std::wstring _input_pipe_name, + std::wstring _output_pipe_name, + callback_function p_func); + ~TwoWayPipeMessageIPC(); + void send(std::wstring msg); + void start(HANDLE _restricted_pipe_token); + void end(); private: - AsyncMessageQueue input_queue; - AsyncMessageQueue output_queue; - std::wstring output_pipe_name; - std::wstring input_pipe_name; - std::thread input_queue_thread; - std::thread output_queue_thread; - std::thread input_pipe_thread; - std::mutex pipe_connect_handle_mutex; // For manipulating the current_connect_pipe - std::wstring outgoing_message; // Store the updated json settings. - - HANDLE current_connect_pipe_handle = NULL; - bool closed = false; - TwoWayPipeMessageIPC::callback_function dispatch_inc_message_function; - const DWORD BUFSIZE = 1024; - - void send_pipe_message(std::wstring message) - { - // Adapted from https://docs.microsoft.com/en-us/windows/win32/ipc/named-pipe-client - HANDLE output_pipe_handle; - const wchar_t* message_send = message.c_str(); - BOOL fSuccess = FALSE; - DWORD cbToWrite, cbWritten, dwMode; - const wchar_t* lpszPipename = output_pipe_name.c_str(); - - // Try to open a named pipe; wait for it, if necessary. - - while (1) - { - output_pipe_handle = CreateFile( - lpszPipename, // pipe name - GENERIC_READ | // read and write access - GENERIC_WRITE, - 0, // no sharing - NULL, // default security attributes - OPEN_EXISTING, // opens existing pipe - 0, // default attributes - NULL); // no template file - - // Break if the pipe handle is valid. - - if (output_pipe_handle != INVALID_HANDLE_VALUE) - break; - - // Exit if an error other than ERROR_PIPE_BUSY occurs. - DWORD curr_error = 0; - if ((curr_error = GetLastError()) != ERROR_PIPE_BUSY) - { - return; - } - - // All pipe instances are busy, so wait for 20 seconds. - - if (!WaitNamedPipe(lpszPipename, 20000)) - { - return; - } - } - dwMode = PIPE_READMODE_MESSAGE; - fSuccess = SetNamedPipeHandleState( - output_pipe_handle, // pipe handle - &dwMode, // new pipe mode - NULL, // don't set maximum bytes - NULL); // don't set maximum time - if (!fSuccess) - { - return; - } - - // Send a message to the pipe server. - - cbToWrite = (lstrlen(message_send)) * sizeof(WCHAR); // no need to send final '\0'. Pipe is in message mode. - - fSuccess = WriteFile( - output_pipe_handle, // pipe handle - message_send, // message - cbToWrite, // message length - &cbWritten, // bytes written - NULL); // not overlapped - if (!fSuccess) - { - return; - } - CloseHandle(output_pipe_handle); - return; - } - void consume_output_queue_thread() - { - while (!closed) - { - std::wstring message = output_queue.pop_message(); - if (message.length() == 0) - { - break; - } - send_pipe_message(message); - } - } - - BOOL GetLogonSID(HANDLE hToken, PSID* ppsid) - { - // From https://docs.microsoft.com/en-us/previous-versions/aa446670(v=vs.85) - BOOL bSuccess = FALSE; - DWORD dwIndex; - DWORD dwLength = 0; - PTOKEN_GROUPS ptg = NULL; - - // Verify the parameter passed in is not NULL. - if (NULL == ppsid) - goto Cleanup; - - // Get required buffer size and allocate the TOKEN_GROUPS buffer. - - if (!GetTokenInformation( - hToken, // handle to the access token - TokenGroups, // get information about the token's groups - (LPVOID)ptg, // pointer to TOKEN_GROUPS buffer - 0, // size of buffer - &dwLength // receives required buffer size - )) - { - if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) - goto Cleanup; - - ptg = (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(), - HEAP_ZERO_MEMORY, - dwLength); - - if (ptg == NULL) - goto Cleanup; - } - - // Get the token group information from the access token. - - if (!GetTokenInformation( - hToken, // handle to the access token - TokenGroups, // get information about the token's groups - (LPVOID)ptg, // pointer to TOKEN_GROUPS buffer - dwLength, // size of buffer - &dwLength // receives required buffer size - )) - { - goto Cleanup; - } - - // Loop through the groups to find the logon SID. - - for (dwIndex = 0; dwIndex < ptg->GroupCount; dwIndex++) - if ((ptg->Groups[dwIndex].Attributes & SE_GROUP_LOGON_ID) == SE_GROUP_LOGON_ID) - { - // Found the logon SID; make a copy of it. - - dwLength = GetLengthSid(ptg->Groups[dwIndex].Sid); - *ppsid = (PSID)HeapAlloc(GetProcessHeap(), - HEAP_ZERO_MEMORY, - dwLength); - if (*ppsid == NULL) - goto Cleanup; - if (!CopySid(dwLength, *ppsid, ptg->Groups[dwIndex].Sid)) - { - HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid); - goto Cleanup; - } - break; - } - - bSuccess = TRUE; - - Cleanup: - - // Free the buffer for the token groups. - - if (ptg != NULL) - HeapFree(GetProcessHeap(), 0, (LPVOID)ptg); - - return bSuccess; - } - - VOID FreeLogonSID(PSID* ppsid) - { - // From https://docs.microsoft.com/en-us/previous-versions/aa446670(v=vs.85) - HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid); - } - - int change_pipe_security_allow_restricted_token(HANDLE handle, HANDLE token) - { - PACL old_dacl, new_dacl; - PSECURITY_DESCRIPTOR sd; - EXPLICIT_ACCESS ea; - PSID user_restricted; - int error; - - if (!GetLogonSID(token, &user_restricted)) - { - error = 5; // No access error. - goto Ldone; - } - - if (GetSecurityInfo(handle, - SE_KERNEL_OBJECT, - DACL_SECURITY_INFORMATION, - NULL, - NULL, - &old_dacl, - NULL, - &sd)) - { - error = GetLastError(); - goto Lclean_sid; - } - - memset(&ea, 0, sizeof(EXPLICIT_ACCESS)); - ea.grfAccessPermissions |= GENERIC_READ | FILE_WRITE_ATTRIBUTES; - ea.grfAccessPermissions |= GENERIC_WRITE | FILE_READ_ATTRIBUTES; - ea.grfAccessPermissions |= SYNCHRONIZE; - ea.grfAccessMode = SET_ACCESS; - ea.grfInheritance = NO_INHERITANCE; - ea.Trustee.TrusteeForm = TRUSTEE_IS_SID; - ea.Trustee.TrusteeType = TRUSTEE_IS_USER; - ea.Trustee.ptstrName = (LPTSTR)user_restricted; - - if (SetEntriesInAcl(1, &ea, old_dacl, &new_dacl)) - { - error = GetLastError(); - goto Lclean_sd; - } - - if (SetSecurityInfo(handle, - SE_KERNEL_OBJECT, - DACL_SECURITY_INFORMATION, - NULL, - NULL, - new_dacl, - NULL)) - { - error = GetLastError(); - goto Lclean_dacl; - } - - error = 0; - - Lclean_dacl: - LocalFree((HLOCAL)new_dacl); - Lclean_sd: - LocalFree((HLOCAL)sd); - Lclean_sid: - FreeLogonSID(&user_restricted); - Ldone: - return error; - } - - HANDLE create_medium_integrity_token() - { - HANDLE restricted_token_handle; - SAFER_LEVEL_HANDLE level_handle = NULL; - DWORD sid_size = SECURITY_MAX_SID_SIZE; - BYTE medium_sid[SECURITY_MAX_SID_SIZE]; - if (!SaferCreateLevel(SAFER_SCOPEID_USER, SAFER_LEVELID_NORMALUSER, SAFER_LEVEL_OPEN, &level_handle, NULL)) - { - return NULL; - } - if (!SaferComputeTokenFromLevel(level_handle, NULL, &restricted_token_handle, 0, NULL)) - { - SaferCloseLevel(level_handle); - return NULL; - } - SaferCloseLevel(level_handle); - - if (!CreateWellKnownSid(WinMediumLabelSid, nullptr, medium_sid, &sid_size)) - { - CloseHandle(restricted_token_handle); - return NULL; - } - - TOKEN_MANDATORY_LABEL integrity_level = { 0 }; - integrity_level.Label.Attributes = SE_GROUP_INTEGRITY; - integrity_level.Label.Sid = reinterpret_cast(medium_sid); - - if (!SetTokenInformation(restricted_token_handle, TokenIntegrityLevel, &integrity_level, sizeof(integrity_level))) - { - CloseHandle(restricted_token_handle); - return NULL; - } - - return restricted_token_handle; - } - - void handle_pipe_connection(HANDLE input_pipe_handle) - { - //Adapted from https://docs.microsoft.com/en-us/windows/win32/ipc/multithreaded-pipe-server - HANDLE hHeap = GetProcessHeap(); - uint8_t* pchRequest = (uint8_t*)HeapAlloc(hHeap, 0, BUFSIZE * sizeof(uint8_t)); - - DWORD cbBytesRead = 0, cbReplyBytes = 0, cbWritten = 0; - BOOL fSuccess = FALSE; - - // Do some extra error checking since the app will keep running even if this thread fails. - std::list> message_parts; - - if (input_pipe_handle == NULL) - { - if (pchRequest != NULL) - HeapFree(hHeap, 0, pchRequest); - return; - } - - if (pchRequest == NULL) - { - return; - } - - // Loop until done reading - do - { - // Read client requests from the pipe. This simplistic code only allows messages - // up to BUFSIZE characters in length. - ZeroMemory(pchRequest, BUFSIZE * sizeof(uint8_t)); - fSuccess = ReadFile( - input_pipe_handle, // handle to pipe - pchRequest, // buffer to receive data - BUFSIZE * sizeof(uint8_t), // size of buffer - &cbBytesRead, // number of bytes read - NULL); // not overlapped I/O - - if (!fSuccess && GetLastError() != ERROR_MORE_DATA) - { - break; - } - std::vector part_vector; - part_vector.reserve(cbBytesRead); - std::copy(pchRequest, pchRequest + cbBytesRead, std::back_inserter(part_vector)); - message_parts.push_back(part_vector); - } while (!fSuccess); - - if (fSuccess) - { - // Reconstruct the total_message. - std::vector reconstructed_message; - size_t total_size = 0; - for (auto& part_vector : message_parts) - { - total_size += part_vector.size(); - } - reconstructed_message.reserve(total_size); - for (auto& part_vector : message_parts) - { - std::move(part_vector.begin(), part_vector.end(), std::back_inserter(reconstructed_message)); - } - std::wstring unicode_msg; - unicode_msg.assign(reinterpret_cast(reconstructed_message.data()), reconstructed_message.size() / sizeof(std::wstring::value_type)); - input_queue.queue_message(unicode_msg); - } - - // Flush the pipe to allow the client to read the pipe's contents - // before disconnecting. Then disconnect the pipe, and close the - // handle to this pipe instance. - - FlushFileBuffers(input_pipe_handle); - DisconnectNamedPipe(input_pipe_handle); - CloseHandle(input_pipe_handle); - - HeapFree(hHeap, 0, pchRequest); - - printf("InstanceThread exitting.\n"); - } - - void start_named_pipe_server(HANDLE token) - { - // Adapted from https://docs.microsoft.com/en-us/windows/win32/ipc/multithreaded-pipe-server - const wchar_t* pipe_name = input_pipe_name.c_str(); - BOOL connected = FALSE; - HANDLE connect_pipe_handle = INVALID_HANDLE_VALUE; - while (!closed) - { - { - std::unique_lock lock(pipe_connect_handle_mutex); - connect_pipe_handle = CreateNamedPipe( - pipe_name, - PIPE_ACCESS_DUPLEX | - WRITE_DAC, - PIPE_TYPE_MESSAGE | - PIPE_READMODE_MESSAGE | - PIPE_WAIT, - PIPE_UNLIMITED_INSTANCES, - BUFSIZE, - BUFSIZE, - 0, - NULL); - - if (connect_pipe_handle == INVALID_HANDLE_VALUE) - { - return; - } - - if (token != NULL) - { - int err = change_pipe_security_allow_restricted_token(connect_pipe_handle, token); - } - current_connect_pipe_handle = connect_pipe_handle; - } - connected = ConnectNamedPipe(connect_pipe_handle, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); - { - std::unique_lock lock(pipe_connect_handle_mutex); - current_connect_pipe_handle = NULL; - } - if (connected) - { - std::thread(&TwoWayPipeMessageIPC::handle_pipe_connection, this, connect_pipe_handle).detach(); - } - else - { - // Client could not connect. - CloseHandle(connect_pipe_handle); - } - } - } - - void consume_input_queue_thread() - { - while (!closed) - { - outgoing_message = L""; - std::wstring message = input_queue.pop_message(); - if (message.length() == 0) - { - break; - } - - // Check if callback method exists first before trying to call it. - // otherwise just store the response message in a variable. - if (dispatch_inc_message_function != nullptr) - { - dispatch_inc_message_function(message); - } - outgoing_message = message; - } - } -}; + class TwoWayPipeMessageIPCImpl; + TwoWayPipeMessageIPCImpl* impl; +}; \ No newline at end of file diff --git a/src/common/two_way_pipe_message_ipc_impl.h b/src/common/two_way_pipe_message_ipc_impl.h new file mode 100644 index 0000000000..401fbacb95 --- /dev/null +++ b/src/common/two_way_pipe_message_ipc_impl.h @@ -0,0 +1,43 @@ +#pragma once +#include +#include "async_message_queue.h" +#include +#include +#include +#include +#include "two_way_pipe_message_ipc.h" + +class TwoWayPipeMessageIPC::TwoWayPipeMessageIPCImpl +{ +public: + void send(std::wstring msg); + TwoWayPipeMessageIPCImpl(std::wstring _input_pipe_name, std::wstring _output_pipe_name, callback_function p_func); + void start(HANDLE _restricted_pipe_token); + void end(); + +private: + AsyncMessageQueue input_queue; + AsyncMessageQueue output_queue; + std::wstring output_pipe_name; + std::wstring input_pipe_name; + std::thread input_queue_thread; + std::thread output_queue_thread; + std::thread input_pipe_thread; + std::mutex pipe_connect_handle_mutex; // For manipulating the current_connect_pipe + std::wstring outgoing_message; // Store the updated json settings. + + HANDLE current_connect_pipe_handle = NULL; + bool closed = false; + TwoWayPipeMessageIPC::callback_function dispatch_inc_message_function; + const DWORD BUFSIZE = 1024; + + void send_pipe_message(std::wstring message); + void consume_output_queue_thread(); + BOOL GetLogonSID(HANDLE hToken, PSID* ppsid); + VOID FreeLogonSID(PSID* ppsid); + int change_pipe_security_allow_restricted_token(HANDLE handle, HANDLE token); + HANDLE create_medium_integrity_token(); + void handle_pipe_connection(HANDLE input_pipe_handle); + void start_named_pipe_server(HANDLE token); + void consume_input_queue_thread(); +}; diff --git a/src/core/Microsoft.PowerToys.Settings.IPCWrapperPS/Microsoft.PowerToys.Settings.IPCWrapperPS.vcxproj b/src/core/Microsoft.PowerToys.Settings.IPCWrapperPS/Microsoft.PowerToys.Settings.IPCWrapperPS.vcxproj deleted file mode 100644 index 4565626b55..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.IPCWrapperPS/Microsoft.PowerToys.Settings.IPCWrapperPS.vcxproj +++ /dev/null @@ -1,210 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 16.0 - {C073B057-B157-40F0-8678-1DCD119D841C} - 10.0 - AtlPSProj - - - - DynamicLibrary - true - v142 - Unicode - - - DynamicLibrary - false - v142 - Unicode - - - DynamicLibrary - true - v142 - Unicode - - - DynamicLibrary - false - v142 - Unicode - - - - - - - - - - - - - - - - - - - - - $(Configuration)PS\ - - - $(Configuration)PS\ - - - $(Configuration)PS\ - - - $(Configuration)PS\ - - - - REGISTER_PROXY_DLL;_DEBUG;%(PreprocessorDefinitions) - - - kernel32.lib;rpcns4.lib;rpcrt4.lib;oleaut32.lib;uuid.lib;%(AdditionalDependencies) - Microsoft.PowerToys.Settings.IPCWrapperPS.def - true - - - if exist dlldata.c goto :END -echo Error: MIDL will not generate DLLDATA.C unless you have at least 1 interface in the main project. -Exit 1 -:END - - Checking for required files - - - - - WIN32;REGISTER_PROXY_DLL;_DEBUG;%(PreprocessorDefinitions) - - - kernel32.lib;rpcns4.lib;rpcrt4.lib;oleaut32.lib;uuid.lib;%(AdditionalDependencies) - Microsoft.PowerToys.Settings.IPCWrapperPS.def - true - - - if exist dlldata.c goto :END -echo Error: MIDL will not generate DLLDATA.C unless you have at least 1 interface in the main project. -Exit 1 -:END - - Checking for required files - - - - - WIN32;REGISTER_PROXY_DLL;NDEBUG;%(PreprocessorDefinitions) - MaxSpeed - - - kernel32.lib;rpcns4.lib;rpcrt4.lib;oleaut32.lib;uuid.lib;%(AdditionalDependencies) - Microsoft.PowerToys.Settings.IPCWrapperPS.def - true - true - true - - - if exist dlldata.c goto :END -echo Error: MIDL will not generate DLLDATA.C unless you have at least 1 interface in the main project. -Exit 1 -:END - - Checking for required files - - - - - REGISTER_PROXY_DLL;NDEBUG;%(PreprocessorDefinitions) - MaxSpeed - - - kernel32.lib;rpcns4.lib;rpcrt4.lib;oleaut32.lib;uuid.lib;%(AdditionalDependencies) - Microsoft.PowerToys.Settings.IPCWrapperPS.def - true - true - true - - - if exist dlldata.c goto :END -echo Error: MIDL will not generate DLLDATA.C unless you have at least 1 interface in the main project. -Exit 1 -:END - - Checking for required files - - - - - - - - false - false - false - false - - - - - - - - - - - false - false - false - false - - - - - - - - - - - false - false - false - false - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/core/Microsoft.PowerToys.Settings.IPCWrapperPS/Microsoft.PowerToys.Settings.IPCWrapperPS.vcxproj.filters b/src/core/Microsoft.PowerToys.Settings.IPCWrapperPS/Microsoft.PowerToys.Settings.IPCWrapperPS.vcxproj.filters deleted file mode 100644 index 372729ca8b..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.IPCWrapperPS/Microsoft.PowerToys.Settings.IPCWrapperPS.vcxproj.filters +++ /dev/null @@ -1,37 +0,0 @@ - - - - - {48f2eb30-48be-400a-88ce-8f93929d29c8} - False - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Source Files - - - - - Generated Files - - - Generated Files - - - Generated Files - - - \ No newline at end of file diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.cpp b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.cpp deleted file mode 100644 index 52a023a200..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.cpp +++ /dev/null @@ -1,72 +0,0 @@ -// TwoWayIPCLib.cpp : Implementation of DLL Exports. - - -#include "pch.h" -#include "framework.h" -#include "resource.h" -#include "TwoWayIPCLib_i.h" -#include "dllmain.h" - -using namespace ATL; - -// Used to determine whether the DLL can be unloaded by OLE. -_Use_decl_annotations_ -STDAPI DllCanUnloadNow(void) -{ - return _AtlModule.DllCanUnloadNow(); -} - -// Returns a class factory to create an object of the requested type. -_Use_decl_annotations_ -STDAPI DllGetClassObject(_In_ REFCLSID rclsid, _In_ REFIID riid, _Outptr_ LPVOID* ppv) -{ - return _AtlModule.DllGetClassObject(rclsid, riid, ppv); -} - -// DllRegisterServer - Adds entries to the system registry. -_Use_decl_annotations_ -STDAPI DllRegisterServer(void) -{ - // registers object, typelib and all interfaces in typelib - HRESULT hr = _AtlModule.DllRegisterServer(); - return hr; -} - -// DllUnregisterServer - Removes entries from the system registry. -_Use_decl_annotations_ -STDAPI DllUnregisterServer(void) -{ - HRESULT hr = _AtlModule.DllUnregisterServer(); - return hr; -} - -// DllInstall - Adds/Removes entries to the system registry per user per machine. -STDAPI DllInstall(BOOL bInstall, _In_opt_ LPCWSTR pszCmdLine) -{ - HRESULT hr = E_FAIL; - static const wchar_t szUserSwitch[] = L"user"; - - if (pszCmdLine != nullptr) - { - if (_wcsnicmp(pszCmdLine, szUserSwitch, _countof(szUserSwitch)) == 0) - { - ATL::AtlSetPerUserRegistration(true); - } - } - - if (bInstall) - { - hr = DllRegisterServer(); - if (FAILED(hr)) - { - DllUnregisterServer(); - } - } - else - { - hr = DllUnregisterServer(); - } - - return hr; -} - diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.def b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.def deleted file mode 100644 index e3802e7245..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.def +++ /dev/null @@ -1,10 +0,0 @@ -; TwoWayIPCLib.def : Declares the module parameters. - -LIBRARY - -EXPORTS - DllCanUnloadNow PRIVATE - DllGetClassObject PRIVATE - DllRegisterServer PRIVATE - DllUnregisterServer PRIVATE - DllInstall PRIVATE diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.idl b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.idl deleted file mode 100644 index 6ee9807f79..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.idl +++ /dev/null @@ -1,48 +0,0 @@ -// TwoWayIPCLib.idl : IDL source for TwoWayIPCLib -// - -// This file will be processed by the MIDL tool to -// produce the type library (TwoWayIPCLib.tlb) and marshalling code. - -import "oaidl.idl"; -import "ocidl.idl"; - -[ - local, - object, - uuid(8160da0a-c224-485c-b5ba-66484f212cf4), - dual, - nonextensible, - pointer_default(unique) -] -interface ITwoWayIPCManager : IDispatch -{ - [id(1)] HRESULT SendMessage([in] BSTR message); - [id(2)] HRESULT Initialize([in] BSTR runnerPipeName, [in] BSTR settingsPipeName); -}; -[ - uuid(7e00d5a3-11df-45c1-a675-96e408bd5bf5), - version(1.0), -] -library TwoWayIPCLibLib -{ - importlib("stdole2.tlb"); - [ - uuid(62d4786d-c6be-4e2a-8362-0f3ed73d3321) - ] - dispinterface _ITwoWayIPCManagerEvents - { - properties: - methods: - }; - [ - uuid(cbd4dd85-c226-425f-af85-66de72bc5f7e) - ] - coclass TwoWayIPCManager - { - [default] interface ITwoWayIPCManager; - }; -}; - - -import "shobjidl.idl"; diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.rc b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.rc deleted file mode 100644 index 600387acdf415bdb1fc2c583a7588eb5f5bae2dc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6080 zcmdUzU2j@P5QgWvQvbs#zDd-`Hc4(;Rh9gKDJcfaVA@EQkQ0n+iGT&TA5wpN+vnY7 z3t>1>|379 z?(Eivv@zpT`W|CrdaZlp?IZ6Q|HSRy9-!H`uk9;#ISBn8qeEr~Q1=3DX=VMs{br>= zt(AL$t{>Kk_mN##%i4Bjr&hD3b?wqBjGpq|u)4ixq{Z*K9Wzqr-S?LayZ5+s-e*=t zFUvaaqot93;`~#zBR#Y^^{@QaIQ0qVls+nqzoso)WYq?Jc98!2P_JrKJUeLbXE>fF z?KZ^+#=#?|_B*`B*xD_(7tYYbYW?K;KY~r8tcxpGJtT4;<5%*qIj!q#_pB<3B;6i< zzE0E6@!j1&rB0jSD?_|tfK?~>L1r)T$szw7CtAEk)R@@I0xz8ejYX}=Z!-HP7yCUp z9@!aQA7j3Shh7o=C+ytCPwPD5`!`RPT-k=lB{9}zRfF{}!>)Bk8;oAjSDDG+6-Odn zmH#!cCNuZi+u`(++)?NmC;!WuQY#*#;XCz^e>w| z#hKyS4rkdx_KWDGRl>b(_E1LJ1P!mjplkd(W~jNWMVYM>JiZ7cOYlhX5&B2uOw+7l zWXV?XblomR9Exx@%_C)rH(OdFddYk8dnH8MF?>oqZB90_U%-yP@R5sza z@2UnDXH*|(T&pwQYS^o?M3WKquZY#y$}`#v_&15#E3DXl)(Yo-7Vx%;bmt&*i`pcE z`=9oP=&c9tF_Il=l@Tf+%N59ymvhc3awtEozr~*>=xIIu;R3x!YJNo%`6BPP$f)P- zY|UI-ZE93`dZc^3G9PnHp2~MF#!F?SCVG`-N=PsTVHD+eSm>UU-D0O9+8Qw%<&Bl%WGq1bCfJZkB=Qj!eq(R*PltFsPTPQ|RI%swLq}6TkCXWhjxCs#^*h+VY#n=@3M>JARUZw&R(k>#~T-y1Y z=GDHW+^t)p>SKNPxFm15{C<`i*mz04Zc`;SX-Cj_rBovay7jzY=I7(7eq9Zw>UJ`V zcgd5MMR~hH)BWo5Nv6*s%X#pT8zhNeA(mxweeI^rT{`b|zntf8-D+;Aa+SZGLcLn_ z2UTNLsE)#&v=U~DbGmG!IxkjX?}9bXL;fwceA@02bMsM^uBkuImx0~1o}_Ync)#hL tQtNj6bbddAifv*4E)@3?QhSUlA_Ws;E=zkbI*yaEL diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.rgs b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.rgs deleted file mode 100644 index e7d37400e1..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.rgs +++ /dev/null @@ -1,3 +0,0 @@ -HKCR -{ -} diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.vcxproj b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.vcxproj deleted file mode 100644 index df37c436ac..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.vcxproj +++ /dev/null @@ -1,296 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 16.0 - {F88B6FD1-14BD-48DB-85C2-6C51B8045121} - AtlProj - 10.0 - Microsoft.PowerToys.Settings.TwoWayIPCLib - - - - DynamicLibrary - true - v142 - Unicode - - - DynamicLibrary - false - v142 - Unicode - - - DynamicLibrary - true - v142 - Unicode - - - DynamicLibrary - false - v142 - Unicode - - - - - - - - - - - - - - - - - - - - - true - true - $(SolutionDir)$(Platform)\ - - - true - true - $(SolutionDir) - - - true - false - $(SolutionDir) - - - true - false - $(SolutionDir)$(Platform)\ - - - - Use - Level3 - Disabled - _WINDOWS;_DEBUG;_USRDLL;%(PreprocessorDefinitions) - pch.h - true - ..\..\..\common\inc;..\..\..\common\Telemetry;..\;..\..\;..\..\..\deps\cpprestsdk\include;..\..\..\common;..\..\..\;..\..\common;%(AdditionalIncludeDirectories) - stdcpplatest - - - false - _DEBUG;%(PreprocessorDefinitions) - TwoWayIPCLib_i.h - TwoWayIPCLib_i.c - TwoWayIPCLib_p.c - true - $(IntDir)TwoWayIPCLib.tlb - - true - - - 0x0409 - $(IntDir);%(AdditionalIncludeDirectories) - _DEBUG;%(PreprocessorDefinitions) - - - Windows - .\TwoWayIPCLib.def - true - true - - - - - Use - Level3 - Disabled - WIN32;_WINDOWS;_DEBUG;_USRDLL;%(PreprocessorDefinitions) - pch.h - true - - - false - Win32 - _DEBUG;%(PreprocessorDefinitions) - TwoWayIPCLib_i.h - TwoWayIPCLib_i.c - TwoWayIPCLib_p.c - true - $(IntDir)TwoWayIPCLib.tlb - - true - - - 0x0409 - $(IntDir);%(AdditionalIncludeDirectories) - _DEBUG;%(PreprocessorDefinitions) - - - Windows - .\TwoWayIPCLib.def - true - - - - - Use - Level3 - MaxSpeed - WIN32;_WINDOWS;NDEBUG;_USRDLL;%(PreprocessorDefinitions) - pch.h - true - - - false - Win32 - NDEBUG;%(PreprocessorDefinitions) - TwoWayIPCLib_i.h - TwoWayIPCLib_i.c - TwoWayIPCLib_p.c - true - $(IntDir)TwoWayIPCLib.tlb - - true - - - 0x0409 - $(IntDir);%(AdditionalIncludeDirectories) - NDEBUG;%(PreprocessorDefinitions) - - - Windows - .\TwoWayIPCLib.def - true - true - true - - - - - Use - Level3 - MaxSpeed - _WINDOWS;NDEBUG;_USRDLL;%(PreprocessorDefinitions) - pch.h - true - stdcpplatest - ..\..\..\common\inc;..\..\..\common\Telemetry;..\;..\..\;..\..\..\deps\cpprestsdk\include;..\..\..\common;..\..\..\;..\..\common;%(AdditionalIncludeDirectories) - EditAndContinue - true - - - false - NDEBUG;%(PreprocessorDefinitions) - TwoWayIPCLib_i.h - TwoWayIPCLib_i.c - TwoWayIPCLib_p.c - true - $(IntDir)TwoWayIPCLib.tlb - - true - - - 0x0409 - $(IntDir);%(AdditionalIncludeDirectories) - NDEBUG;%(PreprocessorDefinitions) - - - Windows - .\TwoWayIPCLib.def - true - true - true - true - - - - - - - - - - - - - - - false - false - false - false - - - - - - - - - - - Create - Create - Create - Create - - - - false - false - false - false - - - - - - - - - - - - - - - - - - - - - - - - - {74485049-c722-400f-abe5-86ac52d929b3} - - - - - - \ No newline at end of file diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.vcxproj.filters b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.vcxproj.filters deleted file mode 100644 index 6168e1310c..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLib.vcxproj.filters +++ /dev/null @@ -1,85 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - {27e408d7-3292-4c84-9621-bd3b4a4925d3} - False - - - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Generated Files - - - Header Files - - - Header Files - - - Header Files - - - - - Source Files - - - Source Files - - - Generated Files - - - Source Files - - - Source Files - - - - - Resource Files - - - - - Resource Files - - - Source Files - - - Resource Files - - - - - Source Files - - - \ No newline at end of file diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLibps.def b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLibps.def deleted file mode 100644 index 2a4265afa9..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCLibps.def +++ /dev/null @@ -1,8 +0,0 @@ - -LIBRARY - -EXPORTS - DllGetClassObject PRIVATE - DllCanUnloadNow PRIVATE - DllRegisterServer PRIVATE - DllUnregisterServer PRIVATE diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.cpp b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.cpp deleted file mode 100644 index bc1561a02f..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.cpp +++ /dev/null @@ -1,54 +0,0 @@ -// TwoWayIPCManager.cpp : Implementation of CTwoWayIPCManager - -#include "pch.h" -#include "TwoWayIPCManager.h" -#include -#include - - -using namespace std; - -/* -* Default constructor, initialized the IPC variabl to null. -*/ -CTwoWayIPCManager::CTwoWayIPCManager() -{ - this->m_MessagePipe = nullptr; -} - -CTwoWayIPCManager::~CTwoWayIPCManager() -{ - delete this->m_MessagePipe; -} - -/* -* Send IPC Message. -*/ -STDMETHODIMP CTwoWayIPCManager::SendMessage(BSTR message) -{ - wstring strMessage = (PCWSTR)message; - this->m_MessagePipe->send(strMessage); - wcout << "sending message" << L"\n"; - return S_OK; -} - -/* -* This method is meant to initialize the IPC constructor and -* and start the threading process. -*/ -STDMETHODIMP CTwoWayIPCManager::Initialize(BSTR runnerPipeName, BSTR settingsPipeName) -{ - try - { - wstring powertoys_pipe_name = (PCWSTR)runnerPipeName; - wstring settings_pipe_name = (PCWSTR)settingsPipeName; - - this->m_MessagePipe = new TwoWayPipeMessageIPC(powertoys_pipe_name, settings_pipe_name, nullptr); - this->m_MessagePipe->start(nullptr); - } - catch (std::exception exp) - { - S_FALSE; - } - return S_OK; -} diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.h b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.h deleted file mode 100644 index 705e1f3ca4..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.h +++ /dev/null @@ -1,59 +0,0 @@ -// TwoWayIPCManager.h : Declaration of the CTwoWayIPCManager - -#pragma once -#include "resource.h" // main symbols - -#include "TwoWayIPCLib_i.h" -#include "_ITwoWayIPCManagerEvents_CP.h" -#include "common/two_way_pipe_message_ipc.h" - -using namespace ATL; - -// CTwoWayIPCManager - -class ATL_NO_VTABLE CTwoWayIPCManager : - public CComObjectRootEx, - public CComCoClass, - public IConnectionPointContainerImpl, - public CProxy_ITwoWayIPCManagerEvents, - public IDispatchImpl -{ -public: - CTwoWayIPCManager(); - ~CTwoWayIPCManager(); - - DECLARE_REGISTRY_RESOURCEID(106) - - BEGIN_COM_MAP(CTwoWayIPCManager) - COM_INTERFACE_ENTRY(ITwoWayIPCManager) - COM_INTERFACE_ENTRY(IDispatch) - COM_INTERFACE_ENTRY(IConnectionPointContainer) - END_COM_MAP() - - BEGIN_CONNECTION_POINT_MAP(CTwoWayIPCManager) - CONNECTION_POINT_ENTRY(__uuidof(_ITwoWayIPCManagerEvents)) - END_CONNECTION_POINT_MAP() - - DECLARE_PROTECT_FINAL_CONSTRUCT() - - HRESULT FinalConstruct() - { - return S_OK; - } - - void FinalRelease() - { - } - - STDMETHOD(SendMessage) - (BSTR message); - - STDMETHOD(Initialize) - (BSTR runnerPipeName, BSTR settingsPipeName); - -private: - TwoWayPipeMessageIPC* m_MessagePipe; - BSTR m_message_from_runner; -}; - -OBJECT_ENTRY_AUTO(__uuidof(TwoWayIPCManager), CTwoWayIPCManager) diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.rgs b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.rgs deleted file mode 100644 index 2d4d3514dd..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/TwoWayIPCManager.rgs +++ /dev/null @@ -1,26 +0,0 @@ -HKCR -{ - TwoWayIPCLib.TwoWayIPCManager.1 = s 'TwoWayIPCManager class' - { - CLSID = s '{cbd4dd85-c226-425f-af85-66de72bc5f7e}' - } - TwoWayIPCLib.TwoWayIPCManager = s 'TwoWayIPCManager class' - { - CurVer = s 'TwoWayIPCLib.TwoWayIPCManager.1' - } - NoRemove CLSID - { - ForceRemove {cbd4dd85-c226-425f-af85-66de72bc5f7e} = s 'TwoWayIPCManager class' - { - ProgID = s 'TwoWayIPCLib.TwoWayIPCManager.1' - VersionIndependentProgID = s 'TwoWayIPCLib.TwoWayIPCManager' - ForceRemove Programmable - InprocServer32 = s '%MODULE%' - { - val ThreadingModel = s 'Apartment' - } - TypeLib = s '{7e00d5a3-11df-45c1-a675-96e408bd5bf5}' - Version = s '1.0' - } - } -} diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/_ITwoWayIPCManagerEvents_CP.h b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/_ITwoWayIPCManagerEvents_CP.h deleted file mode 100644 index 266ca0d916..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/_ITwoWayIPCManagerEvents_CP.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -template -class CProxy_ITwoWayIPCManagerEvents : - public ATL::IConnectionPointImpl -{ -public: - -}; diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/dllmain.cpp b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/dllmain.cpp deleted file mode 100644 index f4ed9020d0..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/dllmain.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// dllmain.cpp : Implementation of DllMain. - -#include "pch.h" -#include "framework.h" -#include "resource.h" -#include "TwoWayIPCLib_i.h" -#include "dllmain.h" - -CTwoWayIPCLibModule _AtlModule; - -// DLL Entry Point -extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) -{ - hInstance; - return _AtlModule.DllMain(dwReason, lpReserved); -} diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/dllmain.h b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/dllmain.h deleted file mode 100644 index 5376fee1e4..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/dllmain.h +++ /dev/null @@ -1,10 +0,0 @@ -// dllmain.h : Declaration of module class. - -class CTwoWayIPCLibModule : public ATL::CAtlDllModuleT< CTwoWayIPCLibModule > -{ -public : - DECLARE_LIBID(LIBID_TwoWayIPCLibLib) - DECLARE_REGISTRY_APPID_RESOURCEID(IDR_TWOWAYIPCLIB, "{7e00d5a3-11df-45c1-a675-96e408bd5bf5}") -}; - -extern class CTwoWayIPCLibModule _AtlModule; diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/framework.h b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/framework.h deleted file mode 100644 index bdecff6a78..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/framework.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#ifndef STRICT -#define STRICT -#endif - -#include "targetver.h" - -#define _ATL_APARTMENT_THREADED - -#define _ATL_NO_AUTOMATIC_NAMESPACE - -#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit - - -#define ATL_NO_ASSERT_ON_DESTROY_NONEXISTENT_WINDOW - -#include "resource.h" -#include -#include -#include diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/pch.cpp b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/pch.cpp deleted file mode 100644 index 64b7eef6d6..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/pch.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// pch.cpp: source file corresponding to the pre-compiled header - -#include "pch.h" - -// When you are using pre-compiled headers, this source file is necessary for compilation to succeed. diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/pch.h b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/pch.h deleted file mode 100644 index 885d5d62e4..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/pch.h +++ /dev/null @@ -1,13 +0,0 @@ -// pch.h: This is a precompiled header file. -// Files listed below are compiled only once, improving build performance for future builds. -// This also affects IntelliSense performance, including code completion and many code browsing features. -// However, files listed here are ALL re-compiled if any one of them is updated between builds. -// Do not add files here that you will be updating frequently as this negates the performance advantage. - -#ifndef PCH_H -#define PCH_H - -// add headers that you want to pre-compile here -#include "framework.h" - -#endif //PCH_H diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/resource.h b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/resource.h deleted file mode 100644 index b98eccdc78..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/resource.h +++ /dev/null @@ -1,18 +0,0 @@ -//{{NO_DEPENDENCIES}} -// Microsoft Visual C++ generated include file. -// Used by TwoWayIPCLib.rc -// -#define IDS_PROJNAME 100 -#define IDR_TWOWAYIPCLIB 101 -#define IDR_TWOWAYIPCMANAGER 106 - -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 201 -#define _APS_NEXT_COMMAND_VALUE 32768 -#define _APS_NEXT_CONTROL_VALUE 201 -#define _APS_NEXT_SYMED_VALUE 107 -#endif -#endif diff --git a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/targetver.h b/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/targetver.h deleted file mode 100644 index 87c0086de7..0000000000 --- a/src/core/Microsoft.PowerToys.Settings.TwoWayIPCLib/targetver.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -// Including SDKDDKVer.h defines the highest available Windows platform. - -// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and -// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. - -#include diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Runner/MainWindow.xaml.cs b/src/core/Microsoft.PowerToys.Settings.UI.Runner/MainWindow.xaml.cs index d079b5d3d5..96176fb0a1 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Runner/MainWindow.xaml.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Runner/MainWindow.xaml.cs @@ -28,7 +28,7 @@ namespace Microsoft.PowerToys.Settings.UI.Runner // send IPC Message shellPage.SetDefaultSndMessageCallback(msg => { - Program.GetTwoWayIPCManager().SendMessage(msg); + Program.GetTwoWayIPCManager().Send(msg); }); } } diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj index ae438eae2c..216232bbf2 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj +++ b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj @@ -71,6 +71,7 @@ + diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Program.cs b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Program.cs index f7517c7f3a..c5e2a43c5c 100644 --- a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Program.cs +++ b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Program.cs @@ -4,14 +4,14 @@ using System; using System.Windows; -using TwoWayIPCLibLib; +using interop; namespace Microsoft.PowerToys.Settings.UI.Runner { public class Program { // Create an instance of the IPC wrapper. - private static ITwoWayIPCManager ipcmanager = new TwoWayIPCManager(); + private static TwoWayPipeMessageIPCManaged ipcmanager; [STAThread] public static void Main(string[] args) @@ -23,7 +23,8 @@ namespace Microsoft.PowerToys.Settings.UI.Runner if (args.Length > 1) { - ipcmanager.Initialize(args[1], args[0]); + ipcmanager = new TwoWayPipeMessageIPCManaged(args[1], args[0], null); + ipcmanager.Start(); app.Run(); } else @@ -37,7 +38,7 @@ namespace Microsoft.PowerToys.Settings.UI.Runner } } - public static ITwoWayIPCManager GetTwoWayIPCManager() + public static TwoWayPipeMessageIPCManaged GetTwoWayIPCManager() { return ipcmanager; } diff --git a/src/settings/main.cpp b/src/settings/main.cpp index 7f24e26b1f..e15737924b 100644 --- a/src/settings/main.cpp +++ b/src/settings/main.cpp @@ -7,6 +7,7 @@ #include "resource.h" #include #include +#include #include "trace.h"