# [SOLVED] [C#] WMI handling SecurityCenter



## Emphyrio (May 16, 2009)

Hi 

I want to check the Firewall state on a XP machine.
Both "productUpdate" en "onAccessScanningEnabled" properties.

Todo that I use the following C# code smippet:

```
using System.Management;
string computer = Environment.MachineName;
string wmipath = "";
if (OSInfo.Name == "Windows XP") { wmipath = @"\\" + computer + @".\root\SecurityCenter"; }
else{wmipath = @"\\" + computer + @".\root\SecurityCenter2";}
SecTool = "SELECT * FROM FirewallProduct";
.......

lblFirew.Text = "Firewall (3th party): " + SecToolSearch(wmipath, SecTool);
............


private string SecToolSearch(string wmi,string tool)
           {
               string state = "Not present";
               try
                 {
                ManagementObjectSearcher AnVi_Info2 = new ManagementObjectSearcher(wmi, tool);
                ManagementObjectCollection instances = AnVi_Info2.Get();
                if (instances.Count != 0)
                {
                    foreach (ManagementObject queryObj in instances)
                    {
                        switch (OSInfo.Name)
                        {
                            case ("Windows XP"):
                              
                                if (queryObj["productUptoDate"])  { state += " [ Updated - "; }
                                else { state += " [ Need Update - "; }
                            
                               if (queryObj["OnAccessScanningEnabled"]) { state += "Running ]"; }
                                else { state += "Not Running ]"; }
                                
                             break;

                            default: // here is the handling routine for Security Center2 (Vista onwards)
                            ..........
                             }
                        return state;
                    }
                }
            }
            catch (Exception e) {MessageBox(e.mesage); }            
            return state;
           }
        }
```
Using this on a XP with Online Armor Free as a firewall, doesn't give me any output.


```
queryObj["productUptoDate"]
```
 should give me a "Boolean".
I am realy stuck here, could anyone point me back on track ?

Kind regards,

Emphyrio


----------



## Emphyrio (May 16, 2009)

*Re: [C#] WMI handling SecurityCenter*

*Correction*:


```
if (queryObj["Enabled"]) { state += "Running ]"; }
else { state += "Not Running ]"; }
```

Ignore the two if .. statements from above 

Error message still readds:



> Error	2	Cannot implicitly convert type 'object' to 'bool'. An explicit conversion exists (are you missing a cast?)


----------



## Emphyrio (May 16, 2009)

*Re: [C#] WMI handling SecurityCenter*

*Extra* :


```
MessageBox.Show((queryObj["Enabled"]).GetType ().ToString ());
```
gives me 











_(I'm sorry but I could'nt edit my post anymore.)_


----------



## Emphyrio (May 16, 2009)

*Re: [C#] WMI handling SecurityCenter*

This is working:


```
if ((queryObj["Enabled"].ToString() == "True")) { state += " [Running]"; }
else { state += "[Not Running ]"; }
```
Problem solved


----------



## Ninjaboi (Dec 27, 2010)

Hello Emphyrio.

Thank you for posting the fix for others to see. Nice debugging skill :grin:.


----------



## Emphyrio (May 16, 2009)

Thanks, Ninjaboi  

I do this because I know its a pain to find good information about surten routines, especially Security Center.

I spend e few weeks on this looking everywhere.


----------



## Ninjaboi (Dec 27, 2010)

Well it definitely shows that you worked for it, hope your application turns out great!

Happy coding!


----------

