Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[System.Management] ManagementObjectSearcher throws MarshalDirectiveException #28840

Closed
GeertvanHorrik opened this issue Mar 1, 2019 · 35 comments

Comments

@GeertvanHorrik
Copy link
Contributor

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

@danmoseley
Copy link
Member

@GeertvanHorrik would you like to offer a PR? Right now our focus is on other libraries so it might be some time before we could look at this ourselves.

@GeertvanHorrik
Copy link
Contributor Author

I will do it, but only for the endless credits, fame and joy it will bring!

But jokes aside, I will try to PR and fix this, might be next week. Is there a time limit?

@danmoseley
Copy link
Member

No, but the sooner you do it the sooner you have the fix 😉

Please do let us know if you need any help with setup, etc. The materials in the documentation folder in this repo should cover it all. Start with the developer guide.

@jkoritzinsky
Copy link
Member

Have you tried the newest preview version of the System.Management package? When we added that exception, we also updated System.Management to use char arrays instead of strings for those out parameters.

@GeertvanHorrik
Copy link
Contributor Author

@jkoritzinsky just tried 4.6.0-preview.19073.11, but that seems not to work either. Will double check next week, if it doesn't work, I will fix & PR.

@GeertvanHorrik
Copy link
Contributor Author

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 👍

@danmoseley
Copy link
Member

I learned how to fork & build corefx

@GeertvanHorrik in that case do you have any interest in contributing something else 😃 What are your interests?

https://github.com/dotnet/corefx/issues?q=is%3Aopen+is%3Aissue+label%3Aup-for-grabs

@godrose
Copy link

godrose commented Apr 12, 2019

Same issue here

@GeertvanHorrik
Copy link
Contributor Author

@godrose it's fixed in the codebase, it just needs a new release (and thus patience ;-) )

@godrose
Copy link

godrose commented Apr 12, 2019 via email

@GeertvanHorrik
Copy link
Contributor Author

I have no idea, I don't work for MS :-)

@godrose
Copy link

godrose commented Apr 12, 2019

@GeertvanHorrik yeah I figured that out ))

@danmoseley
Copy link
Member

We don't have a date but it shouldn't be too long. Please do post back confirmation it works, when released and you are able.

@godrose
Copy link

godrose commented Apr 17, 2019

@danmosemsft Okay

@godrose
Copy link

godrose commented Apr 19, 2019

@danmosemsft Same issue.

ManagementObjectSearcher searcher = new ManagementObjectSearcher
                ("Select * From Win32_Process Where ParentProcessID=" + pid);

@danmoseley
Copy link
Member

@Anipik owns this area and can help check your bits contain this change. Is it possible for you to step into it to check? Symbols should be on Microsoft symbol server. What does "dotnet --info" show?

@godrose
Copy link

godrose commented Apr 19, 2019

@danmosemsft @Anipik

.NET Core SDK (reflecting any global.json):
 Version:   3.0.100-preview4-011223
 Commit:    118dd862c8

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.0.100-preview4-011223\

Host (useful for support):
  Version: 3.0.0-preview4-27615-11
  Commit:  ee54d4cbd2

.NET Core SDKs installed:
  1.0.4 [C:\Program Files\dotnet\sdk]
  1.1.0 [C:\Program Files\dotnet\sdk]
  2.0.0 [C:\Program Files\dotnet\sdk]
  2.0.3 [C:\Program Files\dotnet\sdk]
  2.1.2 [C:\Program Files\dotnet\sdk]
  2.1.4 [C:\Program Files\dotnet\sdk]
  2.1.100 [C:\Program Files\dotnet\sdk]
  2.1.101 [C:\Program Files\dotnet\sdk]
  2.1.103 [C:\Program Files\dotnet\sdk]
  2.1.104 [C:\Program Files\dotnet\sdk]
  2.1.200-preview-007517 [C:\Program Files\dotnet\sdk]
  2.1.200-preview-007576 [C:\Program Files\dotnet\sdk]
  2.1.200-preview-007589 [C:\Program Files\dotnet\sdk]
  2.1.200 [C:\Program Files\dotnet\sdk]
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.401 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.403 [C:\Program Files\dotnet\sdk]
  2.1.500 [C:\Program Files\dotnet\sdk]
  2.1.502 [C:\Program Files\dotnet\sdk]
  2.1.503 [C:\Program Files\dotnet\sdk]
  2.1.504 [C:\Program Files\dotnet\sdk]
  2.1.505 [C:\Program Files\dotnet\sdk]
  2.1.600-preview-009426 [C:\Program Files\dotnet\sdk]
  2.1.600-preview-009472 [C:\Program Files\dotnet\sdk]
  2.1.600-preview-009497 [C:\Program Files\dotnet\sdk]
  2.1.600 [C:\Program Files\dotnet\sdk]
  2.1.602 [C:\Program Files\dotnet\sdk]
  2.2.101 [C:\Program Files\dotnet\sdk]
  2.2.200-preview-009748 [C:\Program Files\dotnet\sdk]
  2.2.200-preview-009804 [C:\Program Files\dotnet\sdk]
  2.2.200 [C:\Program Files\dotnet\sdk]
  2.2.202 [C:\Program Files\dotnet\sdk]
  3.0.100-preview4-011223 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview4-19216-03 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview4-27615-11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview4-27613-28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

