Skip to content

Commit d50f6b5

Browse files
authored
Merge pull request #7 from dhohmeyer/LoadLibraryErrorHandling
Load Library Error Handling
2 parents 233ed93 + 77e821d commit d50f6b5

5 files changed

Lines changed: 63 additions & 7 deletions

File tree

FastText.NetWrapper/FastText.NetWrapper.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>FastText.NetWrapper</RootNamespace>
1111
<AssemblyName>FastText.NetWrapper</AssemblyName>
12-
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
1617
<DebugSymbols>true</DebugSymbols>

FastText.NetWrapper/FastTextWrapper.LoadLibrary.cs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Reflection;
45
using System.Runtime.InteropServices;
56
using FastText.NetWrapper.Logging;
@@ -39,7 +40,13 @@ private static void LoadLibrary(string path)
3940
{
4041
_log.Info($"Directly loading {path}...");
4142
var result = LoadLibraryEx(path, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_SYSTEM32 | LoadLibraryFlags.LOAD_LIBRARY_SEARCH_USER_DIRS);
42-
_log.Info(result == IntPtr.Zero ? "FAILED!" : "Success");
43+
if (result == IntPtr.Zero)
44+
{
45+
var error = Marshal.GetLastWin32Error();
46+
_log.Error($"FAILED! Last Win32 error is: {error}");
47+
throw new Exception($"Failed to load library with path \"{path}\"");
48+
}
49+
_log.Info("Successfully loaded library.");
4350
}
4451

4552
private static string UnpackResources()
@@ -66,9 +73,46 @@ private static void UnpackFile(string curDir, string fileName, byte[] bytes)
6673
{
6774
var path = !string.IsNullOrEmpty(curDir) ? Path.Combine(curDir, fileName) : fileName;
6875

69-
_log.Info($"Unpacking {fileName}.");
76+
try
77+
{
78+
if (File.Exists(path))
79+
{
80+
var existingFileContents = File.ReadAllBytes(path);
81+
if (existingFileContents.Length == bytes.Length)
82+
{
83+
if (existingFileContents.SequenceEqual(bytes))
84+
{
85+
_log.Info($"File {path} already exists and is the same (length and contents)");
86+
return;
87+
}
88+
}
89+
}
90+
}
91+
catch (Exception e)
92+
{
93+
_log.Warn($"Unable to determine size of existing file, will replace with our version. Message: {e.Message}");
94+
try
95+
{
96+
File.Delete(path);
97+
}
98+
catch (Exception deleteException)
99+
{
100+
_log.Error($"Unable to delete existing file: {path}. Message: {deleteException.Message}");
101+
return;
102+
}
103+
}
104+
105+
_log.Info($"Unpacking {fileName} to path {curDir}.");
70106

71-
File.WriteAllBytes(path, bytes);
107+
try
108+
{
109+
File.WriteAllBytes(path, bytes);
110+
}
111+
catch (Exception writeException)
112+
{
113+
_log.Error($"Unable to write: {path}. Message: {writeException.Message}");
114+
throw;
115+
}
72116
}
73117

74118
#region LoadLibraryEx

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# FastText.NetWrapper
2+
3+
## Table of contents
4+
5+
* [Requirements](#requirements)
6+
7+
## Requirements
8+
9+
To run on a Windows host (.NET Framework), the host must have VS Runtime Version 140 installed. Visit the MS Downloads page (https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads) and select the appropriate redistributable.
10+

TestUtil/App.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<?xml version="1.0" encoding="utf-8" ?>
1+
<?xml version="1.0" encoding="utf-8" ?>
22
<configuration>
33
<startup>
4-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
55
</startup>
66
</configuration>

TestUtil/TestUtil.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
<OutputType>Exe</OutputType>
99
<RootNamespace>TestUtil</RootNamespace>
1010
<AssemblyName>TestUtil</AssemblyName>
11-
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
11+
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
1617
<DebugSymbols>true</DebugSymbols>

0 commit comments

Comments
 (0)