mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-06-07 17:42:45 +08:00
ImageResizer: fix crash on using UseNewSettings (#8661)
* ImageResizer: fix crash on using UseNewSettings - add unit test to InteropTests
This commit is contained in:
parent
3a3cb27b36
commit
c9c4c9ba8c
1
.github/actions/spell-check/expect.txt
vendored
1
.github/actions/spell-check/expect.txt
vendored
@ -342,6 +342,7 @@ CPPARM
|
|||||||
cppblog
|
cppblog
|
||||||
cppm
|
cppm
|
||||||
cpprestsdk
|
cpprestsdk
|
||||||
|
cppruntime
|
||||||
cppstd
|
cppstd
|
||||||
cppwinrt
|
cppwinrt
|
||||||
CPPx
|
CPPx
|
||||||
|
@ -84,9 +84,7 @@
|
|||||||
</Target>
|
</Target>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="HotkeyManager.h" />
|
<ClInclude Include="HotkeyManager.h" />
|
||||||
<ClInclude Include="interop.h" />
|
|
||||||
<ClInclude Include="KeyboardHook.h" />
|
<ClInclude Include="KeyboardHook.h" />
|
||||||
<ClInclude Include="os-detect.h" />
|
|
||||||
<ClInclude Include="pch.h" />
|
<ClInclude Include="pch.h" />
|
||||||
<ClInclude Include="resource.h" />
|
<ClInclude Include="resource.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
@ -96,12 +94,10 @@
|
|||||||
<ClCompile Include="interop.cpp" />
|
<ClCompile Include="interop.cpp" />
|
||||||
<ClCompile Include="KeyboardHook.cpp" />
|
<ClCompile Include="KeyboardHook.cpp" />
|
||||||
<ClCompile Include="keyboard_layout.cpp">
|
<ClCompile Include="keyboard_layout.cpp">
|
||||||
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</CompileAsManaged>
|
<CompileAsManaged>false</CompileAsManaged>
|
||||||
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</CompileAsManaged>
|
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="two_way_pipe_message_ipc.cpp">
|
<ClCompile Include="two_way_pipe_message_ipc.cpp">
|
||||||
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</CompileAsManaged>
|
<CompileAsManaged>false</CompileAsManaged>
|
||||||
<CompileAsManaged Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</CompileAsManaged>
|
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -15,9 +15,6 @@
|
|||||||
</Filter>
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="interop.h">
|
|
||||||
<Filter>Header Files</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="pch.h">
|
<ClInclude Include="pch.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
@ -30,9 +27,6 @@
|
|||||||
<ClInclude Include="resource.h">
|
<ClInclude Include="resource.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="os-detect.h">
|
|
||||||
<Filter>Header Files</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="interop.cpp">
|
<ClCompile Include="interop.cpp">
|
||||||
|
@ -22,6 +22,9 @@ namespace Microsoft.Interop.Tests
|
|||||||
[TestInitialize]
|
[TestInitialize]
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
|
// Make sure we don't crash
|
||||||
|
Assert.IsTrue(CommonManaged.ShouldNewSettingsBeUsed() == CommonManaged.ShouldNewSettingsBeUsed());
|
||||||
|
|
||||||
ClientPipe = new TwoWayPipeMessageIPCManaged(ClientSidePipe, ServerSidePipe, null);
|
ClientPipe = new TwoWayPipeMessageIPCManaged(ClientSidePipe, ServerSidePipe, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,142 @@
|
|||||||
#include "pch.h"
|
#include "pch.h"
|
||||||
|
|
||||||
#include "interop.h"
|
#include <msclr/marshal.h>
|
||||||
|
#include <msclr/marshal_cppstd.h>
|
||||||
|
#include <functional>
|
||||||
|
#include "keyboard_layout.h"
|
||||||
|
#include "two_way_pipe_message_ipc.h"
|
||||||
|
#include "shared_constants.h"
|
||||||
|
|
||||||
|
// We cannot use C++/WinRT APIs when compiled with /clr (we'll get a runtime crash). os-detect API is used
|
||||||
|
// in both native C++ and C++/CX.
|
||||||
|
// We also cannot compile it as a library, since we use different cppruntime linkage in C++/CX and native C++.
|
||||||
|
// Therefore the simplest way is to compile these functions as native using the pragmas below.
|
||||||
|
#pragma managed(push, off)
|
||||||
|
#include "../utils/os-detect.h"
|
||||||
|
#pragma managed(pop)
|
||||||
|
|
||||||
|
#include <common/version/version.h>
|
||||||
|
|
||||||
|
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:
|
||||||
|
LayoutMapManaged() :
|
||||||
|
_map(new LayoutMap) {}
|
||||||
|
|
||||||
|
~LayoutMapManaged()
|
||||||
|
{
|
||||||
|
delete _map;
|
||||||
|
}
|
||||||
|
|
||||||
|
String ^ GetKeyName(DWORD key) {
|
||||||
|
return gcnew String(_map->GetKeyName(key).c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Updatelayout()
|
||||||
|
{
|
||||||
|
_map->UpdateLayout();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
!LayoutMapManaged()
|
||||||
|
{
|
||||||
|
delete _map;
|
||||||
|
}
|
||||||
|
|
||||||
|
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<std::wstring>(inputPipeName),
|
||||||
|
msclr::interop::marshal_as<std::wstring>(outputPipeName),
|
||||||
|
cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
~TwoWayPipeMessageIPCManaged()
|
||||||
|
{
|
||||||
|
delete _pipe;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Send(String ^ msg)
|
||||||
|
{
|
||||||
|
_pipe->send(msclr::interop::marshal_as<std::wstring>(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()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public
|
||||||
|
ref class CommonManaged
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static String ^ GetProductVersion() {
|
||||||
|
return gcnew String(get_product_version().c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ShouldNewSettingsBeUsed()
|
||||||
|
{
|
||||||
|
return UseNewSettings();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public
|
||||||
|
ref class Constants
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
literal int VK_WIN_BOTH = CommonSharedConstants::VK_WIN_BOTH;
|
||||||
|
|
||||||
|
static String ^ PowerLauncherSharedEvent() {
|
||||||
|
return gcnew String(CommonSharedConstants::POWER_LAUNCHER_SHARED_EVENT);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@ -1,135 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <msclr/marshal.h>
|
|
||||||
#include <msclr/marshal_cppstd.h>
|
|
||||||
#include <functional>
|
|
||||||
#include "keyboard_layout.h"
|
|
||||||
#include "two_way_pipe_message_ipc.h"
|
|
||||||
#include "shared_constants.h"
|
|
||||||
#include "../utils/os-detect.h"
|
|
||||||
|
|
||||||
#include <common/version/version.h>
|
|
||||||
|
|
||||||
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:
|
|
||||||
LayoutMapManaged() :
|
|
||||||
_map(new LayoutMap) {}
|
|
||||||
|
|
||||||
~LayoutMapManaged()
|
|
||||||
{
|
|
||||||
delete _map;
|
|
||||||
}
|
|
||||||
|
|
||||||
String ^ GetKeyName(DWORD key) {
|
|
||||||
return gcnew String(_map->GetKeyName(key).c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void Updatelayout()
|
|
||||||
{
|
|
||||||
_map->UpdateLayout();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
!LayoutMapManaged()
|
|
||||||
{
|
|
||||||
delete _map;
|
|
||||||
}
|
|
||||||
|
|
||||||
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<std::wstring>(inputPipeName),
|
|
||||||
msclr::interop::marshal_as<std::wstring>(outputPipeName),
|
|
||||||
cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
~TwoWayPipeMessageIPCManaged()
|
|
||||||
{
|
|
||||||
delete _pipe;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Send(String ^ msg)
|
|
||||||
{
|
|
||||||
_pipe->send(msclr::interop::marshal_as<std::wstring>(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()));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public
|
|
||||||
ref class CommonManaged
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
static String ^ GetProductVersion() {
|
|
||||||
return gcnew String(get_product_version().c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool ShouldNewSettingsBeUsed()
|
|
||||||
{
|
|
||||||
return UseNewSettings();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public
|
|
||||||
ref class Constants
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
literal int VK_WIN_BOTH = CommonSharedConstants::VK_WIN_BOTH;
|
|
||||||
|
|
||||||
static String ^ PowerLauncherSharedEvent() {
|
|
||||||
return gcnew String(CommonSharedConstants::POWER_LAUNCHER_SHARED_EVENT);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
@ -2,6 +2,10 @@
|
|||||||
#include "keyboard_layout_impl.h"
|
#include "keyboard_layout_impl.h"
|
||||||
#include "shared_constants.h"
|
#include "shared_constants.h"
|
||||||
|
|
||||||
|
#include <winrt/Windows.UI.Core.h>
|
||||||
|
|
||||||
|
using namespace winrt;
|
||||||
|
|
||||||
LayoutMap::LayoutMap() :
|
LayoutMap::LayoutMap() :
|
||||||
impl(new LayoutMap::LayoutMapImpl())
|
impl(new LayoutMap::LayoutMapImpl())
|
||||||
{
|
{
|
||||||
|
@ -3,9 +3,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <winrt/Windows.UI.Core.h>
|
|
||||||
|
|
||||||
using namespace winrt;
|
|
||||||
|
|
||||||
// Wrapper class to handle keyboard layout
|
// Wrapper class to handle keyboard layout
|
||||||
class LayoutMap::LayoutMapImpl
|
class LayoutMap::LayoutMapImpl
|
||||||
|
Loading…
Reference in New Issue
Block a user