@danmoseley
Copy link
Member

OK, and if you check in the debugger, the 3.0 preview 4 binaries are indeed getting loaded? Just want to exclude the obvious stuff.

@godrose
Copy link

godrose commented Apr 20, 2019

@danmosemsft

3>C:\Program Files\dotnet\sdk\3.0.100-preview4-011223\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): message NETSDK1057: You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
1>C:\Program Files\dotnet\sdk\3.0.100-preview4-011223\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): message NETSDK1057: You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
4>C:\Program Files\dotnet\sdk\3.0.100-preview4-011223\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): message NETSDK1057: You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
2>C:\Program Files\dotnet\sdk\3.0.100-preview4-011223\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): message NETSDK1057: You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview

@godrose
Copy link

godrose commented Apr 20, 2019

@danmosemsft Is this covered by a unit test? Because it looks like it's easily reproducible, at least at my machine.

@jkoritzinsky
Copy link
Member

This is covered by unit tests. That’s how I originally found it and fixed it. That’s why it’s weird that you’re still hitting it. It’s been fixed in the source here since the preview 3 timeframe (the fix went in before the CoreCLR change that causes the exception was merged).

@godrose
Copy link

godrose commented Apr 20, 2019

@jkoritzinsky Some kind of cache thing I guess. Few restarts and re-adding the package did the trick.
It works as expected without throwing the aforementioned exception. Thanks for quick reply!

@RobertGBryan
Copy link

It works when I built against a .NET Core 3 project preview 4. Make sure that when installing the NuGet package that the previews checkbox is selected. I initially installed version 4.5, which is over a year old, and which throws the exception. The new version gives the correct number of physical cores and does not throw any errors.

@danmoseley
Copy link
Member

Great!

@nicoriff
Copy link

nicoriff commented Aug 9, 2019

@danmosemsft when will be this shipping to production?

@danmoseley
Copy link
Member

@nicoriff it will be in 3.0 which will is scheduled for release in Sept, see timeline: https://devblogs.microsoft.com/dotnet/introducing-net-5/

@zezba9000
Copy link

I'm now running into this in a WPF .NET Core 3.0 app that includes a .NET Standard 2.0 lib that uses it.
Latest VS RC1 and .NET Core 3 RC.

@danmoseley
Copy link
Member

I think the fix @jkoritzinsky made was 2746a21. @zezba9000 for the avoidance of doubt, perhaps you could use a debugger to see where System.Management.dll is getting loaded from, and use ILSPY or similar to check that change is present?

@zezba9000
Copy link

zezba9000 commented Sep 18, 2019

Oops never mind. I just needed to update my CoreFX libs to the RC versions to match up with .NET Core 3 stuff.

Sorry about that.

@danmoseley
Copy link
Member

@zezba9000 no problem, glad it's working!

Incidentally, now you're here, perhaps you'd be interested in considering a contribution? We have 100's of up for grabs issues. Some are new API ("api-approved" label).
https://github.com/dotnet/corefx/issues?q=is%3Aopen+is%3Aissue+label%3Aup-for-grabs

@zezba9000
Copy link

@danmosemsft I'll take a look!

@danmoseley
Copy link
Member

Cool, ping any of us on an issue if you're interested.

@tqinli
Copy link
Contributor

tqinli commented Oct 16, 2019

Hi, I am using released .net runtime 3.0 and I am still seeing this issue. Can some one clarify where the fix is and how to consume it?

@danmoseley
Copy link
Member

@tqinli can you please use a debugger or by some means find out for certain where your app is loading System.Management.dll from. Then use ILSPY or Reflector or some similar tool to make sure that binary contains 2746a21

@tqinli
Copy link
Contributor

tqinli commented Oct 16, 2019

@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.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants