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

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\notifyob
  4.  * File Name: common.cpp
  5.  * Purpose  : Debug & Utility functions
  6.  */
  7.  
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <netcfgn.h>
  11. #include "common.h"
  12. #include <winioctl.h>
  13. #include <ntddndis.h>
  14. #include <nuiouser.h>
  15.  
  16. #ifdef DBG
  17.  
  18. void TraceMsg (LPWSTR szFormat, ...)
  19. {
  20.     static WCHAR szTempBuf[4096];
  21.  
  22.     va_list arglist;
  23.  
  24.     va_start(arglist, szFormat);
  25.  
  26.     vswprintf( szTempBuf, szFormat, arglist );
  27.  
  28.     OutputDebugStringW( szTempBuf );
  29.  
  30.     va_end(arglist);
  31. }
  32.  
  33. void DumpChangeFlag (DWORD dwChangeFlag)
  34. {
  35.     TraceMsg( L"   ChangeFlag:" );
  36.  
  37.     if ( dwChangeFlag & NCN_ADD ) {
  38.         TraceMsg( L" NCN_ADD" );
  39.     }
  40.  
  41.     if ( dwChangeFlag & NCN_REMOVE ) {
  42.         TraceMsg( L" NCN_REMOVE" );
  43.     }
  44.  
  45.     if ( dwChangeFlag & NCN_UPDATE ) {
  46.         TraceMsg( L" NCN_UPDATE" );
  47.     }
  48.  
  49.     if ( dwChangeFlag & NCN_ENABLE ) {
  50.         TraceMsg( L" NCN_ENABLE" );
  51.     }
  52.  
  53.     if ( dwChangeFlag & NCN_DISABLE ) {
  54.         TraceMsg( L" NCN_DISABLE" );
  55.     }
  56.  
  57.     if ( dwChangeFlag & NCN_BINDING_PATH ) {
  58.         TraceMsg( L" NCN_BINDING_PATH" );
  59.     }
  60.  
  61.     if ( dwChangeFlag & NCN_PROPERTYCHANGE ) {
  62.         TraceMsg( L" NCN_PROPERTYCHANGE" );
  63.     }
  64.  
  65.     if ( dwChangeFlag & NCN_NET ) {
  66.         TraceMsg( L" NCN_NET" );
  67.     }
  68.  
  69.     if ( dwChangeFlag & NCN_NETTRANS ) {
  70.         TraceMsg( L" NCN_NETTRANS" );
  71.     }
  72.  
  73.     if ( dwChangeFlag & NCN_NETCLIENT ) {
  74.         TraceMsg( L" NCN_NETCLIENT" );
  75.     }
  76.  
  77.     if ( dwChangeFlag & NCN_NETSERVICE ) {
  78.         TraceMsg( L" NCN_NETSERVICE" );
  79.     }
  80.  
  81.     TraceMsg( L"\n" );
  82.     return;
  83. }
  84.  
  85. void DumpBindingPath (INetCfgBindingPath *pncbp)
  86. {
  87.     INetCfgComponent             *pncc;
  88.     IEnumNetCfgBindingInterface  *pencbi;
  89.     INetCfgBindingInterface      *pncbi;
  90.     LPWSTR                       lpsz;
  91.     DWORD                        dwIndex;
  92.     ULONG                        ulCount;
  93.     HRESULT                      hr;
  94.  
  95.     hr = pncbp->GetPathToken( &lpsz );
  96.  
  97.     if ( hr == S_OK ) {
  98.  
  99.         TraceMsg( L"   BindingPath: %s\n",
  100.                lpsz );
  101.  
  102.         CoTaskMemFree( lpsz );
  103.     }
  104.     else {
  105.  
  106.     TraceMsg( L"   BindingPath: GetPathToken failed(HRESULT %x).\n",
  107.            hr );
  108.     }
  109.  
  110. #ifdef VERBOSE_TRACE
  111.  
  112.     hr = pncbp->EnumBindingInterfaces( &pencbi );
  113.  
  114.     if ( hr == S_OK ) {
  115.  
  116.         hr = pencbi ->Next( 1, &pncbi, &ulCount );
  117.  
  118.         for (dwIndex=0; hr == S_OK; dwIndex++ ) {
  119.  
  120.             hr = pncbi->GetName( &lpsz );
  121.  
  122.             if ( hr == S_OK ) {
  123.  
  124.                TraceMsg( L"   BindingInterface(%d): %s\n",
  125.                          dwIndex, lpsz );
  126.  
  127.                CoTaskMemFree( lpsz );
  128.             }
  129.             else {
  130.  
  131.                TraceMsg( L"   BindingInterface(%d): GetName failed(HRESULT %x).\n",
  132.                          dwIndex, hr );
  133.             }
  134.  
  135.             hr = pncbi->GetUpperComponent( &pncc );
  136.  
  137.             if ( hr == S_OK ) {
  138.  
  139.                 TraceMsg( L"   \tUpperComponent of the interface(%d)...\n",
  140.                          dwIndex );
  141.  
  142.                 DumpComponent( pncc );
  143.  
  144.                 ReleaseObj( pncc );
  145.             }
  146.             else {
  147.  
  148.                 TraceMsg( L"   UpperComponent: GetUpperComponent failed(HRESULT = %x).\n",
  149.                          hr );
  150.             }
  151.             hr = pncbi->GetLowerComponent( &pncc );
  152.  
  153.             if ( hr == S_OK ) {
  154.  
  155.                 TraceMsg( L"   \tLowerComponent of the interface(%d)...\n",
  156.                          dwIndex );
  157.                 DumpComponent( pncc );
  158.  
  159.                 ReleaseObj( pncc );
  160.             }
  161.             else {
  162.  
  163.                 TraceMsg( L"   LowerComponent: GetLowerComponent failed(HRESULT = %x).\n",
  164.                          hr );
  165.             }
  166.  
  167.             ReleaseObj( pncbi );
  168.  
  169.             hr = pencbi ->Next( 1,
  170.                                      &pncbi,
  171.                                      &ulCount );
  172.         }
  173.  
  174.         ReleaseObj( pencbi );
  175.     }
  176.     else {
  177.  
  178.         TraceMsg( L"   EnumBindingInterfaces failed, (HRESULT = %x)\n",
  179.                   hr ); 
  180.     }
  181. #endif 
  182.     return;
  183. }
  184.  
  185. void DumpComponent (INetCfgComponent *pncc)
  186. {
  187.     LPWSTR  lpsz;
  188.     DWORD   dwChars;
  189.     ULONG   ulStatus;
  190.     HRESULT hr;
  191.  
  192.     hr = pncc->GetDisplayName( &lpsz );
  193.  
  194.     if ( hr == S_OK ) {
  195.  
  196.         TraceMsg( L"   \t\tComponent: %s\n",
  197.                lpsz );
  198.  
  199.         CoTaskMemFree( lpsz );
  200.     }
  201.     else {
  202.  
  203.         TraceMsg( L"   GetDisplay failed(HRESULT %x).\n",
  204.                hr );
  205.     }
  206.  
  207.     hr = pncc->GetCharacteristics( &dwChars );
  208.  
  209.     if ( hr == S_OK ) {
  210.  
  211.         TraceMsg( L"   \t\tCharacteristics:" );
  212.  
  213.         if ( dwChars & NCF_HIDDEN ) {
  214.            TraceMsg( L" NCF_HIDDEN" );
  215.         }
  216.  
  217.         if ( dwChars & NCF_NO_SERVICE ) {
  218.            TraceMsg( L" NCF_NO_SERVICE" );
  219.         }
  220.  
  221.         if ( dwChars & NCF_VIRTUAL ) {
  222.            TraceMsg( L" NCF_VIRTUAL" );
  223.         }
  224.  
  225.         if ( dwChars & NCF_PHYSICAL ) {
  226.            TraceMsg( L" NCF_PHYSICAL" );
  227.         }
  228.  
  229.         if ( dwChars & NCF_FILTER ) {
  230.            TraceMsg( L" NCF_FILTER" );
  231.         }
  232.  
  233.         if ( dwChars & NCF_NOT_USER_REMOVABLE ) {
  234.            TraceMsg( L" NCF_NOT_USER_REMOVABLE" );
  235.         }
  236.  
  237.         if ( dwChars & NCF_HAS_UI ) {
  238.            TraceMsg( L" NCF_HAS_UI" );
  239.         }
  240.  
  241.         if ( dwChars & NCF_SOFTWARE_ENUMERATED ) {
  242.            TraceMsg( L" NCF_SOFTWARE_ENUMERATED" );
  243.         }
  244.  
  245.         if ( dwChars & NCF_MULTIPORT_INSTANCED_ADAPTER ) {
  246.            TraceMsg( L" NCF_MULTIPORT_INSTANCED_ADAPTER" );
  247.         }
  248.  
  249.         TraceMsg( L"\n" );
  250.     }
  251.     else {
  252.  
  253.         TraceMsg( L"   GetCharacteristics failed(HRESULT %x).\n",
  254.                hr );
  255.     }
  256.  
  257.     hr = pncc->GetId( &lpsz );
  258.  
  259.     if ( hr == S_OK ) {
  260.  
  261.         TraceMsg( L"   \t\tHardware Id: %s\n",
  262.                lpsz );
  263.  
  264.         CoTaskMemFree( lpsz );
  265.     }
  266.     else {
  267.  
  268.         TraceMsg( L"   GetId failed(HRESULT %x).\n",
  269.                hr );
  270.     }
  271.  
  272.     hr = pncc->GetBindName( &lpsz );
  273.  
  274.     if ( hr == S_OK ) {
  275.  
  276.         TraceMsg( L"   \t\tBindName: %s\n",
  277.                lpsz );
  278.  
  279.         CoTaskMemFree( lpsz );
  280.     }
  281.     else {
  282.  
  283.         TraceMsg( L"   GetBindName failed(HRESULT %x).\n",
  284.                hr );
  285.     }
  286.  
  287.     if ( dwChars & NCF_PHYSICAL ) {
  288.         hr = pncc->GetDeviceStatus( &ulStatus );
  289.  
  290.         if ( hr == S_OK ) {
  291.  
  292.             TraceMsg( L"   \t\tDeviceStatus: %#x\n",
  293.                       ulStatus );
  294.         }
  295.         else {
  296.  
  297.             TraceMsg( L"   GetDeviceStatus failed(HRESULT %x).\n",
  298.                       hr );
  299.         }
  300.     }
  301.  
  302.   return;
  303. }
  304.  
  305. #endif
  306.  
  307. HRESULT HrFindInstance (INetCfg *pnc,
  308.                         GUID &guidInstance,
  309.                         INetCfgComponent **ppnccMiniport)
  310. {
  311.     IEnumNetCfgComponent  *pencc;
  312.     INetCfgComponent      *pncc;
  313.     GUID                  guid;
  314.     WCHAR                 szGuid[MAX_PATH+1];
  315.     ULONG                 ulCount;
  316.     BOOL                  found;
  317.     HRESULT               hr;
  318.  
  319.     TraceMsg( L"-->HrFindInstance.\n" );
  320.  
  321.     hr = pnc->EnumComponents( &GUID_DEVCLASS_NET,
  322.                             &pencc );
  323.  
  324.     if ( hr == S_OK ) {
  325.  
  326.         StringFromGUID2( guidInstance,
  327.                       szGuid,
  328.                       MAX_PATH+1 );
  329.  
  330.         TraceMsg( L"  Looking for component with InstanceGuid %s\n",
  331.                szGuid );
  332.  
  333.         hr = pencc->Next( 1,
  334.                           &pncc,
  335.                           &ulCount );
  336.  
  337.         for ( found=FALSE; (hr == S_OK) && (found == FALSE); ) {
  338.  
  339.             hr = pncc->GetInstanceGuid( &guid );
  340.  
  341.             if ( hr == S_OK ) {
  342.  
  343.                 StringFromGUID2( guid,
  344.                                 szGuid,
  345.                                 MAX_PATH+1 );
  346.  
  347.                 TraceMsg( L"  Found component with InstanceGuid %s\n",
  348.                          szGuid );
  349.  
  350.                 found = IsEqualGUID( guid,
  351.                                     guidInstance );
  352.  
  353.                 if ( found == FALSE ) {
  354.  
  355.                     ReleaseObj( pncc );
  356.  
  357.                     hr = pencc->Next( 1,
  358.                                     &pncc,
  359.                                     &ulCount );
  360.                 }
  361.                 else {
  362.                     *ppnccMiniport = pncc;
  363.                 }
  364.             }
  365.         }   
  366.  
  367.         ReleaseObj( pencc );
  368.     }
  369.     else {
  370.  
  371.      TraceMsg( L"   EnumComponents failed(HRESULT = %x).\n",
  372.                hr );
  373.     }
  374.  
  375.     TraceMsg( L"<--HrFindInstance(HRESULT = %x).\n",
  376.             hr );
  377.  
  378.     return hr;
  379. }
  380.  
  381. LONG AddToMultiSzValue( HKEY hkeyAdapterGuid,
  382.                         LPWSTR szMiniportGuid)
  383. {
  384.     LPWSTR lpCurrentValue=NULL;
  385.     LPWSTR lpNewValue=NULL;
  386.     DWORD  dwLen;
  387.     DWORD  dwNewLen;
  388.     LONG   lResult;
  389.  
  390.     dwLen = 0;
  391.     lResult =  RegQueryValueExW(
  392.                         hkeyAdapterGuid,
  393.                         c_szUpperBindings,
  394.                         NULL,
  395.                         NULL,
  396.                         NULL,
  397.                         &dwLen );
  398.  
  399.     if ( dwLen != 0 ) {
  400.         lpCurrentValue = (LPWSTR)calloc( dwLen, 1 );
  401.  
  402.         if ( lpCurrentValue ) {
  403.  
  404.         lResult =  RegQueryValueExW( hkeyAdapterGuid,
  405.                                      c_szUpperBindings,
  406.                                      NULL,
  407.                                      NULL,
  408.                                      (LPBYTE)lpCurrentValue,
  409.                                      &dwLen );
  410.  
  411.         }
  412.         else {
  413.  
  414.             lResult = ERROR_NOT_ENOUGH_MEMORY;
  415.         }
  416.     }
  417.     else {
  418.         dwLen = sizeof(WCHAR);
  419.         lpCurrentValue = (LPWSTR)calloc( dwLen, 1 );
  420.  
  421.         if ( !lpCurrentValue ) {
  422.             lResult = ERROR_NOT_ENOUGH_MEMORY;
  423.         }
  424.         else {
  425.             lResult = ERROR_SUCCESS;
  426.         }
  427.     }
  428.  
  429.     if ( lResult == ERROR_SUCCESS ) {
  430.  
  431.         dwNewLen = dwLen + ((wcslen(szMiniportGuid) + 1) * sizeof(WCHAR));
  432.  
  433.         lpNewValue = (LPWSTR)malloc( dwNewLen );
  434.  
  435.         if ( lpNewValue ) {
  436.  
  437.             wcscpy( lpNewValue, szMiniportGuid );
  438.  
  439.             CopyMemory( lpNewValue+wcslen(szMiniportGuid)+1,
  440.                         lpCurrentValue,
  441.                         dwLen );
  442.  
  443.             lResult = RegSetValueExW( hkeyAdapterGuid,
  444.                                     c_szUpperBindings,
  445.                                     0,
  446.                                     REG_MULTI_SZ,
  447.                                     (LPBYTE)lpNewValue,
  448.                                     dwNewLen );
  449.         }
  450.         else {
  451.             lResult = ERROR_NOT_ENOUGH_MEMORY;
  452.         }
  453.     }
  454.  
  455.     if ( lpCurrentValue ) {
  456.         free( lpCurrentValue );
  457.     }
  458.  
  459.     if ( lpNewValue ) {
  460.         free( lpNewValue );
  461.     }
  462.  
  463.     return lResult;
  464. }
  465.  
  466. LONG DeleteFromMultiSzValue( HKEY hkeyAdapterGuid,
  467.                              LPWSTR szMiniportGuid)
  468. {
  469.     LPWSTR lpCurrentValue=NULL;
  470.     LPWSTR lpNewValue=NULL;
  471.     LPWSTR lpCurrentValueTemp;
  472.     LPWSTR lpNewValueTemp;
  473.     DWORD  dwLen;
  474.     DWORD  dwNewLen;
  475.     LONG   lResult;
  476.  
  477.     dwLen = 0;
  478.     lResult =  RegQueryValueExW(
  479.                         hkeyAdapterGuid,
  480.                         c_szUpperBindings,
  481.                         NULL,
  482.                         NULL,
  483.                         NULL,
  484.                         &dwLen );
  485.  
  486.     if ( lResult == ERROR_SUCCESS ) {
  487.         lpCurrentValue = (LPWSTR)calloc( dwLen, 1 );
  488.         lpNewValue = (LPWSTR)calloc( dwLen, 1 );
  489.  
  490.         if ( lpCurrentValue && lpNewValue ) {
  491.  
  492.             lResult =  RegQueryValueExW(
  493.                               hkeyAdapterGuid,
  494.                               c_szUpperBindings,
  495.                               NULL,
  496.                               NULL,
  497.                               (LPBYTE)lpCurrentValue,
  498.                               &dwLen );
  499.  
  500.             if ( lResult == ERROR_SUCCESS ) {
  501.  
  502.                 lpCurrentValueTemp = lpCurrentValue;
  503.                 lpNewValueTemp = lpNewValue;
  504.  
  505.                 while( wcslen(lpCurrentValueTemp) ) {
  506.  
  507.                     if ( _wcsicmp(lpCurrentValueTemp, szMiniportGuid) ) {
  508.                         wcscpy( lpNewValueTemp, lpCurrentValueTemp );
  509.                         lpNewValueTemp += wcslen(lpNewValueTemp) + 1;
  510.                     }
  511.  
  512.                     lpCurrentValueTemp += wcslen(lpCurrentValueTemp) + 1;
  513.                 } //end of while
  514.  
  515.                 dwNewLen = dwLen - ((wcslen(szMiniportGuid) + 1) * sizeof(WCHAR));
  516.  
  517.                 if ( dwNewLen > 2 ) {
  518.                     lResult = RegSetValueExW( hkeyAdapterGuid,
  519.                                           c_szUpperBindings,
  520.                                           0,
  521.                                           REG_MULTI_SZ,
  522.                                           (LPBYTE)lpNewValue,
  523.                                           dwNewLen );
  524.                 }
  525.                 else {
  526.                     lResult = RegDeleteValueW( hkeyAdapterGuid,
  527.                                                c_szUpperBindings );
  528.                 }
  529.             }
  530.         }
  531.         else {
  532.             lResult = ERROR_NOT_ENOUGH_MEMORY;
  533.         }
  534.     }
  535.  
  536.     if ( lpCurrentValue ) {
  537.         free( lpCurrentValue );
  538.     }
  539.  
  540.     if ( lpNewValue ) {
  541.         free( lpNewValue );
  542.     }
  543.  
  544.     return lResult;
  545. }
  546.  
  547. LPWSTR AddDevicePrefix (LPWSTR lpStr)
  548. {
  549.     LPWSTR lpNewStr;
  550.  
  551.     lpNewStr = (LPWSTR)malloc( (wcslen(lpStr) +
  552.                               wcslen(c_szDevicePrefix) + 1) *
  553.                                            sizeof(WCHAR) );
  554.     if ( lpNewStr ) {
  555.  
  556.         wcscpy( lpNewStr, c_szDevicePrefix );
  557.         wcscat( lpNewStr, lpStr );
  558.     }
  559.  
  560.     return lpNewStr;
  561. }
  562.  
  563. LPWSTR RemoveDevicePrefix (LPWSTR lpStr)
  564. {
  565.     int i;
  566.     LPWSTR lpNewStr;
  567.     LPWSTR lpTemp;
  568.  
  569.     lpTemp = wcsrchr( lpStr, '\\' );
  570.  
  571.     if ( lpTemp ) {
  572.  
  573.         lpNewStr = _wcsdup( lpTemp+1 );
  574.     }
  575.     else {
  576.         lpNewStr = NULL;
  577.     }
  578.  
  579.     return lpNewStr;
  580. }
  581.  
  582. HRESULT OpenInterfaceHandle( LPWSTR adapterGuid, HANDLE *pHandle)
  583. {
  584.     WCHAR deviceGuid[128];
  585.     HANDLE intfHandle;
  586.     HRESULT hr = S_OK;
  587.     DWORD dwDummy;
  588.     DWORD dwErr = ERROR_SUCCESS;
  589.  
  590.     wcscpy( deviceGuid, L"\\DEVICE\\" );
  591.     wcscat( deviceGuid, adapterGuid );
  592.  
  593.     intfHandle = CreateFileA(
  594.         "\\\\.\\\\Ndisuio",
  595.         GENERIC_READ | GENERIC_WRITE,
  596.         FILE_SHARE_READ | FILE_SHARE_WRITE,
  597.         NULL,
  598.         OPEN_EXISTING,
  599.         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  600.         INVALID_HANDLE_VALUE);
  601.  
  602.     if ( intfHandle == INVALID_HANDLE_VALUE ) {
  603.         hr = E_FAIL;    
  604.         TraceMsg(L"CreateFile failed\n");
  605.     }
  606.     if (hr = S_OK)
  607.         TraceMsg(L"CreateFile successful\n");
  608.  
  609.     if ( hr == S_OK )
  610.     {
  611.         if ( !DeviceIoControl(
  612.             intfHandle,
  613.             IOCTL_NDISUIO_OPEN_DEVICE,
  614.             (LPVOID)deviceGuid,
  615.             wcslen(deviceGuid)*sizeof(WCHAR),
  616.             NULL,
  617.             0,
  618.             &dwDummy,
  619.             NULL) ) {
  620.                 dwErr = GetLastError();
  621.                 hr = E_FAIL;
  622.                 TraceMsg(L"Open Device failed with Error %d\n", dwErr);
  623.             }
  624.         else {
  625.             *pHandle = intfHandle;
  626.             hr = S_OK;
  627.             TraceMsg(L"Open Device successful\n");
  628.         }
  629.     }
  630.     return hr;
  631. }
  632.  
  633.