0

I'm using a function to return the current WiFi signal strength value given here: http://www.dotnet247.com/247reference/msgs/42/211278.aspx

When I run the code in Visual Studio 2008, I get the compile errors:

Type of namespace 'ManagementObjectSearcher'cannot be found.

I am using 'using System.Manangement'

My over all goal is to acquire the signal strength and plug it into a text box on a windows form, so once I grab the value using the function below, I simply will pop it into the box for the user to see.

Any idea on why I'm getting these errors?

Code:

 public static void signalStrentgh()
        {
           ManagementObjectSearcher searcher = new  
           ManagementObjectSearcher(@"root\WMI", "select 
           Ndis80211ReceivedSignalStrength from MSNdis_80211_ReceivedSignalStrength 
           where active=true");

            foreach (ManagementObject mo in searcher.Get())
            {
                Console.WriteLine("{0}", mo["Ndis80211ReceivedSignalStrength"]);
            }
        }

Note* Posted below is the old, incorrect version of the code, done in C++. This is in reference to the comments and help.

int GetSignalStrength()
{
  ManagementObjectSearcher *searcher = new ManagementObjectSearcher(
  "root\\WMI",
  "select Ndis80211ReceivedSignalStrength from
  MSNdis_80211_ReceivedSignalStrength where active=true");

  ManagementObjectCollection *queryCollection = searcher->Get();

  ManagementObjectCollection::ManagementObjectEnumer ator* queryEnum =
  queryCollection->GetEnumerator();
  while (queryEnum->MoveNext());

  ManagementBaseObject* object = queryEnum->get_Current();
  Object* signalStrength =
  object->GetPropertyValue(L"Ndis80211ReceivedSignalStrengt h");
  return (Convert::ToInt32(signalStrength->ToString()));
}
J.C.Morris
  • 803
  • 3
  • 13
  • 26
  • 3
    I see C++ pointers in the code, a C# tag and Visual Basic in the question. What are you doing? – Emond Aug 15 '12 at 14:00
  • 2
    You've tagged this as C# but the code you've posted seems to be C++. Which are you using? – Dan Puzey Aug 15 '12 at 14:03
  • 1
    It might be `unsafe` method, if it is C#: unsafe int GetSignalStrength() – Rolice Aug 15 '12 at 14:08
  • @Rolice, what about `signalStrength->ToString()`? Could this be C#, no matter how unsafe we get? – Darin Dimitrov Aug 15 '12 at 14:15
  • @DarinDimitrov, agreed, seems to be C++ CLR, example but not C#. – Rolice Aug 15 '12 at 14:16
  • @Rolice, yes, thus the previous comments about the wrong tags in this question. – Darin Dimitrov Aug 15 '12 at 14:17
  • I meant to say Visual Studio 2008. The source was originally written in C# and a forum member posted a C++ version. I copied the wrong code over. Editing now. – J.C.Morris Aug 15 '12 at 14:18
  • Possibly, as I understand he wants to compile it in C#, but a wrong example of nearly looking C++ CLR code is posted. – Rolice Aug 15 '12 at 14:21
  • It is technically and syntactically valid C# code. The error makes sense if you understand the conventions here. He's trying to use a keyword for a variable name. – villecoder Aug 15 '12 at 14:27
  • Ok I have edited the code. Sorry for the confusion. – J.C.Morris Aug 15 '12 at 14:36
  • @J.C.Morris Actually, just leave the original code, in case someone else has the same questions you did. (That, and your accepted answer will make more sense). Show the corrected code below your original code. – villecoder Aug 15 '12 at 15:01

2 Answers2

1

The thing I found for C# is something like on MSDN: http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/34a66ee5-34f8-473d-b6f2-830a14e2300b/

Read carefully. It seems you are using or had adapted C++ CLR code. Your code seems to be almost right.

Rolice
  • 3,063
  • 2
  • 24
  • 32
1

To answer your question, you can't use object as a variable name. object is a keyword. Try renaming the variable to obj or o and see if that works.

For further reference, I think you're compiling c# code using some really old syntactical tricks. And I think that's throwing a lot of people off in trying to determine if this is really c#.

The pointer syntax is probably not necessary. ManagementObjectSearcher is part of the System.Management namespace and is therefore safe to use without the unsafe pointer syntax.

Also, why are you using the namespace alias qualifier (::) ? It should be safe to use the dot operator directly (ManagementObjectCollection.ManagementObjectEnumerator). In fact, it should be safe to use the dot operator everywhere here instead of the pointer syntax.

villecoder
  • 13,323
  • 2
  • 33
  • 52