runtime: [System.Management] ManagementObjectSearcher throws MarshalDirectiveException

Version: 4.5.0 Platform: .NET Core 3.0 (WPF) app

                var wmi = new ManagementObjectSearcher("select * from Win32_OperatingSystem")
                    .Get()
                    .Cast<ManagementObject>()
                    .First();

Then I get this exception:

System.Runtime.InteropServices.MarshalDirectiveException: 'Cannot marshal 'parameter dotnet/corefx#3': Cannot marshal a string by-value with the [Out] attribute.'

The exact issue is in ManagementPath.GetWbemPath:

image

It could be caused by a “wrong” p/invoke here:

https://github.com/dotnet/corefx/blob/000aa2e0740524bdc2db690201e1b3521dd417a3/src/System.Management/src/System/Management/InteropClasses/WMIInterop.cs#L745

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 35 (21 by maintainers)

Most upvoted comments

Just forked & build a local copy using the latest sources, and that no longer contains the exception! No need to create a PR, but I learned how to fork & build corefx so still lots of added value 👍

@danmosemsft thanks very much for the pointers, yes you are right the dll I used didn’t contain important classes, I should use dll from this folder runtimes\win\lib\netcoreapp2.0 from v4.6.0 nuget package of system.management.

@danmosemsft I’ll take a look!