Skip to content

Commit 8253f74

Browse files
authored
Minor improvements in virtual device (#2990)
1 parent aa925c9 commit 8253f74

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

targets/netcore/nanoFramework.nanoCLR.CLI/ClrInstanceOperationsProcessor.cs

+15-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System;
99
using System.Collections.Generic;
1010
using System.IO;
11-
using System.Linq;
1211
using System.Net.Http;
1312
using System.Reflection;
1413
using System.Threading.Tasks;
@@ -64,12 +63,17 @@ private static async Task<ExitCode> UpdateNanoCLRAsync(
6463
}
6564
else
6665
{
67-
currentVersion = currentVersion.Substring(0, currentVersion.IndexOf("+") < 0 ? currentVersion.Length : currentVersion.IndexOf("+"));
66+
currentVersion = currentVersion.Substring(
67+
0,
68+
currentVersion.IndexOf("+") < 0 ? currentVersion.Length : currentVersion.IndexOf("+"));
6869
}
6970

7071
Version version = Version.Parse(currentVersion);
7172

72-
string nanoClrDllLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "NanoCLR", "nanoFramework.nanoCLR.dll");
73+
string nanoClrDllLocation = Path.Combine(
74+
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
75+
"NanoCLR",
76+
"nanoFramework.nanoCLR.dll");
7377

