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 diff --git a/DllImportGenerator/Demo/Demo.csproj b/DllImportGenerator/Demo/Demo.csproj index 09299b00addb..4cee92efcd88 100644 --- a/DllImportGenerator/Demo/Demo.csproj +++ b/DllImportGenerator/Demo/Demo.csproj @@ -5,11 +5,15 @@ net5.0 true preview + + + true + diff --git a/DllImportGenerator/Demo/Program.cs b/DllImportGenerator/Demo/Program.cs index 335cef053259..ff7d931ccd3c 100644 --- a/DllImportGenerator/Demo/Program.cs +++ b/DllImportGenerator/Demo/Program.cs @@ -1,23 +1,36 @@ using System; -using System.Collections.Generic; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; 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 = "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); } 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 = 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/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..70e5b074e8b8 --- /dev/null +++ b/DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj @@ -0,0 +1,35 @@ + + + + net5.0 + true + true + true + + + + + + + + + + + + + + + diff --git a/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs b/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs new file mode 100644 index 000000000000..4d2bb92006f6 --- /dev/null +++ b/DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs @@ -0,0 +1,26 @@ +using System; +using System.Runtime.InteropServices; + +namespace NativeExports +{ + public unsafe class ScalarOps + { + [UnmanagedCallersOnly(EntryPoint = "sumi")] + 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) + { + *b += a; + } + } +} 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)