Skip to content

Commit

Permalink
Correction to load the correct native module without forcing exceptio…
Browse files Browse the repository at this point in the history
…ns (#126)

Co-authored-by: Paulo Ferro <[email protected]>
  • Loading branch information
POFerro and Paulo Ferro authored Apr 4, 2024
1 parent c9bd7f8 commit fbcac19
Show file tree
Hide file tree
Showing 3 changed files with 424 additions and 80 deletions.
140 changes: 60 additions & 80 deletions src/WkHtmlToPdf-DotNet/ModuleFactory.cs
Original file line number Diff line number Diff line change
@@ -1,83 +1,63 @@
using System;
using System.Runtime.InteropServices;

namespace WkHtmlToPdfDotNet
{
internal static class ModuleFactory
{
public static IWkHtmlModule GetModule()
{
#if NETSTANDARD2_0
// Windows allows us to probe for either 64 or 86 bit versions
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
# endif
try
{
// Try 64-bit
VersionWin64();

return new WkHtmlModule();
}
catch
{
}

try
{
// Try 86-bit
VersionWin86();

return new WkHtmlModule();
}
catch
{
}
#if NETSTANDARD2_0
}
else
{
try
{
return new WkHtmlModuleLinux64();
}
catch
{
}

try
{
return new WkHtmlModuleLinux86();
}
catch
{
}
using System;
using System.Runtime.InteropServices;

try
{
return new WkHtmlModuleLinuxArm64();
}
catch
{
}
namespace WkHtmlToPdfDotNet
{
internal static class ModuleFactory
{
public static IWkHtmlModule GetModule()
{
try
{
#if NETSTANDARD2_0
// Windows allows us to probe for either 64 or 86 bit versions
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (RuntimeInformation.ProcessArchitecture == Architecture.X64)
{
#else
if (IntPtr.Size == 8) // Is64 bit Arch
{
#endif
return new WkHtmlModuleWin64();
}
else
{
return new WkHtmlModuleWin86();
}
#if NETSTANDARD2_0
}
else
{
switch (RuntimeInformation.ProcessArchitecture)
{
case Architecture.X64:
return new WkHtmlModuleLinux64();
case Architecture.X86:
return new WkHtmlModuleLinux86();
case Architecture.Arm:
case Architecture.Arm64:
return new WkHtmlModuleLinuxArm64();
default: // Unreachable
return new WkHtmlModule();
}
}
#endif
}
#endif
catch (Exception)
{
}

// Also try to load it with the method that should use the deps file
try
{
return new WkHtmlModule();
}
catch
{
}

throw new NotSupportedException("Unable to load native library. The platform may be missing native dependencies (libjpeg62, etc). Or the current platform is not supported.");
}

[DllImport(@"runtimes\win-x64\native\wkhtmltox", CharSet = CharSet.Unicode, EntryPoint = "wkhtmltopdf_version")]
public static extern IntPtr VersionWin64();

[DllImport(@"runtimes\win-x86\native\wkhtmltox", CharSet = CharSet.Unicode, EntryPoint = "wkhtmltopdf_version")]
public static extern IntPtr VersionWin86();
}
}
try
{
return new WkHtmlModule();
}
catch
{
}

throw new NotSupportedException("Unable to load native library. The platform may be missing native dependencies (libjpeg62, etc). Or the current platform is not supported.");
}
}
}
182 changes: 182 additions & 0 deletions src/WkHtmlToPdf-DotNet/WkHtmlModuleWin64.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using System;
using System.Runtime.InteropServices;

namespace WkHtmlToPdfDotNet
{
internal class WkHtmlModuleWin64 : IWkHtmlModule
{
public WkHtmlModuleWin64()
{
// Test
Version();
}

public int ExtendedQT() => wkhtmltopdf_extended_qt();

public IntPtr Version() => wkhtmltopdf_version();

public int Init(int useGraphics) => wkhtmltopdf_init(useGraphics);

public int DeInit() => wkhtmltopdf_deinit();

public IntPtr CreateGlobalSettings() => wkhtmltopdf_create_global_settings();

public int SetGlobalSetting(IntPtr settings, [MarshalAs((short)CustomUnmanagedType.LPUTF8Str)] string name, [MarshalAs((short)CustomUnmanagedType.LPUTF8Str)] string value) => wkhtmltopdf_set_global_setting(settings, name, value);

public int GetGlobalSetting(IntPtr settings, [MarshalAs((short)CustomUnmanagedType.LPUTF8Str)] string name, IntPtr value, int valueSize) => wkhtmltopdf_get_global_setting(settings, name, value, valueSize);

public int DestroyGlobalSettings(IntPtr settings) => wkhtmltopdf_destroy_global_settings(settings);

public IntPtr CreateObjectSettings() => wkhtmltopdf_create_object_settings();

public int SetObjectSetting(IntPtr settings, [MarshalAs((short)CustomUnmanagedType.LPUTF8Str)] string name, [MarshalAs((short)CustomUnmanagedType.LPUTF8Str)] string value) => wkhtmltopdf_set_object_setting(settings, name, value);

public int GetObjectSetting(IntPtr settings,
[MarshalAs((short)CustomUnmanagedType.LPUTF8Str)]
string name, IntPtr value, int valueSize) => wkhtmltopdf_get_object_setting(settings, name, value, valueSize);

public int DestroyObjectSettings(IntPtr settings) => wkhtmltopdf_destroy_object_settings(settings);

public IntPtr CreateConverter(IntPtr globalSettings) => wkhtmltopdf_create_converter(globalSettings);

public void AddObject(IntPtr converter,
IntPtr objectSettings,
byte[] data) => wkhtmltopdf_add_object(converter, objectSettings, data);

public void AddObject(IntPtr converter,
IntPtr objectSettings,
[MarshalAs((short)CustomUnmanagedType.LPUTF8Str)] string data) => wkhtmltopdf_add_object(converter, objectSettings, data);

public bool Convert(IntPtr converter) => wkhtmltopdf_convert(converter);

public void DestroyConverter(IntPtr converter) => wkhtmltopdf_destroy_converter(converter);

public int GetOutput(IntPtr converter, out IntPtr data) => wkhtmltopdf_get_output(converter, out data);

public int SetPhaseChangedCallback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] VoidCallback callback) => wkhtmltopdf_set_phase_changed_callback(converter, callback);

public int SetProgressChangedCallback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] VoidCallback callback) => wkhtmltopdf_set_progress_changed_callback(converter, callback);

