0

I'm trying to create an impersonation of users with c#. I downloaded the System.management.dll to do it and I found a code on the Internet, but when I launch the code i found this error: System.Runtime.InteropServices.MarshalDirectiveException: 'Cannot marshal 'parameter #3': Cannot marshal a string by-value with the [Out] attribute.'

My code is:

public MainWindow()
{
        // Build an options object for the remote connection
        // if you plan to connect to the remote
        // computer with a different user name
        // and password than the one you are currently using.
        // This example uses the default values.
        ConnectionOptions options = new ConnectionOptions();
        options.Impersonation = System.Management.ImpersonationLevel.Impersonate;

        // Make a connection to a remote computer.
        // Replace the "FullComputerName" section of the
        // string "\\\\FullComputerName\\root\\cimv2" with
        // the full computer name or IP address of the
        // remote computer.
        ManagementScope scope = new ManagementScope("\\\\10.1.2.29\\root\\cimv2", options);
        scope.Connect();
}

I receive the exception on ManagementScope, I think that the program has problems on the IP address. Do you have any idea?

Victor Wilson
  • 1,720
  • 1
  • 11
  • 22
  • The one thing you definitely do **not** want to do is "download the System.management.dll". It is readily available in the GAC for a project that targets the legacy .NETFramework, available as a Nuget package for .NET5+. https://www.nuget.org/packages/System.Management/6.0.0 – Hans Passant Sep 26 '22 at 15:05

0 Answers0