Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consume DNNE for testing #124

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DllImportGenerator/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vs/
**/bin
**/obj
**/obj
*.binlog
4 changes: 4 additions & 0 deletions DllImportGenerator/Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
<TargetFramework>net5.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>preview</LangVersion>

<!-- See https://github.com/dotnet/runtime/issues/42745 for why this is needed. -->
<UseAppHost>true</UseAppHost>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Ancillary.Interop\Ancillary.Interop.csproj" />
<ProjectReference Include="..\DllImportGenerator\DllImportGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\TestAssets\NativeExports\NativeExports.csproj" />
</ItemGroup>

</Project>
29 changes: 21 additions & 8 deletions DllImportGenerator/Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
13 changes: 12 additions & 1 deletion DllImportGenerator/DllImportGenerator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions DllImportGenerator/TestAssets/NativeExports/NativeExports.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableDynamicLoading>true</EnableDynamicLoading>
<DnneAddGeneratedBinaryToProject>true</DnneAddGeneratedBinaryToProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DNNE" Version="1.0.11" />
</ItemGroup>

<!--
The Target below is used to mitigate a limitation when referencing
application projects. The work to improve this is tracked with:
https://github.com/dotnet/sdk/issues/1675
See https://github.com/dotnet/sdk/issues/1675#issuecomment-658779827
-->
<Target Name="AddRuntimeDependenciesToContent"
BeforeTargets="GetCopyToOutputDirectoryItems"
DependsOnTargets="GenerateBuildDependencyFile;GenerateBuildRuntimeConfigurationFiles">
<ItemGroup>
<ContentWithTargetPath Include="$(ProjectDepsFilePath)"
Condition="'$(GenerateDependencyFile)' == 'true'"
CopyToOutputDirectory="PreserveNewest"
TargetPath="$(ProjectDepsFileName)" />
<ContentWithTargetPath Include="$(ProjectRuntimeConfigFilePath)"
Condition="'$(GenerateRuntimeConfigurationFiles)' == 'true'"
CopyToOutputDirectory="PreserveNewest"
TargetPath="$(ProjectRuntimeConfigFileName)" />
</ItemGroup>
</Target>

</Project>
26 changes: 26 additions & 0 deletions DllImportGenerator/TestAssets/NativeExports/ScalarOps.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
6 changes: 5 additions & 1 deletion DllImportGenerator/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)