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

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\install
  4.  * File Name: renameconn.cpp
  5.  * Purpose: Contains code to rename a connection
  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 HRESULT HrGetINetCfg (IN BOOL fGetWriteLock,
  18.                       IN LPCWSTR lpszAppName,
  19.                       OUT INetCfg** ppnc,
  20.                       OUT LPWSTR *lpszLockedBy);
  21.  
  22. extern HRESULT HrReleaseINetCfg (IN INetCfg* pnc,
  23.                           IN BOOL fHasWriteLock);
  24.  
  25.  
  26. extern HRESULT HrGetComponentEnum (INetCfg* pnc,
  27.                             IN const GUID* pguidClass,
  28.                             OUT IEnumNetCfgComponent **ppencc);
  29.  
  30. extern HRESULT HrGetFirstComponent (IN IEnumNetCfgComponent* pencc,
  31.                              OUT INetCfgComponent **ppncc);
  32.  
  33. extern bool Verbose;
  34.  
  35. extern 
  36. VOID RenameConnection ( LPCWSTR ConnectionName )
  37. {
  38.     INetCfg                 *pnc;
  39.     IEnumNetCfgComponent    *penccAdapter;
  40.     INetCfgComponent        *pnccAdapter;
  41.     INetCfgComponent        *pnccTrans;
  42.     INetConnectionManager   *pConMan;
  43.     IEnumNetConnection      *pEnumCon;
  44.     INetConnection          *pCon;
  45.     INetConnectionPropertyUi *pConUi;
  46.     GUID                    guidAdapter;
  47.     CLSID                   ClsId;
  48.     NETCON_PROPERTIES       *pConProps;
  49.     BOOL                    fLanConn;
  50.     BOOL                      gotAdapter = FALSE;
  51.     HRESULT                 hr;
  52.     ULONG                   count;
  53.  
  54.     hr = HrGetINetCfg( TRUE,
  55.         L"RENAMECONN1",
  56.         &pnc,
  57.         NULL );
  58.  
  59.     if ( hr == S_OK ) {
  60.         hr = pnc->FindComponent( L"MS_VWiFiP", &pnccTrans );
  61.  
  62.         if ( hr == S_OK ) {
  63.             hr = CoCreateInstance( CLSID_ConnectionManager, NULL,
  64.                 CLSCTX_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  65.                 IID_INetConnectionManager,
  66.                 (LPVOID *)&pConMan );
  67.  
  68.             if ( hr == S_OK ) {
  69.                 hr = pConMan->EnumConnections( NCME_DEFAULT, &pEnumCon );
  70.  
  71.                 if ( hr == S_OK ) {
  72.  
  73.                     while(!gotAdapter) {
  74.                         hr = pEnumCon->Next( 1, &pCon, &count );
  75.  
  76.                         if (hr == S_OK) {
  77.                             hr = pCon->GetProperties( &pConProps );    
  78.                             if (Verbose) {
  79.                                 wprintf(L"Device Name is %s\n", pConProps->pszwName);
  80.                                 wprintf(L"Adapter Name is %s\n", pConProps->pszwDeviceName);
  81.                             }
  82.                             if (( wcsstr( pConProps->pszwName, L"Local Area" ) != NULL )
  83.                                 && (wcsstr( pConProps->pszwDeviceName, L"VirtualWiFi" )))
  84.                             { 
  85.                                 gotAdapter = TRUE;
  86.                                 if (Verbose) 
  87.                                     printf("Found Adapter\n");
  88.                                 hr = pCon->Rename(ConnectionName);
  89.                                 if (hr == S_OK) {
  90.                                     if (Verbose) 
  91.                                         printf("Rename Successful\n");
  92.                                 } else {
  93.                                     printf("Could not rename the connection! \n");
  94.                                 }
  95.                             }
  96.                         } else 
  97.                             break;
  98.                     }
  99.                 }
  100.                 else 
  101.                 {
  102.                     printf("EnumConnections failed.");
  103.                 }
  104.                 pConMan->Release();
  105.             }
  106.             else 
  107.             {
  108.                 printf("CoCreateInstance on IID_INetConnectionManager failed.");
  109.             }
  110.             pnccTrans->Release();
  111.         }
  112.         HrReleaseINetCfg( pnc, TRUE );
  113.     }
  114.     else 
  115.     {
  116.         printf("HrGetINetCfg failed with %d.", hr);
  117.     }
  118.  
  119.     return;
  120. }
  121.  
  122. extern 
  123. BOOL DoesConnectionExist ( LPCWSTR ConnectionName )
  124. {
  125.     INetCfg                 *pnc;
  126.     IEnumNetCfgComponent    *penccAdapter;
  127.     INetCfgComponent        *pnccAdapter;
  128.     INetCfgComponent        *pnccTrans;
  129.     INetConnectionManager   *pConMan;
  130.     IEnumNetConnection      *pEnumCon;
  131.     INetConnection          *pCon;
  132.     INetConnectionPropertyUi *pConUi;
  133.     GUID                    guidAdapter;
  134.     CLSID                   ClsId;
  135.     NETCON_PROPERTIES       *pConProps;
  136.     BOOL                    flag = FALSE;
  137.     BOOL                      gotAdapter = FALSE;
  138.     HRESULT                 hr;
  139.     ULONG                   count;
  140.     INetConnectionConnectUi*     pConnectionUI;
  141.  
  142.  
  143.     hr = HrGetINetCfg( TRUE,
  144.         L"RENAMECONN1",
  145.         &pnc,
  146.         NULL );
  147.  
  148.     if ( hr == S_OK ) {
  149.         hr = pnc->FindComponent( L"MS_VWiFiP", &pnccTrans );
  150.  
  151.         if ( hr == S_OK ) {
  152.             hr = CoCreateInstance( CLSID_ConnectionManager, NULL,
  153.                 CLSCTX_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  154.                 IID_INetConnectionManager,
  155.                 (LPVOID *)&pConMan );
  156.  
  157.             if ( hr == S_OK ) {
  158.                 hr = pConMan->EnumConnections( NCME_DEFAULT, &pEnumCon );
  159.  
  160.                 if ( hr == S_OK ) {
  161.  
  162.                     while(!gotAdapter) {
  163.                         hr = pEnumCon->Next( 1, &pCon, &count );
  164.  
  165.                         if (hr == S_OK) {
  166.                             hr = pCon->GetProperties( &pConProps );    
  167.                             if (Verbose) {
  168.                                 wprintf(L"Device Name is %s\n", pConProps->pszwName);
  169.                                 wprintf(L"Adapter Name is %s\n", pConProps->pszwDeviceName);
  170.                             }
  171.                             if ( wcsstr( pConProps->pszwName, ConnectionName ) != NULL )
  172.                             { 
  173.                                 flag = TRUE;
  174.                                 break;
  175.                             } 
  176.                         } else 
  177.                             break;
  178.                     }
  179.                 }
  180.                 else 
  181.                 {
  182.                     printf("EnumConnections failed.");
  183.                 }
  184.                 pConMan->Release();
  185.             }
  186.             else 
  187.             {
  188.                 printf("CoCreateInstance on IID_INetConnectionManager failed.");
  189.             }
  190.             pnccTrans->Release();
  191.         }
  192.         HrReleaseINetCfg( pnc, TRUE );
  193.     }
  194.     else 
  195.     {
  196.         printf("HrGetINetCfg failed with %d.", hr);
  197.     }
  198.  
  199.     return flag;
  200. }
  201.  
  202. extern 
  203. BOOL DoesEnabledConnectionExist ( LPCWSTR ConnectionName )
  204. {
  205.     INetCfg                 *pnc;
  206.     IEnumNetCfgComponent    *penccAdapter;
  207.     INetCfgComponent        *pnccAdapter;
  208.     INetCfgComponent        *pnccTrans;
  209.     INetConnectionManager   *pConMan;
  210.     IEnumNetConnection      *pEnumCon;
  211.     INetConnection          *pCon;
  212.     INetConnectionPropertyUi *pConUi;
  213.     GUID                    guidAdapter;
  214.     CLSID                   ClsId;
  215.     NETCON_PROPERTIES       *pConProps;
  216.     BOOL                    flag = FALSE;
  217.     BOOL                      gotAdapter = FALSE;
  218.     HRESULT                 hr;
  219.     ULONG                   count;
  220.     INetConnectionConnectUi*     pConnectionUI;
  221.  
  222.  
  223.     hr = HrGetINetCfg( TRUE,
  224.         L"RENAMECONN1",
  225.         &pnc,
  226.         NULL );
  227.  
  228.     if ( hr == S_OK ) {
  229.         hr = pnc->FindComponent( L"MS_VWiFiP", &pnccTrans );
  230.  
  231.         if ( hr == S_OK ) {
  232.             hr = CoCreateInstance( CLSID_ConnectionManager, NULL,
  233.                 CLSCTX_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  234.                 IID_INetConnectionManager,
  235.                 (LPVOID *)&pConMan );
  236.  
  237.             if ( hr == S_OK ) {
  238.                 hr = pConMan->EnumConnections( NCME_DEFAULT, &pEnumCon );
  239.  
  240.                 if ( hr == S_OK ) {
  241.  
  242.                     while(!gotAdapter) {
  243.                         hr = pEnumCon->Next( 1, &pCon, &count );
  244.  
  245.                         if (hr == S_OK) {
  246.                             hr = pCon->GetProperties( &pConProps );    
  247.                             if (Verbose) {
  248.                                 wprintf(L"Device Name is %s\n", pConProps->pszwName);
  249.                                 wprintf(L"Adapter Name is %s\n", pConProps->pszwDeviceName);
  250.                             }
  251.                             if ( wcsstr( pConProps->pszwName, ConnectionName ) != NULL )
  252.                             { 
  253.                                 if ( pConProps->Status == NCS_CONNECTED || pConProps->Status == NCS_CONNECTING 
  254.                                     || pConProps->Status == NCS_AUTHENTICATING || pConProps->Status == NCS_AUTHENTICATION_SUCCEEDED ) 
  255.                                     flag = TRUE;
  256.                                 break;
  257.                             } 
  258.                         } else 
  259.                             break;
  260.                     }
  261.                 }
  262.                 else 
  263.                 {
  264.                     printf("EnumConnections failed.");
  265.                 }
  266.                 pConMan->Release();
  267.             }
  268.             else 
  269.             {
  270.                 printf("CoCreateInstance on IID_INetConnectionManager failed.");
  271.             }
  272.             pnccTrans->Release();
  273.         }
  274.         HrReleaseINetCfg( pnc, TRUE );
  275.     }
  276.     else 
  277.     {
  278.         printf("HrGetINetCfg failed with %d.", hr);
  279.     }
  280.  
  281.     return flag;
  282. }
  283.  
  284. extern 
  285. HRESULT EnableDisableConnection ( LPCWSTR ConnectionName, BOOL enableConnection )
  286. {
  287.     INetCfg                 *pnc;
  288.     IEnumNetCfgComponent    *penccAdapter;
  289.     INetCfgComponent        *pnccAdapter;
  290.     INetCfgComponent        *pnccTrans;
  291.     INetConnectionManager   *pConMan;
  292.     IEnumNetConnection      *pEnumCon;
  293.     INetConnection          *pCon;
  294.     INetConnectionPropertyUi *pConUi;
  295.     GUID                    guidAdapter;
  296.     CLSID                   ClsId;
  297.     NETCON_PROPERTIES       *pConProps;
  298.     BOOL                    flag = FALSE;
  299.     BOOL                      gotAdapter = FALSE;
  300.     HRESULT                 hr;
  301.     ULONG                   count;
  302.  
  303.  
  304.     hr = HrGetINetCfg( TRUE,
  305.         L"RENAMECONN1",
  306.         &pnc,
  307.         NULL );
  308.  
  309.     if ( hr == S_OK ) {
  310.         hr = pnc->FindComponent( L"MS_VWiFiP", &pnccTrans );
  311.  
  312.         if ( hr == S_OK ) {
  313.             hr = CoCreateInstance( CLSID_ConnectionManager, NULL,
  314.                 CLSCTX_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  315.                 IID_INetConnectionManager,
  316.                 (LPVOID *)&pConMan );
  317.  
  318.             if ( hr == S_OK ) {
  319.                 hr = pConMan->EnumConnections( NCME_DEFAULT, &pEnumCon );
  320.  
  321.                 if ( hr == S_OK ) {
  322.  
  323.                     while(!gotAdapter) {
  324.                         hr = pEnumCon->Next( 1, &pCon, &count );
  325.  
  326.                         if (hr == S_OK) {
  327.                             hr = pCon->GetProperties( &pConProps );    
  328.                             if (Verbose) {
  329.                                 wprintf(L"Device Name is %s\n", pConProps->pszwName);
  330.                                 wprintf(L"Adapter Name is %s\n", pConProps->pszwDeviceName);
  331.                             }
  332.                             if ( wcsstr( pConProps->pszwName, ConnectionName ) != NULL )
  333.                             { 
  334.                                 if(!enableConnection) {
  335.                                     pCon->Disconnect();
  336.                                 } else {
  337.                                     pCon->Connect();
  338.                                 }
  339.                                 break;
  340.                             } 
  341.                         } else 
  342.                             break;
  343.                     }
  344.                 }
  345.                 else 
  346.                 {
  347.                     printf("EnumConnections failed.");
  348.                 }
  349.                 pConMan->Release();
  350.             }
  351.             else 
  352.             {
  353.                 printf("CoCreateInstance on IID_INetConnectionManager failed.");
  354.             }
  355.             pnccTrans->Release();
  356.         }
  357.         HrReleaseINetCfg( pnc, TRUE );
  358.     }
  359.     else 
  360.     {
  361.         printf("HrGetINetCfg failed with %d.", hr);
  362.     }
  363.  
  364.     return hr;
  365. }
  366.  
  367.