home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / surpriz / MSRMesh-VirtualWIFI.MSI / addnetwork.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-24  |  13.6 KB  |  508 lines

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\install
  4.  * File Name: addnetwork.cpp
  5.  * Purpose  : Has the code to add a VirtualWiFi network
  6.  */
  7.  
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <strsafe.h>
  11. #include <setupapi.h>
  12. #include <netcfgx.h>
  13. #include <devguid.h>
  14. #include <netcon.h>
  15. #include <netcfgn.h>
  16.  
  17. extern bool Verbose;
  18.  
  19. extern VOID ReleaseRef (IN IUnknown* punk)
  20. {
  21.     if ( punk ) {
  22.         punk->Release();
  23.     }
  24.  
  25.     return;
  26. }
  27.  
  28. extern HRESULT HrGetINetCfg (IN BOOL fGetWriteLock,
  29.                       IN LPCWSTR lpszAppName,
  30.                       OUT INetCfg** ppnc,
  31.                       OUT LPWSTR *lpszLockedBy)
  32. {
  33.     INetCfg      *pnc = NULL;
  34.     INetCfgLock  *pncLock = NULL;
  35.     HRESULT      hr = S_OK;
  36.  
  37.     //
  38.     // Initialize the output parameters.
  39.     //
  40.  
  41.     *ppnc = NULL;
  42.  
  43.     if ( lpszLockedBy )
  44.     {
  45.         *lpszLockedBy = NULL;
  46.     }
  47.     //
  48.     // Initialize COM
  49.     //
  50.     CoUninitialize( );
  51.  
  52.     hr = CoInitialize( NULL );
  53.  
  54.     if ( hr == S_OK ) {
  55.  
  56.         //
  57.         // Create the object implementing INetCfg.
  58.         //
  59.  
  60.         hr = CoCreateInstance( CLSID_CNetCfg,
  61.                                NULL, CLSCTX_INPROC_SERVER,
  62.                                IID_INetCfg,
  63.                                (void**)&pnc );
  64.         if ( hr == S_OK ) {
  65.             if(Verbose)
  66.                 printf("CoCreateInstance successful\n");
  67.  
  68.             if ( fGetWriteLock ) {
  69.  
  70.                 //
  71.                 // Get the locking reference
  72.                 //
  73.  
  74.                 hr = pnc->QueryInterface( IID_INetCfgLock,
  75.                                           (LPVOID *)&pncLock );
  76.                 if ( hr == S_OK ) {
  77.                     if(Verbose)
  78.                         printf("QueryInterface successful\n");
  79.  
  80.                     //
  81.                     // Attempt to lock the INetCfg for read/write
  82.                     //
  83.  
  84.                     hr = pncLock->AcquireWriteLock( 5000,
  85.                                                     lpszAppName,
  86.                                                     lpszLockedBy);
  87.                     if (hr == S_FALSE ) {
  88.                         printf("Could not acquire write lock\n");
  89.                         hr = NETCFG_E_NO_WRITE_LOCK;
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             if ( hr == S_OK ) {
  95.                 if(Verbose)
  96.                     printf("Everything fine\n");
  97.                 //
  98.                 // Initialize the INetCfg object.
  99.                 //
  100.  
  101.                 hr = pnc->Initialize( NULL );
  102.  
  103.                 if ( hr == S_OK ) {
  104.                     if(Verbose)
  105.                         printf("Initialize successful\n");
  106.                     *ppnc = pnc;
  107.                     pnc->AddRef();
  108.                 }
  109.                 else {
  110.  
  111.                     //
  112.                     // Initialize failed, if obtained lock, release it
  113.                     //
  114.  
  115.                     if ( pncLock ) {
  116.                         pncLock->ReleaseWriteLock();
  117.                     }
  118.                 }
  119.             }
  120.  
  121.             ReleaseRef( pncLock );
  122.             ReleaseRef( pnc );
  123.         }
  124.         else 
  125.             printf("cocreateinstance failed\n");
  126.  
  127.         //
  128.         // In case of error, uninitialize COM.
  129.         //
  130.  
  131.         if ( hr != S_OK ) {
  132.             CoUninitialize();
  133.         }
  134.     }
  135.     else 
  136.         printf("CoInitialize Null failed\n");
  137.     return hr;
  138. }
  139.  
  140. //
  141. // Function:  HrReleaseINetCfg
  142. //
  143. // Purpose:   Get a reference to INetCfg.
  144. //
  145. // Arguments:
  146. //    pnc           [in] Reference to INetCfg to release.
  147. //    fHasWriteLock [in] If TRUE, reference was held with write lock.
  148. //
  149. // Returns:   S_OK on sucess, otherwise an error code.
  150. //
  151. // Notes:
  152. //
  153.  
  154. extern HRESULT HrReleaseINetCfg (IN INetCfg* pnc,
  155.                           IN BOOL fHasWriteLock)
  156. {
  157.     INetCfgLock    *pncLock = NULL;
  158.     HRESULT        hr = S_OK;
  159.  
  160.     //
  161.     // Uninitialize INetCfg
  162.     //
  163.  
  164.     hr = pnc->Uninitialize();
  165.  
  166.     //
  167.     // If write lock is present, unlock it
  168.     //
  169.  
  170.     if ( hr == S_OK && fHasWriteLock ) {
  171.  
  172.         //
  173.         // Get the locking reference
  174.         //
  175.  
  176.         hr = pnc->QueryInterface( IID_INetCfgLock,
  177.                                   (LPVOID *)&pncLock);
  178.         if ( hr == S_OK ) {
  179.            hr = pncLock->ReleaseWriteLock();
  180.            ReleaseRef( pncLock );
  181.         }
  182.     }
  183.  
  184.     ReleaseRef( pnc );
  185.  
  186.     //
  187.     // Uninitialize COM.
  188.     //
  189.  
  190.     CoUninitialize();
  191.  
  192.     return hr;
  193. }
  194.  
  195.  
  196. //
  197. // Function:  HrGetComponentEnum
  198. //
  199. // Purpose:   Get network component enumerator reference.
  200. //
  201. // Arguments:
  202. //    pnc         [in]  Reference to INetCfg.
  203. //    pguidClass  [in]  Class GUID of the network component.
  204. //    ppencc      [out] Enumerator reference.
  205. //
  206. // Returns:   S_OK on sucess, otherwise an error code.
  207. //
  208. // Notes:
  209. //
  210.  
  211. extern HRESULT HrGetComponentEnum (INetCfg* pnc,
  212.                             IN const GUID* pguidClass,
  213.                             OUT IEnumNetCfgComponent **ppencc)
  214. {
  215.     INetCfgClass  *pncclass;
  216.     HRESULT       hr;
  217.  
  218.     *ppencc = NULL;
  219.  
  220.     //
  221.     // Get the class reference.
  222.     //
  223.  
  224.     hr = pnc->QueryNetCfgClass( pguidClass,
  225.                                 IID_INetCfgClass,
  226.                                 (PVOID *)&pncclass );
  227.  
  228.     if ( hr == S_OK ) {
  229.  
  230.         //
  231.         // Get the enumerator reference.
  232.         //
  233.  
  234.         hr = pncclass->EnumComponents( ppencc );
  235.  
  236.         //
  237.         // We don't need the class reference any more.
  238.         //
  239.  
  240.         ReleaseRef( pncclass );
  241.     }
  242.  
  243.     return hr;
  244. }
  245.  
  246. //
  247. // Function:  HrGetFirstComponent
  248. //
  249. // Purpose:   Enumerates the first network component.
  250. //
  251. // Arguments:
  252. //    pencc      [in]  Component enumerator reference.
  253. //    ppncc      [out] Network component reference.
  254. //
  255. // Returns:   S_OK on sucess, otherwise an error code.
  256. //
  257. // Notes:
  258. //
  259.  
  260. extern HRESULT HrGetFirstComponent (IN IEnumNetCfgComponent* pencc,
  261.                              OUT INetCfgComponent **ppncc)
  262. {
  263.     HRESULT  hr;
  264.     ULONG    ulCount;
  265.  
  266.     *ppncc = NULL;
  267.  
  268.     pencc->Reset();
  269.  
  270.     hr = pencc->Next( 1,
  271.                       ppncc,
  272.                       &ulCount );
  273.     return hr;
  274. }
  275.  
  276. DWORD WINAPI PressEnter( LPVOID lpParam ) 
  277.     Sleep(500);
  278.     // These two lines imulate enter
  279.     keybd_event(VK_RETURN, 0, 0, 0);
  280.     keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
  281.  
  282.     return 0; 
  283.  
  284. DWORD WINAPI PressDownAndEnter( LPVOID lpParam ) 
  285.     Sleep(500);
  286.     printf("Pressing down and Enter\n");
  287.     // These two lines emulate the down key
  288.     keybd_event(VK_DOWN, 0, 0, 0);
  289.     keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
  290.     // These two lines emulate enter
  291.     keybd_event(VK_RETURN, 0, 0, 0);
  292.     keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
  293.  
  294.     return 0; 
  295.  
  296. extern 
  297. VOID AddNetwork (HWND hwndDlg, BOOL addNetwork)
  298. {
  299.   INetCfg                 *pnc;
  300.   IEnumNetCfgComponent    *penccAdapter;
  301.   INetCfgComponent        *pnccAdapter;
  302.   INetCfgComponent        *pnccTrans;
  303.   INetConnectionManager   *pConMan;
  304.   IEnumNetConnection      *pEnumCon;
  305.   INetConnection          *pCon;
  306.   INetConnectionPropertyUi *pConUi;
  307.   GUID                    guidAdapter;
  308.   CLSID                   ClsId;
  309.   NETCON_PROPERTIES       *pConProps;
  310.   BOOL                    fLanConn;
  311.   BOOL                      gotAdapter = FALSE;
  312.   HRESULT                 hr;
  313.   ULONG                   count;
  314.   DWORD dwThreadId, dwThrdParam=1; 
  315.   HANDLE hThread; 
  316.  
  317.   hr = HrGetINetCfg( TRUE,
  318.                      L"NETCONN",
  319.                      &pnc,
  320.                      NULL );
  321.  
  322.   if ( hr == S_OK ) {
  323.  
  324.      //
  325.      // Get Component Enumerator Interface.
  326.      //
  327.  
  328.      //hr = HrGetComponentEnum( pnc,
  329.      //                         &GUID_DEVCLASS_NET,
  330.      //                         &penccAdapter );
  331.      if ( hr == S_OK ) {
  332.  
  333.         //hr = HrGetFirstComponent( penccAdapter, &pnccAdapter );
  334.  
  335.         if ( hr == S_OK ) {
  336.  
  337.            //hr = pnccAdapter->GetInstanceGuid( &guidAdapter );
  338.  
  339.            if ( hr == S_OK ) {
  340.               hr = pnc->FindComponent( L"MS_VWiFiP", &pnccTrans );
  341.  
  342.               if ( hr == S_OK ) {
  343.                  hr = CoCreateInstance( CLSID_ConnectionManager, NULL,
  344.                                         CLSCTX_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  345.                                         IID_INetConnectionManager,
  346.                                         (LPVOID *)&pConMan );
  347.                  if ( hr == S_OK ) {
  348.  
  349.                     hr = pConMan->EnumConnections( NCME_DEFAULT, &pEnumCon );
  350.  
  351.                     if ( hr == S_OK ) {
  352.                        
  353.                         while(!gotAdapter) {
  354.                            hr = pEnumCon->Next( 1, &pCon, &count );
  355.  
  356.                            if (hr == S_OK) {
  357.                                hr = pCon->GetProperties( &pConProps );    
  358.                                if(Verbose) {
  359.                                    wprintf(L"Device Name is %s\n", pConProps->pszwName);
  360.                                    wprintf(L"Adapter Name is %s\n", pConProps->pszwDeviceName);
  361.                                }
  362.                                if ( wcsstr( pConProps->pszwName, L"Wireless" ) != NULL ) { 
  363.                                    gotAdapter = TRUE;
  364.                                    if(Verbose)
  365.                                         printf("Found Adapter\n");
  366.                                }
  367.                            }
  368.                         }
  369.  
  370.                        fLanConn = FALSE;
  371.  
  372.                        while ( (hr == S_OK) && !fLanConn ) {
  373.                           
  374.                           hr = pCon->GetProperties( &pConProps );
  375.  
  376.                           if ( hr == S_OK ) {
  377.                              fLanConn = pConProps->MediaType == NCM_LAN;
  378.                              CoTaskMemFree( pConProps );
  379.                           }
  380.  
  381.                           if ( fLanConn ) {
  382.                              hr = pCon->GetUiObjectClassId( &ClsId );
  383.                              if ( hr == S_OK ) {
  384.                                 hr = CoCreateInstance( ClsId, NULL,
  385.                                                        CLSCTX_INPROC_SERVER |                                                       
  386.                                                        CLSCTX_NO_CODE_DOWNLOAD,
  387.                                                        IID_INetConnectionPropertyUi,
  388.                                                        (LPVOID *)&pConUi );
  389.                                 if ( hr == S_OK ) {
  390.                                     hr = pConUi->SetConnection( pCon );
  391.  
  392.                                     if(addNetwork) {
  393.                                         hThread = CreateThread( 
  394.                                             NULL,                        // no security attributes 
  395.                                             0,                           // use default stack size  
  396.                                             PressEnter,                  // thread function 
  397.                                             &dwThrdParam,                 // argument to thread function
  398.                                             0,                           // use default creation flags 
  399.                                             &dwThreadId);                // returns the thread identifier 
  400.                                     } else {
  401.                                         hThread = CreateThread( 
  402.                                             NULL,                        // no security attributes 
  403.                                             0,                           // use default stack size  
  404.                                             PressDownAndEnter,                  // thread function 
  405.                                             &dwThrdParam,                 // argument to thread function
  406.                                             0,                           // use default creation flags 
  407.                                             &dwThreadId);                // returns the thread identifier 
  408.                                     }
  409.                                  
  410.                                     // Check the return value for success. 
  411.                                     hr = pnccTrans->RaisePropertyUi( hwndDlg,
  412.                                                                     NCRP_SHOW_PROPERTY_UI,
  413.                                                                     pConUi 
  414.                                                                     );
  415.    
  416.                                    
  417.                                     
  418.                                     if (hThread == NULL) 
  419.                                     {
  420.                                         printf("CreateThread failed." ); 
  421.                                     }
  422.                                     else 
  423.                                     {
  424.                                         Sleep(1000);
  425.                                         CloseHandle( hThread );
  426.                                     }
  427.  
  428.                                    if ( hr == S_OK ) {
  429.                                       if(Verbose)
  430.                                           printf("RaisePropertyUi successful.");
  431.                                       pnc->Apply();
  432.                                    }
  433.                                    else 
  434.                                    {
  435.                                        printf("RaisePropertyUi failed.");
  436.                                    }
  437.                                    pConUi->Release();
  438.                                 }
  439.                                 else 
  440.                                 {
  441.                                     printf("CoCreateInstance on IID_INetConnectionCommonUi failed.");
  442.                                 }
  443.                              }
  444.                              else 
  445.                              {
  446.                                  printf("GetUiObjectClassId failed.");
  447.                              }
  448.                           }
  449.                           pCon->Release() ;
  450.                           hr = pEnumCon->Next( 1, &pCon, &count );
  451.                        }
  452.                        pEnumCon->Release();
  453.                     }
  454.                     else 
  455.                     {
  456.                        printf("EnumConnections failed.");
  457.                     }
  458.                     pConMan->Release();
  459.                  }
  460.                  else 
  461.                  {
  462.                     printf("CoCreateInstance on IID_INetConnectionManager failed.");
  463.                  }
  464.                  pnccTrans->Release();
  465.               }
  466.            }
  467.            else 
  468.            {
  469.               printf("GetInstanceGuid failed.");
  470.            }
  471.            //pnccAdapter->Release();
  472.         }
  473.         else 
  474.         {
  475.            printf("HrGetFirstComponent failed.");
  476.         }
  477.         //penccAdapter->Release();
  478.      }
  479.      else 
  480.      {
  481.         printf("HrGetComponentEnum failed.");
  482.      }
  483.      HrReleaseINetCfg( pnc, TRUE );
  484.   }
  485.   else 
  486.   {
  487.      printf("HrGetINetCfg failed.");
  488.   }
  489.  
  490.  
  491.   /*
  492.   INPUT input[4] = {INPUT_KEYBOARD, VK_TAB, 0, 0, 0, 0,
  493.                         INPUT_KEYBOARD, VK_TAB, 0, KEYEVENTF_KEYUP, 0, 0,
  494.                         INPUT_KEYBOARD, VK_RETURN, 0, 0, 0, 0,
  495.                         INPUT_KEYBOARD, VK_RETURN, 0, KEYEVENTF_KEYUP, 0, 0};
  496.  
  497.  
  498.   SendInput(4, input, sizeof input[0]);
  499.   */
  500.  
  501.   return;
  502. }
  503.  
  504.