-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
129 additions
and
90 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Ivi.Visa.ConflictManager; | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace IviVisaNetSample | ||
{ | ||
#if NET5_0_OR_GREATER | ||
/// <summary> | ||
/// Class used to load .NET Framework assemblies located in GAC from .NET 5+ | ||
/// Requred only for expiremental using VISA.NET library in .NET 5+ | ||
/// </summary> | ||
internal static class GacLoader | ||
{ | ||
/// <summary> | ||
/// Load an assembly from the GAC. | ||
/// </summary> | ||
/// <param name="assemblyName"></param> | ||
/// <returns>Loaded assembly</returns> | ||
/// <exception cref="FileNotFoundException"></exception> | ||
public static Assembly Load(AssemblyName assemblyName) | ||
{ | ||
var gacPaths = new[] | ||
{ | ||
$"{Environment.GetFolderPath(Environment.SpecialFolder.Windows)}\\Microsoft.NET\\assembly\\GAC_MSIL\\{assemblyName.Name}", | ||
$"{Environment.GetFolderPath(Environment.SpecialFolder.Windows)}\\assembly\\GAC_MSIL\\{assemblyName.Name}", | ||
}; | ||
|
||
foreach (var folder in gacPaths.Where(f => Directory.Exists(f))) | ||
{ | ||
foreach (var subfolder in Directory.EnumerateDirectories(folder)) | ||
{ | ||
if (subfolder.Contains(Convert.ToHexString(assemblyName.GetPublicKeyToken()), StringComparison.OrdinalIgnoreCase) | ||
&& subfolder.Contains(assemblyName.Version.ToString(), StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
var assemblyPath = Path.Combine(subfolder, assemblyName.Name + ".dll"); | ||
if (File.Exists(assemblyPath)) | ||
return Assembly.LoadFrom(assemblyPath); | ||
} | ||
} | ||
} | ||
throw new FileNotFoundException($"Assembly {assemblyName} not found."); | ||
} | ||
|
||
/// <summary> | ||
/// Preloading installed VISA implementation assemblies for NET 5+ | ||
/// </summary> | ||
public static void LoadInstalledVisaAssemblies() { | ||
var installedVisas = new ConflictManager().GetInstalledVisas(ApiType.DotNet); | ||
foreach (var visaLibrary in installedVisas) | ||
{ | ||
try | ||
{ | ||
var inst = GacLoader.Load(new AssemblyName(visaLibrary.Location.Substring(visaLibrary.Location.IndexOf(",") + 1))); | ||
Console.WriteLine($"Loaded assembly \"{visaLibrary.FriendlyName}\"."); | ||
} | ||
catch (Exception exception) | ||
{ | ||
Console.WriteLine($"Failed to load assembly \"{visaLibrary.FriendlyName}\": {exception.Message}"); | ||
} | ||
} | ||
} | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{181CDD33-8577-4F46-AF93-2A02660E51AC}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>IviVisaNetSample</RootNamespace> | ||
<AssemblyName>IviVisaNetSample</AssemblyName> | ||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
<TargetFrameworkProfile /> | ||
<TargetFrameworks>net48;net5.0;net8.0</TargetFrameworks> | ||
<Platforms>AnyCPU;x64;x86</Platforms> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<Prefer32Bit>false</Prefer32Bit> | ||
</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> | ||
<Prefer32Bit>false</Prefer32Bit> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Kelary.Ivi.Visa" Version="7.*" /> | ||
<PackageReference Include="Kelary.Ivi.Visa" Version="7.2.0" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters