CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com eBook Library


Newest Articles at CodeGuru.com:

  • MFC Application: Adding Service Mode Support
  • Creating Alarms and Reminders for Windows Phone
  • Building and Using the Secret Service Java API
  • Implementing AutoPostBack in ASP.NET MVC

  • Search CodeGuru:
     




    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1    
    Old January 2nd, 2009, 03:02 PM
    Huffmand Huffmand is offline
    Junior Member
     
    Join Date: Jan 2009
    Posts: 12
    Huffmand is an unknown quantity at this point (<10)
    Exclamation SSID using visual c++

    I'm trying to obtain the SSID from a wireless router. I can already get the IP address but that does me no good since what I am using it for needs a static IP address. Anyone have any helpful hints???

    Thank you
    David
    huffmand1@citadel.edu
    Reply With Quote
      #2    
    Old January 2nd, 2009, 05:13 PM
    Andreas Masur Andreas Masur is offline
    Moderator
    Power Poster
     
    Join Date: May 2000
    Location: KY, USA
    Posts: 18,652
    Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)
    Re: SSID using visual c++

    What for?
    __________________
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)
    Reply With Quote
      #3    
    Old January 5th, 2009, 10:14 AM
    Huffmand Huffmand is offline
    Junior Member
     
    Join Date: Jan 2009
    Posts: 12
    Huffmand is an unknown quantity at this point (<10)
    Re: SSID using visual c++

    for a senior design project. We're setting up a model of transporting data to a driver via wimax but for budget and transmission issues we've down scaled it to wifi on our campus. Although we've done a lot of research and attempts at getting the SSID we haven't been successful. Our guidance from faculty is to search online for an already created API that does it for us, but so far no luck. So I decided to see if the forums approach might speed our success!! We know it's easily done since a computer automatically recognizes the SSID every time it sees a network, yet we can't find the code or construct the code that allows our design group to do this!
    Reply With Quote
      #4    
    Old January 5th, 2009, 12:21 PM
    Codeplug's Avatar
    Codeplug Codeplug is online now
    Senior Member
     
    Join Date: Nov 2003
    Posts: 1,631
    Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)
    Re: SSID using visual c++

    You can use WMI to get it, but you can only get the SSID's of AP's you are currently associated with. (Not talking about profiles, but "active" connections.)

    If you want know what AP's are currently visible, that's a little more work: like writing your own user mode NDIS protocol driver - or using Microsoft's (which they can change at any time).

    For the WMI approach, you would connect to "root\wmi" and query with:
    Code:
    SELECT * FROM MSNdis_80211_ServiceSetIdentifier 
    WHERE Active = true 
      AND NOT InstanceName LIKE '%Packet Scheduler Miniport'
    This will return an object instance for each wireless adapter - whether connected or not. The object member "Ndis80211SsId" is an array of bytes. If the adapter isn't connected to an AP, this will be all zero's. If it is, then byte offset 4 will be where the SSID name begins. The first 4 bytes is the string's length in little-endian byte order.

    gg
    Reply With Quote
      #5    
    Old January 5th, 2009, 12:55 PM
    Huffmand Huffmand is offline
    Junior Member
     
    Join Date: Jan 2009
    Posts: 12
    Huffmand is an unknown quantity at this point (<10)
    Re: SSID using visual c++

    Are you saying that can be used with visual c++ or we'll have to use a different language? We have to create a way to automatically connect once we press the run button, show connection, and more, which we've already accomplished this using the IP address so we're hoping to stick with visual C++ if possible. the main reason we need the SSID is the same as the IP, so we can take it and compare it to a database of stored locations and calculate the direction a vehicle is moving.
    Reply With Quote
      #6    
    Old January 5th, 2009, 02:15 PM
    Codeplug's Avatar
    Codeplug Codeplug is online now
    Senior Member
     
    Join Date: Nov 2003
    Posts: 1,631
    Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)
    Re: SSID using visual c++

    You can use WMI with Visual C++, but if you'll need to be familiar with COM client programming. You can also WMI with any automation scripting language (which may be easier for you).

    I don't really understand your previous post. Do you need to establish a connection with a given AP - without using Wireless-Zero-Config (or the Wifi management software that came with your adapter)?

    >> which we've already accomplished this using the IP address
    Where are you getting an IP address? IPHelper API?

    gg
    Reply With Quote
      #7    
    Old January 6th, 2009, 09:18 AM
    Huffmand Huffmand is offline
    Junior Member
     
    Join Date: Jan 2009
    Posts: 12
    Huffmand is an unknown quantity at this point (<10)
    Re: SSID using visual c++

    we use a wireless profile setup to automatically connect to wireless routers as it goes into dead space and loses connection and will automatically reconnect to the next router. With this we found some c++ code that alllow us to pull the IP address and we use that and compare it to pre-established variables that allows us to do our calculations... but our department head said he needed SSID for our final design. Can I possibly get some kind of example or quick tutorial on COM client programming bc due to our time constraints of this semester I think it would be easier for me to learn client programming vs. another language??
    Reply With Quote
      #8    
    Old January 6th, 2009, 04:14 PM
    Codeplug's Avatar
    Codeplug Codeplug is online now
    Senior Member
     
    Join Date: Nov 2003
    Posts: 1,631
    Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)
    Re: SSID using visual c++

    Example WMI code using Window Script Hosting or C# are a dime a dozen. A quick Google of "MSNdis_80211_ServiceSetIdentifier" will turn up lots of examples (many of which aren't quit complete though). I've never seen a COM tutorial that comes close to a good book on the subject either.

    Here's a complete (Visual) C++ example for posterity:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <string>
    using namespace std;
    
    #ifndef UNICODE
    #   define UNICODE
    #endif
    #include <Windows.h>
    
    #include <comdef.h>
    #include <comutil.h>
    
    #include <Wbemidl.h>
    #pragma comment(lib, "wbemuuid.lib")
    
    //------------------------------------------------------------------------------
    
    _COM_SMARTPTR_TYPEDEF(IWbemLocator, __uuidof(IWbemLocator));
    _COM_SMARTPTR_TYPEDEF(IWbemServices, __uuidof(IWbemServices));
    _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, __uuidof(IEnumWbemClassObject));
    _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject));
    
    //------------------------------------------------------------------------------
    
    inline void TESTHR(HRESULT hr) 
    { 
        if (FAILED(hr))
            _com_issue_error(hr);
    }//TESTHR
    
    //------------------------------------------------------------------------------
    
    void report_com_error(_com_error &err)
    {
        cerr << "COM error : " << hex << setw(8) << err.Error() << dec << endl;
        _bstr_t bstrSrc = err.Source();
        wchar_t *src = (wchar_t*)bstrSrc;
        if (src)
            wcout << L", src=[" << src << L"]" << endl;
    
        _bstr_t bstrDesc = err.Description();
        wchar_t *desc = (wchar_t*)bstrDesc;
        if (desc)
            wcout << L", desc=[" << desc << L"]" << endl;
    
        const wchar_t *errmsg = err.ErrorMessage();
        if (errmsg)
        {
            wcout << L", msg=[" << errmsg << L"]" << endl;
            LocalFree((HLOCAL)errmsg);
        }//if
    }//report_com_error
    
    //------------------------------------------------------------------------------
    
    struct CoInit
    {
        HRESULT hr;
        CoInit() {hr = CoInitializeEx(0, COINIT_MULTITHREADED);}
        ~CoInit() {if (!FAILED(hr)) CoUninitialize();}
    };//CoInit
    
    //------------------------------------------------------------------------------
    
    void WMI_Connect(IWbemServicesPtr &pSvc, const wchar_t *server)
    {
        IWbemLocatorPtr pLoc;
        TESTHR(CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, 
                                IID_IWbemLocator, (void**)&pLoc));
        
        TESTHR(pLoc->ConnectServer(_bstr_t(server), 0, 0, 0, 0, 0, 0, &pSvc));
    
        TESTHR(CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, 0,
                                 RPC_C_AUTHN_LEVEL_CALL, 
                                 RPC_C_IMP_LEVEL_IMPERSONATE, 0, EOAC_NONE));
    }//WMI_Connect
    
    //------------------------------------------------------------------------------
    
    struct SSID_Info
    {
        wstring adapter_name;
        string ssid; 
    
        bool Connected() const {return !ssid.empty();}
    };//SSID_Info
    
    //------------------------------------------------------------------------------
    
    void Get_SSID_Info(IWbemServicesPtr &pSvc, vector<SSID_Info> &vinfo)
    {
        vinfo.clear();
    
        IEnumWbemClassObjectPtr pObjEnum;
        const long flags = WBEM_FLAG_FORWARD_ONLY | 
                           WBEM_FLAG_RETURN_IMMEDIATELY;
    
        const wchar_t *query = 
            L"SELECT * FROM MSNdis_80211_ServiceSetIdentifier "
            L"WHERE Active = true "
            L"AND NOT InstanceName LIKE '%Packet Scheduler Miniport'";
    
        TESTHR(pSvc->ExecQuery(_bstr_t("WQL"), _bstr_t(query), 
                               flags, 0, &pObjEnum));
    
        IWbemClassObjectPtr pMSNdis_SSID;
        ULONG returned = 0;
        for (;;)
        {
            HRESULT hr = pObjEnum->Next(WBEM_INFINITE, 1, &pMSNdis_SSID, &returned);
            if (hr == WBEM_E_NOT_SUPPORTED)
                break;
            TESTHR(hr);
    
            if (!returned)
                break;
            
            _variant_t vProp;
            CIMTYPE cim_type;
            TESTHR(pMSNdis_SSID->Get(L"InstanceName", 0, &vProp, &cim_type, 0));
    
            SSID_Info sinfo;
            sinfo.adapter_name = (const wchar_t*)(_bstr_t)vProp;
            vProp.Clear(); // so we can re-use vProp
    
            TESTHR(pMSNdis_SSID->Get(L"Ndis80211SsId", 0, &vProp, &cim_type, 0));
            SAFEARRAY *psa = V_ARRAY(&vProp);
            BYTE *psa_bytes;
            TESTHR(SafeArrayAccessData(psa, (void**)&psa_bytes));
            size_t ssid_len = *reinterpret_cast<unsigned __int32*>(psa_bytes);
            sinfo.ssid.assign((const char*)psa_bytes + 4, ssid_len);
            TESTHR(SafeArrayUnaccessData(psa));
    
            vinfo.push_back(sinfo);
        }//for
    }//Get_SSID_Info
    
    //------------------------------------------------------------------------------
    
    int main()
    {
        CoInit com_init;
        if (FAILED(com_init.hr))
        {
            cerr << "CoInitializeEx failed!" << endl;
            return 1;
        }//if
    
        // one SSID_Info object per wireless adapter
        vector<SSID_Info> ssid_info;
    
        // do all the COM work in this try/catch
        try
        {
            // do this only once per process
            TESTHR(CoInitializeSecurity(0, -1, 0, 0, RPC_C_AUTHN_LEVEL_DEFAULT,
                                        RPC_C_IMP_LEVEL_IMPERSONATE, 0, 
                                        EOAC_NONE, 0));
            
            // connect to WMI - do this only once for efficiency - connection will 
            // be closed when pSvc goes out of scope
            IWbemServicesPtr pSvc;
            WMI_Connect(pSvc, L"ROOT\\WMI");
    
            // get a current snapshot of SSID info - this can be called multiple
            // times while connected to WMI
            Get_SSID_Info(pSvc, ssid_info);
        }//try
        catch (_com_error &err)
        {
            report_com_error(err);
            return 1;
        }//catch
    
        if (ssid_info.empty())
        {
            cout << "No wireless adapters found." << endl;
        }//if
        else
        {
            vector<SSID_Info>::const_iterator it = ssid_info.begin(),
                                              it_end = ssid_info.end();
            for (; it != it_end; ++it)
            {
                wcout << L"Adapter: " << it->adapter_name << endl;
                cout <<   "   SSID: ";
                if (it->Connected())
                    cout << it->ssid << endl;
                else
                    cout << "<Not Connected>" << endl;
            }//for
        }//else
    
        return 0;
    }//main
    gg
    Reply With Quote
      #9    
    Old January 8th, 2009, 01:04 PM
    Huffmand Huffmand is offline
    Junior Member
     
    Join Date: Jan 2009
    Posts: 12
    Huffmand is an unknown quantity at this point (<10)
    Re: SSID using visual c++

    thank you for all the help!! the right wording for search engines goes a long way when doing the online research!! we appreciate everything, have a great new year.
    Reply With Quote
      #10    
    Old January 28th, 2009, 02:26 PM
    Huffmand Huffmand is offline
    Junior Member
     
    Join Date: Jan 2009
    Posts: 12
    Huffmand is an unknown quantity at this point (<10)
    Re: SSID using visual c++

    After running the code several times, we were only able to get the message "No wireless adapters found."

    We ran the code one line at a time in debug mode and we noticed that after running the line:
    "vector<SSID_Info> ssid_info;"
    We Get:
    NAME: ssid_info
    VALUE: [0]()
    TYPE: std::vector<SSID_Info,std::allocator<SSID_Info> >

    We think that this line of code isn't running properly. The value shouldn't be [0]() unless there is not adapter, right? After this, the program continues to run and eventually outputs "No wireless adapters found."

    The computer we are using has the following specifications:
    Windows Edition:
    Windows Vista Home Premium
    Service Pack 1

    System Info:
    HP Pavilion dv9000 class
    Intel Core 2 CPU T5300 @ 1.73 GHz, 2 GB RAM


    We were wondering if the code is specifically tailored for a system running Windows XP, and if not, if you had any insight as to what our problem may be.

    Thank you!

    Last edited by Huffmand; January 28th, 2009 at 02:28 PM. Reason: NAME, VALUE, TYPE values were not displayed correctly
    Reply With Quote
      #11    
    Old January 28th, 2009, 02:44 PM
    Andreas Masur Andreas Masur is offline
    Moderator
    Power Poster
     
    Join Date: May 2000
    Location: KY, USA
    Posts: 18,652
    Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)Andreas Masur has a brilliant future (2000+)
    Re: SSID using visual c++

    Well...I don't know where the output comes from but it looks okay to me....that line constructs an empty vector....thus, I would expect it to have 0 elements....

    You actually need to push something in the vector first....which the given line certainly does not. More interesting would be what the output is after the following line:

    Get_SSID_Info(...);

    But may be I am missing something here....
    __________________
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)
    Reply With Quote
      #12    
    Old January 28th, 2009, 03:07 PM
    Huffmand Huffmand is offline
    Junior Member
     
    Join Date: Jan 2009
    Posts: 12
    Huffmand is an unknown quantity at this point (<10)
    Re: SSID using visual c++

    Here is the information you requested. Look at the attached JPEG image to see the values after running "Get_SSID_Info(pSvc, ssid_info);"
    Attached Images
     
    Reply With Quote
      #13    
    Old January 28th, 2009, 03:22 PM
    Codeplug's Avatar
    Codeplug Codeplug is online now
    Senior Member
     
    Join Date: Nov 2003
    Posts: 1,631
    Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)Codeplug is a name known to all (1000+)
    Re: SSID using visual c++

    Code:
            HRESULT hr = pObjEnum->Next(WBEM_INFINITE, 1, &pMSNdis_SSID, &returned);
            if (hr == WBEM_E_NOT_SUPPORTED)
                break;
            TESTHR(hr);
    
            if (!returned)
                break;
    What happens when you step through that code in debugger? Have no idea if this should work under Vista.

    The (documented) Native Wifi API should be used if you want XP SP2 and Vista support.

    gg
    Reply With Quote
      #14    
    Old February 4th, 2009, 01:24 PM
    Huffmand Huffmand is offline
    Junior Member
     
    Join Date: Jan 2009
    Posts: 12
    Huffmand is an unknown quantity at this point (<10)
    Re: SSID using visual c++

    Thank you for pointing us in the right direction!!!
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 11:14 AM.



    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.