From d98225a22897ff5348e25878d876466538ce8455 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 24 Sep 2020 18:44:34 -0700 Subject: [PATCH 1/9] Use DNNE to create native testing assets Instead of relying on a native project use DNNE to generate native exports for managed binaries. This allows us to avoid writing native code for testing code generation. --- DllImportGenerator/Demo/Demo.csproj | 1 + DllImportGenerator/Demo/Program.cs | 20 +++++++--- DllImportGenerator/DllImportGenerator.sln | 13 ++++++- .../NativeExports/NativeExports.csproj | 39 +++++++++++++++++++ .../TestAssets/NativeExports/ScalarOps.cs | 20 ++++++++++ 5 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj create mode 100644 DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs diff --git a/DllImportGenerator/Demo/Demo.csproj b/DllImportGenerator/Demo/Demo.csproj index 09299b00addb..7e2866b294e3 100644 --- a/DllImportGenerator/Demo/Demo.csproj +++ b/DllImportGenerator/Demo/Demo.csproj @@ -10,6 +10,7 @@ + diff --git a/DllImportGenerator/Demo/Program.cs b/DllImportGenerator/Demo/Program.cs index 335cef053259..62c79360b41d 100644 --- a/DllImportGenerator/Demo/Program.cs +++ b/DllImportGenerator/Demo/Program.cs @@ -5,19 +5,27 @@ namespace Demo { - partial class Kernel32 + partial class NativeExportsNE { - [GeneratedDllImport(nameof(Kernel32), EntryPoint = "QueryPerformanceCounter")] - public static partial int Method(ref long t); + [GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "sumi")] + public static partial int Sum(int a, int b); + + [GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "sumrefi")] + public static partial void Sum(int a, ref int b); } unsafe class Program { static void Main(string[] args) { - var ts = (long)0; - int suc = Kernel32.Method(ref ts); - Console.WriteLine($"{suc}: 0x{ts:x}"); + int a = 12; + int b = 13; + int c = NativeExportsNE.Sum(a, b); + Console.WriteLine($"{a} + {b} = {c}"); + + c = b; + NativeExportsNE.Sum(a, ref c); + Console.WriteLine($"{a} + {b} = {c}"); } } } diff --git a/DllImportGenerator/DllImportGenerator.sln b/DllImportGenerator/DllImportGenerator.sln index 2ecea783df57..83e498c7727e 100644 --- a/DllImportGenerator/DllImportGenerator.sln +++ b/DllImportGenerator/DllImportGenerator.sln @@ -9,7 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DllImportGenerator.Test", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo", "Demo\Demo.csproj", "{E478EE77-E072-4A42-B453-EBFDA7728717}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ancillary.Interop", "Ancillary.Interop\Ancillary.Interop.csproj", "{E59F0B6A-1137-4179-A91D-33464A775DEB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ancillary.Interop", "Ancillary.Interop\Ancillary.Interop.csproj", "{E59F0B6A-1137-4179-A91D-33464A775DEB}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestAssets", "TestAssets", "{2CFB9A7A-4AAF-4B6A-8CC8-540F64C3B45F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeExports", "TestAssets\NativeExports\NativeExports.csproj", "{32FDA079-0E9F-4A36-ADA5-6593B67A54AC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,10 +37,17 @@ Global {E59F0B6A-1137-4179-A91D-33464A775DEB}.Debug|Any CPU.Build.0 = Debug|Any CPU {E59F0B6A-1137-4179-A91D-33464A775DEB}.Release|Any CPU.ActiveCfg = Release|Any CPU {E59F0B6A-1137-4179-A91D-33464A775DEB}.Release|Any CPU.Build.0 = Release|Any CPU + {32FDA079-0E9F-4A36-ADA5-6593B67A54AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {32FDA079-0E9F-4A36-ADA5-6593B67A54AC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {32FDA079-0E9F-4A36-ADA5-6593B67A54AC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {32FDA079-0E9F-4A36-ADA5-6593B67A54AC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {32FDA079-0E9F-4A36-ADA5-6593B67A54AC} = {2CFB9A7A-4AAF-4B6A-8CC8-540F64C3B45F} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5344B739-3A02-402A-8777-0D54DEC4F3BA} EndGlobalSection diff --git a/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj b/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj new file mode 100644 index 000000000000..d713cdad3e1c --- /dev/null +++ b/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj @@ -0,0 +1,39 @@ + + + + net5.0 + true + true + + + + + + + + + + $(DnneNativeExportsBinaryName)$(DnneNativeBinaryExt) + PreserveNewest + false + + + + + $(TargetName).runtimeconfig.json + PreserveNewest + false + + + + diff --git a/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs b/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs new file mode 100644 index 000000000000..c363b8ade2e4 --- /dev/null +++ b/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs @@ -0,0 +1,20 @@ +using System; +using System.Runtime.InteropServices; + +namespace NativeExportsNE +{ + public unsafe class ScalarOps + { + [UnmanagedCallersOnly(EntryPoint = "sumi")] + public static int Sum(int a, int b) + { + return a + b; + } + + [UnmanagedCallersOnly(EntryPoint = "sumrefi")] + public static void SumRef(int a, int* b) + { + *b += a; + } + } +} From 0415b65c42e017b01a4e28a69e0093b6d669e7ac Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 24 Sep 2020 18:45:00 -0700 Subject: [PATCH 2/9] Add *.binlog to .gitignore --- DllImportGenerator/.gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DllImportGenerator/.gitignore b/DllImportGenerator/.gitignore index 7d37c147c16b..831de0b592d4 100644 --- a/DllImportGenerator/.gitignore +++ b/DllImportGenerator/.gitignore @@ -1,3 +1,4 @@ .vs/ **/bin -**/obj \ No newline at end of file +**/obj +*.binlog \ No newline at end of file From c5f2ceec6cfcf96ba7e8355fb36c081abc254e73 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 24 Sep 2020 18:50:24 -0700 Subject: [PATCH 3/9] Update readme.md --- DllImportGenerator/readme.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/DllImportGenerator/readme.md b/DllImportGenerator/readme.md index a765a7a51257..9ba80ca33220 100644 --- a/DllImportGenerator/readme.md +++ b/DllImportGenerator/readme.md @@ -8,13 +8,17 @@ Below are additional work items that are not presently captured in this reposito ### Required -* Add `GeneratedDllImportAttribute` to the BCL. See [`GeneratedDllImportAttribute.cs`](./DllImportGeneratorAttribute/GeneratedDllImportAttribute.cs). + +* Add `GeneratedDllImportAttribute` to the BCL. See [`GeneratedDllImportAttribute.cs`](./Ancillary.Interop/GeneratedDllImportAttribute.cs). * Add support for calling [`SetLastError()`](https://docs.microsoft.com/windows/win32/api/errhandlingapi/nf-errhandlingapi-setlasterror) from managed code with the expected semantics. During P/Invoke transitions the runtime manipulates the associated thread's error code and thus breaks `SetLastError()` semantics. +* APIs to handle [`SafeHandle`](https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.safehandle) manipulation. + ### Optional * A tool to compare the resulting IL from the generated source to that generated by the built-in IL Marshaller system. This would help with validation of what is being generated. ## Designs +- [Code generation pipeline](./designs/Pipeline.md) - [Struct Marshalling](./designs/StructMarshalling.md) From 96d4de4ceadf6cd0129f30300ccdf9e19b0923a4 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 24 Sep 2020 21:17:24 -0700 Subject: [PATCH 4/9] Add fix to avoid double build issues. --- .../NativeExports/NativeExports.csproj | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj b/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj index d713cdad3e1c..8c7f6cc84956 100644 --- a/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj +++ b/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj @@ -20,20 +20,28 @@ PreserveNewest false - - - - $(TargetName).runtimeconfig.json - PreserveNewest - false - + + + + + + + + From 52c9df3af9d2adbd4bfc9d81e1791ef5e7beeaed Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 24 Sep 2020 21:23:16 -0700 Subject: [PATCH 5/9] Add out example to the Sum demo. --- DllImportGenerator/Demo/Program.cs | 9 +++++++-- DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/DllImportGenerator/Demo/Program.cs b/DllImportGenerator/Demo/Program.cs index 62c79360b41d..ff7d931ccd3c 100644 --- a/DllImportGenerator/Demo/Program.cs +++ b/DllImportGenerator/Demo/Program.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Demo @@ -10,6 +8,9 @@ partial class NativeExportsNE [GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "sumi")] public static partial int Sum(int a, int b); + [GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "sumouti")] + public static partial void Sum(int a, int b, out int c); + [GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "sumrefi")] public static partial void Sum(int a, ref int b); } @@ -23,6 +24,10 @@ static void Main(string[] args) int c = NativeExportsNE.Sum(a, b); Console.WriteLine($"{a} + {b} = {c}"); + c = 0; + NativeExportsNE.Sum(a, b, out c); + Console.WriteLine($"{a} + {b} = {c}"); + c = b; NativeExportsNE.Sum(a, ref c); Console.WriteLine($"{a} + {b} = {c}"); diff --git a/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs b/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs index c363b8ade2e4..4a2a01c8792a 100644 --- a/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs +++ b/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs @@ -11,6 +11,12 @@ public static int Sum(int a, int b) return a + b; } + [UnmanagedCallersOnly(EntryPoint = "sumouti")] + public static void SumOut(int a, int b, int* c) + { + *c = a + b; + } + [UnmanagedCallersOnly(EntryPoint = "sumrefi")] public static void SumRef(int a, int* b) { From 378e555e8f31121fcffc2f62c91fab112cb65cdf Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Thu, 24 Sep 2020 21:39:11 -0700 Subject: [PATCH 6/9] Fix namespace. --- DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs b/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs index 4a2a01c8792a..4d2bb92006f6 100644 --- a/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs +++ b/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs @@ -1,7 +1,7 @@ using System; using System.Runtime.InteropServices; -namespace NativeExportsNE +namespace NativeExports { public unsafe class ScalarOps { From 13d1b8bbd7b8bc37b9fd4a8f4121a293e30b3c52 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Fri, 25 Sep 2020 10:57:02 -0700 Subject: [PATCH 7/9] Force 'UseAppHost' to fix scenario on macOS. --- DllImportGenerator/Demo/Demo.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DllImportGenerator/Demo/Demo.csproj b/DllImportGenerator/Demo/Demo.csproj index 7e2866b294e3..4cee92efcd88 100644 --- a/DllImportGenerator/Demo/Demo.csproj +++ b/DllImportGenerator/Demo/Demo.csproj @@ -5,6 +5,9 @@ net5.0 true preview + + + true From d5c2bf62733048108d886cd327a527abd5ecd3fb Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Fri, 25 Sep 2020 14:06:53 -0700 Subject: [PATCH 8/9] Consume support in DNNE for adding native binary to project. --- .../TestAssets/NativeExports/NativeExports.csproj | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj b/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj index 8c7f6cc84956..a5fb42c9bd3b 100644 --- a/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj +++ b/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj @@ -4,24 +4,13 @@ net5.0 true true + true - - - - $(DnneNativeExportsBinaryName)$(DnneNativeBinaryExt) - PreserveNewest - false - - -