7478
_httpClient.BaseAddress = new Uri(_cloudSmithApiUrl);
7579
_httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
@@ -86,24 +90,24 @@ private static async Task<ExitCode> UpdateNanoCLRAsync(
8690

8791
var packageInfo = JsonConvert.DeserializeObject<List<CloudsmithPackageInfo>>(responseBody);
8892

89-
if (packageInfo.Count() != 1)
93+
if (packageInfo.Count != 1)
9094
{
9195
Console.WriteLine($"Error parsing latest nanoCLR version.");
9296
return ExitCode.E9005;
9397
}
9498
else
9599
{
96-
Version latestFwVersion = Version.Parse(packageInfo.ElementAt(0).Version);
100+
Version latestFwVersion = Version.Parse(packageInfo[0].Version);
97101

98102
if (latestFwVersion < version)
99103
{
100-
Console.WriteLine($"Current version {version} lower than available version {packageInfo.ElementAt(0).Version}");
104+
Console.WriteLine($"Current version {version} lower than available version {packageInfo[0].Version}");
101105
}
102106
else if (latestFwVersion > version
103107
|| (!string.IsNullOrEmpty(targetVersion)
104108
&& (Version.Parse(targetVersion) > Version.Parse(currentVersion))))
105109
{
106-
response = await _httpClient.GetAsync(packageInfo.ElementAt(0).DownloadUrl);
110+
response = await _httpClient.GetAsync(packageInfo[0].DownloadUrl);
107111
response.EnsureSuccessStatusCode();
108112

109113
// need to unload the DLL before updating it
@@ -113,15 +117,15 @@ private static async Task<ExitCode> UpdateNanoCLRAsync(
113117
await using var fs = File.OpenWrite(nanoClrDllLocation);
114118

115119
ms.Seek(0, SeekOrigin.Begin);
116-
ms.CopyTo(fs);
120+
await ms.CopyToAsync(fs);
117121

118-
fs.Flush();
122+
await fs.FlushAsync();
119123

120-
Console.WriteLine($"Updated to v{packageInfo.ElementAt(0).Version}");
124+
Console.WriteLine($"Updated to v{packageInfo[0].Version}");
121125
}
122126
else
123127
{
124-
Console.WriteLine($"Already at v{packageInfo.ElementAt(0).Version}");
128+
Console.WriteLine($"Already at v{packageInfo[0].Version}");
125129
}
126130

127131
return ExitCode.OK;

targets/netcore/nanoFramework.nanoCLR.Host/Interop/nanoCLR.cs

+37-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
//
55

66
using System;
7+
using System.Diagnostics;
78
using System.IO;
9+
using System.Linq;
810
using System.Runtime.InteropServices;
911

1012
namespace nanoFramework.nanoCLR.Host.Interop
@@ -97,33 +99,52 @@ public static void UnloadNanoClrImageDll()
9799
{
98100
string nanoClrDllLocation = Path.Combine(DllPath, _nanoClrDllName);
99101

100-
foreach (System.Diagnostics.ProcessModule mod in System.Diagnostics.Process.GetCurrentProcess().Modules)
102+
var modules = Process.GetCurrentProcess().Modules.Cast<ProcessModule>()
103+
.Where(mod => mod.FileName.Equals(nanoClrDllLocation, StringComparison.OrdinalIgnoreCase));
104+
105+
foreach (var mod in modules)
101106
{
102-
if (mod.FileName.Equals(nanoClrDllLocation, StringComparison.OrdinalIgnoreCase))
107+
try
103108
{
104-
FreeLibrary(mod.BaseAddress);
105-
106-
// done here!
107-
break;
109+
if (FreeLibrary(mod.BaseAddress))
110+
{
111+
// Successfully unloaded the DLL
112+
break;
113+
}
114+
else
115+
{
116+
// Handle the error if FreeLibrary fails
117+
int errorCode = Marshal.GetLastWin32Error();
118+
Console.WriteLine($"Failed to unload nanoCLR DLL. Error code: {errorCode}");
119+
}
120+
}
121+
catch (Exception ex)
122+
{
123+
// Handle any exceptions that occur during the unload process
124+
Console.WriteLine($"Exception occurred while unloading nanoCLR DLL: {ex.Message}");
108125
}
109126
}
110127
}
111128

112129
public static string FindNanoClrDll()
113130
{
114-
// perform dummy call to load DLL, in case it's not loaded
115-
_ = nanoCLR_GetVersion();
131+
try
132+
{
133+
// Perform dummy call to load DLL, in case it's not loaded
134+
_ = nanoCLR_GetVersion();
116135

117-
// sweep processes and look for a DLL with the nanoCLR namme
118-
foreach (System.Diagnostics.ProcessModule mod in System.Diagnostics.Process.GetCurrentProcess().Modules)
136+
// Sweep processes and look for a DLL with the nanoCLR name
137+
var module = Process.GetCurrentProcess().Modules.Cast<ProcessModule>()
138+
.FirstOrDefault(mod => mod.FileName.EndsWith(_nanoClrDllName, StringComparison.OrdinalIgnoreCase));
139+
140+
return module?.FileName ?? string.Empty;
141+
}
142+
catch (Exception ex)
119143
{
120-
if (mod.FileName.EndsWith(_nanoClrDllName, StringComparison.OrdinalIgnoreCase))
121-
{
122-
return mod.FileName;
123-
}
144+
// Handle any exceptions that occur during the process
145+
Console.WriteLine($"Exception occurred while finding nanoCLR DLL: {ex.Message}");
146+
return string.Empty;
124147
}
125-
126-
return "";
127148
}
128149

129150
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]

targets/netcore/nanoFramework.nanoCLR.Host/NanoClrHostBuilder.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,8 @@ public nanoCLRHost Build()
130130
return s_nanoClrHost;
131131
}
132132

133-
public void UnloadNanoClrDll()
134-
{
135-
Interop.nanoCLR.UnloadNanoClrImageDll();
136-
}
133+
public void UnloadNanoClrDll() => Interop.nanoCLR.UnloadNanoClrImageDll();
137134

138-
public void OutputNanoClrDllInfo()
139-
{
140-
Console.WriteLine($"nanoCLR loaded from '{Interop.nanoCLR.FindNanoClrDll()}'");
141-
}
135+
public void OutputNanoClrDllInfo() => Console.WriteLine($"nanoCLR loaded from '{Interop.nanoCLR.FindNanoClrDll()}'");
142136
}
143137
}

0 commit comments

Comments
 (0)