Skip to content

Commit

Permalink
Get working on Windows
Browse files Browse the repository at this point in the history
This will break Linux because of the dll name change.

There isn't a DllMap in .NET Core, so there isn't a nice way to work around this.
See: https://github.com/dotnet/coreclr/issues/930
  • Loading branch information
dlech committed Jun 2, 2018
1 parent b2a5bc9 commit ff36338
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
15 changes: 15 additions & 0 deletions example/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Dandy.GPG.Assuan;
using Dandy.GPG.Rt;

Expand Down Expand Up @@ -53,8 +54,22 @@ static void commandHandler(Fd fd)
}
}

[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern int AddDllDirectory(string NewDirectory);

static void Main(string[] args)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
if (RuntimeInformation.ProcessArchitecture != Architecture.X86) {
throw new NotSupportedException("Must run as 32-bit process");
}
var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
foreach (var dir in new [] { @"Gpg4win\bin", @"GnuPG\bin" }) {
var path = Path.Combine(programFiles, dir);
AddDllDirectory(path);
}
}

Runtime.Init();

if (args.Length > 0) {
Expand Down
1 change: 1 addition & 0 deletions example/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RuntimeIdentifiers>win7-x86;unix</RuntimeIdentifiers>
</PropertyGroup>

<PropertyGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/Assuan/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace Dandy.GPG.Assuan
{
static class Global
{
internal const string AssuanLibrary = "assuan";
internal const string GPGRuntimeLibrary = "gpg-error";
internal const string AssuanLibrary = "libassuan-0";
}
}
8 changes: 4 additions & 4 deletions src/Rt/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct Error

public ErrorCode Code => (ErrorCode)(error & codeMask);

[DllImport(GPGRuntimeLibrary, CallingConvention = CallingConvention.Cdecl)]
[DllImport(RuntimeLibrary, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr gpg_strerror(uint err);

public string CodeDescription {
Expand All @@ -30,7 +30,7 @@ public string CodeDescription {

public ErrorSource Source => (ErrorSource)((error >> sourceShift) & sourceMask);

[DllImport(GPGRuntimeLibrary, CallingConvention = CallingConvention.Cdecl)]
[DllImport(RuntimeLibrary, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr gpg_strsource(uint err);

public string SourceDescription {
Expand Down Expand Up @@ -74,10 +74,10 @@ public void Assert()
}
}

[DllImport(GPGRuntimeLibrary, EntryPoint = "gpg_err_code_from_errno", CallingConvention = CallingConvention.Cdecl)]
[DllImport(RuntimeLibrary, EntryPoint = "gpg_err_code_from_errno", CallingConvention = CallingConvention.Cdecl)]
public static extern ErrorCode CodeFromErrno(int err);

[DllImport(GPGRuntimeLibrary, EntryPoint = "gpg_err_code_to_errno", CallingConvention = CallingConvention.Cdecl)]
[DllImport(RuntimeLibrary, EntryPoint = "gpg_err_code_to_errno", CallingConvention = CallingConvention.Cdecl)]
public static extern int CodeToErrno(ErrorCode code);
}
}
6 changes: 3 additions & 3 deletions src/Rt/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Dandy.GPG.Rt
{
public static class Runtime
{
internal const string GPGRuntimeLibrary = "gpg-error";
internal const string RuntimeLibrary = "libgpg-error-0";

[DllImport(GPGRuntimeLibrary, CallingConvention = CallingConvention.Cdecl)]
[DllImport(RuntimeLibrary, CallingConvention = CallingConvention.Cdecl)]
static extern Error gpg_err_init();

public static void Init()
Expand All @@ -19,7 +19,7 @@ public static void Init()
err.Assert();
}

[DllImport(GPGRuntimeLibrary, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[DllImport(RuntimeLibrary, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr gpgrt_check_version(string rec_version);

public static string CheckVersion(string requiredVersion)
Expand Down

0 comments on commit ff36338

Please sign in to comment.