public int SetFinishedCallback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] IntCallback callback) => wkhtmltopdf_set_finished_callback(converter, callback);

public int SetWarningCallback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] StringCallback callback) => wkhtmltopdf_set_warning_callback(converter, callback);

public int SetErrorCallback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] StringCallback callback) => wkhtmltopdf_set_error_callback(converter, callback);

public int PhaseCount(IntPtr converter) => wkhtmltopdf_phase_count(converter);

public int CurrentPhase(IntPtr converter) => wkhtmltopdf_current_phase(converter);

public IntPtr PhaseDescription(IntPtr converter, int phase) => wkhtmltopdf_phase_description(converter, phase);

public IntPtr ProgressString(IntPtr converter) => wkhtmltopdf_progress_string(converter);

public int HttpErrorCode(IntPtr converter) => wkhtmltopdf_http_error_code(converter);

const string DLLNAME = "runtimes\\win-x64\\native\\wkhtmltox";

const CharSet CHARSET = CharSet.Unicode;

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_extended_qt();

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern IntPtr wkhtmltopdf_version();

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_init(int useGraphics);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_deinit();

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern IntPtr wkhtmltopdf_create_global_settings();

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_set_global_setting(IntPtr settings,
[MarshalAs((short)CustomUnmanagedType.LPUTF8Str)]
string name,
[MarshalAs((short)CustomUnmanagedType.LPUTF8Str)]
string value);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_get_global_setting(IntPtr settings,
[MarshalAs((short)CustomUnmanagedType.LPUTF8Str)]
string name,
IntPtr value, int valueSize);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_destroy_global_settings(IntPtr settings);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern IntPtr wkhtmltopdf_create_object_settings();

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_set_object_setting(IntPtr settings,
[MarshalAs((short)CustomUnmanagedType.LPUTF8Str)]
string name,
[MarshalAs((short)CustomUnmanagedType.LPUTF8Str)]
string value);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_get_object_setting(IntPtr settings,
[MarshalAs((short)CustomUnmanagedType.LPUTF8Str)]
string name,
IntPtr value, int valueSize);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_destroy_object_settings(IntPtr settings);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern IntPtr wkhtmltopdf_create_converter(IntPtr globalSettings);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern void wkhtmltopdf_add_object(IntPtr converter,
IntPtr objectSettings,
byte[] data);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern void wkhtmltopdf_add_object(IntPtr converter,
IntPtr objectSettings,
[MarshalAs((short)CustomUnmanagedType.LPUTF8Str)] string data);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern bool wkhtmltopdf_convert(IntPtr converter);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern void wkhtmltopdf_destroy_converter(IntPtr converter);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_get_output(IntPtr converter, out IntPtr data);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_set_phase_changed_callback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] VoidCallback callback);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_set_progress_changed_callback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] VoidCallback callback);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_set_finished_callback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] IntCallback callback);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_set_warning_callback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] StringCallback callback);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_set_error_callback(IntPtr converter, [MarshalAs(UnmanagedType.FunctionPtr)] StringCallback callback);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_phase_count(IntPtr converter);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_current_phase(IntPtr converter);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern IntPtr wkhtmltopdf_phase_description(IntPtr converter, int phase);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern IntPtr wkhtmltopdf_progress_string(IntPtr converter);

[DllImport(DLLNAME, CharSet = CHARSET)]
public static extern int wkhtmltopdf_http_error_code(IntPtr converter);
}
}
Loading

0 comments on commit fbcac19

Please sign in to comment.