martinkuschnik / wmilight Goto Github PK
View Code? Open in Web Editor NEWA simple and light wmi framework.
License: MIT License
A simple and light wmi framework.
License: MIT License
Does this library limit enumeration options such as EnumerateDeep, Rewindable, Timeout, etc?
An example of the assertion: "The .Net framework implementation has one big problem. It leaks a little bit memory on each remote operation." would be valuable for persuading my colleagues.
I did try a simple test (ManagementObject managementObject in searcher.Get()) with 1.8M iterations. I only saw only a small change in Peak Working Set and GC Total Memory. What metrics should I be tracking?
my code:
string query = string.Format(
"SELECT * from Win32_Printer WHERE Name = '{0}'",
printerName);
using WmiConnection con = new WmiConnection();
ushort status = (ushort)PrinterStatus.Unknown;
foreach (WmiObject process in con.CreateQuery(query))
{
status = (ushort)process["PrinterStatus"];
}
return (PrinterStatus)status;
the error:
Common Language Runtime detected an invalid program. The body of method 'Void WmiLight.Wbem.WbemLocator..ctor()' is invalid.
System.InvalidProgramException: Common Language Runtime detected an invalid program. The body of method 'Void WmiLight.Wbem.WbemLocator..ctor()' is invalid.
at Internal.Runtime.TypeLoaderExceptionHelper.CreateInvalidProgramException(ExceptionStringID, String) + 0x40
at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowInvalidProgramExceptionWithArgument(ExceptionStringID, String) + 0x9
at WmiLight.Wbem.WbemLocator..ctor() + 0x15
at WmiLight.WmiConnection.Open() + 0x43
at WmiLight.WmiConnection.InternalExecuteQuery(WmiQuery query) + 0x39
at WmiLight.WmiConnection.ExecuteQuery(WmiQuery query) + 0x2c
csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<InvariantGlobalization>true</InvariantGlobalization>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<PublishAot>true</PublishAot>
</PropertyGroup>
<ItemGroup>
<TrimmerRootAssembly Include="myassemblyname" />
<TrimmerRootAssembly Include="WmiLight" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.EventLog" Version="3.1.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="8.0.0" />
<PackageReference Include="WmiLight" Version="4.0.0" />
</ItemGroup>
</Project>
Built-in COM support is required for WmiLight but is not trim compatible.
I know this was discussed in #7, though despite the OP of that issue saying that enabling BuiltInComInteropSupport
allowed their app to work with full trimming, in my app I'm getting the following exception:
Exception Info: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Stack:
at WmiLight.Wbem.IWbemServices.ExecQuery(System.String, System.String, WmiLight.Wbem.WbemClassObjectEnumeratorBehaviorOption, WmiLight.Wbem.IWbemContext, WmiLight.Wbem.IWbemClassObjectEnumerator ByRef)
at WmiLight.Wbem.IWbemServices.ExecQuery(System.String, System.String, WmiLight.Wbem.WbemClassObjectEnumeratorBehaviorOption, WmiLight.Wbem.IWbemContext, WmiLight.Wbem.IWbemClassObjectEnumerator ByRef)
at WmiLight.Wbem.IWbemServicesExtensions.ExecQuery(WmiLight.Wbem.IWbemServices, System.String, WmiLight.Wbem.WbemClassObjectEnumeratorBehaviorOption, WmiLight.Wbem.IWbemContext)
at WmiLight.WmiConnection.InternalExecuteQuery(WmiLight.WmiQuery)
at WmiLight.WmiConnection.ExecuteQuery(WmiLight.WmiQuery)
and when compiling I get the warning:
ILLink : Trim analysis warning IL2026: Internal.Runtime.InteropServices.ComActivator.GetClassFactoryForTypeInternal(ComActivationContextInternal*): Using member 'Internal.Runtime.InteropServices.ComActivator.GetClassFactoryForType(ComActivationContext)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Built-in COM support is not trim compatible. https://aka.ms/dotnet-illink/com
It does work fine with TrimMode
set to partial, however this is not ideal.
At the link given in the trim warning, there is a link to this page which discusses using the ComWrappers
api which is Trimming/AOT friendly.
I may try to implement this myself, but I see in #9 that a version 4.0 is in the works so I will wait for that before investigating further.
I've attached a screenshot of query output from both System.Management WMI interaction and WmiLight WMI interaction and you can see that WmiLight uses int
for all integral types while System.Management uses other types like uint
, ushort
, etc. This means you get an InvalidCastException
when casting and if you use the .GetPropertyValue<T>
overload, the same thing occurs.
I looked at the code and it's not obvious to me where the int
is materializing, but it does mean that WmiLight is difficult to drop in as a System.Management replacement. Additionally, in a case like int
vs uint
or int
vs long
, it may cause semantic problems - though it seems like the library simply uses string
for long
. I'd like to use this library for its support for trimming; Microsoft.Management.Infrastructure sucks in not only the same ways that System.Management does, but also requires a very specific RuntimeIdentifier
.
Hey.
Came across your library on this discussion and decided to give it a try and it worked quite nicely. Only noticed at the last second that it's GPL licensed. Any chance you could use a more permissive license like MIT or Apache 2.0?
... rather than when used directly in the main program.
Note: I am using dotnet 8.0.202
Actually, the only reason I was eventually able to get a simple WmiLight program to work, was by setting PublishWmiLightStaticallyLinked
to true in the main project.
Unfortunately, I don't seem to be able to get this to work when the WMI code is in a dependent library (setting the attribute either in the main project or library project has no discernable impact).
dotnet new globaljson --sdk-version 8.0.202 --force
dotnet new console -n Main
dotnet new classlib -n Lib
dotnet add Main reference Lib
dotnet add Lib package WmiLight
We set IsAotCompatible
(or just IsTrimmable
) in both Main
and Lib
, but we set PublishAot
in the Main
project alone (I don't think the latter have any effect if set in the library).
Sadly, setting PublishWmiLightStaticallyLinked
in either the Main
, the Lib
, or both, has no effect.
The code in Lib/ClassLib.cs
:
using WmiLight;
namespace Lib;
public class Class1
{
public static void GetNetworkAddresses()
{
using (WmiConnection con = new WmiConnection("root\\StandardCimv2"))
{
foreach (WmiObject managementObject in con.CreateQuery("SELECT PermanentAddress FROM MSFT_NetAdapter"))
{
var permanentAddress = managementObject["PermanentAddress"] as string;
if (!string.IsNullOrEmpty(permanentAddress))
{
Console.WriteLine($" {permanentAddress.ToLower()}");
}
}
}
}
}
The code in Main/Program.cs
:
Lib.Class1.GetNetworkAddresses();
The command to publish
:
$ dotnet publish Main
and to run:
$ Main/bin/Release/net8.0/win-x64/publish/Main.exe
14cc19d330a0
025049000021
18dc18e3301c
But the WmiLight.Native.dll
is never substituted for the static library.
$ ll Main/bin/Release/net8.0/win-x64/publish/
total 10656
-rw-r--r-- 1 danielo 197121 11448 Mar 13 09:13 Lib.pdb
-rwxr-xr-x 1 danielo 197121 1860608 Mar 13 09:13 Main.exe*
-rw-r--r-- 1 danielo 197121 8925184 Mar 13 09:13 Main.pdb
-rwxr-xr-x 1 danielo 197121 109056 Dec 11 05:16 WmiLight.Native.dll*
Thanks for your help.
The Type System.StubHelpers.ObjectMarshaler
is trimmed. This causes a System.TypeLoadException
.
Hi,
When connecting to remote computers using windows auth in a domain, wmilight doesn't seem to handle that scenario unless I missed somethings.
I added:
public bool UseIntegratedWindowsAuthentication { get; set; }
to WmiConnectionOptions, then added:
if (this.options.UseIntegratedWindowsAuthentication)
{
this.ignoreCredential = true;
this.wbemServices = locator.ConnectServer(this.path, null, null, null, WbemConnectOption.None, null, this.context);
this.wbemServices.SetProxy(ImpersonationLevel.Impersonate, authLevel);
}
to WmiConnection but there must be a better way of doing this.
Hello i m using WmiLight but when i release a dotnet 4.6.2 project in output lots of dll.
How can i prevent this please ?
I got the following error when running from native code:
Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program. The body of method 'Void WmiLight.Wbem.WbemLocator..ctor()' is invalid.
at Internal.Runtime.TypeLoaderExceptionHelper.CreateInvalidProgramException(ExceptionStringID, String) + 0x40
at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowInvalidProgramExceptionWithArgument(ExceptionStringID, String) + 0x9
at WmiLight.Wbem.WbemLocator..ctor() + 0x15
at WmiLight.WmiConnection.Open() + 0x49
at WmiLight.WmiConnection.InternalExecuteQuery(WmiQuery query) + 0x39
at WmiLight.WmiConnection.ExecuteQuery(WmiQuery query) + 0x2c
...
in my code, the exception occurs in this line:
using (WmiConnection con = new WmiConnection())
foreach (WmiObject process in con.CreateQuery("SELECT * FROM Win32_Processor"))
am I doing something wrong? Does WmiLight support trimming?
Hi,
I just upgraded a project using WmiLight 2.1 to 3.0 and can no longer compile my code since the assembly WmiLight.dll no longer is strong name signed.
I also noticed the lack of assembly metadata such as version.
Is this intended or a regression?
/Nicke
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.