nicoriff / ORMi

A Light-ORM for accesing WMI
MIT License
209 stars 28 forks source link

Method Name is incorrect #1

Closed jampot5000 closed 6 years ago

jampot5000 commented 6 years ago

When attempting to use WMIMethod.ExecuteMethod with parameters. The wrong method name is selected and the calling method name is used instead.

 [WMIClass("Win32_NetworkAdapterConfiguration")]
    public class NetworkAdapterConfiguration
    {
        public string Caption { get; set; }
        public string Description { get; set; }

        public UInt32 InterfaceIndex { get; set; }

        public bool SetStatic(string ip, string netmask)
        {
            int retVal = WMIMethod.ExecuteMethod(this, new {IPAddress = new string[] {ip}, SubnetMask = new string[] {netmask}});

            if(retVal != 0)
                Console.WriteLine($"Failed to set network settings with error code {retVal}");

            return retVal == 0;
        }
    }
void SetupInterfaces(){
 var interfaces = helper.Query<NetworkAdapterConfiguration>()

foreach(var interface in interfaces){
    interface.SetStatic("123.123.123.123","255.255.255.0");
}
}

SetupInterface ends up being selected as the MethodName instead of SetStatic.

nicoriff commented 6 years ago

Hi Jampot5000 let me take a look at it. I´m on it.

nicoriff commented 6 years ago

Ok. It is solved. The reflected name of the method was being captured incorrectly.

I´ll make some observations on your code:

1) foreach(var interface in interfaces)

You are using the reserved word interface on your foreach statement. That won´t compile. I assume you put this on the example but you are using another name.

2) In my case I do not have a SetStatic method. What I do have is a EnableStatic one.

I´ve already pushed the corrected version. Let me know how it worked out.

nicoriff commented 6 years ago

Another observation is that you are using InterfaceIndex as the key. It seems that the CIM_KEY for that class is Index. I think that you´ll have to change that in order to make it work.

You can take a look at the doc right here: https://docs.microsoft.com/en-us/windows/desktop/CIMWin32Prov/win32-networkadapterconfiguration