4

I have the following .csx script:

#r "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Management.dll"
using System.Management;
ManagementClass cls = new ManagementClass("\\\\.\\root\\default:StdRegProv");

When I run this using dotnet script I get this error:

System.NullReferenceException: Object reference not set to an instance of an object.
    at System.Management.MTAHelper.IsNoContextMTA()
    at System.Management.MTAHelper.CreateInMTA(Type type)
    at System.Management.ManagementPath.CreateWbemPath(String path)
    at System.Management.ManagementPath..ctor(String path)
    at System.Management.ManagementClass..ctor(String path)
    at Submission#0.<<Initialize>>d__0.MoveNext() in <..path..>\script.csx:line 3
 --- End of stack trace from previous location ---
    at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in <...path...>\Temp\tmpBFB\Dotnet.Script.Core\ScriptRunner.cs:line 110

If I compile the following code in visual studio, everything works fine:

using System.Management;
class Script {
    static void Main() {
        ManagementClass cls = new ManagementClass("\\\\.\\root\\default:StdRegProv");
    }
}

Why ? How can I get my .csx script to work ?

Matt
  • 25,467
  • 18
  • 120
  • 187
  • Please supply a stack trace for the exception. It is not possible for `new` to return a null reference by itself – Charlieface Oct 24 '21 at 21:59
  • Works on my machine™, after correcting the path to v4.0_4.0.0.0__b03f5f7f11d50a3a. How you got an assembly in the GAC with such a wonky PublicKeyToken is hard to guess. Not one to get too excited about loading, I'd say. – Hans Passant Oct 24 '21 at 22:01
  • Misspelled my key intetionally, thought it won't matter, sorry. Added the stack trace. – Markus Peterson Oct 24 '21 at 22:22
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ken White Oct 24 '21 at 22:27
  • 1
    @KenWhite Not really – Markus Peterson Oct 24 '21 at 22:30
  • @HansPassant Can you please tell me how exactly did you run this script ? – Markus Peterson Oct 24 '21 at 22:31
  • Yes, it does. The answers tell you how to track down the cause of the problem and how to fix it. Please read again. – Ken White Oct 24 '21 at 22:37
  • @KenWhite Just read it again and I'm still clueless about how to fix my problem. I'm a beginner so maybe I'm missing something important. Can you point to me what should help me from that answer ? – Markus Peterson Oct 24 '21 at 22:50
  • @KenWhite have a look at the stacktrace, it looks like a bug in `System.Management.MTAHelper.IsNoContextMTA()`, not in user-code. – Steeeve Oct 24 '21 at 22:55
  • 2
    @MarkusPeterson When your code is running in an executable, it will most probably run on an STAThread. Apparently if you execute as script, you'll have an MTAThread and the ManagementClass has a problem with it. Googling for the error there are some results with your problem, for example [here](https://stackoverflow.com/questions/62055047/trying-to-generate-hardwareid-in-c-sharp-and-getting-system-management-manageme) – Steeeve Oct 24 '21 at 23:12
  • @Steeeve Thanks! Do you think is solvable ? I'm not very familiar with .net and c#, so probably I will try to use Microsoft.Management.Infrastructure if I can't make it work. – Markus Peterson Oct 24 '21 at 23:18
  • @MarkusPeterson sadly, I'm not familiar with csx so I can't tell you how to execute the code on an STAthread. Maybe someone else? – Steeeve Oct 24 '21 at 23:22

1 Answers1

1

dotnet script runs off .NET Core and I don't think WMI works in .NET Core as it needs COM interop. See this.

Also, global runtime dll for .NET CORE is not in GAC but rather in a runtime package store.

TDao
  • 514
  • 4
  • 13
  • 1
    Made it work using nuget and the package Microsoft.Windows.Compatibility (which includes System.Management). Now I'm trying to reproduce it, but can't get it to work again (probably I'm missing some steps). I'll update this with a step-by-step guide when I solve it. – Markus Peterson Oct 25 '21 at 07:43