From 8c6b4e6b8c8317f87832a55246ab4293be2cc44e Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 16 Aug 2024 08:57:59 -0700 Subject: [PATCH 01/43] Drop support for NetStandard 2.0, also means NetFx isn't supported for server anymore (clients can still be) --- Shmuelie.WinRTServer.sln | 319 +++++++++--------- .../Internal/BaseActivationFactoryProxy.cs | 8 +- .../Internal/BaseClassFactoryProxy.cs | 10 +- .../Internal/UIntPtrExtensions.cs | 67 ---- .../Shmuelie.WinRTServer.csproj | 13 +- .../Windows/shared/uuids/__uuidof.cs | 125 ------- .../Windows/winrt/wrl/client/ComPtr.cs | 18 +- .../Package.appxmanifest | 31 -- ...hmuelie.WinRTServer.Sample.Package.wapproj | 279 ++++++++------- 9 files changed, 297 insertions(+), 573 deletions(-) delete mode 100644 src/Shmuelie.WinRTServer/Internal/UIntPtrExtensions.cs delete mode 100644 src/Shmuelie.WinRTServer/Windows/shared/uuids/__uuidof.cs diff --git a/Shmuelie.WinRTServer.sln b/Shmuelie.WinRTServer.sln index f324269..d030eaf 100644 --- a/Shmuelie.WinRTServer.sln +++ b/Shmuelie.WinRTServer.sln @@ -1,165 +1,154 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.3.32929.385 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_SolutionItems", "{50DD472E-AD00-45C5-A2F7-4CB77CB8CD67}" - ProjectSection(SolutionItems) = preProject - .editorconfig = .editorconfig - .gitignore = .gitignore - Directory.Build.props = Directory.Build.props - Directory.Build.targets = Directory.Build.targets - global.json = global.json - LICENSE = LICENSE - README.md = README.md - EndProjectSection -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{CB701FA8-9663-444D-9610-1A8CDFF30E61}" - ProjectSection(SolutionItems) = preProject - tests\Directory.Build.props = tests\Directory.Build.props - EndProjectSection -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer", "src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj", "{F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Src", "Src", "{E1670C4F-46C0-45BE-A351-E504DCB17A45}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.Interfaces", "tests\Shmuelie.WinRTServer.Sample.Interfaces\Shmuelie.WinRTServer.Sample.Interfaces.csproj", "{7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.Server", "tests\Shmuelie.WinRTServer.Sample.Server\Shmuelie.WinRTServer.Sample.Server.csproj", "{87FC77FB-0955-4262-A0EF-39440C72A62A}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Shmuelie.WinRTServer.Sample.Metadata", "tests\Shmuelie.WinRTServer.Sample.Metadata\Shmuelie.WinRTServer.Sample.Metadata.vcxproj", "{AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}" - ProjectSection(ProjectDependencies) = postProject - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F} = {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F} - EndProjectSection -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shmuelie.WinRTServer.Sample.UwpClient", "tests\Shmuelie.WinRTServer.Sample.UwpClient\Shmuelie.WinRTServer.Sample.UwpClient.csproj", "{1C27B092-88C3-4631-848B-F7165F8CF5E9}" - ProjectSection(ProjectDependencies) = postProject - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} = {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} - EndProjectSection -EndProject -Project("{C7167F0D-BC9F-4E6E-AFE1-012C56B48DB5}") = "Shmuelie.WinRTServer.Sample.Package", "tests\Shmuelie.WinRTServer.Sample.Package\Shmuelie.WinRTServer.Sample.Package.wapproj", "{080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.WpfNetFxClient", "tests\Shmuelie.WinRTServer.Sample.WpfNetFxClient\Shmuelie.WinRTServer.Sample.WpfNetFxClient.csproj", "{6B799403-7548-4FD8-9AF8-76BD13473029}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Shmuelie.WinRTServer.Sample.CppConsoleClient", "tests\Shmuelie.WinRTServer.Sample.CppConsoleClient\Shmuelie.WinRTServer.Sample.CppConsoleClient.vcxproj", "{A78E8601-3191-4987-B61A-E48256869A39}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.WpfNet6Client", "tests\Shmuelie.WinRTServer.Sample.WpfNet6Client\Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj", "{8FFAA094-2D9C-49B5-BB36-4B2BB7512444}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.ServerNet6", "tests\Shmuelie.WinRTServer.Sample.ServerNet6\Shmuelie.WinRTServer.Sample.ServerNet6.csproj", "{F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Debug|x64.ActiveCfg = Debug|Any CPU - {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Debug|x64.Build.0 = Debug|Any CPU - {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Debug|x86.ActiveCfg = Debug|Any CPU - {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Debug|x86.Build.0 = Debug|Any CPU - {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Release|x64.ActiveCfg = Release|Any CPU - {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Release|x64.Build.0 = Release|Any CPU - {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Release|x86.ActiveCfg = Release|Any CPU - {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Release|x86.Build.0 = Release|Any CPU - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Debug|x64.ActiveCfg = Debug|x64 - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Debug|x64.Build.0 = Debug|x64 - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Debug|x86.ActiveCfg = Debug|x86 - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Debug|x86.Build.0 = Debug|x86 - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Release|x64.ActiveCfg = Release|x64 - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Release|x64.Build.0 = Release|x64 - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Release|x86.ActiveCfg = Release|x86 - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Release|x86.Build.0 = Release|x86 - {87FC77FB-0955-4262-A0EF-39440C72A62A}.Debug|x64.ActiveCfg = Debug|x64 - {87FC77FB-0955-4262-A0EF-39440C72A62A}.Debug|x64.Build.0 = Debug|x64 - {87FC77FB-0955-4262-A0EF-39440C72A62A}.Debug|x86.ActiveCfg = Debug|x86 - {87FC77FB-0955-4262-A0EF-39440C72A62A}.Debug|x86.Build.0 = Debug|x86 - {87FC77FB-0955-4262-A0EF-39440C72A62A}.Release|x64.ActiveCfg = Release|x64 - {87FC77FB-0955-4262-A0EF-39440C72A62A}.Release|x64.Build.0 = Release|x64 - {87FC77FB-0955-4262-A0EF-39440C72A62A}.Release|x86.ActiveCfg = Release|x86 - {87FC77FB-0955-4262-A0EF-39440C72A62A}.Release|x86.Build.0 = Release|x86 - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Debug|x64.ActiveCfg = Debug|x64 - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Debug|x64.Build.0 = Debug|x64 - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Debug|x86.ActiveCfg = Debug|Win32 - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Debug|x86.Build.0 = Debug|Win32 - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Release|x64.ActiveCfg = Release|x64 - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Release|x64.Build.0 = Release|x64 - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Release|x86.ActiveCfg = Release|Win32 - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Release|x86.Build.0 = Release|Win32 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x64.ActiveCfg = Debug|x64 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x64.Build.0 = Debug|x64 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x64.Deploy.0 = Debug|x64 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x86.ActiveCfg = Debug|x86 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x86.Build.0 = Debug|x86 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x86.Deploy.0 = Debug|x86 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x64.ActiveCfg = Release|x64 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x64.Build.0 = Release|x64 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x64.Deploy.0 = Release|x64 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x86.ActiveCfg = Release|x86 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x86.Build.0 = Release|x86 - {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x86.Deploy.0 = Release|x86 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x64.ActiveCfg = Debug|x64 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x64.Build.0 = Debug|x64 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x64.Deploy.0 = Debug|x64 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x86.ActiveCfg = Debug|x86 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x86.Build.0 = Debug|x86 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x86.Deploy.0 = Debug|x86 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x64.ActiveCfg = Release|x64 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x64.Build.0 = Release|x64 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x64.Deploy.0 = Release|x64 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x86.ActiveCfg = Release|x86 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x86.Build.0 = Release|x86 - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x86.Deploy.0 = Release|x86 - {6B799403-7548-4FD8-9AF8-76BD13473029}.Debug|x64.ActiveCfg = Debug|x64 - {6B799403-7548-4FD8-9AF8-76BD13473029}.Debug|x64.Build.0 = Debug|x64 - {6B799403-7548-4FD8-9AF8-76BD13473029}.Debug|x86.ActiveCfg = Debug|x86 - {6B799403-7548-4FD8-9AF8-76BD13473029}.Debug|x86.Build.0 = Debug|x86 - {6B799403-7548-4FD8-9AF8-76BD13473029}.Release|x64.ActiveCfg = Release|x64 - {6B799403-7548-4FD8-9AF8-76BD13473029}.Release|x64.Build.0 = Release|x64 - {6B799403-7548-4FD8-9AF8-76BD13473029}.Release|x86.ActiveCfg = Release|x86 - {6B799403-7548-4FD8-9AF8-76BD13473029}.Release|x86.Build.0 = Release|x86 - {A78E8601-3191-4987-B61A-E48256869A39}.Debug|x64.ActiveCfg = Debug|x64 - {A78E8601-3191-4987-B61A-E48256869A39}.Debug|x64.Build.0 = Debug|x64 - {A78E8601-3191-4987-B61A-E48256869A39}.Debug|x86.ActiveCfg = Debug|Win32 - {A78E8601-3191-4987-B61A-E48256869A39}.Debug|x86.Build.0 = Debug|Win32 - {A78E8601-3191-4987-B61A-E48256869A39}.Release|x64.ActiveCfg = Release|x64 - {A78E8601-3191-4987-B61A-E48256869A39}.Release|x64.Build.0 = Release|x64 - {A78E8601-3191-4987-B61A-E48256869A39}.Release|x86.ActiveCfg = Release|Win32 - {A78E8601-3191-4987-B61A-E48256869A39}.Release|x86.Build.0 = Release|Win32 - {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Debug|x64.ActiveCfg = Debug|x64 - {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Debug|x64.Build.0 = Debug|x64 - {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Debug|x86.ActiveCfg = Debug|x86 - {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Debug|x86.Build.0 = Debug|x86 - {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Release|x64.ActiveCfg = Release|x64 - {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Release|x64.Build.0 = Release|x64 - {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Release|x86.ActiveCfg = Release|x86 - {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Release|x86.Build.0 = Release|x86 - {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Debug|x64.ActiveCfg = Debug|x64 - {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Debug|x64.Build.0 = Debug|x64 - {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Debug|x86.ActiveCfg = Debug|x86 - {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Debug|x86.Build.0 = Debug|x86 - {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Release|x64.ActiveCfg = Release|x64 - {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Release|x64.Build.0 = Release|x64 - {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Release|x86.ActiveCfg = Release|x86 - {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Release|x86.Build.0 = Release|x86 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9} = {E1670C4F-46C0-45BE-A351-E504DCB17A45} - {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} - {87FC77FB-0955-4262-A0EF-39440C72A62A} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} - {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} - {1C27B092-88C3-4631-848B-F7165F8CF5E9} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} - {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} - {6B799403-7548-4FD8-9AF8-76BD13473029} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} - {A78E8601-3191-4987-B61A-E48256869A39} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} - {8FFAA094-2D9C-49B5-BB36-4B2BB7512444} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} - {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {97F14D53-B029-4E3E-B7DE-51A510B2D428} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_SolutionItems", "{50DD472E-AD00-45C5-A2F7-4CB77CB8CD67}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + .gitignore = .gitignore + Directory.Build.props = Directory.Build.props + Directory.Build.targets = Directory.Build.targets + global.json = global.json + LICENSE = LICENSE + README.md = README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{CB701FA8-9663-444D-9610-1A8CDFF30E61}" + ProjectSection(SolutionItems) = preProject + tests\Directory.Build.props = tests\Directory.Build.props + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer", "src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj", "{F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Src", "Src", "{E1670C4F-46C0-45BE-A351-E504DCB17A45}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.Interfaces", "tests\Shmuelie.WinRTServer.Sample.Interfaces\Shmuelie.WinRTServer.Sample.Interfaces.csproj", "{7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Shmuelie.WinRTServer.Sample.Metadata", "tests\Shmuelie.WinRTServer.Sample.Metadata\Shmuelie.WinRTServer.Sample.Metadata.vcxproj", "{AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}" + ProjectSection(ProjectDependencies) = postProject + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F} = {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F} + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shmuelie.WinRTServer.Sample.UwpClient", "tests\Shmuelie.WinRTServer.Sample.UwpClient\Shmuelie.WinRTServer.Sample.UwpClient.csproj", "{1C27B092-88C3-4631-848B-F7165F8CF5E9}" + ProjectSection(ProjectDependencies) = postProject + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} = {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} + EndProjectSection +EndProject +Project("{C7167F0D-BC9F-4E6E-AFE1-012C56B48DB5}") = "Shmuelie.WinRTServer.Sample.Package", "tests\Shmuelie.WinRTServer.Sample.Package\Shmuelie.WinRTServer.Sample.Package.wapproj", "{080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.WpfNetFxClient", "tests\Shmuelie.WinRTServer.Sample.WpfNetFxClient\Shmuelie.WinRTServer.Sample.WpfNetFxClient.csproj", "{6B799403-7548-4FD8-9AF8-76BD13473029}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Shmuelie.WinRTServer.Sample.CppConsoleClient", "tests\Shmuelie.WinRTServer.Sample.CppConsoleClient\Shmuelie.WinRTServer.Sample.CppConsoleClient.vcxproj", "{A78E8601-3191-4987-B61A-E48256869A39}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.WpfNet6Client", "tests\Shmuelie.WinRTServer.Sample.WpfNet6Client\Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj", "{8FFAA094-2D9C-49B5-BB36-4B2BB7512444}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.ServerNet6", "tests\Shmuelie.WinRTServer.Sample.ServerNet6\Shmuelie.WinRTServer.Sample.ServerNet6.csproj", "{F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Debug|x64.ActiveCfg = Debug|Any CPU + {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Debug|x64.Build.0 = Debug|Any CPU + {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Debug|x86.ActiveCfg = Debug|Any CPU + {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Debug|x86.Build.0 = Debug|Any CPU + {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Release|x64.ActiveCfg = Release|Any CPU + {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Release|x64.Build.0 = Release|Any CPU + {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Release|x86.ActiveCfg = Release|Any CPU + {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9}.Release|x86.Build.0 = Release|Any CPU + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Debug|x64.ActiveCfg = Debug|x64 + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Debug|x64.Build.0 = Debug|x64 + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Debug|x86.ActiveCfg = Debug|x86 + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Debug|x86.Build.0 = Debug|x86 + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Release|x64.ActiveCfg = Release|x64 + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Release|x64.Build.0 = Release|x64 + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Release|x86.ActiveCfg = Release|x86 + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F}.Release|x86.Build.0 = Release|x86 + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Debug|x64.ActiveCfg = Debug|x64 + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Debug|x64.Build.0 = Debug|x64 + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Debug|x86.ActiveCfg = Debug|Win32 + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Debug|x86.Build.0 = Debug|Win32 + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Release|x64.ActiveCfg = Release|x64 + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Release|x64.Build.0 = Release|x64 + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Release|x86.ActiveCfg = Release|Win32 + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4}.Release|x86.Build.0 = Release|Win32 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x64.ActiveCfg = Debug|x64 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x64.Build.0 = Debug|x64 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x64.Deploy.0 = Debug|x64 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x86.ActiveCfg = Debug|x86 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x86.Build.0 = Debug|x86 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Debug|x86.Deploy.0 = Debug|x86 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x64.ActiveCfg = Release|x64 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x64.Build.0 = Release|x64 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x64.Deploy.0 = Release|x64 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x86.ActiveCfg = Release|x86 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x86.Build.0 = Release|x86 + {1C27B092-88C3-4631-848B-F7165F8CF5E9}.Release|x86.Deploy.0 = Release|x86 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x64.ActiveCfg = Debug|x64 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x64.Build.0 = Debug|x64 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x64.Deploy.0 = Debug|x64 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x86.ActiveCfg = Debug|x86 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x86.Build.0 = Debug|x86 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Debug|x86.Deploy.0 = Debug|x86 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x64.ActiveCfg = Release|x64 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x64.Build.0 = Release|x64 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x64.Deploy.0 = Release|x64 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x86.ActiveCfg = Release|x86 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x86.Build.0 = Release|x86 + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}.Release|x86.Deploy.0 = Release|x86 + {6B799403-7548-4FD8-9AF8-76BD13473029}.Debug|x64.ActiveCfg = Debug|x64 + {6B799403-7548-4FD8-9AF8-76BD13473029}.Debug|x64.Build.0 = Debug|x64 + {6B799403-7548-4FD8-9AF8-76BD13473029}.Debug|x86.ActiveCfg = Debug|x86 + {6B799403-7548-4FD8-9AF8-76BD13473029}.Debug|x86.Build.0 = Debug|x86 + {6B799403-7548-4FD8-9AF8-76BD13473029}.Release|x64.ActiveCfg = Release|x64 + {6B799403-7548-4FD8-9AF8-76BD13473029}.Release|x64.Build.0 = Release|x64 + {6B799403-7548-4FD8-9AF8-76BD13473029}.Release|x86.ActiveCfg = Release|x86 + {6B799403-7548-4FD8-9AF8-76BD13473029}.Release|x86.Build.0 = Release|x86 + {A78E8601-3191-4987-B61A-E48256869A39}.Debug|x64.ActiveCfg = Debug|x64 + {A78E8601-3191-4987-B61A-E48256869A39}.Debug|x64.Build.0 = Debug|x64 + {A78E8601-3191-4987-B61A-E48256869A39}.Debug|x86.ActiveCfg = Debug|Win32 + {A78E8601-3191-4987-B61A-E48256869A39}.Debug|x86.Build.0 = Debug|Win32 + {A78E8601-3191-4987-B61A-E48256869A39}.Release|x64.ActiveCfg = Release|x64 + {A78E8601-3191-4987-B61A-E48256869A39}.Release|x64.Build.0 = Release|x64 + {A78E8601-3191-4987-B61A-E48256869A39}.Release|x86.ActiveCfg = Release|Win32 + {A78E8601-3191-4987-B61A-E48256869A39}.Release|x86.Build.0 = Release|Win32 + {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Debug|x64.ActiveCfg = Debug|x64 + {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Debug|x64.Build.0 = Debug|x64 + {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Debug|x86.ActiveCfg = Debug|x86 + {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Debug|x86.Build.0 = Debug|x86 + {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Release|x64.ActiveCfg = Release|x64 + {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Release|x64.Build.0 = Release|x64 + {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Release|x86.ActiveCfg = Release|x86 + {8FFAA094-2D9C-49B5-BB36-4B2BB7512444}.Release|x86.Build.0 = Release|x86 + {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Debug|x64.ActiveCfg = Debug|x64 + {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Debug|x64.Build.0 = Debug|x64 + {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Debug|x86.ActiveCfg = Debug|x86 + {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Debug|x86.Build.0 = Debug|x86 + {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Release|x64.ActiveCfg = Release|x64 + {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Release|x64.Build.0 = Release|x64 + {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Release|x86.ActiveCfg = Release|x86 + {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {F09FEFE0-ED5E-4A17-9C54-DB6C1CD559E9} = {E1670C4F-46C0-45BE-A351-E504DCB17A45} + {7E420EEB-3D6C-49FB-9E60-AE448BAC8B1F} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} + {1C27B092-88C3-4631-848B-F7165F8CF5E9} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} + {080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} + {6B799403-7548-4FD8-9AF8-76BD13473029} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} + {A78E8601-3191-4987-B61A-E48256869A39} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} + {8FFAA094-2D9C-49B5-BB36-4B2BB7512444} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} + {F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3} = {CB701FA8-9663-444D-9610-1A8CDFF30E61} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {97F14D53-B029-4E3E-B7DE-51A510B2D428} + EndGlobalSection +EndGlobal diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryProxy.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryProxy.cs index 974739c..85d36e4 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryProxy.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryProxy.cs @@ -234,13 +234,7 @@ public static HRESULT ActivateInstance(BaseActivationFactoryProxy* @this, IInspe object managedInstance = factory.ActivateInstance(); using ComPtr unkwnPtr = default; - unkwnPtr.Attach((IUnknown*) -#if NETSTANDARD - Marshal.GetIUnknownForObject(managedInstance) -#else - WinRT.ComWrappersSupport.CreateCCWForObject(managedInstance).GetRef() -#endif - ); + unkwnPtr.Attach((IUnknown*)WinRT.ComWrappersSupport.CreateCCWForObject(managedInstance).GetRef()); HRESULT result = unkwnPtr.CopyTo(instance); if (result != HRESULT.S_OK) { diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryProxy.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryProxy.cs index bb7b655..205ab83 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryProxy.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryProxy.cs @@ -164,11 +164,7 @@ public static HRESULT CreateInstance(BaseClassFactoryProxy* @this, IUnknown* pUn if (riid->Equals(IUnknown.IID_Guid)) { -#if NETSTANDARD - *ppvObject = (void*)Marshal.GetIUnknownForObject(instance); -#else *ppvObject = (void*)WinRT.ComWrappersSupport.CreateCCWForObject(instance).GetRef(); -#endif } else { @@ -178,11 +174,7 @@ public static HRESULT CreateInstance(BaseClassFactoryProxy* @this, IUnknown* pUn { return HRESULT.E_UNEXPECTED; } -#if NETSTANDARD - *ppvObject = (void*)Marshal.GetComInterfaceForObject(instance, t); -#else - * ppvObject = (void*)(nint)typeof(WinRT.MarshalInterface<>).MakeGenericType(t).GetMethod("FromManaged")!.Invoke(null, [instance])!; -#endif + *ppvObject = (void*)(nint)typeof(WinRT.MarshalInterface<>).MakeGenericType(t).GetMethod("FromManaged")!.Invoke(null, [instance])!; } factory.OnInstanceCreated(instance); diff --git a/src/Shmuelie.WinRTServer/Internal/UIntPtrExtensions.cs b/src/Shmuelie.WinRTServer/Internal/UIntPtrExtensions.cs deleted file mode 100644 index 29fe2b9..0000000 --- a/src/Shmuelie.WinRTServer/Internal/UIntPtrExtensions.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; - -namespace Shmuelie.WinRTServer; - -#if NETSTANDARD2_0 -/// -/// Implements methods for missing in NS2.0. -/// -internal static class UIntPtrExtensions -{ - /// - /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. - /// - /// - /// An unsigned native integer to compare. - /// - /// A value that indicates the relative order of the objects being compared. The return value has these meanings: - /// - /// - /// Value - /// Meaning - /// - /// - /// Less than zero - /// This instance precedes in the sort order. - /// - /// - /// Zero - /// This instance occurs in the same position in the sort order as . - /// - /// - /// Greater than zero - /// This instance follows in the sort order. - /// - /// - /// - public static int CompareTo(this nuint @this, nuint value) - { - return ((UIntPtr)@this).ToUInt64().CompareTo(((UIntPtr)value).ToUInt64()); - } - - /// - /// Converts the numeric value of this instance to its equivalent string representation, using the specified format. - /// - /// - /// A standard or custom numeric format string. - /// The string representation of the value of this instance as specified by . - /// is invalid or not supported. - public static string ToString(this nuint @this, string? format) - { - return ((UIntPtr)@this).ToUInt64().ToString(format); - } - - /// - /// Formats the value of the current instance using the specified format. - /// - /// - /// The format to use. -or- A reference to use the default format defined for the type of the implementation. - /// An object that supplies culture-specific formatting information. - /// The value of the current instance in the specified format. - /// is invalid or not supported. - public static string ToString(this nuint @this, string? format, IFormatProvider? provider) - { - return ((UIntPtr)@this).ToUInt64().ToString(format, provider); - } -} -#endif \ No newline at end of file diff --git a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj index c1d9c58..5ec04a3 100644 --- a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj +++ b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj @@ -2,21 +2,12 @@ 10.0.22000.0 10.0.18362.0 - netstandard2.0;net8.0-windows$(TargetPlatformVersion) + net8.0-windows$(TargetPlatformVersion) true Added support for modern .NET servers. - true - - - - - - - - - + diff --git a/src/Shmuelie.WinRTServer/Windows/shared/uuids/__uuidof.cs b/src/Shmuelie.WinRTServer/Windows/shared/uuids/__uuidof.cs deleted file mode 100644 index de1e2fc..0000000 --- a/src/Shmuelie.WinRTServer/Windows/shared/uuids/__uuidof.cs +++ /dev/null @@ -1,125 +0,0 @@ -#if NETSTANDARD -using System; -using System.ComponentModel; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace Shmuelie.Interop.Windows; - -#pragma warning disable IDE1006 // Naming Styles - -/// -/// Helpers from the windows.h header. -/// -internal static class Windows -{ - /// - /// Retrieves the GUID of of a specified type. - /// - /// A value of type . - /// The type to retrieve the GUID for. - /// A value wrapping a pointer to the GUID data for the input type. This value can be either converted to a pointer, or implicitly assigned to a value. - [MethodImpl(MethodImplOptions.AggressiveInlining)] -#pragma warning disable IDE0060 // Remove unused parameter - public static unsafe UuidOfType __uuidof(T value) // for type inference similar to C++'s __uuidof -#pragma warning restore IDE0060 // Remove unused parameter - where T : unmanaged - { - return new(UUID.RIID); - } - - /// - /// Retrieves the GUID of of a specified type. - /// - /// A pointer to a value of type . - /// The type to retrieve the GUID for. - /// A value wrapping a pointer to the GUID data for the input type. This value can be either converted to a pointer, or implicitly assigned to a value. - [MethodImpl(MethodImplOptions.AggressiveInlining)] -#pragma warning disable IDE0060 // Remove unused parameter - public static unsafe UuidOfType __uuidof(T* value) // for type inference similar to C++'s __uuidof -#pragma warning restore IDE0060 // Remove unused parameter - where T : unmanaged - { - return new(UUID.RIID); - } - - /// - /// Retrieves the GUID of of a specified type. - /// - /// The type to retrieve the GUID for. - /// A value wrapping a pointer to the GUID data for the input type. This value can be either converted to a pointer, or implicitly assigned to a value. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe UuidOfType __uuidof() - where T : unmanaged - { - return new(UUID.RIID); - } - - /// - /// A proxy type that wraps a pointer to GUID data. Values of this type can be implicitly converted to and assigned to * or parameters. - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public readonly unsafe ref struct UuidOfType - { - /// - /// The pointer to the target value. - /// - private readonly Guid* riid; - - /// - /// Creates a new instance with the specified parameter. - /// - /// A pointer to the input value. - internal UuidOfType(Guid* riid) - { - this.riid = riid; - } - - /// - /// Reads a value from the GUID buffer for a given instance. - /// - /// The input instance to read data for. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Guid(UuidOfType guid) => *guid.riid; - - /// - /// Returns the * pointer to the GUID buffer for a given instance. - /// - /// The input instance to read data for. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Guid*(UuidOfType guid) => guid.riid; - } - - /// - /// A helper type to provide static GUID buffers for specific types. - /// - /// The type to allocate a GUID buffer for. - private static unsafe class UUID - where T : unmanaged - { - /// - /// The pointer to the value for the current type. - /// - /// The target memory area should never be written to. - public static readonly Guid* RIID = CreateRIID(); - - /// - /// Allocates memory for a value and initializes it. - /// - /// A pointer to memory holding the value for the current type. - private static Guid* CreateRIID() - { - // This memory is never released, but since UWP doesn't support assembly unloading we don't really care about - // this bit anyway: once initialized, each generic type instantiation would live forever anyway. This means - // that even with RuntimeHelpers.AllocateTypeAssociatedMemory on .NET 5+, the result would've been the same. - Guid* p = (Guid*)Marshal.AllocHGlobal(sizeof(Guid)); - - *p = typeof(T).GUID; - - return p; - } - } -} - -#pragma warning restore IDE1006 // Naming Styles -#endif \ No newline at end of file diff --git a/src/Shmuelie.WinRTServer/Windows/winrt/wrl/client/ComPtr.cs b/src/Shmuelie.WinRTServer/Windows/winrt/wrl/client/ComPtr.cs index 97640ef..5b2656b 100644 --- a/src/Shmuelie.WinRTServer/Windows/winrt/wrl/client/ComPtr.cs +++ b/src/Shmuelie.WinRTServer/Windows/winrt/wrl/client/ComPtr.cs @@ -121,17 +121,10 @@ public readonly HRESULT CopyTo(ref T* ptr) /// The target raw pointer to copy the address of the current COM object to. /// The result of for the target type . public readonly HRESULT CopyTo(U** ptr) - where U : unmanaged -#if !NETSTANDARD - , IComIID -#endif + where U : unmanaged, IComIID { -#if NETSTANDARD - return ((IUnknown*)ptr_)->QueryInterface(Windows.__uuidof(), (void**)ptr); -#else Guid iid = U.Guid; return ((IUnknown*)ptr_)->QueryInterface(&iid, (void**)ptr); -#endif } /// @@ -140,19 +133,12 @@ public readonly HRESULT CopyTo(U** ptr) /// The target raw pointer to copy the address of the current COM object to. /// The result of for the target type . public readonly HRESULT CopyTo(ref U* ptr) - where U : unmanaged -#if !NETSTANDARD - , IComIID -#endif + where U : unmanaged, IComIID { void** tmp = null; -#if NETSTANDARD - HRESULT @ref = ((IUnknown*)ptr_)->QueryInterface(Windows.__uuidof(), tmp); -#else Guid iid = U.Guid; HRESULT @ref = ((IUnknown*)ptr_)->QueryInterface(&iid, tmp); -#endif ptr = (U*)tmp; diff --git a/tests/Shmuelie.WinRTServer.Sample.Package/Package.appxmanifest b/tests/Shmuelie.WinRTServer.Sample.Package/Package.appxmanifest index 372f5ac..7fab37d 100644 --- a/tests/Shmuelie.WinRTServer.Sample.Package/Package.appxmanifest +++ b/tests/Shmuelie.WinRTServer.Sample.Package/Package.appxmanifest @@ -96,37 +96,6 @@ Square310x310Logo="Images\LargeTile.png"/> - - - - - - - - - - - - - - - - - diff --git a/tests/Shmuelie.WinRTServer.Sample.Package/Shmuelie.WinRTServer.Sample.Package.wapproj b/tests/Shmuelie.WinRTServer.Sample.Package/Shmuelie.WinRTServer.Sample.Package.wapproj index 9436b34..1b114c1 100644 --- a/tests/Shmuelie.WinRTServer.Sample.Package/Shmuelie.WinRTServer.Sample.Package.wapproj +++ b/tests/Shmuelie.WinRTServer.Sample.Package/Shmuelie.WinRTServer.Sample.Package.wapproj @@ -1,143 +1,138 @@ - - - - 15.0 - - - - Debug - x86 - - - Release - x86 - - - Debug - x64 - - - Release - x64 - - - Debug - ARM - - - Release - ARM - - - Debug - ARM64 - - - Release - ARM64 - - - Debug - AnyCPU - - - Release - AnyCPU - - - - $(MSBuildExtensionsPath)\Microsoft\DesktopBridge\ - - - - 080d9051-af78-4b2a-b5cc-9f5ba79a0a9b - 10.0.22000.0 - 10.0.18362.0 - en-US - false - $(NoWarn);NU1702;APPX1707;APPX0006 - ..\Shmuelie.WinRTServer.Sample.UwpClient\Shmuelie.WinRTServer.Sample.UwpClient.csproj - - - - Designer - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - native;net462;net481;net6.0-windows$(TargetPlatformVersion);net8.0-windows10.0.22000.0;$(AssetTargetFallback) - - - - - - - exe - DispatchToInnerBuilds - net462 - - - exe - DispatchToInnerBuilds - net8.0-windows10.0.22000.0 - - - - - - - + + + + 15.0 + + + + Debug + x86 + + + Release + x86 + + + Debug + x64 + + + Release + x64 + + + Debug + ARM + + + Release + ARM + + + Debug + ARM64 + + + Release + ARM64 + + + Debug + AnyCPU + + + Release + AnyCPU + + + + $(MSBuildExtensionsPath)\Microsoft\DesktopBridge\ + + + + 080d9051-af78-4b2a-b5cc-9f5ba79a0a9b + 10.0.22000.0 + 10.0.18362.0 + en-US + false + $(NoWarn);NU1702;APPX1707;APPX0006 + ..\Shmuelie.WinRTServer.Sample.UwpClient\Shmuelie.WinRTServer.Sample.UwpClient.csproj + + + + Designer + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + native;net462;net481;net6.0-windows$(TargetPlatformVersion);net8.0-windows10.0.22000.0;$(AssetTargetFallback) + + + + + + + exe + DispatchToInnerBuilds + net8.0-windows10.0.22000.0 + + + + + + + \ No newline at end of file From 8bf89d2150ab65bfd5e1d7241c5c608f8bb030f1 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Sat, 24 Aug 2024 19:00:27 -0700 Subject: [PATCH 02/43] Update dependencies --- src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj index 5ec04a3..3109aab 100644 --- a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj +++ b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj @@ -7,8 +7,8 @@ Added support for modern .NET servers. - - + + From 3d0d1519e9a20391cce3063abd12262a696e56f6 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Sat, 24 Aug 2024 20:35:10 -0700 Subject: [PATCH 03/43] Initial prototype of using COM Source Generators and removing usage of runtime marshaling and making AOT safe --- src/Shmuelie.WinRTServer/ComServer.cs | 12 +- .../Internal/BaseActivationFactoryProxy.cs | 255 ------------------ .../Internal/BaseActivationFactoryWrapper.cs | 97 +++++++ .../Internal/BaseClassFactoryProxy.cs | 210 --------------- .../Internal/BaseClassFactoryWrapper.cs | 77 ++++++ src/Shmuelie.WinRTServer/NativeMethods.json | 5 +- src/Shmuelie.WinRTServer/NativeMethods.txt | 32 +-- .../Shmuelie.WinRTServer.csproj | 4 +- src/Shmuelie.WinRTServer/WinRtServer.cs | 7 +- src/Shmuelie.WinRTServer/Windows/Com/BOOL.cs | 7 + .../Windows/Com/HRESULT.cs | 7 + .../Windows/Com/HSTRING.cs | 7 + .../Windows/Com/IActivationFactory.cs | 15 ++ .../Windows/Com/IClassFactory.cs | 19 ++ .../Windows/Com/IInspectable.cs | 23 ++ .../Windows/Com/Marshalling/BoolMarshaller.cs | 15 ++ .../Com/Marshalling/HResultMarshaller.cs | 15 ++ .../Com/Marshalling/HStringMarshaller.cs | 16 ++ .../Com/Marshalling/TrustLevelMarshaller.cs | 15 ++ .../Windows/{winrt/wrl/client => }/ComPtr.cs | 2 +- .../roapi => }/DllGetActivationFactory.cs | 2 +- .../Windows/{winrt => }/HSTRING.cs | 2 +- 22 files changed, 353 insertions(+), 491 deletions(-) delete mode 100644 src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryProxy.cs create mode 100644 src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs delete mode 100644 src/Shmuelie.WinRTServer/Internal/BaseClassFactoryProxy.cs create mode 100644 src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/BOOL.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/HRESULT.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/HSTRING.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/Marshalling/BoolMarshaller.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HResultMarshaller.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HStringMarshaller.cs create mode 100644 src/Shmuelie.WinRTServer/Windows/Com/Marshalling/TrustLevelMarshaller.cs rename src/Shmuelie.WinRTServer/Windows/{winrt/wrl/client => }/ComPtr.cs (99%) rename src/Shmuelie.WinRTServer/Windows/{winrt/roapi => }/DllGetActivationFactory.cs (98%) rename src/Shmuelie.WinRTServer/Windows/{winrt => }/HSTRING.cs (92%) diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index 9607f5f..873d5bb 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -1,9 +1,12 @@ using System; using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; using System.Runtime.Versioning; using System.Threading.Tasks; using System.Timers; -using Shmuelie.Interop.Windows; +using Shmuelie.WinRTServer.Internal; +using Shmuelie.WinRTServer.Windows; using Windows.Win32.Foundation; using Windows.Win32.System.Com; using static Windows.Win32.PInvoke; @@ -33,6 +36,8 @@ public sealed class ComServer : IAsyncDisposable /// private readonly Timer lifetimeCheckTimer; + private readonly StrategyBasedComWrappers comWrappers = new(); + /// /// Tracks the creation of the first instance after server is started. /// @@ -127,11 +132,10 @@ public unsafe bool RegisterClassFactory(BaseClassFactory factory) factory.InstanceCreated += Factory_InstanceCreated; - using ComPtr proxy = default; - proxy.Attach(BaseClassFactoryProxy.Create(factory)); + nint wrapper = comWrappers.GetOrCreateComInterfaceForObject(new BaseClassFactoryWrapper(factory), CreateComInterfaceFlags.None); uint cookie; - CoRegisterClassObject(&clsid, (IUnknown*)proxy.Get(), CLSCTX.CLSCTX_LOCAL_SERVER, (REGCLS.REGCLS_MULTIPLEUSE | REGCLS.REGCLS_SUSPENDED), &cookie).ThrowOnFailure(); + CoRegisterClassObject(&clsid, (IUnknown*)wrapper, CLSCTX.CLSCTX_LOCAL_SERVER, (REGCLS.REGCLS_MULTIPLEUSE | REGCLS.REGCLS_SUSPENDED), &cookie).ThrowOnFailure(); factories.Add(clsid, (factory, cookie)); return true; diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryProxy.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryProxy.cs deleted file mode 100644 index 85d36e4..0000000 --- a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryProxy.cs +++ /dev/null @@ -1,255 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Runtime.Versioning; -using System.Threading; -using Shmuelie.Interop.Windows; -using Windows.Win32.Foundation; -using Windows.Win32.System.Com; -using Windows.Win32.System.WinRT; -using static Windows.Win32.PInvoke; - -namespace Shmuelie.WinRTServer; - -/// -/// CCW for . -/// -[SupportedOSPlatform("windows8.0")] -internal unsafe struct BaseActivationFactoryProxy -{ - private static readonly void** Vtbl = InitVtbl(); - - private static void** InitVtbl() - { - void** lpVtbl = (void**)Marshal.AllocHGlobal(sizeof(void*) * 7); - - lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.QueryInterfaceWrapper); - lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.AddRefWrapper); - lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.ReleaseWrapper); - lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.GetIidsWrapper); - lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.GetRuntimeClassNameWrapper); - lpVtbl[5] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.GetTrustLevelWrapper); - lpVtbl[6] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.ActivateInstanceWrapper); - - return lpVtbl; - } - - /// - /// The vtable pointer for the current instance. - /// - private void** _lpVtbl; - - private GCHandle _factory; - - /// - /// The current reference count for the object (from IUnknown). - /// - private uint _referenceCount; - - public static BaseActivationFactoryProxy* Create(BaseActivationFactory factory) - { - BaseActivationFactoryProxy* @this = (BaseActivationFactoryProxy*)Marshal.AllocHGlobal(sizeof(BaseActivationFactoryProxy)); - @this->_lpVtbl = Vtbl; - @this->_factory = GCHandle.Alloc(factory); - @this->_referenceCount = 1; - - return @this; - } - - /// - /// Devirtualized API for IUnknown.Release(). - /// - /// The updated reference count for the current instance. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public uint Release() - { - return Impl.Release((BaseActivationFactoryProxy*)Unsafe.AsPointer(ref this)); - } - - private static class Impl - { - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate HRESULT QueryInterfaceDelegate(BaseActivationFactoryProxy* @this, Guid* riid, void** ppvObject); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate uint AddRefDelegate(BaseActivationFactoryProxy* @this); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate uint ReleaseDelegate(BaseActivationFactoryProxy* @this); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate HRESULT GetIidsDelegate(BaseActivationFactoryProxy* @this, uint* iidCount, Guid** iids); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate HRESULT GetRuntimeClassNameDelegate(BaseActivationFactoryProxy* @this, HSTRING* className); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate HRESULT GetTrustLevelDelegate(BaseActivationFactoryProxy* @this, TrustLevel* trustLevel); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate HRESULT ActivateInstanceDelegate(BaseActivationFactoryProxy* @this, IInspectable** instance); - - /// - /// The cached for IUnknown.QueryInterface(REFIID, void**). - /// - public static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface; - - /// - /// The cached for IUnknown.AddRef(). - /// - public static readonly AddRefDelegate AddRefWrapper = AddRef; - - /// - /// The cached for IUnknown.Release(). - /// - public static readonly ReleaseDelegate ReleaseWrapper = Release; - - public static readonly GetIidsDelegate GetIidsWrapper = GetIids; - - public static readonly GetRuntimeClassNameDelegate GetRuntimeClassNameWrapper = GetRuntimeClassName; - - public static readonly GetTrustLevelDelegate GetTrustLevelWrapper = GetTrustLevel; - - public static readonly ActivateInstanceDelegate ActivateInstanceWrapper = ActivateInstance; - - /// - /// Implements IUnknown.QueryInterface(REFIID, void**). - /// - private static HRESULT QueryInterface(BaseActivationFactoryProxy* @this, Guid* riid, void** ppvObject) - { - if (riid->Equals(IUnknown.IID_Guid) || - riid->Equals(IInspectable.IID_Guid) || - riid->Equals(IActivationFactory.IID_Guid)) - { - _ = Interlocked.Increment(ref Unsafe.As(ref @this->_referenceCount)); - - *ppvObject = @this; - - return HRESULT.S_OK; - } - - return HRESULT.E_NOINTERFACE; - } - - /// - /// Implements IUnknown.AddRef(). - /// - private static uint AddRef(BaseActivationFactoryProxy* @this) - { - return (uint)Interlocked.Increment(ref Unsafe.As(ref @this->_referenceCount)); - } - - /// - /// Implements IUnknown.Release(). - /// - public static uint Release(BaseActivationFactoryProxy* @this) - { - uint referenceCount = (uint)Interlocked.Decrement(ref Unsafe.As(ref @this->_referenceCount)); - - if (referenceCount == 0) - { - @this->_factory.Free(); - - Marshal.FreeHGlobal((IntPtr)@this); - } - - return referenceCount; - } - - public static HRESULT GetIids(BaseActivationFactoryProxy* @this, uint* iidCount, Guid** iids) - { - if (iidCount is null || iids is null) - { - return HRESULT.E_INVALIDARG; - } - - *iidCount = 1; - *iids = (Guid*)Marshal.AllocHGlobal(sizeof(Guid)); - *iids[0] = IActivationFactory.IID_Guid; - return HRESULT.S_OK; - } - - public static HRESULT GetRuntimeClassName(BaseActivationFactoryProxy* @this, HSTRING* className) - { - try - { - if (className is null) - { - return HRESULT.E_INVALIDARG; - } - - BaseActivationFactory? factory = Unsafe.As(@this->_factory.Target); - - if (factory is null) - { - return HRESULT.E_HANDLE; - } - - string? fullName = factory.GetType().FullName; - - if (fullName is null) - { - return HRESULT.E_UNEXPECTED; - } - - fixed (char* fullNamePtr = fullName) - { - return WindowsCreateString((PCWSTR)fullNamePtr, (uint)fullName.Length, className); - } - - } - catch (Exception e) - { - return (HRESULT)Marshal.GetHRForException(e); - } - } - - public static HRESULT GetTrustLevel(BaseActivationFactoryProxy* @this, TrustLevel* trustLevel) - { - if (trustLevel is null) - { - return HRESULT.E_INVALIDARG; - } - - *trustLevel = TrustLevel.BaseTrust; - return HRESULT.S_OK; - } - - public static HRESULT ActivateInstance(BaseActivationFactoryProxy* @this, IInspectable** instance) - { - try - { - if (instance is null) - { - return HRESULT.E_INVALIDARG; - } - - BaseActivationFactory? factory = Unsafe.As(@this->_factory.Target); - - if (factory is null) - { - return HRESULT.E_HANDLE; - } - - object managedInstance = factory.ActivateInstance(); - - using ComPtr unkwnPtr = default; - unkwnPtr.Attach((IUnknown*)WinRT.ComWrappersSupport.CreateCCWForObject(managedInstance).GetRef()); - HRESULT result = unkwnPtr.CopyTo(instance); - if (result != HRESULT.S_OK) - { - return result; - } - - factory.OnInstanceCreated(managedInstance); - - return HRESULT.S_OK; - - } - catch (Exception e) - { - return (HRESULT)Marshal.GetHRForException(e); - } - } - } -} diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs new file mode 100644 index 0000000..e372779 --- /dev/null +++ b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs @@ -0,0 +1,97 @@ +using System; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; +using Windows.Win32.Foundation; +using IInspectable = Windows.Win32.System.WinRT.IInspectable; +using static Windows.Win32.PInvoke; + +namespace Shmuelie.WinRTServer.Internal; + +[GeneratedComClass] +internal partial class BaseActivationFactoryWrapper(BaseActivationFactory factory) : IActivationFactory +{ + private readonly StrategyBasedComWrappers comWrappers = new(); + + [return: MarshalUsing(typeof(HResultMarshaller))] + public unsafe HRESULT ActivateInstance(void** instance) + { + if (instance is null) + { + return HRESULT.E_INVALIDARG; + } + + try + { + object managedInstance = factory.ActivateInstance(); + var unknown = comWrappers.GetOrCreateComInterfaceForObject(managedInstance, CreateComInterfaceFlags.None); + var hr = (HRESULT)StrategyBasedComWrappers.DefaultIUnknownStrategy.QueryInterface((void*)unknown, IInspectable.IID_Guid, out *instance); + if (hr.Failed) + { + return hr; + } + + factory.OnInstanceCreated(managedInstance); + } + catch (Exception e) + { + return (HRESULT)Marshal.GetHRForException(e); + } + return HRESULT.S_OK; + } + + [return: MarshalUsing(typeof(HResultMarshaller))] + public unsafe HRESULT GetIids(uint* iidCount, Guid** iids) + { + if (iidCount is null || iids is null) + { + return HRESULT.E_INVALIDARG; + } + + *iidCount = 1; + *iids = (Guid*)Marshal.AllocHGlobal(sizeof(Guid)); + *iids[0] = global::Windows.Win32.System.WinRT.IActivationFactory.IID_Guid; + return HRESULT.S_OK; + } + + [return: MarshalUsing(typeof(HResultMarshaller))] + public unsafe HRESULT GetRuntimeClassName([MarshalUsing(typeof(HStringMarshaller))] global::Windows.Win32.System.WinRT.HSTRING* className) + { + if (className is null) + { + return HRESULT.E_INVALIDARG; + } + + try + { + string? fullName = factory.GetType().FullName; + + if (fullName is null) + { + return HRESULT.E_UNEXPECTED; + } + + fixed (char* fullNamePtr = fullName) + { + return WindowsCreateString((PCWSTR)fullNamePtr, (uint)fullName.Length, className); + } + } + catch (Exception e) + { + return (HRESULT)Marshal.GetHRForException(e); + } + } + + [return: MarshalUsing(typeof(HResultMarshaller))] + public unsafe HRESULT GetTrustLevel([MarshalUsing(typeof(TrustLevelMarshaller))] global::Windows.Win32.System.WinRT.TrustLevel* trustLevel) + { + if (trustLevel is null) + { + return HRESULT.E_INVALIDARG; + } + + *trustLevel = global::Windows.Win32.System.WinRT.TrustLevel.BaseTrust; + return HRESULT.S_OK; + } +} diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryProxy.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryProxy.cs deleted file mode 100644 index 205ab83..0000000 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryProxy.cs +++ /dev/null @@ -1,210 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Threading; -using Windows.Win32.Foundation; -using Windows.Win32.System.Com; -using static Windows.Win32.PInvoke; - -namespace Shmuelie.WinRTServer; - -/// -/// CCW for . -/// -internal unsafe struct BaseClassFactoryProxy -{ - private static readonly void** Vtbl = InitVtbl(); - - private static void** InitVtbl() - { - void** lpVtbl = (void**)Marshal.AllocHGlobal(sizeof(void*) * 5); - - lpVtbl[0] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.QueryInterfaceWrapper); - lpVtbl[1] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.AddRefWrapper); - lpVtbl[2] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.ReleaseWrapper); - lpVtbl[3] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.CreateInstanceWrapper); - lpVtbl[4] = (void*)Marshal.GetFunctionPointerForDelegate(Impl.LockServerWrapper); - - return lpVtbl; - } - - /// - /// The vtable pointer for the current instance. - /// - private void** _lpVtbl; - - private GCHandle _factory; - - /// - /// The current reference count for the object (from IUnknown). - /// - private uint _referenceCount; - - public static BaseClassFactoryProxy* Create(BaseClassFactory factory) - { - BaseClassFactoryProxy* @this = (BaseClassFactoryProxy*)Marshal.AllocHGlobal(sizeof(BaseClassFactoryProxy)); - @this->_lpVtbl = Vtbl; - @this->_factory = GCHandle.Alloc(factory); - @this->_referenceCount = 1; - - return @this; - } - - /// - /// Devirtualized API for IUnknown.Release(). - /// - /// The updated reference count for the current instance. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public uint Release() - { - return Impl.Release((BaseClassFactoryProxy*)Unsafe.AsPointer(ref this)); - } - - private static class Impl - { - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate HRESULT QueryInterfaceDelegate(BaseClassFactoryProxy* @this, Guid* riid, void** ppvObject); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate uint AddRefDelegate(BaseClassFactoryProxy* @this); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate uint ReleaseDelegate(BaseClassFactoryProxy* @this); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate HRESULT CreateInstanceDelegate(BaseClassFactoryProxy* @this, IUnknown* pUnkOuter, Guid* riid, void** ppvObject); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - public delegate HRESULT LockServerDelegate(BaseClassFactoryProxy* @this, int fLock); - - /// - /// The cached for IUnknown.QueryInterface(REFIID, void**). - /// - public static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface; - - /// - /// The cached for IUnknown.AddRef(). - /// - public static readonly AddRefDelegate AddRefWrapper = AddRef; - - /// - /// The cached for IUnknown.Release(). - /// - public static readonly ReleaseDelegate ReleaseWrapper = Release; - - public static readonly CreateInstanceDelegate CreateInstanceWrapper = CreateInstance; - - public static readonly LockServerDelegate LockServerWrapper = LockServer; - - /// - /// Implements IUnknown.QueryInterface(REFIID, void**). - /// - private static HRESULT QueryInterface(BaseClassFactoryProxy* @this, Guid* riid, void** ppvObject) - { - if (riid->Equals(IUnknown.IID_Guid) || - riid->Equals(IClassFactory.IID_Guid)) - { - _ = Interlocked.Increment(ref Unsafe.As(ref @this->_referenceCount)); - - *ppvObject = @this; - - return HRESULT.S_OK; - } - - return HRESULT.E_NOINTERFACE; - } - - /// - /// Implements IUnknown.AddRef(). - /// - private static uint AddRef(BaseClassFactoryProxy* @this) - { - return (uint)Interlocked.Increment(ref Unsafe.As(ref @this->_referenceCount)); - } - - /// - /// Implements IUnknown.Release(). - /// - public static uint Release(BaseClassFactoryProxy* @this) - { - uint referenceCount = (uint)Interlocked.Decrement(ref Unsafe.As(ref @this->_referenceCount)); - - if (referenceCount == 0) - { - @this->_factory.Free(); - - Marshal.FreeHGlobal((IntPtr)@this); - } - - return referenceCount; - } - - public static HRESULT CreateInstance(BaseClassFactoryProxy* @this, IUnknown* pUnkOuter, Guid* riid, void** ppvObject) - { - try - { - if (pUnkOuter is not null) - { - return HRESULT.CLASS_E_NOAGGREGATION; - } - - BaseClassFactory? factory = Unsafe.As(@this->_factory.Target); - - if (factory is null) - { - return HRESULT.E_HANDLE; - } - - if (!riid->Equals(IUnknown.IID_Guid) && !riid->Equals(factory.Iid)) - { - return HRESULT.E_NOINTERFACE; - } - - var instance = factory.CreateInstance(); - - if (riid->Equals(IUnknown.IID_Guid)) - { - *ppvObject = (void*)WinRT.ComWrappersSupport.CreateCCWForObject(instance).GetRef(); - } - else - { - Type? t = Marshal.GetTypeFromCLSID(factory.Clsid); - - if (t is null) - { - return HRESULT.E_UNEXPECTED; - } - *ppvObject = (void*)(nint)typeof(WinRT.MarshalInterface<>).MakeGenericType(t).GetMethod("FromManaged")!.Invoke(null, [instance])!; - } - - factory.OnInstanceCreated(instance); - } - catch (Exception e) - { - return (HRESULT)Marshal.GetHRForException(e); - } - return HRESULT.S_OK; - } - - public static HRESULT LockServer(BaseClassFactoryProxy* @this, int fLock) - { - try - { - if (fLock != 0) - { - _ = CoAddRefServerProcess(); - } - else - { - _ = CoReleaseServerProcess(); - } - } - catch (Exception e) - { - return (HRESULT)Marshal.GetHRForException(e); - } - return HRESULT.S_OK; - } - } -} - diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs new file mode 100644 index 0000000..674a972 --- /dev/null +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -0,0 +1,77 @@ +using System; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com; +using IUnknown = Windows.Win32.System.Com.IUnknown; + +using static Windows.Win32.PInvoke; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; + +namespace Shmuelie.WinRTServer.Internal; + +[GeneratedComClass] +internal partial class BaseClassFactoryWrapper(BaseClassFactory factory) : IClassFactory +{ + private readonly StrategyBasedComWrappers comWrappers = new(); + + [return: MarshalUsing(typeof(HResultMarshaller))] + public unsafe global::Windows.Win32.Foundation.HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject) + { + if (pUnkOuter is not null) + { + return global::Windows.Win32.Foundation.HRESULT.CLASS_E_NOAGGREGATION; + } + + if (!riid->Equals(IUnknown.IID_Guid) && !riid->Equals(factory.Iid)) + { + return global::Windows.Win32.Foundation.HRESULT.E_NOINTERFACE; + } + + try + { + var instance = factory.CreateInstance(); + var unknown = comWrappers.GetOrCreateComInterfaceForObject(instance, CreateComInterfaceFlags.None); + + if (riid->Equals(IUnknown.IID_Guid)) + { + *ppvObject = (void*)unknown; + } + else + { + var hr = (global::Windows.Win32.Foundation.HRESULT)StrategyBasedComWrappers.DefaultIUnknownStrategy.QueryInterface((void*)unknown, factory.Clsid, out *ppvObject); + if (hr.Failed) + { + return hr; + } + } + + factory.OnInstanceCreated(instance); + } + catch (Exception e) + { + return (global::Windows.Win32.Foundation.HRESULT)Marshal.GetHRForException(e); + } + return global::Windows.Win32.Foundation.HRESULT.S_OK; + } + + [return: MarshalUsing(typeof(HResultMarshaller))] + public global::Windows.Win32.Foundation.HRESULT LockServer([MarshalUsing(typeof(BoolMarshaller))] global::Windows.Win32.Foundation.BOOL fLock) + { + try + { + if (fLock != 0) + { + _ = CoAddRefServerProcess(); + } + else + { + _ = CoReleaseServerProcess(); + } + } + catch (Exception e) + { + return (global::Windows.Win32.Foundation.HRESULT)Marshal.GetHRForException(e); + } + return global::Windows.Win32.Foundation.HRESULT.S_OK; + } +} diff --git a/src/Shmuelie.WinRTServer/NativeMethods.json b/src/Shmuelie.WinRTServer/NativeMethods.json index 38318d6..a90ae06 100644 --- a/src/Shmuelie.WinRTServer/NativeMethods.json +++ b/src/Shmuelie.WinRTServer/NativeMethods.json @@ -4,5 +4,8 @@ "comInterop": { "preserveSigMethods": [ "*" ] }, - "useSafeHandles": false + "useSafeHandles": false, + "friendlyOverloads": { + "enabled": false + } } diff --git a/src/Shmuelie.WinRTServer/NativeMethods.txt b/src/Shmuelie.WinRTServer/NativeMethods.txt index a267404..4c5384e 100644 --- a/src/Shmuelie.WinRTServer/NativeMethods.txt +++ b/src/Shmuelie.WinRTServer/NativeMethods.txt @@ -1,25 +1,27 @@ -WindowsCreateString -IInspectable -IUnknown -IActivationFactory -E_NOINTERFACE -S_OK -E_INVALIDARG -E_HANDLE -E_UNEXPECTED -IClassFactory +BOOL CLASS_E_NOAGGREGATION CoAddRefServerProcess -CoReleaseServerProcess -IGlobalOptions CoCreateInstance CoRegisterClassObject -CoRevokeClassObject +CoReleaseServerProcess CoResumeClassObjects +CoRevokeClassObject CoSuspendClassObjects +E_HANDLE +E_INVALIDARG +E_NOINTERFACE +E_UNEXPECTED +GLOBALOPT_RO_FLAGS +HRESULT +HSTRING +IGlobalOptions +IInspectable +IUnknown RoInitialize RoRegisterActivationFactories RoRevokeActivationFactories S_FALSE -GLOBALOPT_RO_FLAGS -WindowsGetStringRawBuffer \ No newline at end of file +S_OK +TrustLevel +WindowsCreateString +WindowsGetStringRawBuffer diff --git a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj index 3109aab..06c5e19 100644 --- a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj +++ b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj @@ -5,9 +5,11 @@ net8.0-windows$(TargetPlatformVersion) true Added support for modern .NET servers. + True + True + True - diff --git a/src/Shmuelie.WinRTServer/WinRtServer.cs b/src/Shmuelie.WinRTServer/WinRtServer.cs index 54d5438..c8e531f 100644 --- a/src/Shmuelie.WinRTServer/WinRtServer.cs +++ b/src/Shmuelie.WinRTServer/WinRtServer.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; using System.Runtime.Versioning; using System.Threading.Tasks; using System.Timers; -using Shmuelie.Interop.Windows; +using Shmuelie.WinRTServer.Internal; +using Shmuelie.WinRTServer.Windows; using Windows.Win32.Foundation; using Windows.Win32.System.Com; using Windows.Win32.System.WinRT; @@ -26,6 +28,7 @@ public sealed class WinRtServer : IAsyncDisposable private readonly unsafe DllGetActivationFactory activationFactoryCallbackWrapper; private unsafe readonly DllActivationCallback activationFactoryCallbackPointer; + private readonly StrategyBasedComWrappers comWrappers = new(); /// /// Collection of created instances. @@ -188,7 +191,7 @@ private unsafe HRESULT ActivationFactoryCallback(HSTRING activatableClassId, IAc return HRESULT.E_NOINTERFACE; } - *factory = (IActivationFactory*)BaseActivationFactoryProxy.Create(managedFactory); + *factory = (IActivationFactory*)comWrappers.GetOrCreateComInterfaceForObject(new BaseActivationFactoryWrapper(managedFactory), CreateComInterfaceFlags.None); return HRESULT.S_OK; } diff --git a/src/Shmuelie.WinRTServer/Windows/Com/BOOL.cs b/src/Shmuelie.WinRTServer/Windows/Com/BOOL.cs new file mode 100644 index 0000000..7c6564f --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/BOOL.cs @@ -0,0 +1,7 @@ +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; + +namespace Windows.Win32.Foundation; + +[NativeMarshalling(typeof(BoolMarshaller))] +partial struct BOOL; \ No newline at end of file diff --git a/src/Shmuelie.WinRTServer/Windows/Com/HRESULT.cs b/src/Shmuelie.WinRTServer/Windows/Com/HRESULT.cs new file mode 100644 index 0000000..18567f9 --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/HRESULT.cs @@ -0,0 +1,7 @@ +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; + +namespace Windows.Win32.Foundation; + +[NativeMarshalling(typeof(HResultMarshaller))] +partial struct HRESULT; diff --git a/src/Shmuelie.WinRTServer/Windows/Com/HSTRING.cs b/src/Shmuelie.WinRTServer/Windows/Com/HSTRING.cs new file mode 100644 index 0000000..ade023d --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/HSTRING.cs @@ -0,0 +1,7 @@ +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; + +namespace Windows.Win32.System.WinRT; + +[NativeMarshalling(typeof(HStringMarshaller))] +partial struct HSTRING; \ No newline at end of file diff --git a/src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs b/src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs new file mode 100644 index 0000000..92ea7dd --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs @@ -0,0 +1,15 @@ +using System; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; + +namespace Shmuelie.WinRTServer.Windows.Com; + +[GeneratedComInterface] +[Guid("00000035-0000-0000-C000-000000000046")] +internal unsafe partial interface IActivationFactory : IInspectable +{ + [PreserveSig] + [return: MarshalUsing(typeof(HResultMarshaller))] + global::Windows.Win32.Foundation.HRESULT ActivateInstance(void** instance); +} \ No newline at end of file diff --git a/src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs b/src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs new file mode 100644 index 0000000..7c8289f --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs @@ -0,0 +1,19 @@ +using System; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; + +namespace Shmuelie.WinRTServer.Windows.Com; + +[GeneratedComInterface] +[Guid("00000001-0000-0000-C000-000000000046")] +internal unsafe partial interface IClassFactory +{ + [PreserveSig] + [return: MarshalUsing(typeof(HResultMarshaller))] + global::Windows.Win32.Foundation.HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject); + + [PreserveSig] + [return: MarshalUsing(typeof(HResultMarshaller))] + global::Windows.Win32.Foundation.HRESULT LockServer([MarshalUsing(typeof(BoolMarshaller))] global::Windows.Win32.Foundation.BOOL fLock); +} diff --git a/src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs b/src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs new file mode 100644 index 0000000..927e59b --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs @@ -0,0 +1,23 @@ +using System; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; + +namespace Shmuelie.WinRTServer.Windows.Com; + +[GeneratedComInterface] +[Guid("AF86E2E0-B12D-4C6A-9C5A-D7AA65101E90")] +internal unsafe partial interface IInspectable +{ + [PreserveSig] + [return: MarshalUsing(typeof(HResultMarshaller))] + global::Windows.Win32.Foundation.HRESULT GetIids(uint* iidCount, Guid** iids); + + [PreserveSig] + [return: MarshalUsing(typeof(HResultMarshaller))] + global::Windows.Win32.Foundation.HRESULT GetRuntimeClassName([MarshalUsing(typeof(HStringMarshaller))] global::Windows.Win32.System.WinRT.HSTRING* className); + + [PreserveSig] + [return: MarshalUsing(typeof(HResultMarshaller))] + global::Windows.Win32.Foundation.HRESULT GetTrustLevel([MarshalUsing(typeof(TrustLevelMarshaller))] global::Windows.Win32.System.WinRT.TrustLevel* trustLevel); +} diff --git a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/BoolMarshaller.cs b/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/BoolMarshaller.cs new file mode 100644 index 0000000..2fff0d5 --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/BoolMarshaller.cs @@ -0,0 +1,15 @@ +using System.Runtime.InteropServices.Marshalling; +using Windows.Win32.Foundation; + +namespace Shmuelie.WinRTServer.Windows.Com.Marshalling; + +[CustomMarshaller(typeof(BOOL), MarshalMode.UnmanagedToManagedOut, typeof(BoolMarshaller))] +[CustomMarshaller(typeof(BOOL), MarshalMode.UnmanagedToManagedIn, typeof(BoolMarshaller))] +[CustomMarshaller(typeof(BOOL), MarshalMode.ManagedToUnmanagedOut, typeof(BoolMarshaller))] +[CustomMarshaller(typeof(BOOL), MarshalMode.ManagedToUnmanagedIn, typeof(BoolMarshaller))] +internal static class BoolMarshaller +{ + public static BOOL ConvertToManaged(int nativeValue) => new BOOL(nativeValue); + + public static int ConvertToUnmanaged(BOOL value) => value.Value; +} \ No newline at end of file diff --git a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HResultMarshaller.cs b/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HResultMarshaller.cs new file mode 100644 index 0000000..5086d32 --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HResultMarshaller.cs @@ -0,0 +1,15 @@ +using System.Runtime.InteropServices.Marshalling; +using Windows.Win32.Foundation; + +namespace Shmuelie.WinRTServer.Windows.Com.Marshalling; + +[CustomMarshaller(typeof(HRESULT), MarshalMode.UnmanagedToManagedOut, typeof(HResultMarshaller))] +[CustomMarshaller(typeof(HRESULT), MarshalMode.UnmanagedToManagedIn, typeof(HResultMarshaller))] +[CustomMarshaller(typeof(HRESULT), MarshalMode.ManagedToUnmanagedOut, typeof(HResultMarshaller))] +[CustomMarshaller(typeof(HRESULT), MarshalMode.ManagedToUnmanagedIn, typeof(HResultMarshaller))] +internal static class HResultMarshaller +{ + public static HRESULT ConvertToManaged(int nativeValue) => new HRESULT(nativeValue); + + public static int ConvertToUnmanaged(HRESULT value) => value.Value; +} diff --git a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HStringMarshaller.cs b/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HStringMarshaller.cs new file mode 100644 index 0000000..f6d0efb --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HStringMarshaller.cs @@ -0,0 +1,16 @@ +using System; +using System.Runtime.InteropServices.Marshalling; +using Windows.Win32.System.WinRT; + +namespace Shmuelie.WinRTServer.Windows.Com.Marshalling; + +[CustomMarshaller(typeof(HSTRING), MarshalMode.UnmanagedToManagedOut, typeof(HStringMarshaller))] +[CustomMarshaller(typeof(HSTRING), MarshalMode.UnmanagedToManagedIn, typeof(HStringMarshaller))] +[CustomMarshaller(typeof(HSTRING), MarshalMode.ManagedToUnmanagedOut, typeof(HStringMarshaller))] +[CustomMarshaller(typeof(HSTRING), MarshalMode.ManagedToUnmanagedIn, typeof(HStringMarshaller))] +internal static class HStringMarshaller +{ + public static HSTRING ConvertToManaged(IntPtr nativeValue) => new HSTRING(nativeValue); + + public static IntPtr ConvertToUnmanaged(HSTRING value) => value.Value; +} diff --git a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/TrustLevelMarshaller.cs b/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/TrustLevelMarshaller.cs new file mode 100644 index 0000000..ccc2462 --- /dev/null +++ b/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/TrustLevelMarshaller.cs @@ -0,0 +1,15 @@ +using System.Runtime.InteropServices.Marshalling; +using Windows.Win32.System.WinRT; + +namespace Shmuelie.WinRTServer.Windows.Com.Marshalling; + +[CustomMarshaller(typeof(TrustLevel), MarshalMode.UnmanagedToManagedOut, typeof(TrustLevelMarshaller))] +[CustomMarshaller(typeof(TrustLevel), MarshalMode.UnmanagedToManagedIn, typeof(TrustLevelMarshaller))] +[CustomMarshaller(typeof(TrustLevel), MarshalMode.ManagedToUnmanagedOut, typeof(TrustLevelMarshaller))] +[CustomMarshaller(typeof(TrustLevel), MarshalMode.ManagedToUnmanagedIn, typeof(TrustLevelMarshaller))] +internal static class TrustLevelMarshaller +{ + public static TrustLevel ConvertToManaged(int nativeValue) => (TrustLevel)nativeValue; + + public static int ConvertToUnmanaged(TrustLevel value) => (int)value; +} diff --git a/src/Shmuelie.WinRTServer/Windows/winrt/wrl/client/ComPtr.cs b/src/Shmuelie.WinRTServer/Windows/ComPtr.cs similarity index 99% rename from src/Shmuelie.WinRTServer/Windows/winrt/wrl/client/ComPtr.cs rename to src/Shmuelie.WinRTServer/Windows/ComPtr.cs index 5b2656b..1816b41 100644 --- a/src/Shmuelie.WinRTServer/Windows/winrt/wrl/client/ComPtr.cs +++ b/src/Shmuelie.WinRTServer/Windows/ComPtr.cs @@ -4,7 +4,7 @@ using Windows.Win32.Foundation; using Windows.Win32.System.Com; -namespace Shmuelie.Interop.Windows; +namespace Shmuelie.WinRTServer.Windows; /// /// A type that allows working with pointers to COM objects more securely. diff --git a/src/Shmuelie.WinRTServer/Windows/winrt/roapi/DllGetActivationFactory.cs b/src/Shmuelie.WinRTServer/Windows/DllGetActivationFactory.cs similarity index 98% rename from src/Shmuelie.WinRTServer/Windows/winrt/roapi/DllGetActivationFactory.cs rename to src/Shmuelie.WinRTServer/Windows/DllGetActivationFactory.cs index c1f0a02..ce11281 100644 --- a/src/Shmuelie.WinRTServer/Windows/winrt/roapi/DllGetActivationFactory.cs +++ b/src/Shmuelie.WinRTServer/Windows/DllGetActivationFactory.cs @@ -2,7 +2,7 @@ using Windows.Win32.Foundation; using Windows.Win32.System.WinRT; -namespace Shmuelie.Interop.Windows; +namespace Shmuelie.WinRTServer.Windows; /// /// Retrieves the activation factory from a DLL that contains activatable Windows Runtime classes. diff --git a/src/Shmuelie.WinRTServer/Windows/winrt/HSTRING.cs b/src/Shmuelie.WinRTServer/Windows/HSTRING.cs similarity index 92% rename from src/Shmuelie.WinRTServer/Windows/winrt/HSTRING.cs rename to src/Shmuelie.WinRTServer/Windows/HSTRING.cs index 13afa2f..e99dfc0 100644 --- a/src/Shmuelie.WinRTServer/Windows/winrt/HSTRING.cs +++ b/src/Shmuelie.WinRTServer/Windows/HSTRING.cs @@ -3,7 +3,7 @@ using Windows.Win32.System.WinRT; using static Windows.Win32.PInvoke; -namespace Shmuelie.Interop.Windows; +namespace Shmuelie.WinRTServer.Windows; internal static class HSTRING_ { From 98f7bd6798ca91514d57498a29aaa84b1a2856ec Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Sat, 24 Aug 2024 20:50:50 -0700 Subject: [PATCH 04/43] Update sample projects references and make sample server AOT --- Directory.Build.props | 2 +- .../Shmuelie.WinRTServer.Sample.CppConsoleClient.vcxproj | 8 ++++---- .../packages.config | 2 +- .../Shmuelie.WinRTServer.Sample.Interfaces.csproj | 2 +- .../Shmuelie.WinRTServer.Sample.Metadata.vcxproj | 8 ++++---- .../Shmuelie.WinRTServer.Sample.Metadata/packages.config | 2 +- .../Shmuelie.WinRTServer.Sample.Package.wapproj | 2 +- tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs | 4 ++-- .../Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs | 8 +++++--- .../Shmuelie.WinRTServer.Sample.ServerNet6.csproj | 6 +++++- tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs | 4 ++-- .../Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj | 2 +- .../Shmuelie.WinRTServer.Sample.WpfNetFxClient.csproj | 2 +- 13 files changed, 29 insertions(+), 23 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index f305f9c..625168b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -70,7 +70,7 @@ - + diff --git a/tests/Shmuelie.WinRTServer.Sample.CppConsoleClient/Shmuelie.WinRTServer.Sample.CppConsoleClient.vcxproj b/tests/Shmuelie.WinRTServer.Sample.CppConsoleClient/Shmuelie.WinRTServer.Sample.CppConsoleClient.vcxproj index 377a15a..37a2c2a 100644 --- a/tests/Shmuelie.WinRTServer.Sample.CppConsoleClient/Shmuelie.WinRTServer.Sample.CppConsoleClient.vcxproj +++ b/tests/Shmuelie.WinRTServer.Sample.CppConsoleClient/Shmuelie.WinRTServer.Sample.CppConsoleClient.vcxproj @@ -1,6 +1,6 @@ - + true true @@ -126,14 +126,14 @@ - + 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/tests/Shmuelie.WinRTServer.Sample.CppConsoleClient/packages.config b/tests/Shmuelie.WinRTServer.Sample.CppConsoleClient/packages.config index 1b8cfbd..7a9561b 100644 --- a/tests/Shmuelie.WinRTServer.Sample.CppConsoleClient/packages.config +++ b/tests/Shmuelie.WinRTServer.Sample.CppConsoleClient/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/tests/Shmuelie.WinRTServer.Sample.Interfaces/Shmuelie.WinRTServer.Sample.Interfaces.csproj b/tests/Shmuelie.WinRTServer.Sample.Interfaces/Shmuelie.WinRTServer.Sample.Interfaces.csproj index a46ce37..8436a25 100644 --- a/tests/Shmuelie.WinRTServer.Sample.Interfaces/Shmuelie.WinRTServer.Sample.Interfaces.csproj +++ b/tests/Shmuelie.WinRTServer.Sample.Interfaces/Shmuelie.WinRTServer.Sample.Interfaces.csproj @@ -9,6 +9,6 @@ disable - + \ No newline at end of file diff --git a/tests/Shmuelie.WinRTServer.Sample.Metadata/Shmuelie.WinRTServer.Sample.Metadata.vcxproj b/tests/Shmuelie.WinRTServer.Sample.Metadata/Shmuelie.WinRTServer.Sample.Metadata.vcxproj index f1c8b3d..32d2b6c 100644 --- a/tests/Shmuelie.WinRTServer.Sample.Metadata/Shmuelie.WinRTServer.Sample.Metadata.vcxproj +++ b/tests/Shmuelie.WinRTServer.Sample.Metadata/Shmuelie.WinRTServer.Sample.Metadata.vcxproj @@ -1,6 +1,6 @@ - + Debug @@ -57,14 +57,14 @@ - + 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/tests/Shmuelie.WinRTServer.Sample.Metadata/packages.config b/tests/Shmuelie.WinRTServer.Sample.Metadata/packages.config index 1b8cfbd..7a9561b 100644 --- a/tests/Shmuelie.WinRTServer.Sample.Metadata/packages.config +++ b/tests/Shmuelie.WinRTServer.Sample.Metadata/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/tests/Shmuelie.WinRTServer.Sample.Package/Shmuelie.WinRTServer.Sample.Package.wapproj b/tests/Shmuelie.WinRTServer.Sample.Package/Shmuelie.WinRTServer.Sample.Package.wapproj index 1b114c1..7d42adb 100644 --- a/tests/Shmuelie.WinRTServer.Sample.Package/Shmuelie.WinRTServer.Sample.Package.wapproj +++ b/tests/Shmuelie.WinRTServer.Sample.Package/Shmuelie.WinRTServer.Sample.Package.wapproj @@ -121,7 +121,7 @@ native;net462;net481;net6.0-windows$(TargetPlatformVersion);net8.0-windows10.0.22000.0;$(AssetTargetFallback) - + diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs index 12d5f84..2558eb3 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs @@ -2,10 +2,10 @@ using System.Runtime.InteropServices; using Shmuelie.WinRTServer.Sample.Interfaces; -namespace Shmuelie.WinRTServer.Sample; +namespace Shmuelie.WinRTServer.Sample.Server; [Guid("2F8EDC40-C145-4128-B451-559D45CD9074")] -public sealed class Input : IInput +public sealed partial class Input : IInput { public string? Name { get; set; } public string? Description { get; set; } diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs index bd60ccf..7728659 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs @@ -10,10 +10,12 @@ using Windows.Foundation; using Windows.Storage.Streams; -namespace Shmuelie.WinRTServer.Sample; +namespace Shmuelie.WinRTServer.Sample.Server; + +#pragma warning disable CA1822 // Mark members as static [Guid("474527DE-81CD-466E-ADCF-6E3809CD5033")] -public sealed class RemoteThing : IRemoteThing +public sealed partial class RemoteThing : IRemoteThing { public int Rem(int a, int b) { @@ -40,7 +42,7 @@ public async Task> GenerateListAsync(ListOptions options, IPr await Task.Delay(options.DelayTicks, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); list.Add(i * 2); - progress?.Report(new ListProgress() { Count = i, Total = options.Count, Last = i * 2}); + progress?.Report(new ListProgress() { Count = i, Total = options.Count, Last = i * 2 }); } return list; } diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj index 7e72eaf..8464bfb 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj @@ -12,9 +12,13 @@ Shmuelie.WinRTServer.Sample $(DefineConstants);NET full + True + True + True + Shmuelie.WinRTServer.Sample.Server - + %(Filename) diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs index 624406d..f799723 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs @@ -2,10 +2,10 @@ using System.Runtime.InteropServices; using Shmuelie.WinRTServer.Sample.Interfaces; -namespace Shmuelie.WinRTServer.Sample; +namespace Shmuelie.WinRTServer.Sample.Server; [Guid("EC6A0FF9-BBF4-48EA-9BFA-DFAF84D4FAF8")] -public sealed class Times : ITimes +public sealed partial class Times : ITimes { public DateTimeOffset UtcNow { get; set; } public DateTimeOffset LocalNow { get; set; } diff --git a/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj b/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj index b52e6d4..82175fa 100644 --- a/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj +++ b/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj @@ -15,7 +15,7 @@ win-x64;$(RuntimeIdentifiers) - + %(Filename) true diff --git a/tests/Shmuelie.WinRTServer.Sample.WpfNetFxClient/Shmuelie.WinRTServer.Sample.WpfNetFxClient.csproj b/tests/Shmuelie.WinRTServer.Sample.WpfNetFxClient/Shmuelie.WinRTServer.Sample.WpfNetFxClient.csproj index 4da4600..63ebf22 100644 --- a/tests/Shmuelie.WinRTServer.Sample.WpfNetFxClient/Shmuelie.WinRTServer.Sample.WpfNetFxClient.csproj +++ b/tests/Shmuelie.WinRTServer.Sample.WpfNetFxClient/Shmuelie.WinRTServer.Sample.WpfNetFxClient.csproj @@ -8,7 +8,7 @@ win-x64;$(RuntimeIdentifiers) - + %(Filename) true From 221d09b6d75a9e8c2cfba00c76fb22f2934be0fd Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Sun, 25 Aug 2024 16:20:57 -0700 Subject: [PATCH 05/43] Fix project dependencies --- Shmuelie.WinRTServer.sln | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Shmuelie.WinRTServer.sln b/Shmuelie.WinRTServer.sln index d030eaf..f7f150e 100644 --- a/Shmuelie.WinRTServer.sln +++ b/Shmuelie.WinRTServer.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# 17 +# Visual Studio Version 17 VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_SolutionItems", "{50DD472E-AD00-45C5-A2F7-4CB77CB8CD67}" @@ -38,12 +38,24 @@ EndProject Project("{C7167F0D-BC9F-4E6E-AFE1-012C56B48DB5}") = "Shmuelie.WinRTServer.Sample.Package", "tests\Shmuelie.WinRTServer.Sample.Package\Shmuelie.WinRTServer.Sample.Package.wapproj", "{080D9051-AF78-4B2A-B5CC-9F5BA79A0A9B}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.WpfNetFxClient", "tests\Shmuelie.WinRTServer.Sample.WpfNetFxClient\Shmuelie.WinRTServer.Sample.WpfNetFxClient.csproj", "{6B799403-7548-4FD8-9AF8-76BD13473029}" + ProjectSection(ProjectDependencies) = postProject + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} = {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Shmuelie.WinRTServer.Sample.CppConsoleClient", "tests\Shmuelie.WinRTServer.Sample.CppConsoleClient\Shmuelie.WinRTServer.Sample.CppConsoleClient.vcxproj", "{A78E8601-3191-4987-B61A-E48256869A39}" + ProjectSection(ProjectDependencies) = postProject + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} = {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} + EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.WpfNet6Client", "tests\Shmuelie.WinRTServer.Sample.WpfNet6Client\Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj", "{8FFAA094-2D9C-49B5-BB36-4B2BB7512444}" + ProjectSection(ProjectDependencies) = postProject + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} = {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} + EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shmuelie.WinRTServer.Sample.ServerNet6", "tests\Shmuelie.WinRTServer.Sample.ServerNet6\Shmuelie.WinRTServer.Sample.ServerNet6.csproj", "{F37FE3DF-B23B-44C7-A3C1-1E4F20885DB3}" + ProjectSection(ProjectDependencies) = postProject + {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} = {AEAC1534-6CA9-4A7E-9F24-E1F72D261AE4} + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution From ceabee574f4578ded9c8ae89d069253ed1aeff86 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Mon, 26 Aug 2024 14:55:17 -0700 Subject: [PATCH 06/43] Fix for CsWinRT --- .../Shmuelie.WinRTServer.Sample.ServerNet6.csproj | 1 + .../Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj index 8464bfb..95b084f 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj @@ -16,6 +16,7 @@ True True Shmuelie.WinRTServer.Sample.Server + 10.0.22000.41 diff --git a/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj b/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj index 82175fa..dac0699 100644 --- a/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj +++ b/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj @@ -1,4 +1,4 @@ - + 10.0.22000.0 10.0.18362.0 @@ -13,6 +13,7 @@ full native;net481;$(AssetTargetFallback) win-x64;$(RuntimeIdentifiers) + 10.0.22000.41 From 3fa3851f16e2da4ba838bc826be39a3b95de502b Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Mon, 26 Aug 2024 21:43:43 -0700 Subject: [PATCH 07/43] Fix build --- .github/workflows/dotnet.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index d8d703c..5973011 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -24,7 +24,7 @@ jobs: - name: NuGet Restore run: nuget restore - name: Build - run: msbuild -t:restore,build /p:Configuration=${{matrix.configuration}} /p:Platform=x64 /bl + run: msbuild /t:restore;build /p:Configuration=${{matrix.configuration}} /p:Platform=x64 /bl - name: Upload MSBuild binary log uses: actions/upload-artifact@v3 with: @@ -47,9 +47,9 @@ jobs: - name: NuGet Restore run: nuget restore - name: Build - run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj -t:restore,build -p:Configuration=Release + run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj /t:restore;build -p:Configuration=Release - name: Pack - run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj -t:pack -p:Configuration=Release + run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj /t:pack -p:Configuration=Release - name: Upload package artifacts uses: actions/upload-artifact@v3 with: From 3ed9ff45b6eaf5315a49a6add1bed1127e2a9b26 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Mon, 26 Aug 2024 21:47:29 -0700 Subject: [PATCH 08/43] Need to use powershell escape --- .github/workflows/dotnet.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 5973011..80e2108 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -24,7 +24,7 @@ jobs: - name: NuGet Restore run: nuget restore - name: Build - run: msbuild /t:restore;build /p:Configuration=${{matrix.configuration}} /p:Platform=x64 /bl + run: msbuild /t:restore`;build /p:Configuration=${{matrix.configuration}} /p:Platform=x64 /bl - name: Upload MSBuild binary log uses: actions/upload-artifact@v3 with: @@ -47,7 +47,7 @@ jobs: - name: NuGet Restore run: nuget restore - name: Build - run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj /t:restore;build -p:Configuration=Release + run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj /t:restore`;build -p:Configuration=Release - name: Pack run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj /t:pack -p:Configuration=Release - name: Upload package artifacts From 3d82d383db697d299005d24eee7d431dba2ce16f Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Mon, 26 Aug 2024 21:49:20 -0700 Subject: [PATCH 09/43] Standardize args --- .github/workflows/dotnet.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 80e2108..1a7213e 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -47,9 +47,9 @@ jobs: - name: NuGet Restore run: nuget restore - name: Build - run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj /t:restore`;build -p:Configuration=Release + run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj /t:restore`;build /p:Configuration=Release - name: Pack - run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj /t:pack -p:Configuration=Release + run: msbuild .\src\Shmuelie.WinRTServer\Shmuelie.WinRTServer.csproj /t:pack /p:Configuration=Release - name: Upload package artifacts uses: actions/upload-artifact@v3 with: From 4d107a59b70276667f5751a5781110082b4859ee Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Mon, 26 Aug 2024 21:59:15 -0700 Subject: [PATCH 10/43] Specify RIDs --- src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj index 06c5e19..0bcb4f1 100644 --- a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj +++ b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj @@ -8,6 +8,7 @@ True True True + win-x64;win-x86;win-arm64;$(RuntimeIdentifiers) From 14c71223440aace3e9cc0406b629ba61a1dff351 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Tue, 27 Aug 2024 13:22:02 -0700 Subject: [PATCH 11/43] Cast to RIID, not to factory CLSID --- src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index 674a972..4867b1e 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -38,7 +38,7 @@ internal partial class BaseClassFactoryWrapper(BaseClassFactory factory) : IClas } else { - var hr = (global::Windows.Win32.Foundation.HRESULT)StrategyBasedComWrappers.DefaultIUnknownStrategy.QueryInterface((void*)unknown, factory.Clsid, out *ppvObject); + var hr = (global::Windows.Win32.Foundation.HRESULT)StrategyBasedComWrappers.DefaultIUnknownStrategy.QueryInterface((void*)unknown, *riid, out *ppvObject); if (hr.Failed) { return hr; From b30dd0e686317ab1c380859ce54af55b3e5bbdb4 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Tue, 27 Aug 2024 14:34:45 -0700 Subject: [PATCH 12/43] Add support for different ComWrapper implementations, and helpers for CsWinRT's a COM Source generators --- src/Shmuelie.WinRTServer/ComServer.cs | 7 +- .../ComServerExtensions.cs | 19 ++-- .../CsWinRT/ComServerExtensions.cs | 90 +++++++++++++++++++ .../Internal/BaseClassFactoryWrapper.cs | 16 ++-- .../StrategyBased/ComServerExtensions.cs | 90 +++++++++++++++++++ .../Program.cs | 1 + 6 files changed, 204 insertions(+), 19 deletions(-) create mode 100644 src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs create mode 100644 src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index 873d5bb..042b0f3 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -102,13 +102,14 @@ private void LifetimeCheckTimer_Elapsed(object? sender, ElapsedEventArgs e) /// Register a class factory with the server. /// /// The class factory to register. + /// The implementation of to use for wrapping. /// if was registered; otherwise, . /// Only one factory can be registered for a CLSID. /// The instance is disposed. /// is . /// The server is running. /// - public unsafe bool RegisterClassFactory(BaseClassFactory factory) + public unsafe bool RegisterClassFactory(BaseClassFactory factory, ComWrappers comWrappers) { if (IsDisposed) { @@ -132,7 +133,7 @@ public unsafe bool RegisterClassFactory(BaseClassFactory factory) factory.InstanceCreated += Factory_InstanceCreated; - nint wrapper = comWrappers.GetOrCreateComInterfaceForObject(new BaseClassFactoryWrapper(factory), CreateComInterfaceFlags.None); + nint wrapper = comWrappers.GetOrCreateComInterfaceForObject(new BaseClassFactoryWrapper(factory, comWrappers), CreateComInterfaceFlags.None); uint cookie; CoRegisterClassObject(&clsid, (IUnknown*)wrapper, CLSCTX.CLSCTX_LOCAL_SERVER, (REGCLS.REGCLS_MULTIPLEUSE | REGCLS.REGCLS_SUSPENDED), &cookie).ThrowOnFailure(); @@ -148,7 +149,7 @@ public unsafe bool RegisterClassFactory(BaseClassFactory factory) /// if the server was removed; otherwise, . /// The instance is disposed. /// The server is running. - /// + /// public unsafe bool UnregisterClassFactory(Guid clsid) { if (IsDisposed) diff --git a/src/Shmuelie.WinRTServer/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/ComServerExtensions.cs index 79d6b83..8096b78 100644 --- a/src/Shmuelie.WinRTServer/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/ComServerExtensions.cs @@ -1,4 +1,6 @@ using System; +using System.Runtime.InteropServices.Marshalling; +using System.Runtime.InteropServices; using System.Runtime.Versioning; namespace Shmuelie.WinRTServer; @@ -15,19 +17,20 @@ public static class ComServerExtensions /// The type to register. /// The interface that implements. /// The instance. + /// The implementation of to use for wrapping. /// if type was registered; otherwise, . /// Type can only be registered once. /// The instance is disposed. /// is . /// The server is running. - public static bool RegisterClass(this ComServer server) where T : class, TInterface, new() + public static bool RegisterClass(this ComServer server, ComWrappers comWrappers) where T : class, TInterface, new() { if (server is null) { throw new ArgumentNullException(nameof(server)); } - return server.RegisterClassFactory(new GeneralClassFactory()); + return server.RegisterClassFactory(new GeneralClassFactory(), comWrappers); } /// @@ -37,39 +40,41 @@ public static class ComServerExtensions /// The interface that implements. /// The instance. /// Method to create instance of . + /// The implementation of to use for wrapping. /// if type was registered; otherwise, . /// Type can only be registered once. /// The instance is disposed. /// is . /// The server is running. - public static bool RegisterClass(this ComServer server, Func factory) where T : class, TInterface + public static bool RegisterClass(this ComServer server, Func factory, ComWrappers comWrappers) where T : class, TInterface { if (server is null) { throw new ArgumentNullException(nameof(server)); } - return server.RegisterClassFactory(new DelegateClassFactory(factory)); + return server.RegisterClassFactory(new DelegateClassFactory(factory), comWrappers); } /// /// Register a class factory with the server. /// /// The type of the factory to register. - /// /// The instance. + /// The instance. + /// The implementation of to use for wrapping. /// if an instance of was registered; otherwise, . /// Only one factory can be registered for a CLSID. /// The instance is disposed. /// is . /// The server is running. - public static bool RegisterClassFactory(this ComServer server) where T : BaseClassFactory, new() + public static bool RegisterClassFactory(this ComServer server, ComWrappers comWrappers) where T : BaseClassFactory, new() { if (server is null) { throw new ArgumentNullException(nameof(server)); } - return server.RegisterClassFactory(new T()); + return server.RegisterClassFactory(new T(), comWrappers); } /// diff --git a/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs new file mode 100644 index 0000000..d9addcf --- /dev/null +++ b/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs @@ -0,0 +1,90 @@ +using System; +using System.Runtime.Versioning; +using WinRT; + +namespace Shmuelie.WinRTServer.CsWinRT; + +/// +/// Extensions for when using . +/// +[SupportedOSPlatform("windows6.0.6000")] +public static class ComServerExtensions +{ + private static readonly DefaultComWrappers comWrappers = new(); + + /// + /// Register a type with the server. + /// + /// The type to register. + /// The interface that implements. + /// The instance. + /// if type was registered; otherwise, . + /// Type can only be registered once. + /// The instance is disposed. + /// is . + /// The server is running. + public static bool RegisterClass(this ComServer server) where T : class, TInterface, new() + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterClassFactory(new GeneralClassFactory(), comWrappers); + } + + /// + /// Register a type with the server. + /// + /// The type to register. + /// The interface that implements. + /// The instance. + /// Method to create instance of . + /// if type was registered; otherwise, . + /// Type can only be registered once. + /// The instance is disposed. + /// is . + /// The server is running. + public static bool RegisterClass(this ComServer server, Func factory) where T : class, TInterface + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterClassFactory(new DelegateClassFactory(factory), comWrappers); + } + + /// + /// Register a class factory with the server. + /// + /// The type of the factory to register. + /// /// The instance. + /// if an instance of was registered; otherwise, . + /// Only one factory can be registered for a CLSID. + /// The instance is disposed. + /// is . + /// The server is running. + public static bool RegisterClassFactory(this ComServer server) where T : BaseClassFactory, new() + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterClassFactory(new T(), comWrappers); + } + + /// + /// Unregister a class factory. + /// + /// The instance. + /// The class factory to unregister. + /// if the server was removed; otherwise, . + /// The instance is disposed. + /// The server is running. + public static bool UnregisterClassFactory(this ComServer server, BaseClassFactory factory) + { + return server.UnregisterClassFactory(factory.Clsid); + } +} \ No newline at end of file diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index 4867b1e..f18c0d8 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -2,18 +2,15 @@ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; using Shmuelie.WinRTServer.Windows.Com; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; using IUnknown = Windows.Win32.System.Com.IUnknown; - using static Windows.Win32.PInvoke; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; namespace Shmuelie.WinRTServer.Internal; [GeneratedComClass] -internal partial class BaseClassFactoryWrapper(BaseClassFactory factory) : IClassFactory +internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrappers comWrappers) : IClassFactory { - private readonly StrategyBasedComWrappers comWrappers = new(); - [return: MarshalUsing(typeof(HResultMarshaller))] public unsafe global::Windows.Win32.Foundation.HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject) { @@ -38,11 +35,12 @@ internal partial class BaseClassFactoryWrapper(BaseClassFactory factory) : IClas } else { - var hr = (global::Windows.Win32.Foundation.HRESULT)StrategyBasedComWrappers.DefaultIUnknownStrategy.QueryInterface((void*)unknown, *riid, out *ppvObject); - if (hr.Failed) - { - return hr; + global::Windows.Win32.Foundation.HRESULT hr = (global::Windows.Win32.Foundation.HRESULT)Marshal.QueryInterface(unknown, ref *riid, out nint ppv); + if (hr.Failed) + { + return hr; } + *ppvObject = (void*)ppv; } factory.OnInstanceCreated(instance); diff --git a/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs new file mode 100644 index 0000000..bdf2e2f --- /dev/null +++ b/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs @@ -0,0 +1,90 @@ +using System; +using System.Runtime.InteropServices.Marshalling; +using System.Runtime.Versioning; + +namespace Shmuelie.WinRTServer.StrategyBased; + +/// +/// Extensions for when using . +/// +[SupportedOSPlatform("windows6.0.6000")] +public static class ComServerExtensions +{ + private static readonly StrategyBasedComWrappers comWrappers = new(); + + /// + /// Register a type with the server. + /// + /// The type to register. + /// The interface that implements. + /// The instance. + /// if type was registered; otherwise, . + /// Type can only be registered once. + /// The instance is disposed. + /// is . + /// The server is running. + public static bool RegisterClass(this ComServer server) where T : class, TInterface, new() + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterClassFactory(new GeneralClassFactory(), comWrappers); + } + + /// + /// Register a type with the server. + /// + /// The type to register. + /// The interface that implements. + /// The instance. + /// Method to create instance of . + /// if type was registered; otherwise, . + /// Type can only be registered once. + /// The instance is disposed. + /// is . + /// The server is running. + public static bool RegisterClass(this ComServer server, Func factory) where T : class, TInterface + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterClassFactory(new DelegateClassFactory(factory), comWrappers); + } + + /// + /// Register a class factory with the server. + /// + /// The type of the factory to register. + /// /// The instance. + /// if an instance of was registered; otherwise, . + /// Only one factory can be registered for a CLSID. + /// The instance is disposed. + /// is . + /// The server is running. + public static bool RegisterClassFactory(this ComServer server) where T : BaseClassFactory, new() + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterClassFactory(new T(), comWrappers); + } + + /// + /// Unregister a class factory. + /// + /// The instance. + /// The class factory to unregister. + /// if the server was removed; otherwise, . + /// The instance is disposed. + /// The server is running. + public static bool UnregisterClassFactory(this ComServer server, BaseClassFactory factory) + { + return server.UnregisterClassFactory(factory.Clsid); + } +} diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Program.cs b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Program.cs index ba9337b..151c064 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Program.cs +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Program.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Threading.Tasks; using Shmuelie.WinRTServer.Sample.Interfaces; +using Shmuelie.WinRTServer.CsWinRT; namespace Shmuelie.WinRTServer.Sample.Server; From 3ef785a8e5830823a67b714ff6fae8a198a91e28 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Tue, 27 Aug 2024 14:46:20 -0700 Subject: [PATCH 13/43] Use instance field, not parameter --- src/Shmuelie.WinRTServer/ComServer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index 042b0f3..f1b7890 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -133,7 +133,7 @@ public unsafe bool RegisterClassFactory(BaseClassFactory factory, ComWrappers co factory.InstanceCreated += Factory_InstanceCreated; - nint wrapper = comWrappers.GetOrCreateComInterfaceForObject(new BaseClassFactoryWrapper(factory, comWrappers), CreateComInterfaceFlags.None); + nint wrapper = this.comWrappers.GetOrCreateComInterfaceForObject(new BaseClassFactoryWrapper(factory, comWrappers), CreateComInterfaceFlags.None); uint cookie; CoRegisterClassObject(&clsid, (IUnknown*)wrapper, CLSCTX.CLSCTX_LOCAL_SERVER, (REGCLS.REGCLS_MULTIPLEUSE | REGCLS.REGCLS_SUSPENDED), &cookie).ThrowOnFailure(); From ce0b791824caa3391b072b6d02960b782d790a4a Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Tue, 27 Aug 2024 14:51:51 -0700 Subject: [PATCH 14/43] Increment vesion to 2.0.0 --- Directory.Build.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 625168b..7957efe 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -19,7 +19,7 @@ As such, this needs to be changed before a new release as well. --> - 1.2.0 + 2.0.0 false From 6b9db190cd9747bbac82197ce755ed8b8cc02212 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Tue, 27 Aug 2024 15:10:40 -0700 Subject: [PATCH 15/43] Remove NetFX server --- .../Input.cs | 12 --- .../Program.cs | 35 ------- .../RemoteThing.cs | 96 ------------------- .../Shmuelie.WinRTServer.Sample.Server.csproj | 33 ------- .../Times.cs | 13 --- 5 files changed, 189 deletions(-) delete mode 100644 tests/Shmuelie.WinRTServer.Sample.Server/Input.cs delete mode 100644 tests/Shmuelie.WinRTServer.Sample.Server/Program.cs delete mode 100644 tests/Shmuelie.WinRTServer.Sample.Server/RemoteThing.cs delete mode 100644 tests/Shmuelie.WinRTServer.Sample.Server/Shmuelie.WinRTServer.Sample.Server.csproj delete mode 100644 tests/Shmuelie.WinRTServer.Sample.Server/Times.cs diff --git a/tests/Shmuelie.WinRTServer.Sample.Server/Input.cs b/tests/Shmuelie.WinRTServer.Sample.Server/Input.cs deleted file mode 100644 index 7247b5d..0000000 --- a/tests/Shmuelie.WinRTServer.Sample.Server/Input.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using Shmuelie.WinRTServer.Sample.Interfaces; - -namespace Shmuelie.WinRTServer.Sample; - -[Guid("4F59AF92-A98D-4A20-8C8D-1C076647A6B0")] -public sealed class Input : IInput -{ - public string Name { get; set; } - public string Description { get; set; } -} diff --git a/tests/Shmuelie.WinRTServer.Sample.Server/Program.cs b/tests/Shmuelie.WinRTServer.Sample.Server/Program.cs deleted file mode 100644 index ba9337b..0000000 --- a/tests/Shmuelie.WinRTServer.Sample.Server/Program.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Shmuelie.WinRTServer.Sample.Interfaces; - -namespace Shmuelie.WinRTServer.Sample.Server; - -public static class Program -{ - public async static Task Main(string[] args) - { - if (args.Contains("-COM")) - { - await using (ComServer server = new ComServer()) - { - server.RegisterClass(); - server.RegisterClass(); - server.RegisterClass(); - server.Start(); - await server.WaitForFirstObjectAsync(); - } - } - else if (args.Contains("-WINRT")) - { - await using (WinRtServer server = new WinRtServer()) - { - server.RegisterClass(); - server.RegisterClass(); - server.RegisterClass(); - server.Start(); - await server.WaitForFirstObjectAsync(); - } - } - } -} diff --git a/tests/Shmuelie.WinRTServer.Sample.Server/RemoteThing.cs b/tests/Shmuelie.WinRTServer.Sample.Server/RemoteThing.cs deleted file mode 100644 index f093eea..0000000 --- a/tests/Shmuelie.WinRTServer.Sample.Server/RemoteThing.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.WindowsRuntime; -using System.Threading; -using System.Threading.Tasks; -using Shmuelie.WinRTServer.Sample.Interfaces; -using Windows.Foundation; -using Windows.Storage.Streams; - -namespace Shmuelie.WinRTServer.Sample; - -[Guid("4F59AF92-A98D-4A20-8C8D-1C076647A6AE")] -public sealed class RemoteThing : IRemoteThing -{ - public int Rem(int a, int b) - { - return Math.DivRem(a, b, out var _); - } - - public async Task DelayAsync(int ticks, CancellationToken cancellationToken = default) - { - await Task.Delay(ticks, cancellationToken).ConfigureAwait(false); - } - - [DebuggerNonUserCode] - IAsyncAction IRemoteThing.DelayAsync(int ticks) - { - return AsyncInfo.Run(c => DelayAsync(ticks, c)); - } - - public async Task> GenerateListAsync(ListOptions options, IProgress progress = default, CancellationToken cancellationToken = default) - { - List list = []; - for (int i = 0; i < options.Count; i++) - { - cancellationToken.ThrowIfCancellationRequested(); - await Task.Delay(options.DelayTicks, cancellationToken).ConfigureAwait(false); - cancellationToken.ThrowIfCancellationRequested(); - list.Add(i * 2); - progress?.Report(new ListProgress() { Count = i, Total = options.Count, Last = i * 2}); - } - return list; - } - - [DebuggerNonUserCode] - IAsyncOperationWithProgress, ListProgress> IRemoteThing.GenerateListAsync(ListOptions options) - { - return AsyncInfo.Run, ListProgress>((c, p) => GenerateListAsync(options, p, c)); - } - - public async Task LoopAsync(int total, IProgress progress = default, CancellationToken cancellationToken = default) - { - for (int i = 0; i < total; i++) - { - cancellationToken.ThrowIfCancellationRequested(); - await Task.Delay(500, cancellationToken).ConfigureAwait(false); - cancellationToken.ThrowIfCancellationRequested(); - progress?.Report(new LoopProgress() { Count = i, Total = total }); - } - LoopCompleted?.Invoke(this, null); - } - - [DebuggerNonUserCode] - IAsyncActionWithProgress IRemoteThing.LoopAsync(int total) - { - return AsyncInfo.Run((c, p) => LoopAsync(total, p, c)); - } - - public event TypedEventHandler LoopCompleted; - - public DateTimeOffset NowUtc => DateTimeOffset.UtcNow; - - public Stream OpenFile(string path) - { - return File.OpenRead(path); - } - - [DebuggerNonUserCode] - IInputStream IRemoteThing.OpenFile(string path) - { - return OpenFile(path).AsInputStream(); - } - - public ITimes GetTimes(IInput input) - { - return new Times() - { - LocalNow = DateTimeOffset.Now, - UtcNow = DateTimeOffset.UtcNow, - NameAndDescription = input.Name + " " + input.Description - }; - } -} diff --git a/tests/Shmuelie.WinRTServer.Sample.Server/Shmuelie.WinRTServer.Sample.Server.csproj b/tests/Shmuelie.WinRTServer.Sample.Server/Shmuelie.WinRTServer.Sample.Server.csproj deleted file mode 100644 index e657fdb..0000000 --- a/tests/Shmuelie.WinRTServer.Sample.Server/Shmuelie.WinRTServer.Sample.Server.csproj +++ /dev/null @@ -1,33 +0,0 @@ - - - 10.0.22000.0 - 10.0.18362.0 - net462 - x64;x86 - disable - 6.2.13 - $(AssetTargetFallback);netstandard2.0 - - $(NoWarn);NU1702 - winexe - full - true - win-x64;$(RuntimeIdentifiers) - - - - - - - - - - net462 - %(Filename).%(Extension) - Windows - %(RootDir)%(Directory) - %(FullPath) - - - - diff --git a/tests/Shmuelie.WinRTServer.Sample.Server/Times.cs b/tests/Shmuelie.WinRTServer.Sample.Server/Times.cs deleted file mode 100644 index c476852..0000000 --- a/tests/Shmuelie.WinRTServer.Sample.Server/Times.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using Shmuelie.WinRTServer.Sample.Interfaces; - -namespace Shmuelie.WinRTServer.Sample; - -[Guid("4F59AF92-A98D-4A20-8C8D-1C076647A6AF")] -public sealed class Times : ITimes -{ - public DateTimeOffset UtcNow { get; set; } - public DateTimeOffset LocalNow { get; set; } - public string NameAndDescription { get; set; } -} From 6954ad31abaf8b6dfcef16aa8d02424ec54accfe Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 21:07:22 -0700 Subject: [PATCH 16/43] Remove redundant attributes --- .../Windows/Com/IActivationFactory.cs | 2 -- src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs | 5 +---- src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs | 8 ++------ 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs b/src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs index 92ea7dd..0915450 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs +++ b/src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs @@ -1,7 +1,6 @@ using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; namespace Shmuelie.WinRTServer.Windows.Com; @@ -10,6 +9,5 @@ namespace Shmuelie.WinRTServer.Windows.Com; internal unsafe partial interface IActivationFactory : IInspectable { [PreserveSig] - [return: MarshalUsing(typeof(HResultMarshaller))] global::Windows.Win32.Foundation.HRESULT ActivateInstance(void** instance); } \ No newline at end of file diff --git a/src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs b/src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs index 7c8289f..5f4c57c 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs +++ b/src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs @@ -1,7 +1,6 @@ using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; namespace Shmuelie.WinRTServer.Windows.Com; @@ -10,10 +9,8 @@ namespace Shmuelie.WinRTServer.Windows.Com; internal unsafe partial interface IClassFactory { [PreserveSig] - [return: MarshalUsing(typeof(HResultMarshaller))] global::Windows.Win32.Foundation.HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject); [PreserveSig] - [return: MarshalUsing(typeof(HResultMarshaller))] - global::Windows.Win32.Foundation.HRESULT LockServer([MarshalUsing(typeof(BoolMarshaller))] global::Windows.Win32.Foundation.BOOL fLock); + global::Windows.Win32.Foundation.HRESULT LockServer(global::Windows.Win32.Foundation.BOOL fLock); } diff --git a/src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs b/src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs index 927e59b..45ac587 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs +++ b/src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs @@ -1,7 +1,6 @@ using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; namespace Shmuelie.WinRTServer.Windows.Com; @@ -10,14 +9,11 @@ namespace Shmuelie.WinRTServer.Windows.Com; internal unsafe partial interface IInspectable { [PreserveSig] - [return: MarshalUsing(typeof(HResultMarshaller))] global::Windows.Win32.Foundation.HRESULT GetIids(uint* iidCount, Guid** iids); [PreserveSig] - [return: MarshalUsing(typeof(HResultMarshaller))] - global::Windows.Win32.Foundation.HRESULT GetRuntimeClassName([MarshalUsing(typeof(HStringMarshaller))] global::Windows.Win32.System.WinRT.HSTRING* className); + global::Windows.Win32.Foundation.HRESULT GetRuntimeClassName(global::Windows.Win32.System.WinRT.HSTRING* className); [PreserveSig] - [return: MarshalUsing(typeof(HResultMarshaller))] - global::Windows.Win32.Foundation.HRESULT GetTrustLevel([MarshalUsing(typeof(TrustLevelMarshaller))] global::Windows.Win32.System.WinRT.TrustLevel* trustLevel); + global::Windows.Win32.Foundation.HRESULT GetTrustLevel(global::Windows.Win32.System.WinRT.TrustLevel* trustLevel); } From eefac81a5dd53b3b9b6984c93f5e6c9977280e89 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 21:14:39 -0700 Subject: [PATCH 17/43] Fixup projects --- src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj | 5 +---- .../Shmuelie.WinRTServer.Sample.ServerNet6.csproj | 7 ++----- .../Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj | 4 +--- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj index 0bcb4f1..bdc975f 100644 --- a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj +++ b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj @@ -1,13 +1,10 @@  - 10.0.22000.0 - 10.0.18362.0 - net8.0-windows$(TargetPlatformVersion) + net8.0-windows10.0.22000.0 true Added support for modern .NET servers. True True - True win-x64;win-x86;win-arm64;$(RuntimeIdentifiers) diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj index 95b084f..beb8a1e 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj @@ -1,9 +1,7 @@  - 10.0.22000.0 - 10.0.18362.0 WinExe - net8.0-windows$(TargetPlatformVersion) + net8.0-windows10.0.22000.0 enable x64;x86 native;net481;$(AssetTargetFallback) @@ -12,9 +10,8 @@ Shmuelie.WinRTServer.Sample $(DefineConstants);NET full - True True - True + True Shmuelie.WinRTServer.Sample.Server 10.0.22000.41 diff --git a/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj b/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj index dac0699..7b5d138 100644 --- a/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj +++ b/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/Shmuelie.WinRTServer.Sample.WpfNet6Client.csproj @@ -1,9 +1,7 @@  - 10.0.22000.0 - 10.0.18362.0 WinExe - net8.0-windows$(TargetPlatformVersion) + net8.0-windows10.0.22000.0 enable true x64;x86 From ae2889d1c39f5e66435068611f897565067db537 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 21:16:02 -0700 Subject: [PATCH 18/43] Code fixes --- tests/Shmuelie.WinRTServer.Sample.UwpClient/App.xaml.cs | 4 +--- tests/Shmuelie.WinRTServer.Sample.UwpClient/MainPage.xaml.cs | 4 ++-- .../MainWindow.xaml.cs | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/Shmuelie.WinRTServer.Sample.UwpClient/App.xaml.cs b/tests/Shmuelie.WinRTServer.Sample.UwpClient/App.xaml.cs index 92f3384..c14c0c2 100644 --- a/tests/Shmuelie.WinRTServer.Sample.UwpClient/App.xaml.cs +++ b/tests/Shmuelie.WinRTServer.Sample.UwpClient/App.xaml.cs @@ -31,11 +31,9 @@ public App() /// Details about the launch request and process. protected override void OnLaunched(LaunchActivatedEventArgs e) { - Frame? rootFrame = Window.Current.Content as Frame; - // Do not repeat app initialization when the Window already has content, // just ensure that the window is active - if (rootFrame == null) + if (Window.Current.Content is not Frame rootFrame) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); diff --git a/tests/Shmuelie.WinRTServer.Sample.UwpClient/MainPage.xaml.cs b/tests/Shmuelie.WinRTServer.Sample.UwpClient/MainPage.xaml.cs index e21ae68..7823271 100644 --- a/tests/Shmuelie.WinRTServer.Sample.UwpClient/MainPage.xaml.cs +++ b/tests/Shmuelie.WinRTServer.Sample.UwpClient/MainPage.xaml.cs @@ -19,11 +19,11 @@ public MainPage() { this.InitializeComponent(); remoteThing = CreateRemoteThing(); - remoteThing.LoopCompleted += _remoteThing_LoopCompleted; + remoteThing.LoopCompleted += RemoteThing_LoopCompleted; DateTimeUtcBtn.Content = remoteThing.NowUtc.ToString(); } - private void _remoteThing_LoopCompleted(IRemoteThing sender, object args) + private void RemoteThing_LoopCompleted(IRemoteThing sender, object args) { _ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => LoopProg.Value = 0); } diff --git a/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/MainWindow.xaml.cs b/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/MainWindow.xaml.cs index 452d3ff..e312838 100644 --- a/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/MainWindow.xaml.cs +++ b/tests/Shmuelie.WinRTServer.Sample.WpfNet6Client/MainWindow.xaml.cs @@ -70,7 +70,7 @@ private async void OpenFileBtn_Click(object sender, RoutedEventArgs e) { var data = remoteThing.OpenFile("C:\\Windows\\explorer.exe").AsStreamForRead(); byte[] buffer = new byte[10]; - await data.ReadAsync(buffer, 0, buffer.Length); + await data.ReadAsync(buffer); OpenFileTxt.Text = string.Join("", buffer.Select(b => b.ToString("X2"))); } From 02ed788557986b1ed5bf773d5c1b4dd6d3ae9b9b Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 21:17:13 -0700 Subject: [PATCH 19/43] Whitespace --- .../Internal/BaseClassFactoryWrapper.cs | 144 +++++++++--------- 1 file changed, 72 insertions(+), 72 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index f18c0d8..8dee7f4 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -1,75 +1,75 @@ -using System; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Windows.Com; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; -using IUnknown = Windows.Win32.System.Com.IUnknown; -using static Windows.Win32.PInvoke; - -namespace Shmuelie.WinRTServer.Internal; - -[GeneratedComClass] -internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrappers comWrappers) : IClassFactory -{ - [return: MarshalUsing(typeof(HResultMarshaller))] - public unsafe global::Windows.Win32.Foundation.HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject) - { - if (pUnkOuter is not null) - { - return global::Windows.Win32.Foundation.HRESULT.CLASS_E_NOAGGREGATION; - } - - if (!riid->Equals(IUnknown.IID_Guid) && !riid->Equals(factory.Iid)) - { - return global::Windows.Win32.Foundation.HRESULT.E_NOINTERFACE; - } - - try - { - var instance = factory.CreateInstance(); - var unknown = comWrappers.GetOrCreateComInterfaceForObject(instance, CreateComInterfaceFlags.None); - - if (riid->Equals(IUnknown.IID_Guid)) - { - *ppvObject = (void*)unknown; - } - else - { - global::Windows.Win32.Foundation.HRESULT hr = (global::Windows.Win32.Foundation.HRESULT)Marshal.QueryInterface(unknown, ref *riid, out nint ppv); +using System; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com; +using Shmuelie.WinRTServer.Windows.Com.Marshalling; +using IUnknown = Windows.Win32.System.Com.IUnknown; +using static Windows.Win32.PInvoke; + +namespace Shmuelie.WinRTServer.Internal; + +[GeneratedComClass] +internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrappers comWrappers) : IClassFactory +{ + [return: MarshalUsing(typeof(HResultMarshaller))] + public unsafe global::Windows.Win32.Foundation.HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject) + { + if (pUnkOuter is not null) + { + return global::Windows.Win32.Foundation.HRESULT.CLASS_E_NOAGGREGATION; + } + + if (!riid->Equals(IUnknown.IID_Guid) && !riid->Equals(factory.Iid)) + { + return global::Windows.Win32.Foundation.HRESULT.E_NOINTERFACE; + } + + try + { + var instance = factory.CreateInstance(); + var unknown = comWrappers.GetOrCreateComInterfaceForObject(instance, CreateComInterfaceFlags.None); + + if (riid->Equals(IUnknown.IID_Guid)) + { + *ppvObject = (void*)unknown; + } + else + { + global::Windows.Win32.Foundation.HRESULT hr = (global::Windows.Win32.Foundation.HRESULT)Marshal.QueryInterface(unknown, ref *riid, out nint ppv); if (hr.Failed) { return hr; - } - *ppvObject = (void*)ppv; - } - - factory.OnInstanceCreated(instance); - } - catch (Exception e) - { - return (global::Windows.Win32.Foundation.HRESULT)Marshal.GetHRForException(e); - } - return global::Windows.Win32.Foundation.HRESULT.S_OK; - } - - [return: MarshalUsing(typeof(HResultMarshaller))] - public global::Windows.Win32.Foundation.HRESULT LockServer([MarshalUsing(typeof(BoolMarshaller))] global::Windows.Win32.Foundation.BOOL fLock) - { - try - { - if (fLock != 0) - { - _ = CoAddRefServerProcess(); - } - else - { - _ = CoReleaseServerProcess(); - } - } - catch (Exception e) - { - return (global::Windows.Win32.Foundation.HRESULT)Marshal.GetHRForException(e); - } - return global::Windows.Win32.Foundation.HRESULT.S_OK; - } -} + } + *ppvObject = (void*)ppv; + } + + factory.OnInstanceCreated(instance); + } + catch (Exception e) + { + return (global::Windows.Win32.Foundation.HRESULT)Marshal.GetHRForException(e); + } + return global::Windows.Win32.Foundation.HRESULT.S_OK; + } + + [return: MarshalUsing(typeof(HResultMarshaller))] + public global::Windows.Win32.Foundation.HRESULT LockServer([MarshalUsing(typeof(BoolMarshaller))] global::Windows.Win32.Foundation.BOOL fLock) + { + try + { + if (fLock != 0) + { + _ = CoAddRefServerProcess(); + } + else + { + _ = CoReleaseServerProcess(); + } + } + catch (Exception e) + { + return (global::Windows.Win32.Foundation.HRESULT)Marshal.GetHRForException(e); + } + return global::Windows.Win32.Foundation.HRESULT.S_OK; + } +} From a0265660d6ac0829cae96641c96fd0c3517f9414 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 21:24:05 -0700 Subject: [PATCH 20/43] More attribute cleaning --- .../Internal/BaseActivationFactoryWrapper.cs | 9 ++------- .../Internal/BaseClassFactoryWrapper.cs | 5 +---- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs index e372779..763da81 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs @@ -2,7 +2,6 @@ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; using Shmuelie.WinRTServer.Windows.Com; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; using Windows.Win32.Foundation; using IInspectable = Windows.Win32.System.WinRT.IInspectable; using static Windows.Win32.PInvoke; @@ -14,7 +13,6 @@ internal partial class BaseActivationFactoryWrapper(BaseActivationFactory factor { private readonly StrategyBasedComWrappers comWrappers = new(); - [return: MarshalUsing(typeof(HResultMarshaller))] public unsafe HRESULT ActivateInstance(void** instance) { if (instance is null) @@ -41,7 +39,6 @@ public unsafe HRESULT ActivateInstance(void** instance) return HRESULT.S_OK; } - [return: MarshalUsing(typeof(HResultMarshaller))] public unsafe HRESULT GetIids(uint* iidCount, Guid** iids) { if (iidCount is null || iids is null) @@ -55,8 +52,7 @@ public unsafe HRESULT GetIids(uint* iidCount, Guid** iids) return HRESULT.S_OK; } - [return: MarshalUsing(typeof(HResultMarshaller))] - public unsafe HRESULT GetRuntimeClassName([MarshalUsing(typeof(HStringMarshaller))] global::Windows.Win32.System.WinRT.HSTRING* className) + public unsafe HRESULT GetRuntimeClassName(global::Windows.Win32.System.WinRT.HSTRING* className) { if (className is null) { @@ -83,8 +79,7 @@ public unsafe HRESULT GetRuntimeClassName([MarshalUsing(typeof(HStringMarshaller } } - [return: MarshalUsing(typeof(HResultMarshaller))] - public unsafe HRESULT GetTrustLevel([MarshalUsing(typeof(TrustLevelMarshaller))] global::Windows.Win32.System.WinRT.TrustLevel* trustLevel) + public unsafe HRESULT GetTrustLevel(global::Windows.Win32.System.WinRT.TrustLevel* trustLevel) { if (trustLevel is null) { diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index 8dee7f4..70c38f6 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -2,7 +2,6 @@ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; using Shmuelie.WinRTServer.Windows.Com; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; using IUnknown = Windows.Win32.System.Com.IUnknown; using static Windows.Win32.PInvoke; @@ -11,7 +10,6 @@ namespace Shmuelie.WinRTServer.Internal; [GeneratedComClass] internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrappers comWrappers) : IClassFactory { - [return: MarshalUsing(typeof(HResultMarshaller))] public unsafe global::Windows.Win32.Foundation.HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject) { if (pUnkOuter is not null) @@ -52,8 +50,7 @@ internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrap return global::Windows.Win32.Foundation.HRESULT.S_OK; } - [return: MarshalUsing(typeof(HResultMarshaller))] - public global::Windows.Win32.Foundation.HRESULT LockServer([MarshalUsing(typeof(BoolMarshaller))] global::Windows.Win32.Foundation.BOOL fLock) + public global::Windows.Win32.Foundation.HRESULT LockServer(global::Windows.Win32.Foundation.BOOL fLock) { try { From d283accbf588ecf1f3eb7a7a2cf46142f07afa07 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 21:25:58 -0700 Subject: [PATCH 21/43] Simplify namespaces --- .../Internal/BaseClassFactoryWrapper.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index 70c38f6..35491fb 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -4,22 +4,23 @@ using Shmuelie.WinRTServer.Windows.Com; using IUnknown = Windows.Win32.System.Com.IUnknown; using static Windows.Win32.PInvoke; +using Windows.Win32.Foundation; namespace Shmuelie.WinRTServer.Internal; [GeneratedComClass] internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrappers comWrappers) : IClassFactory { - public unsafe global::Windows.Win32.Foundation.HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject) + public unsafe HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject) { if (pUnkOuter is not null) { - return global::Windows.Win32.Foundation.HRESULT.CLASS_E_NOAGGREGATION; + return HRESULT.CLASS_E_NOAGGREGATION; } if (!riid->Equals(IUnknown.IID_Guid) && !riid->Equals(factory.Iid)) { - return global::Windows.Win32.Foundation.HRESULT.E_NOINTERFACE; + return HRESULT.E_NOINTERFACE; } try @@ -33,7 +34,7 @@ internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrap } else { - global::Windows.Win32.Foundation.HRESULT hr = (global::Windows.Win32.Foundation.HRESULT)Marshal.QueryInterface(unknown, ref *riid, out nint ppv); + HRESULT hr = (HRESULT)Marshal.QueryInterface(unknown, ref *riid, out nint ppv); if (hr.Failed) { return hr; @@ -45,12 +46,12 @@ internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrap } catch (Exception e) { - return (global::Windows.Win32.Foundation.HRESULT)Marshal.GetHRForException(e); + return (HRESULT)Marshal.GetHRForException(e); } - return global::Windows.Win32.Foundation.HRESULT.S_OK; + return HRESULT.S_OK; } - public global::Windows.Win32.Foundation.HRESULT LockServer(global::Windows.Win32.Foundation.BOOL fLock) + public HRESULT LockServer(BOOL fLock) { try { @@ -65,8 +66,8 @@ internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrap } catch (Exception e) { - return (global::Windows.Win32.Foundation.HRESULT)Marshal.GetHRForException(e); + return (HRESULT)Marshal.GetHRForException(e); } - return global::Windows.Win32.Foundation.HRESULT.S_OK; + return HRESULT.S_OK; } } From 52a09d4b63ddd0ad57ca6ca270bb7ce7e1fa3197 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 21:27:06 -0700 Subject: [PATCH 22/43] Sorting --- .../Internal/BaseActivationFactoryWrapper.cs | 2 +- src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs index 763da81..5479838 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs @@ -3,8 +3,8 @@ using System.Runtime.InteropServices.Marshalling; using Shmuelie.WinRTServer.Windows.Com; using Windows.Win32.Foundation; -using IInspectable = Windows.Win32.System.WinRT.IInspectable; using static Windows.Win32.PInvoke; +using IInspectable = Windows.Win32.System.WinRT.IInspectable; namespace Shmuelie.WinRTServer.Internal; diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index 35491fb..fbc65bb 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -2,9 +2,9 @@ using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; using Shmuelie.WinRTServer.Windows.Com; -using IUnknown = Windows.Win32.System.Com.IUnknown; -using static Windows.Win32.PInvoke; using Windows.Win32.Foundation; +using static Windows.Win32.PInvoke; +using IUnknown = Windows.Win32.System.Com.IUnknown; namespace Shmuelie.WinRTServer.Internal; From 330ff9048f37b9a81a9e0ae69d6b742747c4bf69 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 21:30:17 -0700 Subject: [PATCH 23/43] Update release notes --- src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj index bdc975f..ef73fc9 100644 --- a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj +++ b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj @@ -2,7 +2,7 @@ net8.0-windows10.0.22000.0 true - Added support for modern .NET servers. + Remove support for .NET Standard 2.0 and made package AOT compatible. True True win-x64;win-x86;win-arm64;$(RuntimeIdentifiers) From a7178d5a11913e7d111b8c21f63ab6a79df203ff Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 21:41:01 -0700 Subject: [PATCH 24/43] Release unknown after QI --- src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index fbc65bb..963764f 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -35,6 +35,7 @@ public unsafe HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObje else { HRESULT hr = (HRESULT)Marshal.QueryInterface(unknown, ref *riid, out nint ppv); + Marshal.Release(unknown); if (hr.Failed) { return hr; From 19874ec806666f17303c8995a39069548a73807f Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Wed, 28 Aug 2024 22:02:34 -0700 Subject: [PATCH 25/43] Change to use ComPtr --- .../Internal/BaseClassFactoryWrapper.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index 963764f..ad7eea6 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -1,6 +1,7 @@ using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows; using Shmuelie.WinRTServer.Windows.Com; using Windows.Win32.Foundation; using static Windows.Win32.PInvoke; @@ -26,21 +27,20 @@ public unsafe HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObje try { var instance = factory.CreateInstance(); - var unknown = comWrappers.GetOrCreateComInterfaceForObject(instance, CreateComInterfaceFlags.None); + using ComPtr unknown = default; + unknown.Attach((IUnknown*)comWrappers.GetOrCreateComInterfaceForObject(instance, CreateComInterfaceFlags.None)); if (riid->Equals(IUnknown.IID_Guid)) { - *ppvObject = (void*)unknown; + unknown.CopyTo((IUnknown**)ppvObject); } else { - HRESULT hr = (HRESULT)Marshal.QueryInterface(unknown, ref *riid, out nint ppv); - Marshal.Release(unknown); + var hr = (HRESULT)unknown.CopyTo(riid, ppvObject); if (hr.Failed) { return hr; } - *ppvObject = (void*)ppv; } factory.OnInstanceCreated(instance); From e9a2463460c243413412bbaa1fa11f1d763b5bee Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Thu, 29 Aug 2024 08:27:31 -0700 Subject: [PATCH 26/43] Moved internal Windows stuff under the internal namespace --- src/Shmuelie.WinRTServer/ComServer.cs | 2 +- .../Internal/BaseClassFactoryWrapper.cs | 2 +- src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/BOOL.cs | 2 +- .../{ => Internal}/Windows/Com/HRESULT.cs | 2 +- .../{ => Internal}/Windows/Com/HSTRING.cs | 2 +- .../{ => Internal}/Windows/Com/IActivationFactory.cs | 0 .../{ => Internal}/Windows/Com/IClassFactory.cs | 0 .../{ => Internal}/Windows/Com/IInspectable.cs | 0 .../Windows/Com/Marshalling/BoolMarshaller.cs | 2 +- .../Windows/Com/Marshalling/HResultMarshaller.cs | 2 +- .../Windows/Com/Marshalling/HStringMarshaller.cs | 6 +++--- .../Windows/Com/Marshalling/TrustLevelMarshaller.cs | 2 +- src/Shmuelie.WinRTServer/{ => Internal}/Windows/ComPtr.cs | 2 +- .../{ => Internal}/Windows/DllGetActivationFactory.cs | 2 +- src/Shmuelie.WinRTServer/{ => Internal}/Windows/HSTRING.cs | 2 +- .../{ => Internal}/Windows/Win32/PInvoke.cs | 0 src/Shmuelie.WinRTServer/WinRtServer.cs | 2 +- 17 files changed, 15 insertions(+), 15 deletions(-) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/BOOL.cs (71%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/HRESULT.cs (72%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/HSTRING.cs (72%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/IActivationFactory.cs (100%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/IClassFactory.cs (100%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/IInspectable.cs (100%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/Marshalling/BoolMarshaller.cs (90%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/Marshalling/HResultMarshaller.cs (91%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/Marshalling/HStringMarshaller.cs (70%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Com/Marshalling/TrustLevelMarshaller.cs (91%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/ComPtr.cs (99%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/DllGetActivationFactory.cs (97%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/HSTRING.cs (91%) rename src/Shmuelie.WinRTServer/{ => Internal}/Windows/Win32/PInvoke.cs (100%) diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index f1b7890..61f8efd 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using System.Timers; using Shmuelie.WinRTServer.Internal; -using Shmuelie.WinRTServer.Windows; +using Shmuelie.WinRTServer.Internal.Windows; using Windows.Win32.Foundation; using Windows.Win32.System.Com; using static Windows.Win32.PInvoke; diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index ad7eea6..58d37e1 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Windows; +using Shmuelie.WinRTServer.Internal.Windows; using Shmuelie.WinRTServer.Windows.Com; using Windows.Win32.Foundation; using static Windows.Win32.PInvoke; diff --git a/src/Shmuelie.WinRTServer/Windows/Com/BOOL.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/BOOL.cs similarity index 71% rename from src/Shmuelie.WinRTServer/Windows/Com/BOOL.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/BOOL.cs index 7c6564f..be064e3 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/BOOL.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/Com/BOOL.cs @@ -1,5 +1,5 @@ using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; +using Shmuelie.WinRTServer.Internal.Windows.Com.Marshalling; namespace Windows.Win32.Foundation; diff --git a/src/Shmuelie.WinRTServer/Windows/Com/HRESULT.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/HRESULT.cs similarity index 72% rename from src/Shmuelie.WinRTServer/Windows/Com/HRESULT.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/HRESULT.cs index 18567f9..92bf708 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/HRESULT.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/Com/HRESULT.cs @@ -1,5 +1,5 @@ using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; +using Shmuelie.WinRTServer.Internal.Windows.Com.Marshalling; namespace Windows.Win32.Foundation; diff --git a/src/Shmuelie.WinRTServer/Windows/Com/HSTRING.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/HSTRING.cs similarity index 72% rename from src/Shmuelie.WinRTServer/Windows/Com/HSTRING.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/HSTRING.cs index ade023d..6357746 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/HSTRING.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/Com/HSTRING.cs @@ -1,5 +1,5 @@ using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Windows.Com.Marshalling; +using Shmuelie.WinRTServer.Internal.Windows.Com.Marshalling; namespace Windows.Win32.System.WinRT; diff --git a/src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/IActivationFactory.cs similarity index 100% rename from src/Shmuelie.WinRTServer/Windows/Com/IActivationFactory.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/IActivationFactory.cs diff --git a/src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/IClassFactory.cs similarity index 100% rename from src/Shmuelie.WinRTServer/Windows/Com/IClassFactory.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/IClassFactory.cs diff --git a/src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/IInspectable.cs similarity index 100% rename from src/Shmuelie.WinRTServer/Windows/Com/IInspectable.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/IInspectable.cs diff --git a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/BoolMarshaller.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/BoolMarshaller.cs similarity index 90% rename from src/Shmuelie.WinRTServer/Windows/Com/Marshalling/BoolMarshaller.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/BoolMarshaller.cs index 2fff0d5..6253922 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/BoolMarshaller.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/BoolMarshaller.cs @@ -1,7 +1,7 @@ using System.Runtime.InteropServices.Marshalling; using Windows.Win32.Foundation; -namespace Shmuelie.WinRTServer.Windows.Com.Marshalling; +namespace Shmuelie.WinRTServer.Internal.Windows.Com.Marshalling; [CustomMarshaller(typeof(BOOL), MarshalMode.UnmanagedToManagedOut, typeof(BoolMarshaller))] [CustomMarshaller(typeof(BOOL), MarshalMode.UnmanagedToManagedIn, typeof(BoolMarshaller))] diff --git a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HResultMarshaller.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/HResultMarshaller.cs similarity index 91% rename from src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HResultMarshaller.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/HResultMarshaller.cs index 5086d32..6c69717 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HResultMarshaller.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/HResultMarshaller.cs @@ -1,7 +1,7 @@ using System.Runtime.InteropServices.Marshalling; using Windows.Win32.Foundation; -namespace Shmuelie.WinRTServer.Windows.Com.Marshalling; +namespace Shmuelie.WinRTServer.Internal.Windows.Com.Marshalling; [CustomMarshaller(typeof(HRESULT), MarshalMode.UnmanagedToManagedOut, typeof(HResultMarshaller))] [CustomMarshaller(typeof(HRESULT), MarshalMode.UnmanagedToManagedIn, typeof(HResultMarshaller))] diff --git a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HStringMarshaller.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/HStringMarshaller.cs similarity index 70% rename from src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HStringMarshaller.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/HStringMarshaller.cs index f6d0efb..3657ea1 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/HStringMarshaller.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/HStringMarshaller.cs @@ -2,7 +2,7 @@ using System.Runtime.InteropServices.Marshalling; using Windows.Win32.System.WinRT; -namespace Shmuelie.WinRTServer.Windows.Com.Marshalling; +namespace Shmuelie.WinRTServer.Internal.Windows.Com.Marshalling; [CustomMarshaller(typeof(HSTRING), MarshalMode.UnmanagedToManagedOut, typeof(HStringMarshaller))] [CustomMarshaller(typeof(HSTRING), MarshalMode.UnmanagedToManagedIn, typeof(HStringMarshaller))] @@ -10,7 +10,7 @@ namespace Shmuelie.WinRTServer.Windows.Com.Marshalling; [CustomMarshaller(typeof(HSTRING), MarshalMode.ManagedToUnmanagedIn, typeof(HStringMarshaller))] internal static class HStringMarshaller { - public static HSTRING ConvertToManaged(IntPtr nativeValue) => new HSTRING(nativeValue); + public static HSTRING ConvertToManaged(nint nativeValue) => new HSTRING(nativeValue); - public static IntPtr ConvertToUnmanaged(HSTRING value) => value.Value; + public static nint ConvertToUnmanaged(HSTRING value) => value.Value; } diff --git a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/TrustLevelMarshaller.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/TrustLevelMarshaller.cs similarity index 91% rename from src/Shmuelie.WinRTServer/Windows/Com/Marshalling/TrustLevelMarshaller.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/TrustLevelMarshaller.cs index ccc2462..85fa22e 100644 --- a/src/Shmuelie.WinRTServer/Windows/Com/Marshalling/TrustLevelMarshaller.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/Com/Marshalling/TrustLevelMarshaller.cs @@ -1,7 +1,7 @@ using System.Runtime.InteropServices.Marshalling; using Windows.Win32.System.WinRT; -namespace Shmuelie.WinRTServer.Windows.Com.Marshalling; +namespace Shmuelie.WinRTServer.Internal.Windows.Com.Marshalling; [CustomMarshaller(typeof(TrustLevel), MarshalMode.UnmanagedToManagedOut, typeof(TrustLevelMarshaller))] [CustomMarshaller(typeof(TrustLevel), MarshalMode.UnmanagedToManagedIn, typeof(TrustLevelMarshaller))] diff --git a/src/Shmuelie.WinRTServer/Windows/ComPtr.cs b/src/Shmuelie.WinRTServer/Internal/Windows/ComPtr.cs similarity index 99% rename from src/Shmuelie.WinRTServer/Windows/ComPtr.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/ComPtr.cs index 1816b41..69dc609 100644 --- a/src/Shmuelie.WinRTServer/Windows/ComPtr.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/ComPtr.cs @@ -4,7 +4,7 @@ using Windows.Win32.Foundation; using Windows.Win32.System.Com; -namespace Shmuelie.WinRTServer.Windows; +namespace Shmuelie.WinRTServer.Internal.Windows; /// /// A type that allows working with pointers to COM objects more securely. diff --git a/src/Shmuelie.WinRTServer/Windows/DllGetActivationFactory.cs b/src/Shmuelie.WinRTServer/Internal/Windows/DllGetActivationFactory.cs similarity index 97% rename from src/Shmuelie.WinRTServer/Windows/DllGetActivationFactory.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/DllGetActivationFactory.cs index ce11281..334a808 100644 --- a/src/Shmuelie.WinRTServer/Windows/DllGetActivationFactory.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/DllGetActivationFactory.cs @@ -2,7 +2,7 @@ using Windows.Win32.Foundation; using Windows.Win32.System.WinRT; -namespace Shmuelie.WinRTServer.Windows; +namespace Shmuelie.WinRTServer.Internal.Windows; /// /// Retrieves the activation factory from a DLL that contains activatable Windows Runtime classes. diff --git a/src/Shmuelie.WinRTServer/Windows/HSTRING.cs b/src/Shmuelie.WinRTServer/Internal/Windows/HSTRING.cs similarity index 91% rename from src/Shmuelie.WinRTServer/Windows/HSTRING.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/HSTRING.cs index e99dfc0..a1ff66c 100644 --- a/src/Shmuelie.WinRTServer/Windows/HSTRING.cs +++ b/src/Shmuelie.WinRTServer/Internal/Windows/HSTRING.cs @@ -3,7 +3,7 @@ using Windows.Win32.System.WinRT; using static Windows.Win32.PInvoke; -namespace Shmuelie.WinRTServer.Windows; +namespace Shmuelie.WinRTServer.Internal.Windows; internal static class HSTRING_ { diff --git a/src/Shmuelie.WinRTServer/Windows/Win32/PInvoke.cs b/src/Shmuelie.WinRTServer/Internal/Windows/Win32/PInvoke.cs similarity index 100% rename from src/Shmuelie.WinRTServer/Windows/Win32/PInvoke.cs rename to src/Shmuelie.WinRTServer/Internal/Windows/Win32/PInvoke.cs diff --git a/src/Shmuelie.WinRTServer/WinRtServer.cs b/src/Shmuelie.WinRTServer/WinRtServer.cs index c8e531f..b4a4d17 100644 --- a/src/Shmuelie.WinRTServer/WinRtServer.cs +++ b/src/Shmuelie.WinRTServer/WinRtServer.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; using System.Timers; using Shmuelie.WinRTServer.Internal; -using Shmuelie.WinRTServer.Windows; +using Shmuelie.WinRTServer.Internal.Windows; using Windows.Win32.Foundation; using Windows.Win32.System.Com; using Windows.Win32.System.WinRT; From 037212b2daa9c79cb588ef1cbed43bbc65302818 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Thu, 29 Aug 2024 08:39:39 -0700 Subject: [PATCH 27/43] Fix type namespaces --- tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs | 2 +- tests/Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs | 2 +- .../Shmuelie.WinRTServer.Sample.ServerNet6.csproj | 2 +- tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs index 2558eb3..abd2119 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Input.cs @@ -2,7 +2,7 @@ using System.Runtime.InteropServices; using Shmuelie.WinRTServer.Sample.Interfaces; -namespace Shmuelie.WinRTServer.Sample.Server; +namespace Shmuelie.WinRTServer.Sample; [Guid("2F8EDC40-C145-4128-B451-559D45CD9074")] public sealed partial class Input : IInput diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs index 7728659..115e10e 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/RemoteThing.cs @@ -10,7 +10,7 @@ using Windows.Foundation; using Windows.Storage.Streams; -namespace Shmuelie.WinRTServer.Sample.Server; +namespace Shmuelie.WinRTServer.Sample; #pragma warning disable CA1822 // Mark members as static diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj index beb8a1e..2007c6a 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Shmuelie.WinRTServer.Sample.ServerNet6.csproj @@ -12,7 +12,7 @@ full True True - Shmuelie.WinRTServer.Sample.Server + Shmuelie.WinRTServer.Sample 10.0.22000.41 diff --git a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs index f799723..0a10c29 100644 --- a/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs +++ b/tests/Shmuelie.WinRTServer.Sample.ServerNet6/Times.cs @@ -2,7 +2,7 @@ using System.Runtime.InteropServices; using Shmuelie.WinRTServer.Sample.Interfaces; -namespace Shmuelie.WinRTServer.Sample.Server; +namespace Shmuelie.WinRTServer.Sample; [Guid("EC6A0FF9-BBF4-48EA-9BFA-DFAF84D4FAF8")] public sealed partial class Times : ITimes From 9669f42e954bfc312c9ed08161d6bd4787feebd8 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Thu, 29 Aug 2024 09:18:15 -0700 Subject: [PATCH 28/43] Update WinRtServer to support other ComWrappers --- .../CsWinRT/WinRtServerExtensions.cs | 72 ++++++++++++++++++ .../Internal/BaseActivationFactoryWrapper.cs | 26 +++++-- .../StrategyBased/WinRtServerExtensions.cs | 73 +++++++++++++++++++ src/Shmuelie.WinRTServer/WinRtServer.cs | 11 +-- .../WinRtServerExtensions.cs | 16 ++-- 5 files changed, 179 insertions(+), 19 deletions(-) create mode 100644 src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs create mode 100644 src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs diff --git a/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs new file mode 100644 index 0000000..3f91240 --- /dev/null +++ b/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs @@ -0,0 +1,72 @@ +using System; +using System.Runtime.Versioning; +using WinRT; + +namespace Shmuelie.WinRTServer.CsWinRT; + +/// +/// Extensions for when using . +/// +[SupportedOSPlatform("windows8.0")] +public static class WinRtServerExtensions +{ + private static readonly DefaultComWrappers comWrappers = new(); + + /// + /// Register a type with the server. + /// + /// The type register. + /// The instance. + /// if type was registered; otherwise, . + /// Type can only be registered once. + /// is . + /// The server is running. + public static bool RegisterClass(this WinRtServer server) where T : class, new() + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterActivationFactory(new GeneralActivationFactory(), comWrappers); + } + + /// + /// Register a type with the server. + /// + /// The type register. + /// The instance. + /// Method to create instance of . + /// if type was registered; otherwise, . + /// Type can only be registered once. + /// is . + /// The server is running. + public static bool RegisterClass(this WinRtServer server, Func factory) where T : class + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterActivationFactory(new DelegateActivationFactory(factory), comWrappers); + } + + /// + /// Register an activation factory with the server. + /// + /// The type of the factory to register. + /// The instance. + /// if an instance of was registered; otherwise, . + /// Only one factory can be registered for a Activatable Class ID. + /// is . + /// The server is running. + public static bool RegisterActivationFactory(this WinRtServer server) where T : BaseActivationFactory, new() + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterActivationFactory(new T(), comWrappers); + } +} diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs index 5479838..5c03844 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs @@ -9,10 +9,8 @@ namespace Shmuelie.WinRTServer.Internal; [GeneratedComClass] -internal partial class BaseActivationFactoryWrapper(BaseActivationFactory factory) : IActivationFactory +internal partial class BaseActivationFactoryWrapper(BaseActivationFactory factory, ComWrappers comWrappers) : IActivationFactory { - private readonly StrategyBasedComWrappers comWrappers = new(); - public unsafe HRESULT ActivateInstance(void** instance) { if (instance is null) @@ -20,15 +18,20 @@ public unsafe HRESULT ActivateInstance(void** instance) return HRESULT.E_INVALIDARG; } + bool shouldReleaseUnknown = false; + nint unknown = 0; try { object managedInstance = factory.ActivateInstance(); - var unknown = comWrappers.GetOrCreateComInterfaceForObject(managedInstance, CreateComInterfaceFlags.None); - var hr = (HRESULT)StrategyBasedComWrappers.DefaultIUnknownStrategy.QueryInterface((void*)unknown, IInspectable.IID_Guid, out *instance); - if (hr.Failed) - { - return hr; + unknown = comWrappers.GetOrCreateComInterfaceForObject(managedInstance, CreateComInterfaceFlags.None); + var riid = IInspectable.IID_Guid; + var hr = (HRESULT)Marshal.QueryInterface(unknown, ref riid, out nint ppv); + shouldReleaseUnknown = true; + if (hr.Failed) + { + return hr; } + *instance = (void*)ppv; factory.OnInstanceCreated(managedInstance); } @@ -36,6 +39,13 @@ public unsafe HRESULT ActivateInstance(void** instance) { return (HRESULT)Marshal.GetHRForException(e); } + finally + { + if (shouldReleaseUnknown) + { + Marshal.Release(unknown); + } + } return HRESULT.S_OK; } diff --git a/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs new file mode 100644 index 0000000..af60cd0 --- /dev/null +++ b/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs @@ -0,0 +1,73 @@ +using System; +using System.Runtime.InteropServices.Marshalling; +using System.Runtime.Versioning; +using Shmuelie.WinRTServer.CsWinRT; + +namespace Shmuelie.WinRTServer.StrategyBased; + +/// +/// Extensions for when using . +/// +[SupportedOSPlatform("windows8.0")] +public static class WinRtServerExtensions +{ + private static readonly StrategyBasedComWrappers comWrappers = new(); + + /// + /// Register a type with the server. + /// + /// The type register. + /// The instance. + /// if type was registered; otherwise, . + /// Type can only be registered once. + /// is . + /// The server is running. + public static bool RegisterClass(this WinRtServer server) where T : class, new() + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterActivationFactory(new GeneralActivationFactory(), comWrappers); + } + + /// + /// Register a type with the server. + /// + /// The type register. + /// The instance. + /// Method to create instance of . + /// if type was registered; otherwise, . + /// Type can only be registered once. + /// is . + /// The server is running. + public static bool RegisterClass(this WinRtServer server, Func factory) where T : class + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterActivationFactory(new DelegateActivationFactory(factory), comWrappers); + } + + /// + /// Register an activation factory with the server. + /// + /// The type of the factory to register. + /// The instance. + /// if an instance of was registered; otherwise, . + /// Only one factory can be registered for a Activatable Class ID. + /// is . + /// The server is running. + public static bool RegisterActivationFactory(this WinRtServer server) where T : BaseActivationFactory, new() + { + if (server is null) + { + throw new ArgumentNullException(nameof(server)); + } + + return server.RegisterActivationFactory(new T(), comWrappers); + } +} diff --git a/src/Shmuelie.WinRTServer/WinRtServer.cs b/src/Shmuelie.WinRTServer/WinRtServer.cs index b4a4d17..9cbd44d 100644 --- a/src/Shmuelie.WinRTServer/WinRtServer.cs +++ b/src/Shmuelie.WinRTServer/WinRtServer.cs @@ -23,7 +23,7 @@ namespace Shmuelie.WinRTServer; [SupportedOSPlatform("windows8.0")] public sealed class WinRtServer : IAsyncDisposable { - private readonly Dictionary factories = []; + private readonly Dictionary factories = []; private readonly unsafe DllGetActivationFactory activationFactoryCallbackWrapper; @@ -125,11 +125,12 @@ private void Factory_InstanceCreated(object? sender, InstanceCreatedEventArgs e) /// Register an activation factory with the server. /// /// The activation factory to register. + /// The implementation of to use for wrapping. /// if was registered; otherwise, . /// Only one factory can be registered for a Activatable Class ID. /// is . /// The server is running. - public bool RegisterActivationFactory(BaseActivationFactory factory) + public bool RegisterActivationFactory(BaseActivationFactory factory, ComWrappers comWrappers) { if (IsDisposed) { @@ -149,7 +150,7 @@ public bool RegisterActivationFactory(BaseActivationFactory factory) return false; } - factories.Add(factory.ActivatableClassId, factory); + factories.Add(factory.ActivatableClassId, (factory, comWrappers)); return true; } @@ -185,13 +186,13 @@ private unsafe HRESULT ActivationFactoryCallback(HSTRING activatableClassId, IAc return HRESULT.E_INVALIDARG; } - if (!factories.TryGetValue(activatableClassId.AsString(), out BaseActivationFactory? managedFactory)) + if (!factories.TryGetValue(activatableClassId.AsString(), out var managedFactory)) { factory = null; return HRESULT.E_NOINTERFACE; } - *factory = (IActivationFactory*)comWrappers.GetOrCreateComInterfaceForObject(new BaseActivationFactoryWrapper(managedFactory), CreateComInterfaceFlags.None); + *factory = (IActivationFactory*)comWrappers.GetOrCreateComInterfaceForObject(new BaseActivationFactoryWrapper(managedFactory.Factory, managedFactory.Wrapper), CreateComInterfaceFlags.None); return HRESULT.S_OK; } diff --git a/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs index 059a644..33c09ac 100644 --- a/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; using System.Runtime.Versioning; namespace Shmuelie.WinRTServer; @@ -14,18 +15,19 @@ public static class WinRtServerExtensions /// /// The type register. /// The instance. + /// The implementation of to use for wrapping. /// if type was registered; otherwise, . /// Type can only be registered once. /// is . /// The server is running. - public static bool RegisterClass(this WinRtServer server) where T : class, new() + public static bool RegisterClass(this WinRtServer server, ComWrappers comWrappers) where T : class, new() { if (server is null) { throw new ArgumentNullException(nameof(server)); } - return server.RegisterActivationFactory(new GeneralActivationFactory()); + return server.RegisterActivationFactory(new GeneralActivationFactory(), comWrappers); } /// @@ -34,18 +36,19 @@ public static class WinRtServerExtensions /// The type register. /// The instance. /// Method to create instance of . + /// The implementation of to use for wrapping. /// if type was registered; otherwise, . /// Type can only be registered once. /// is . /// The server is running. - public static bool RegisterClass(this WinRtServer server, Func factory) where T : class + public static bool RegisterClass(this WinRtServer server, Func factory, ComWrappers comWrappers) where T : class { if (server is null) { throw new ArgumentNullException(nameof(server)); } - return server.RegisterActivationFactory(new DelegateActivationFactory(factory)); + return server.RegisterActivationFactory(new DelegateActivationFactory(factory), comWrappers); } /// @@ -53,17 +56,18 @@ public static bool RegisterClass(this WinRtServer server, Func factory) wh /// /// The type of the factory to register. /// The instance. + /// The implementation of to use for wrapping. /// if an instance of was registered; otherwise, . /// Only one factory can be registered for a Activatable Class ID. /// is . /// The server is running. - public static bool RegisterActivationFactory(this WinRtServer server) where T : BaseActivationFactory, new() + public static bool RegisterActivationFactory(this WinRtServer server, ComWrappers comWrappers) where T : BaseActivationFactory, new() { if (server is null) { throw new ArgumentNullException(nameof(server)); } - return server.RegisterActivationFactory(new T()); + return server.RegisterActivationFactory(new T(), comWrappers); } } From 6354f067ad2271a9f8825e9a8ac9e45161981900 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Thu, 29 Aug 2024 09:18:26 -0700 Subject: [PATCH 29/43] Revert using ComPtr --- .../Internal/BaseClassFactoryWrapper.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index 58d37e1..cc0bb4f 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -1,7 +1,6 @@ using System; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Internal.Windows; using Shmuelie.WinRTServer.Windows.Com; using Windows.Win32.Foundation; using static Windows.Win32.PInvoke; @@ -24,23 +23,26 @@ public unsafe HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObje return HRESULT.E_NOINTERFACE; } + bool shouldReleaseUnknown = false; + nint unknown = 0; try { var instance = factory.CreateInstance(); - using ComPtr unknown = default; - unknown.Attach((IUnknown*)comWrappers.GetOrCreateComInterfaceForObject(instance, CreateComInterfaceFlags.None)); + unknown = comWrappers.GetOrCreateComInterfaceForObject(instance, CreateComInterfaceFlags.None); if (riid->Equals(IUnknown.IID_Guid)) { - unknown.CopyTo((IUnknown**)ppvObject); + *ppvObject = (void*)unknown; } else { - var hr = (HRESULT)unknown.CopyTo(riid, ppvObject); + var hr = (HRESULT)Marshal.QueryInterface(unknown, ref *riid, out nint ppv); + shouldReleaseUnknown = true; if (hr.Failed) { return hr; } + *ppvObject = (void*)ppv; } factory.OnInstanceCreated(instance); @@ -49,6 +51,13 @@ public unsafe HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObje { return (HRESULT)Marshal.GetHRForException(e); } + finally + { + if (shouldReleaseUnknown) + { + Marshal.Release(unknown); + } + } return HRESULT.S_OK; } From 43c86af9d4fcab54b74e123c236ffe08fcf63d3d Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Thu, 29 Aug 2024 13:51:38 -0700 Subject: [PATCH 30/43] Fix whitespace --- .../Internal/BaseActivationFactoryWrapper.cs | 180 +++++++++--------- 1 file changed, 90 insertions(+), 90 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs index 5c03844..da84c60 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs @@ -1,102 +1,102 @@ -using System; -using System.Runtime.InteropServices; -using System.Runtime.InteropServices.Marshalling; -using Shmuelie.WinRTServer.Windows.Com; -using Windows.Win32.Foundation; -using static Windows.Win32.PInvoke; -using IInspectable = Windows.Win32.System.WinRT.IInspectable; - -namespace Shmuelie.WinRTServer.Internal; - -[GeneratedComClass] -internal partial class BaseActivationFactoryWrapper(BaseActivationFactory factory, ComWrappers comWrappers) : IActivationFactory -{ - public unsafe HRESULT ActivateInstance(void** instance) - { - if (instance is null) - { - return HRESULT.E_INVALIDARG; - } - +using System; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.Marshalling; +using Shmuelie.WinRTServer.Windows.Com; +using Windows.Win32.Foundation; +using static Windows.Win32.PInvoke; +using IInspectable = Windows.Win32.System.WinRT.IInspectable; + +namespace Shmuelie.WinRTServer.Internal; + +[GeneratedComClass] +internal partial class BaseActivationFactoryWrapper(BaseActivationFactory factory, ComWrappers comWrappers) : IActivationFactory +{ + public unsafe HRESULT ActivateInstance(void** instance) + { + if (instance is null) + { + return HRESULT.E_INVALIDARG; + } + bool shouldReleaseUnknown = false; - nint unknown = 0; - try - { - object managedInstance = factory.ActivateInstance(); - unknown = comWrappers.GetOrCreateComInterfaceForObject(managedInstance, CreateComInterfaceFlags.None); - var riid = IInspectable.IID_Guid; + nint unknown = 0; + try + { + object managedInstance = factory.ActivateInstance(); + unknown = comWrappers.GetOrCreateComInterfaceForObject(managedInstance, CreateComInterfaceFlags.None); + var riid = IInspectable.IID_Guid; var hr = (HRESULT)Marshal.QueryInterface(unknown, ref riid, out nint ppv); shouldReleaseUnknown = true; if (hr.Failed) { return hr; - } - *instance = (void*)ppv; - - factory.OnInstanceCreated(managedInstance); - } - catch (Exception e) - { - return (HRESULT)Marshal.GetHRForException(e); - } + } + *instance = (void*)ppv; + + factory.OnInstanceCreated(managedInstance); + } + catch (Exception e) + { + return (HRESULT)Marshal.GetHRForException(e); + } finally { if (shouldReleaseUnknown) { Marshal.Release(unknown); } - } - return HRESULT.S_OK; - } - - public unsafe HRESULT GetIids(uint* iidCount, Guid** iids) - { - if (iidCount is null || iids is null) - { - return HRESULT.E_INVALIDARG; - } - - *iidCount = 1; - *iids = (Guid*)Marshal.AllocHGlobal(sizeof(Guid)); - *iids[0] = global::Windows.Win32.System.WinRT.IActivationFactory.IID_Guid; - return HRESULT.S_OK; - } - - public unsafe HRESULT GetRuntimeClassName(global::Windows.Win32.System.WinRT.HSTRING* className) - { - if (className is null) - { - return HRESULT.E_INVALIDARG; - } - - try - { - string? fullName = factory.GetType().FullName; - - if (fullName is null) - { - return HRESULT.E_UNEXPECTED; - } - - fixed (char* fullNamePtr = fullName) - { - return WindowsCreateString((PCWSTR)fullNamePtr, (uint)fullName.Length, className); - } - } - catch (Exception e) - { - return (HRESULT)Marshal.GetHRForException(e); - } - } - - public unsafe HRESULT GetTrustLevel(global::Windows.Win32.System.WinRT.TrustLevel* trustLevel) - { - if (trustLevel is null) - { - return HRESULT.E_INVALIDARG; - } - - *trustLevel = global::Windows.Win32.System.WinRT.TrustLevel.BaseTrust; - return HRESULT.S_OK; - } -} + } + return HRESULT.S_OK; + } + + public unsafe HRESULT GetIids(uint* iidCount, Guid** iids) + { + if (iidCount is null || iids is null) + { + return HRESULT.E_INVALIDARG; + } + + *iidCount = 1; + *iids = (Guid*)Marshal.AllocHGlobal(sizeof(Guid)); + *iids[0] = global::Windows.Win32.System.WinRT.IActivationFactory.IID_Guid; + return HRESULT.S_OK; + } + + public unsafe HRESULT GetRuntimeClassName(global::Windows.Win32.System.WinRT.HSTRING* className) + { + if (className is null) + { + return HRESULT.E_INVALIDARG; + } + + try + { + string? fullName = factory.GetType().FullName; + + if (fullName is null) + { + return HRESULT.E_UNEXPECTED; + } + + fixed (char* fullNamePtr = fullName) + { + return WindowsCreateString((PCWSTR)fullNamePtr, (uint)fullName.Length, className); + } + } + catch (Exception e) + { + return (HRESULT)Marshal.GetHRForException(e); + } + } + + public unsafe HRESULT GetTrustLevel(global::Windows.Win32.System.WinRT.TrustLevel* trustLevel) + { + if (trustLevel is null) + { + return HRESULT.E_INVALIDARG; + } + + *trustLevel = global::Windows.Win32.System.WinRT.TrustLevel.BaseTrust; + return HRESULT.S_OK; + } +} From 0b5c8bdf2717bd6e1c734aadc18e59bc10d06b73 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Thu, 29 Aug 2024 14:01:12 -0700 Subject: [PATCH 31/43] Use COM wrappers type over CsWin32 type --- .../Internal/BaseActivationFactoryWrapper.cs | 3 +-- src/Shmuelie.WinRTServer/NativeMethods.txt | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs index da84c60..cf0745d 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs @@ -4,7 +4,6 @@ using Shmuelie.WinRTServer.Windows.Com; using Windows.Win32.Foundation; using static Windows.Win32.PInvoke; -using IInspectable = Windows.Win32.System.WinRT.IInspectable; namespace Shmuelie.WinRTServer.Internal; @@ -24,7 +23,7 @@ public unsafe HRESULT ActivateInstance(void** instance) { object managedInstance = factory.ActivateInstance(); unknown = comWrappers.GetOrCreateComInterfaceForObject(managedInstance, CreateComInterfaceFlags.None); - var riid = IInspectable.IID_Guid; + var riid = global::Windows.Win32.System.WinRT.IActivationFactory.IID_Guid; var hr = (HRESULT)Marshal.QueryInterface(unknown, ref riid, out nint ppv); shouldReleaseUnknown = true; if (hr.Failed) diff --git a/src/Shmuelie.WinRTServer/NativeMethods.txt b/src/Shmuelie.WinRTServer/NativeMethods.txt index 4c5384e..abdcbab 100644 --- a/src/Shmuelie.WinRTServer/NativeMethods.txt +++ b/src/Shmuelie.WinRTServer/NativeMethods.txt @@ -15,7 +15,6 @@ GLOBALOPT_RO_FLAGS HRESULT HSTRING IGlobalOptions -IInspectable IUnknown RoInitialize RoRegisterActivationFactories From 328d505818db788e55ec6d00c25be1c6067b6064 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 11:03:09 -0700 Subject: [PATCH 32/43] Update XML docs and add additional argument checks --- src/Shmuelie.WinRTServer/ComServer.cs | 6 ++++- .../ComServerExtensions.cs | 27 ++++++++++++++++--- src/Shmuelie.WinRTServer/WinRtServer.cs | 6 ++++- .../WinRtServerExtensions.cs | 26 +++++++++++++++--- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index 61f8efd..04a9ff9 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -106,7 +106,7 @@ private void LifetimeCheckTimer_Elapsed(object? sender, ElapsedEventArgs e) /// if was registered; otherwise, . /// Only one factory can be registered for a CLSID. /// The instance is disposed. - /// is . + /// or is . /// The server is running. /// public unsafe bool RegisterClassFactory(BaseClassFactory factory, ComWrappers comWrappers) @@ -119,6 +119,10 @@ public unsafe bool RegisterClassFactory(BaseClassFactory factory, ComWrappers co { throw new ArgumentNullException(nameof(factory)); } + if (comWrappers is null) + { + throw new ArgumentNullException(nameof(comWrappers)); + } if (lifetimeCheckTimer.Enabled) { throw new InvalidOperationException("Can only add class factories when server is not running."); diff --git a/src/Shmuelie.WinRTServer/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/ComServerExtensions.cs index 8096b78..9b8fde9 100644 --- a/src/Shmuelie.WinRTServer/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/ComServerExtensions.cs @@ -21,7 +21,7 @@ public static class ComServerExtensions /// if type was registered; otherwise, . /// Type can only be registered once. /// The instance is disposed. - /// is . + /// or is . /// The server is running. public static bool RegisterClass(this ComServer server, ComWrappers comWrappers) where T : class, TInterface, new() { @@ -30,6 +30,12 @@ public static class ComServerExtensions throw new ArgumentNullException(nameof(server)); } + if (comWrappers is null) + { + throw new ArgumentNullException(nameof(comWrappers)); + } + + return server.RegisterClassFactory(new GeneralClassFactory(), comWrappers); } @@ -44,7 +50,7 @@ public static class ComServerExtensions /// if type was registered; otherwise, . /// Type can only be registered once. /// The instance is disposed. - /// is . + /// , , or is . /// The server is running. public static bool RegisterClass(this ComServer server, Func factory, ComWrappers comWrappers) where T : class, TInterface { @@ -53,6 +59,16 @@ public static bool RegisterClass(this ComServer server, Func f throw new ArgumentNullException(nameof(server)); } + if (factory is null) + { + throw new ArgumentNullException(nameof(factory)); + } + + if (comWrappers is null) + { + throw new ArgumentNullException(nameof(comWrappers)); + } + return server.RegisterClassFactory(new DelegateClassFactory(factory), comWrappers); } @@ -65,7 +81,7 @@ public static bool RegisterClass(this ComServer server, Func f /// if an instance of was registered; otherwise, . /// Only one factory can be registered for a CLSID. /// The instance is disposed. - /// is . + /// or is . /// The server is running. public static bool RegisterClassFactory(this ComServer server, ComWrappers comWrappers) where T : BaseClassFactory, new() { @@ -74,6 +90,11 @@ public static bool RegisterClass(this ComServer server, Func f throw new ArgumentNullException(nameof(server)); } + if (comWrappers is null) + { + throw new ArgumentNullException(nameof(comWrappers)); + } + return server.RegisterClassFactory(new T(), comWrappers); } diff --git a/src/Shmuelie.WinRTServer/WinRtServer.cs b/src/Shmuelie.WinRTServer/WinRtServer.cs index 9cbd44d..fb2e35c 100644 --- a/src/Shmuelie.WinRTServer/WinRtServer.cs +++ b/src/Shmuelie.WinRTServer/WinRtServer.cs @@ -128,7 +128,7 @@ private void Factory_InstanceCreated(object? sender, InstanceCreatedEventArgs e) /// The implementation of to use for wrapping. /// if was registered; otherwise, . /// Only one factory can be registered for a Activatable Class ID. - /// is . + /// or is . /// The server is running. public bool RegisterActivationFactory(BaseActivationFactory factory, ComWrappers comWrappers) { @@ -144,6 +144,10 @@ public bool RegisterActivationFactory(BaseActivationFactory factory, ComWrappers { throw new ArgumentNullException(nameof(factory)); } + if (comWrappers is null) + { + throw new ArgumentNullException(nameof(comWrappers)); + } if (factories.ContainsKey(factory.ActivatableClassId)) { diff --git a/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs index 33c09ac..2c222bc 100644 --- a/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs @@ -18,7 +18,7 @@ public static class WinRtServerExtensions /// The implementation of to use for wrapping. /// if type was registered; otherwise, . /// Type can only be registered once. - /// is . + /// or is . /// The server is running. public static bool RegisterClass(this WinRtServer server, ComWrappers comWrappers) where T : class, new() { @@ -27,6 +27,11 @@ public static class WinRtServerExtensions throw new ArgumentNullException(nameof(server)); } + if (comWrappers is null) + { + throw new ArgumentNullException(nameof(comWrappers)); + } + return server.RegisterActivationFactory(new GeneralActivationFactory(), comWrappers); } @@ -39,7 +44,7 @@ public static class WinRtServerExtensions /// The implementation of to use for wrapping. /// if type was registered; otherwise, . /// Type can only be registered once. - /// is . + /// , , or is . /// The server is running. public static bool RegisterClass(this WinRtServer server, Func factory, ComWrappers comWrappers) where T : class { @@ -48,6 +53,16 @@ public static bool RegisterClass(this WinRtServer server, Func factory, Co throw new ArgumentNullException(nameof(server)); } + if (factory is null) + { + throw new ArgumentNullException(nameof(factory)); + } + + if (comWrappers is null) + { + throw new ArgumentNullException(nameof(comWrappers)); + } + return server.RegisterActivationFactory(new DelegateActivationFactory(factory), comWrappers); } @@ -59,7 +74,7 @@ public static bool RegisterClass(this WinRtServer server, Func factory, Co /// The implementation of to use for wrapping. /// if an instance of was registered; otherwise, . /// Only one factory can be registered for a Activatable Class ID. - /// is . + /// or is . /// The server is running. public static bool RegisterActivationFactory(this WinRtServer server, ComWrappers comWrappers) where T : BaseActivationFactory, new() { @@ -68,6 +83,11 @@ public static bool RegisterClass(this WinRtServer server, Func factory, Co throw new ArgumentNullException(nameof(server)); } + if (comWrappers is null) + { + throw new ArgumentNullException(nameof(comWrappers)); + } + return server.RegisterActivationFactory(new T(), comWrappers); } } From 860e6221d9aadc59f37dd92d2991dbe45bb2f1e5 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 11:15:15 -0700 Subject: [PATCH 33/43] Improved XML docs --- src/Shmuelie.WinRTServer/ComServer.cs | 15 +++++++++++++++ src/Shmuelie.WinRTServer/WinRtServer.cs | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index 04a9ff9..36b242f 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -16,6 +16,20 @@ namespace Shmuelie.WinRTServer; /// /// An Out of Process COM Server. /// +/// +/// Allows for types to be created using COM activation instead of WinRT activation like . +/// Typical usage is to call from an block, using to not close until it is safe to do so. +/// +/// (); +/// server.Start(); +/// await server.WaitForFirstObjectAsync(); +/// } +/// ]]> +/// +/// /// /// [SupportedOSPlatform("windows6.0.6000")] @@ -197,6 +211,7 @@ private void Factory_InstanceCreated(object? sender, InstanceCreatedEventArgs e) /// /// Starts the server. /// + /// Calling is non-blocking. /// The instance is disposed. public void Start() { diff --git a/src/Shmuelie.WinRTServer/WinRtServer.cs b/src/Shmuelie.WinRTServer/WinRtServer.cs index fb2e35c..971fc31 100644 --- a/src/Shmuelie.WinRTServer/WinRtServer.cs +++ b/src/Shmuelie.WinRTServer/WinRtServer.cs @@ -18,11 +18,28 @@ namespace Shmuelie.WinRTServer; /// /// An Out of Process Windows Runtime Server. /// +/// +/// Allows for types to be created using WinRT activation instead of COM activation like . +/// Typical usage is to call from an block, using to not close until it is safe to do so. +/// +/// (); +/// server.Start(); +/// await server.WaitForFirstObjectAsync(); +/// } +/// ]]> +/// +/// /// /// [SupportedOSPlatform("windows8.0")] public sealed class WinRtServer : IAsyncDisposable { + /// + /// Mapping of Activatable Class IDs to activation factories and their implementation. + /// private readonly Dictionary factories = []; private readonly unsafe DllGetActivationFactory activationFactoryCallbackWrapper; @@ -217,6 +234,7 @@ public bool IsDisposed /// /// Starts the server. /// + /// Calling is non-blocking. public unsafe void Start() { if (IsDisposed) From 8144e8de959652aaa935a24ab8995907fb42b0c5 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 21:10:38 -0700 Subject: [PATCH 34/43] Use ArgumentNullException.ThrowIfNull --- src/Shmuelie.WinRTServer/ComServer.cs | 10 +---- .../ComServerExtensions.cs | 39 ++++--------------- .../CsWinRT/ComServerExtensions.cs | 15 ++----- .../CsWinRT/WinRtServerExtensions.cs | 15 ++----- .../StrategyBased/ComServerExtensions.cs | 15 ++----- .../StrategyBased/WinRtServerExtensions.cs | 15 ++----- src/Shmuelie.WinRTServer/WinRtServer.cs | 15 ++----- .../WinRtServerExtensions.cs | 39 ++++--------------- 8 files changed, 31 insertions(+), 132 deletions(-) diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index 36b242f..cf1c5e5 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -129,14 +129,8 @@ public unsafe bool RegisterClassFactory(BaseClassFactory factory, ComWrappers co { throw new ObjectDisposedException(nameof(ComServer)); } - if (factory is null) - { - throw new ArgumentNullException(nameof(factory)); - } - if (comWrappers is null) - { - throw new ArgumentNullException(nameof(comWrappers)); - } + ArgumentNullException.ThrowIfNull(factory); + ArgumentNullException.ThrowIfNull(comWrappers); if (lifetimeCheckTimer.Enabled) { throw new InvalidOperationException("Can only add class factories when server is not running."); diff --git a/src/Shmuelie.WinRTServer/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/ComServerExtensions.cs index 9b8fde9..e629353 100644 --- a/src/Shmuelie.WinRTServer/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/ComServerExtensions.cs @@ -25,15 +25,8 @@ public static class ComServerExtensions /// The server is running. public static bool RegisterClass(this ComServer server, ComWrappers comWrappers) where T : class, TInterface, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } - - if (comWrappers is null) - { - throw new ArgumentNullException(nameof(comWrappers)); - } + ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(comWrappers); return server.RegisterClassFactory(new GeneralClassFactory(), comWrappers); @@ -54,20 +47,9 @@ public static class ComServerExtensions /// The server is running. public static bool RegisterClass(this ComServer server, Func factory, ComWrappers comWrappers) where T : class, TInterface { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } - - if (factory is null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (comWrappers is null) - { - throw new ArgumentNullException(nameof(comWrappers)); - } + ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(factory); + ArgumentNullException.ThrowIfNull(comWrappers); return server.RegisterClassFactory(new DelegateClassFactory(factory), comWrappers); } @@ -85,15 +67,8 @@ public static bool RegisterClass(this ComServer server, Func f /// The server is running. public static bool RegisterClassFactory(this ComServer server, ComWrappers comWrappers) where T : BaseClassFactory, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } - - if (comWrappers is null) - { - throw new ArgumentNullException(nameof(comWrappers)); - } + ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(comWrappers); return server.RegisterClassFactory(new T(), comWrappers); } diff --git a/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs index d9addcf..bea7a99 100644 --- a/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs @@ -25,10 +25,7 @@ public static class ComServerExtensions /// The server is running. public static bool RegisterClass(this ComServer server) where T : class, TInterface, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterClassFactory(new GeneralClassFactory(), comWrappers); } @@ -47,10 +44,7 @@ public static class ComServerExtensions /// The server is running. public static bool RegisterClass(this ComServer server, Func factory) where T : class, TInterface { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterClassFactory(new DelegateClassFactory(factory), comWrappers); } @@ -67,10 +61,7 @@ public static bool RegisterClass(this ComServer server, Func f /// The server is running. public static bool RegisterClassFactory(this ComServer server) where T : BaseClassFactory, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterClassFactory(new T(), comWrappers); } diff --git a/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs index 3f91240..d36965c 100644 --- a/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs @@ -23,10 +23,7 @@ public static class WinRtServerExtensions /// The server is running. public static bool RegisterClass(this WinRtServer server) where T : class, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterActivationFactory(new GeneralActivationFactory(), comWrappers); } @@ -43,10 +40,7 @@ public static class WinRtServerExtensions /// The server is running. public static bool RegisterClass(this WinRtServer server, Func factory) where T : class { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterActivationFactory(new DelegateActivationFactory(factory), comWrappers); } @@ -62,10 +56,7 @@ public static bool RegisterClass(this WinRtServer server, Func factory) wh /// The server is running. public static bool RegisterActivationFactory(this WinRtServer server) where T : BaseActivationFactory, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterActivationFactory(new T(), comWrappers); } diff --git a/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs index bdf2e2f..ec1b514 100644 --- a/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs @@ -25,10 +25,7 @@ public static class ComServerExtensions /// The server is running. public static bool RegisterClass(this ComServer server) where T : class, TInterface, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterClassFactory(new GeneralClassFactory(), comWrappers); } @@ -47,10 +44,7 @@ public static class ComServerExtensions /// The server is running. public static bool RegisterClass(this ComServer server, Func factory) where T : class, TInterface { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterClassFactory(new DelegateClassFactory(factory), comWrappers); } @@ -67,10 +61,7 @@ public static bool RegisterClass(this ComServer server, Func f /// The server is running. public static bool RegisterClassFactory(this ComServer server) where T : BaseClassFactory, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterClassFactory(new T(), comWrappers); } diff --git a/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs index af60cd0..7fe8ad4 100644 --- a/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs @@ -24,10 +24,7 @@ public static class WinRtServerExtensions /// The server is running. public static bool RegisterClass(this WinRtServer server) where T : class, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterActivationFactory(new GeneralActivationFactory(), comWrappers); } @@ -44,10 +41,7 @@ public static class WinRtServerExtensions /// The server is running. public static bool RegisterClass(this WinRtServer server, Func factory) where T : class { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterActivationFactory(new DelegateActivationFactory(factory), comWrappers); } @@ -63,10 +57,7 @@ public static bool RegisterClass(this WinRtServer server, Func factory) wh /// The server is running. public static bool RegisterActivationFactory(this WinRtServer server) where T : BaseActivationFactory, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } + ArgumentNullException.ThrowIfNull(server); return server.RegisterActivationFactory(new T(), comWrappers); } diff --git a/src/Shmuelie.WinRTServer/WinRtServer.cs b/src/Shmuelie.WinRTServer/WinRtServer.cs index 971fc31..b4a24c7 100644 --- a/src/Shmuelie.WinRTServer/WinRtServer.cs +++ b/src/Shmuelie.WinRTServer/WinRtServer.cs @@ -157,14 +157,8 @@ public bool RegisterActivationFactory(BaseActivationFactory factory, ComWrappers { throw new InvalidOperationException("Can only add activation factories when server is not running"); } - if (factory is null) - { - throw new ArgumentNullException(nameof(factory)); - } - if (comWrappers is null) - { - throw new ArgumentNullException(nameof(comWrappers)); - } + ArgumentNullException.ThrowIfNull(factory); + ArgumentNullException.ThrowIfNull(comWrappers); if (factories.ContainsKey(factory.ActivatableClassId)) { @@ -192,10 +186,7 @@ public bool UnregisterActivationFactory(BaseActivationFactory factory) { throw new InvalidOperationException("Can only remove activation factories when server is not running"); } - if (factory is null) - { - throw new ArgumentNullException(nameof(factory)); - } + ArgumentNullException.ThrowIfNull(factory); return factories.Remove(factory.ActivatableClassId); } diff --git a/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs index 2c222bc..89278fc 100644 --- a/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/WinRtServerExtensions.cs @@ -22,15 +22,8 @@ public static class WinRtServerExtensions /// The server is running. public static bool RegisterClass(this WinRtServer server, ComWrappers comWrappers) where T : class, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } - - if (comWrappers is null) - { - throw new ArgumentNullException(nameof(comWrappers)); - } + ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(comWrappers); return server.RegisterActivationFactory(new GeneralActivationFactory(), comWrappers); } @@ -48,20 +41,9 @@ public static class WinRtServerExtensions /// The server is running. public static bool RegisterClass(this WinRtServer server, Func factory, ComWrappers comWrappers) where T : class { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } - - if (factory is null) - { - throw new ArgumentNullException(nameof(factory)); - } - - if (comWrappers is null) - { - throw new ArgumentNullException(nameof(comWrappers)); - } + ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(factory); + ArgumentNullException.ThrowIfNull(comWrappers); return server.RegisterActivationFactory(new DelegateActivationFactory(factory), comWrappers); } @@ -78,15 +60,8 @@ public static bool RegisterClass(this WinRtServer server, Func factory, Co /// The server is running. public static bool RegisterActivationFactory(this WinRtServer server, ComWrappers comWrappers) where T : BaseActivationFactory, new() { - if (server is null) - { - throw new ArgumentNullException(nameof(server)); - } - - if (comWrappers is null) - { - throw new ArgumentNullException(nameof(comWrappers)); - } + ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(comWrappers); return server.RegisterActivationFactory(new T(), comWrappers); } From c60e318716711ee66852ea75e75c05f08cb996ac Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 21:14:15 -0700 Subject: [PATCH 35/43] Fix docs --- src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs index bea7a99..9d39044 100644 --- a/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs @@ -53,7 +53,7 @@ public static bool RegisterClass(this ComServer server, Func f /// Register a class factory with the server. /// /// The type of the factory to register. - /// /// The instance. + /// The instance. /// if an instance of was registered; otherwise, . /// Only one factory can be registered for a CLSID. /// The instance is disposed. From a4bf46a0f2d701ebd6b441f643db117bce45ba4a Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 21:15:07 -0700 Subject: [PATCH 36/43] Add missing argument checks --- src/Shmuelie.WinRTServer/ComServerExtensions.cs | 4 ++++ src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs | 7 ++++++- src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs | 3 ++- .../StrategyBased/ComServerExtensions.cs | 7 ++++++- .../StrategyBased/WinRtServerExtensions.cs | 3 ++- 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Shmuelie.WinRTServer/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/ComServerExtensions.cs index e629353..28752e8 100644 --- a/src/Shmuelie.WinRTServer/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/ComServerExtensions.cs @@ -81,8 +81,12 @@ public static bool RegisterClass(this ComServer server, Func f /// if the server was removed; otherwise, . /// The instance is disposed. /// The server is running. + /// or is . public static bool UnregisterClassFactory(this ComServer server, BaseClassFactory factory) { + ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(factory); + return server.UnregisterClassFactory(factory.Clsid); } } diff --git a/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs index 9d39044..2f46b5f 100644 --- a/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/CsWinRT/ComServerExtensions.cs @@ -40,11 +40,12 @@ public static class ComServerExtensions /// if type was registered; otherwise, . /// Type can only be registered once. /// The instance is disposed. - /// is . + /// or is . /// The server is running. public static bool RegisterClass(this ComServer server, Func factory) where T : class, TInterface { ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(factory); return server.RegisterClassFactory(new DelegateClassFactory(factory), comWrappers); } @@ -74,8 +75,12 @@ public static bool RegisterClass(this ComServer server, Func f /// if the server was removed; otherwise, . /// The instance is disposed. /// The server is running. + /// or is . public static bool UnregisterClassFactory(this ComServer server, BaseClassFactory factory) { + ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(factory); + return server.UnregisterClassFactory(factory.Clsid); } } \ No newline at end of file diff --git a/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs index d36965c..f3ddad7 100644 --- a/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/CsWinRT/WinRtServerExtensions.cs @@ -36,11 +36,12 @@ public static class WinRtServerExtensions /// Method to create instance of . /// if type was registered; otherwise, . /// Type can only be registered once. - /// is . + /// or is . /// The server is running. public static bool RegisterClass(this WinRtServer server, Func factory) where T : class { ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(factory); return server.RegisterActivationFactory(new DelegateActivationFactory(factory), comWrappers); } diff --git a/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs index ec1b514..1ac2508 100644 --- a/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/StrategyBased/ComServerExtensions.cs @@ -40,11 +40,12 @@ public static class ComServerExtensions /// if type was registered; otherwise, . /// Type can only be registered once. /// The instance is disposed. - /// is . + /// or is . /// The server is running. public static bool RegisterClass(this ComServer server, Func factory) where T : class, TInterface { ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(factory); return server.RegisterClassFactory(new DelegateClassFactory(factory), comWrappers); } @@ -74,8 +75,12 @@ public static bool RegisterClass(this ComServer server, Func f /// if the server was removed; otherwise, . /// The instance is disposed. /// The server is running. + /// or is . public static bool UnregisterClassFactory(this ComServer server, BaseClassFactory factory) { + ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(factory); + return server.UnregisterClassFactory(factory.Clsid); } } diff --git a/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs index 7fe8ad4..d685eac 100644 --- a/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs @@ -37,11 +37,12 @@ public static class WinRtServerExtensions /// Method to create instance of . /// if type was registered; otherwise, . /// Type can only be registered once. - /// is . + /// or is . /// The server is running. public static bool RegisterClass(this WinRtServer server, Func factory) where T : class { ArgumentNullException.ThrowIfNull(server); + ArgumentNullException.ThrowIfNull(factory); return server.RegisterActivationFactory(new DelegateActivationFactory(factory), comWrappers); } From 379cdda25c91bfc03186f684e698def1c7df11f4 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 21:15:15 -0700 Subject: [PATCH 37/43] Cleanup usings --- src/Shmuelie.WinRTServer/ComServerExtensions.cs | 1 - src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Shmuelie.WinRTServer/ComServerExtensions.cs b/src/Shmuelie.WinRTServer/ComServerExtensions.cs index 28752e8..ff6d9de 100644 --- a/src/Shmuelie.WinRTServer/ComServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/ComServerExtensions.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices.Marshalling; using System.Runtime.InteropServices; using System.Runtime.Versioning; diff --git a/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs b/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs index d685eac..34b09bc 100644 --- a/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs +++ b/src/Shmuelie.WinRTServer/StrategyBased/WinRtServerExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Runtime.InteropServices.Marshalling; using System.Runtime.Versioning; -using Shmuelie.WinRTServer.CsWinRT; namespace Shmuelie.WinRTServer.StrategyBased; From 9972eb2a059c0f981a6a4823c2139d72a73f0b45 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 21:27:01 -0700 Subject: [PATCH 38/43] Supress or respect analysis --- src/Shmuelie.WinRTServer/ComServer.cs | 4 ++-- .../Internal/BaseActivationFactoryWrapper.cs | 4 +++- src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs | 4 +++- src/Shmuelie.WinRTServer/WinRtServer.cs | 5 +++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index cf1c5e5..7ddc3c7 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -260,7 +260,7 @@ public void Stop() { return null; } - return await local.Task; + return await local.Task.ConfigureAwait(false); } /// @@ -290,7 +290,7 @@ void Ended(object? sender, EventArgs e) } Empty += Ended; - await tcs.Task; + await tcs.Task.ConfigureAwait(false); Empty -= Ended; } diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs index cf0745d..91b32f0 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs @@ -8,8 +8,9 @@ namespace Shmuelie.WinRTServer.Internal; [GeneratedComClass] -internal partial class BaseActivationFactoryWrapper(BaseActivationFactory factory, ComWrappers comWrappers) : IActivationFactory +internal sealed partial class BaseActivationFactoryWrapper(BaseActivationFactory factory, ComWrappers comWrappers) : IActivationFactory { + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "COM method, must not throw.")] public unsafe HRESULT ActivateInstance(void** instance) { if (instance is null) @@ -61,6 +62,7 @@ public unsafe HRESULT GetIids(uint* iidCount, Guid** iids) return HRESULT.S_OK; } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "COM method, must not throw.")] public unsafe HRESULT GetRuntimeClassName(global::Windows.Win32.System.WinRT.HSTRING* className) { if (className is null) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs index cc0bb4f..85b8e0c 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseClassFactoryWrapper.cs @@ -9,8 +9,9 @@ namespace Shmuelie.WinRTServer.Internal; [GeneratedComClass] -internal partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrappers comWrappers) : IClassFactory +internal sealed partial class BaseClassFactoryWrapper(BaseClassFactory factory, ComWrappers comWrappers) : IClassFactory { + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "COM method, must not throw.")] public unsafe HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObject) { if (pUnkOuter is not null) @@ -61,6 +62,7 @@ public unsafe HRESULT CreateInstance(void* pUnkOuter, Guid* riid, void** ppvObje return HRESULT.S_OK; } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "COM method, must not throw.")] public HRESULT LockServer(BOOL fLock) { try diff --git a/src/Shmuelie.WinRTServer/WinRtServer.cs b/src/Shmuelie.WinRTServer/WinRtServer.cs index b4a24c7..e3762ea 100644 --- a/src/Shmuelie.WinRTServer/WinRtServer.cs +++ b/src/Shmuelie.WinRTServer/WinRtServer.cs @@ -35,6 +35,7 @@ namespace Shmuelie.WinRTServer; /// /// [SupportedOSPlatform("windows8.0")] +[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1724", Justification = "No better idea")] public sealed class WinRtServer : IAsyncDisposable { /// @@ -321,7 +322,7 @@ public void Stop() { return null; } - return await local.Task; + return await local.Task.ConfigureAwait(false); } /// @@ -340,7 +341,7 @@ void Ended(object? sender, EventArgs e) } Empty += Ended; - await tcs.Task; + await tcs.Task.ConfigureAwait(false); Empty -= Ended; } From 71d86a2aa5de12712dd238fdf8e59322b88a3ba4 Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 21:29:23 -0700 Subject: [PATCH 39/43] Remove supressions --- .editorconfig | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.editorconfig b/.editorconfig index 531fe44..333425d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,10 +1,4 @@ [*.{cs,vb}] # IDE0046: Convert to conditional expression -dotnet_style_prefer_conditional_expression_over_return = false - -# CA1510: Use ArgumentNullException throw helper -dotnet_diagnostic.CA1510.severity = none - -# CA1513: Use ObjectDisposedException throw helper -dotnet_diagnostic.CA1513.severity = none +dotnet_style_prefer_conditional_expression_over_return = false \ No newline at end of file From 7fcb99391d17a1da8d75153bbe29a8343db5914c Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 21:29:39 -0700 Subject: [PATCH 40/43] Use ObjectDisposedException.ThrowIf --- src/Shmuelie.WinRTServer/ComServer.cs | 25 +++++-------------------- src/Shmuelie.WinRTServer/WinRtServer.cs | 25 +++++-------------------- 2 files changed, 10 insertions(+), 40 deletions(-) diff --git a/src/Shmuelie.WinRTServer/ComServer.cs b/src/Shmuelie.WinRTServer/ComServer.cs index 7ddc3c7..86ed017 100644 --- a/src/Shmuelie.WinRTServer/ComServer.cs +++ b/src/Shmuelie.WinRTServer/ComServer.cs @@ -125,10 +125,7 @@ private void LifetimeCheckTimer_Elapsed(object? sender, ElapsedEventArgs e) /// public unsafe bool RegisterClassFactory(BaseClassFactory factory, ComWrappers comWrappers) { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(ComServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); ArgumentNullException.ThrowIfNull(factory); ArgumentNullException.ThrowIfNull(comWrappers); if (lifetimeCheckTimer.Enabled) @@ -164,10 +161,7 @@ public unsafe bool RegisterClassFactory(BaseClassFactory factory, ComWrappers co /// public unsafe bool UnregisterClassFactory(Guid clsid) { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(ComServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); if (lifetimeCheckTimer.Enabled) { throw new InvalidOperationException("Can only remove class factories when server is not running."); @@ -209,10 +203,7 @@ private void Factory_InstanceCreated(object? sender, InstanceCreatedEventArgs e) /// The instance is disposed. public void Start() { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(ComServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); if (lifetimeCheckTimer.Enabled) { return; @@ -229,10 +220,7 @@ public void Start() /// The instance is disposed. public void Stop() { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(ComServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); if (!lifetimeCheckTimer.Enabled) { return; @@ -250,10 +238,7 @@ public void Stop() /// The instance is disposed. public async Task WaitForFirstObjectAsync() { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(ComServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); TaskCompletionSource? local = firstInstanceCreated; if (local is null) diff --git a/src/Shmuelie.WinRTServer/WinRtServer.cs b/src/Shmuelie.WinRTServer/WinRtServer.cs index e3762ea..f263232 100644 --- a/src/Shmuelie.WinRTServer/WinRtServer.cs +++ b/src/Shmuelie.WinRTServer/WinRtServer.cs @@ -150,10 +150,7 @@ private void Factory_InstanceCreated(object? sender, InstanceCreatedEventArgs e) /// The server is running. public bool RegisterActivationFactory(BaseActivationFactory factory, ComWrappers comWrappers) { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(WinRtServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); if (IsRunning) { throw new InvalidOperationException("Can only add activation factories when server is not running"); @@ -179,10 +176,7 @@ public bool RegisterActivationFactory(BaseActivationFactory factory, ComWrappers /// The server is running. public bool UnregisterActivationFactory(BaseActivationFactory factory) { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(WinRtServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); if (IsRunning) { throw new InvalidOperationException("Can only remove activation factories when server is not running"); @@ -229,10 +223,7 @@ public bool IsDisposed /// Calling is non-blocking. public unsafe void Start() { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(WinRtServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); if (IsRunning) { return; @@ -289,10 +280,7 @@ public unsafe void Start() /// public void Stop() { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(WinRtServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); if (!IsRunning) { return; @@ -312,10 +300,7 @@ public void Stop() /// The instance is disposed. public async Task WaitForFirstObjectAsync() { - if (IsDisposed) - { - throw new ObjectDisposedException(nameof(WinRtServer)); - } + ObjectDisposedException.ThrowIf(IsDisposed, this); TaskCompletionSource? local = firstInstanceCreated; if (local is null) From 2fa7bfbf25c242bce3d526d98943b419d014478c Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 21:30:59 -0700 Subject: [PATCH 41/43] Suppress CA1708: Identifiers should differ by more than case --- .editorconfig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 333425d..3a1c871 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,4 +1,7 @@ [*.{cs,vb}] # IDE0046: Convert to conditional expression -dotnet_style_prefer_conditional_expression_over_return = false \ No newline at end of file +dotnet_style_prefer_conditional_expression_over_return = false + +# CA1708: Identifiers should differ by more than case +dotnet_diagnostic.CA1708.severity = none \ No newline at end of file From 15c951d55b10cf6fb7c33cbe6a84e2e505227bae Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Fri, 30 Aug 2024 21:31:21 -0700 Subject: [PATCH 42/43] Enable analyzers in Library --- src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj index ef73fc9..e7a5e50 100644 --- a/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj +++ b/src/Shmuelie.WinRTServer/Shmuelie.WinRTServer.csproj @@ -6,6 +6,7 @@ True True win-x64;win-x86;win-arm64;$(RuntimeIdentifiers) + 9.0-all From 6848a5b330f63c43cfee73f2a3cdc1c54fa5b6fa Mon Sep 17 00:00:00 2001 From: Shmueli Englard Date: Tue, 3 Sep 2024 11:10:33 -0700 Subject: [PATCH 43/43] Simplify QI --- .../Internal/BaseActivationFactoryWrapper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs index 91b32f0..5ad5541 100644 --- a/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs +++ b/src/Shmuelie.WinRTServer/Internal/BaseActivationFactoryWrapper.cs @@ -24,8 +24,7 @@ public unsafe HRESULT ActivateInstance(void** instance) { object managedInstance = factory.ActivateInstance(); unknown = comWrappers.GetOrCreateComInterfaceForObject(managedInstance, CreateComInterfaceFlags.None); - var riid = global::Windows.Win32.System.WinRT.IActivationFactory.IID_Guid; - var hr = (HRESULT)Marshal.QueryInterface(unknown, ref riid, out nint ppv); + var hr = (HRESULT)Marshal.QueryInterface(unknown, in global::Windows.Win32.System.WinRT.IActivationFactory.IID_Guid, out nint ppv); shouldReleaseUnknown = true; if (hr.Failed) {