Get .NET Core DLL version on Linux
I use exiftool
:
$ exiftool /usr/lib64/dotnet/sdk/2.0.3/Microsoft/Microsoft.NET.Build.Extensions/tools/netcoreapp1.0/System.Threading.dll | grep -i version
ExifTool Version Number : 10.55
Linker Version : 48.0
OS Version : 4.0
Image Version : 0.0
Subsystem Version : 4.0
File Version Number : 4.6.24705.1
Product Version Number : 0.0.0.0
File Version : 4.6.24705.01
Product Version : 4.6.24705.01. Commit Hash: 4d1af962ca0fede10beb01d197367c2f90e92c97
Assembly Version : 4.0.12.0
Originally discovered from here.
Watch out, though. It doesn't work on crossgen
ed stuff:
$ exiftool /usr/lib64/dotnet/sdk/2.0.3/Roslyn/Microsoft.CodeAnalysis.dll
ExifTool Version Number : 10.55
File Name : Microsoft.CodeAnalysis.dll
Directory : /usr/lib64/dotnet/sdk/2.0.3/Roslyn
File Size : 4.6 MB
File Modification Date/Time : 2018:01:24 13:12:48-05:00
File Access Date/Time : 2018:02:01 12:37:59-05:00
File Inode Change Date/Time : 2018:01:26 09:52:23-05:00
File Permissions : rw-r--r--
Error : File format error
Get dll metadata in Linux environment
Using AssemblyMetadata
from Microsoft.CodeAnalysis.Common
package you can read the content of a dll like Version, Module, Type, Reference, Property, and pretty much everything
var path = @"path/to/dll/file.dll";
var metadata = AssemblyMetadata.CreateFromFile(path);
var module = metadata.GetModules().First();
Console.WriteLine(module.Name);
var reader = module.GetMetadataReader();
var assemblyDef = reader.GetAssemblyDefinition();
Console.WriteLine(reader.GetString(assemblyDef.Name));
Console.WriteLine(assemblyDef.Version.ToString());
foreach (var typeDefHandle in reader.TypeDefinitions)
{
var typeDef = reader.GetTypeDefinition(typeDefHandle);
var fullName = (reader.GetString(typeDef.Namespace) + "::" + reader.GetString(typeDef.Name));
Console.WriteLine(fullName);
}
Linux .NET 6 exe seeks DLL in current dir instead of its own
Turns out the correct link command was:
ln -s /home/UserName/Dev/ProgramName/bin/Debug/net6.0/ProgramName ProgramName
(confusion resulting from the order being swapped relative to Windows's symbolic link command line)
Is there a Linux equivalent module and function for LoadLibrary() from kernel32.dll that i can call from .Net Core?
A couple of thoughts.
First, yes, DllImport
(P/Invoke) works on Linux (and macOS) too.
This seems a bit strange:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate short AdcOpen([MarshalAs(UnmanagedType.LPStr)] string adcName, [MarshalAs(UnmanagedType.LPStr)] string protocol, [MarshalAs(UnmanagedType.LPStr)] string port, ref short handle, byte performSwReset);
private AdcOpen adcOpen;
With the mapping code (LoadLibrary
, GetProcAddress
, FreeLibrary
), this seems like you are manually doing the work that .NET itself does if you do something like this - where you let .NET do the function finding and binding:
[DllImport("ourdevice.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern short AdcOpen([MarshalAs(UnmanagedType.LPStr)] string adcName, [MarshalAs(UnmanagedType.LPStr)] string protocol, [MarshalAs(UnmanagedType.LPStr)] string port, ref short handle, byte performSwReset);
Any reason for doing it so differently?
Because if you were to do it that way, I expect the Linux version will look like this and work pretty much the same way as it does on Windows:
[DllImport("libourdevice.so", CallingConvention = CallingConvention.Cdecl)]
private static extern short AdcOpen([MarshalAs(UnmanagedType.LPStr)] string adcName, [MarshalAs(UnmanagedType.LPStr)] string protocol, [MarshalAs(UnmanagedType.LPStr)] string port, ref short handle, byte performSwReset);
Assuming you are using the same calling convention and argument marshalling.
The only difference is the name change from "ourdevice.dll"
to "libdevice.so"
. You will also need to place libdevice.so
somewhere where it can be found; next to the application executable should work.
If you still want to do it manually via the LoadLibrary
/GetProcAddress
/FreeLibrary
approach, you can use the roughly-equivalent methods on Linux:
dlopen
(similar toLoadLibrary
)dlsym
(similar toGetProcAddress
)dlclose
(similar toFreeLibrary
)
See https://stackoverflow.com/a/13492645/3561275 for examples and sample code on how to use these methods.
How to check the version of an assembly (dll)?
You can use Reflector, ILDASM or ILSpy to get the assembly version.
You usually can find ILDASM in C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\ildasm.exe
(where v8.1A
is the version of the Windows SDK installed).
ILDASM:
Reflector:
Related Topics
.Htaccess Redirect Index.PHP to /
Prevent Fork() from Copying Sockets
Linking a Static Library into a Shared Library
Shell Script to Check Ubuntu Version and Then Copy Files
How to Setup Cron Job on Amazon Linux Ami
Cannot Run 32-Bit Apps on 64-Bit Linux
How to Install Docker 1.9+ in Centos 6.5
Passing an Array as Command Line Argument for Linux Kernel Module
Programmatically Disable Hardware Prefetching on Amd Systems
How to Use Awk to Test If a Column Value Is in Another File
"Hello World" Function Without Using C Printf
Linux: Find a List of Files in a Dictionary Recursively
Stop a Running Dotnet Core Website Running on Kestrel