Port to .NET Standard 2.0 - so far works only on .NET Core derived runtimes and .NET Framework, but in the later case the user has to manually copy the dll

This commit is contained in:
milleniumbug 2021-12-27 22:17:54 +01:00
parent 4567b0f49c
commit c11828a7d5
11 changed files with 459 additions and 214 deletions

View File

@ -1,22 +1,51 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeFileDialogSharp", "NativeFileDialogSharp\NativeFileDialogSharp.csproj", "{4127F279-9FD5-4C37-B904-242C124C1A07}"
# Visual Studio Version 17
VisualStudioVersion = 17.0.31919.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeFileDialogSharp", "NativeFileDialogSharp\NativeFileDialogSharp.csproj", "{4127F279-9FD5-4C37-B904-242C124C1A07}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeFileDialogSharpSandbox", "NativeFileDialogSharpSandbox\NativeFileDialogSharpSandbox.csproj", "{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NativeFileDialogSharpSandbox", "NativeFileDialogSharpSandbox\NativeFileDialogSharpSandbox.csproj", "{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NativeFileDialogSharpSandboxNetFramework", "NativeFileDialogSharpSandboxNetFramework\NativeFileDialogSharpSandboxNetFramework.csproj", "{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|x64.ActiveCfg = Debug|Any CPU
{4127F279-9FD5-4C37-B904-242C124C1A07}.Debug|x64.Build.0 = Debug|Any CPU
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|Any CPU.Build.0 = Release|Any CPU
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|x64.ActiveCfg = Release|Any CPU
{4127F279-9FD5-4C37-B904-242C124C1A07}.Release|x64.Build.0 = Release|Any CPU
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|x64.ActiveCfg = Debug|Any CPU
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Debug|x64.Build.0 = Debug|Any CPU
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|Any CPU.Build.0 = Release|Any CPU
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|x64.ActiveCfg = Release|Any CPU
{427E5F76-8418-4EA3-9AA3-C1DBFDE0478F}.Release|x64.Build.0 = Release|Any CPU
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Debug|x64.ActiveCfg = Debug|x64
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Debug|x64.Build.0 = Debug|x64
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Release|Any CPU.Build.0 = Release|Any CPU
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Release|x64.ActiveCfg = Release|x64
{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {124CB415-A372-4268-9C13-330A0F1093C2}
EndGlobalSection
EndGlobal

View File

@ -1,31 +1,32 @@
using System;
using System.Runtime.InteropServices;
namespace NativeFileDialogSharp.Native;
public struct nfdpathset_t
namespace NativeFileDialogSharp.Native
{
public struct nfdpathset_t
{
public IntPtr buf;
public IntPtr indices;
public UIntPtr count;
}
}
public enum nfdresult_t
{
public enum nfdresult_t
{
NFD_ERROR,
NFD_OKAY,
NFD_CANCEL
}
}
public static class NativeFunctions
{
public static class NativeFunctions
{
public const string LibraryName = "nfd";
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe nfdresult_t NFD_OpenDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe nfdresult_t NFD_OpenDialogMultiple(byte* filterList, byte* defaultPath, nfdpathset_t* outPaths);
public static extern unsafe nfdresult_t NFD_OpenDialogMultiple(byte* filterList, byte* defaultPath,
nfdpathset_t* outPaths);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe nfdresult_t NFD_SaveDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
@ -53,4 +54,44 @@ public static class NativeFunctions
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe void NFD_Free(IntPtr ptr);
}
public static class NativeFunctions32
{
public const string LibraryName = "nfd_x86";
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe nfdresult_t NFD_OpenDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe nfdresult_t NFD_OpenDialogMultiple(byte* filterList, byte* defaultPath,
nfdpathset_t* outPaths);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe nfdresult_t NFD_SaveDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe nfdresult_t NFD_PickFolder(byte* defaultPath, out IntPtr outPath);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe byte* NFD_GetError();
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe UIntPtr NFD_PathSet_GetCount(nfdpathset_t* pathSet);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe byte* NFD_PathSet_GetPath(nfdpathset_t* pathSet, UIntPtr index);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe void NFD_PathSet_Free(nfdpathset_t* pathSet);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe void NFD_Dummy();
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe IntPtr NFD_Malloc(UIntPtr bytes);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern unsafe void NFD_Free(IntPtr ptr);
}
}

View File

@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>10</LangVersion>
<PackageVersion>0.2.5</PackageVersion>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>7.3</LangVersion>
<PackageVersion>0.5.0</PackageVersion>
<Nullable>disable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageLicenseExpression>Zlib</PackageLicenseExpression>
@ -11,6 +10,7 @@
<PackageProjectUrl>https://github.com/milleniumbug/NativeFileDialogSharp</PackageProjectUrl>
<RepositoryUrl>https://github.com/milleniumbug/NativeFileDialogSharp</RepositoryUrl>
<Description>Cross-platform native file dialog controls for Windows, Linux and macOS</Description>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>

View File

@ -1,21 +1,31 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using NativeFileDialogSharp.Native;
namespace NativeFileDialogSharp;
public static class Dialog
namespace NativeFileDialogSharp
{
private static byte[] ToUtf8(string s)
public static class Dialog
{
private static readonly Encoder utf8encoder = Encoding.UTF8.GetEncoder();
private static unsafe byte[] ToUtf8(string s)
{
var byteCount = Encoding.UTF8.GetByteCount(s);
var bytes = new byte[byteCount + 1];
Encoding.UTF8.GetBytes(s.AsSpan(), bytes.AsSpan());
fixed (byte* o = bytes)
fixed (char* input = s)
{
utf8encoder.Convert(input, s.Length, o, bytes.Length, true, out _, out var _,
out var completed);
Debug.Assert(completed);
}
return bytes;
}
private static unsafe Span<byte> MakeSpanFromNullTerminatedString(byte* nullTerminatedString)
private static unsafe int GetNullTerminatedStringLength(byte* nullTerminatedString)
{
int count = 0;
var ptr = nullTerminatedString;
@ -25,12 +35,12 @@ public static class Dialog
count++;
}
return new Span<byte>(nullTerminatedString, count);
return count;
}
private static string FromUtf8(ReadOnlySpan<byte> input)
private static unsafe string FromUtf8(byte* nullTerminatedString)
{
return Encoding.UTF8.GetString(input);
return Encoding.UTF8.GetString(nullTerminatedString, GetNullTerminatedStringLength(nullTerminatedString));
}
public static unsafe DialogResult FileOpen(string filterList = null, string defaultPath = null)
@ -43,12 +53,12 @@ public static class Dialog
var result = NativeFunctions.NFD_OpenDialog(filterListNts, defaultPathNts, out IntPtr outPathIntPtr);
if (result == nfdresult_t.NFD_ERROR)
{
errorMessage = FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_GetError()));
errorMessage = FromUtf8(NativeFunctions.NFD_GetError());
}
else if (result == nfdresult_t.NFD_OKAY)
{
var outPathNts = (byte*)outPathIntPtr.ToPointer();
path = FromUtf8(MakeSpanFromNullTerminatedString(outPathNts));
path = FromUtf8(outPathNts);
NativeFunctions.NFD_Free(outPathIntPtr);
}
@ -66,12 +76,12 @@ public static class Dialog
var result = NativeFunctions.NFD_SaveDialog(filterListNts, defaultPathNts, out IntPtr outPathIntPtr);
if (result == nfdresult_t.NFD_ERROR)
{
errorMessage = FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_GetError()));
errorMessage = FromUtf8(NativeFunctions.NFD_GetError());
}
else if (result == nfdresult_t.NFD_OKAY)
{
var outPathNts = (byte*)outPathIntPtr.ToPointer();
path = FromUtf8(MakeSpanFromNullTerminatedString(outPathNts));
path = FromUtf8(outPathNts);
NativeFunctions.NFD_Free(outPathIntPtr);
}
@ -88,12 +98,12 @@ public static class Dialog
var result = NativeFunctions.NFD_PickFolder(defaultPathNts, out IntPtr outPathIntPtr);
if (result == nfdresult_t.NFD_ERROR)
{
errorMessage = FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_GetError()));
errorMessage = FromUtf8(NativeFunctions.NFD_GetError());
}
else if (result == nfdresult_t.NFD_OKAY)
{
var outPathNts = (byte*)outPathIntPtr.ToPointer();
path = FromUtf8(MakeSpanFromNullTerminatedString(outPathNts));
path = FromUtf8(outPathNts);
NativeFunctions.NFD_Free(outPathIntPtr);
}
@ -112,7 +122,7 @@ public static class Dialog
var result = NativeFunctions.NFD_OpenDialogMultiple(filterListNts, defaultPathNts, &pathSet);
if (result == nfdresult_t.NFD_ERROR)
{
errorMessage = FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_GetError()));
errorMessage = FromUtf8(NativeFunctions.NFD_GetError());
}
else if (result == nfdresult_t.NFD_OKAY)
{
@ -120,18 +130,19 @@ public static class Dialog
paths = new List<string>(pathCount);
for (int i = 0; i < pathCount; i++)
{
paths.Add(FromUtf8(MakeSpanFromNullTerminatedString(NativeFunctions.NFD_PathSet_GetPath(&pathSet, new UIntPtr((uint)i)))));
paths.Add(FromUtf8(NativeFunctions.NFD_PathSet_GetPath(&pathSet, new UIntPtr((uint)i))));
}
NativeFunctions.NFD_PathSet_Free(&pathSet);
}
return new DialogResult(result, null, paths, errorMessage);
}
}
}
}
public class DialogResult
{
public class DialogResult
{
private readonly nfdresult_t result;
public string Path { get; }
@ -153,4 +164,5 @@ public class DialogResult
Paths = paths;
ErrorMessage = errorMessage;
}
}
}

View File

@ -8,7 +8,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NativeFileDialogSharp" Version="0.2.4" />
<PackageReference Include="NativeFileDialogSharp" Version="0.5.0" />
</ItemGroup>
</Project>

View File

@ -1,14 +1,26 @@
// See https://aka.ms/new-console-template for more information
using NativeFileDialogSharp;
using System;
using NativeFileDialogSharp;
using NativeFileDialogSharp.Native;
var result = Dialog.FileOpenMultiple();
Console.WriteLine($"Path: {result.Path}, IsError {result.IsError}, IsOk {result.IsOk}, IsCancelled {result.IsCancelled}, ErrorMessage {result.ErrorMessage}");
if (result.Paths != null)
namespace NativeFileDialogSharpSandbox
{
internal class Program
{
static void Main(string[] args)
{
PrintResult(Dialog.FileOpenMultiple("pdf", null));
PrintResult(Dialog.FileOpen(null));
PrintResult(Dialog.FileSave(null));
PrintResult(Dialog.FolderPicker(null));
}
static void PrintResult(DialogResult result)
{
Console.WriteLine($"Path: {result.Path}, IsError {result.IsError}, IsOk {result.IsOk}, IsCancelled {result.IsCancelled}, ErrorMessage {result.ErrorMessage}");
if (result.Paths != null)
{
Console.WriteLine("Paths");
Console.WriteLine(string.Join("\n", result.Paths));
}
}
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
</configuration>

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0FD91168-D8F5-4776-8D91-CB9B9C55AA1B}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>NativeFileDialogSharpSandboxNetFramework</RootNamespace>
<AssemblyName>NativeFileDialogSharpSandboxNetFramework</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<LangVersion>7.3</LangVersion>
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="NativeFileDialogSharp, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NativeFileDialogSharp.0.4.4\lib\net461\NativeFileDialogSharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,26 @@
using NativeFileDialogSharp;
using System;
namespace NativeFileDialogSharpSandboxNetFramework
{
internal class Program
{
static void Main(string[] args)
{
PrintResult(Dialog.FileOpenMultiple("pdf", null));
PrintResult(Dialog.FileOpen(null));
PrintResult(Dialog.FileSave(null));
PrintResult(Dialog.FolderPicker(null));
}
static void PrintResult(DialogResult result)
{
Console.WriteLine($"Path: {result.Path}, IsError {result.IsError}, IsOk {result.IsOk}, IsCancelled {result.IsCancelled}, ErrorMessage {result.ErrorMessage}");
if (result.Paths != null)
{
Console.WriteLine("Paths");
Console.WriteLine(string.Join("\n", result.Paths));
}
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("NativeFileDialogSharpSandboxNetFramework")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NativeFileDialogSharpSandboxNetFramework")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0fd91168-d8f5-4776-8d91-cb9b9c55aa1b")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.NETCore.Platforms" version="6.0.1" targetFramework="net462" />
<package id="NativeFileDialogSharp" version="0.4.4" targetFramework="net462" />
</packages>