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

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\install
  4.  * File Name: install.cpp
  5.  * Purpose  : Code to install the VirtualWiFi driver
  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_C void ausage(void);
  18. EXTERN_C void RenameAdapter(const GUID *AdapterGuid, const WCHAR *NewName);
  19. extern VOID RenameConnection ( LPCWSTR ConnectionName );
  20. extern bool Verbose;
  21.  
  22. HRESULT
  23. HrCreateINetCfg(
  24.     IN bool fAcquireWriteLock,
  25.     OUT INetCfg** ppINetCfg)
  26. {
  27.     HRESULT hr;
  28.     INetCfg* pINetCfg;
  29.  
  30.     //
  31.     // Get the INetCfg interface.
  32.     //
  33.     hr = CoCreateInstance(
  34.         CLSID_CNetCfg,
  35.         NULL,
  36.         CLSCTX_INPROC_SERVER | CLSCTX_NO_CODE_DOWNLOAD,
  37.         IID_INetCfg,
  38.         reinterpret_cast<void**>(&pINetCfg));
  39.     if (! SUCCEEDED(hr))
  40.         return hr;
  41.  
  42.     INetCfgLock * pnclock = NULL;
  43.  
  44.     if (fAcquireWriteLock) {
  45.         //
  46.         // Get the locking interface.
  47.         //
  48.         hr = pINetCfg->QueryInterface(IID_INetCfgLock,
  49.                                  reinterpret_cast<LPVOID *>(&pnclock));
  50.         if (SUCCEEDED(hr)) {
  51.             LPWSTR pwszLockHolder;
  52.             //
  53.             // Attempt to lock the INetCfg for read/write.
  54.             //
  55.             hr = pnclock->AcquireWriteLock(100, L"InstallVirtualWiFi",
  56.                 &pwszLockHolder);
  57.             if (hr == S_FALSE) {
  58.                 //
  59.                 // Could not acquire the lock.
  60.                 //
  61.                 hr = NETCFG_E_NO_WRITE_LOCK;
  62.                 printf("The write lock could not be acquired.\n");
  63.                 printf("You must close %ls first.\n", pwszLockHolder);
  64.  
  65.             }
  66.             if (pwszLockHolder != NULL)
  67.                 CoTaskMemFree(pwszLockHolder);
  68.         }
  69.     }
  70.  
  71.     if (SUCCEEDED(hr)) {
  72.         hr = pINetCfg->Initialize(NULL);
  73.         if (SUCCEEDED(hr)) {
  74.             //
  75.             // Add a reference for our caller.
  76.             //
  77.             pINetCfg->AddRef();
  78.             *ppINetCfg = pINetCfg;
  79.         }
  80.         else {
  81.             if (pnclock != NULL)
  82.                 pnclock->ReleaseWriteLock();
  83.         }
  84.     }
  85.  
  86.     if (pnclock != NULL)
  87.         pnclock->Release();
  88.  
  89.     pINetCfg->Release();
  90.     return S_OK;
  91. }
  92.  
  93. HRESULT
  94. AddOrRemoveComponent(
  95.     INetCfg *pINetCfg,
  96.     const GUID *Guid,
  97.     const WCHAR *Name,
  98.     const WCHAR *ConnectionName,
  99.     bool AddOrRemove)
  100. {
  101.     INetCfgClassSetup *pSetup;
  102.     HRESULT hr;
  103.  
  104.     //
  105.     // Get the setup interface used for installing
  106.     // and uninstalling components.
  107.     //
  108.     hr = pINetCfg->QueryNetCfgClass(
  109.         Guid,
  110.         IID_INetCfgClassSetup,
  111.         (void**)&pSetup);
  112.     if (! SUCCEEDED(hr)) {
  113.         fprintf(stderr, "VirtualWiFi: Could not get setup interface.\n");
  114.         exit(1);
  115.     }
  116.  
  117.     OBO_TOKEN OboToken;
  118.     INetCfgComponent *pIComp;
  119.  
  120.     ZeroMemory(&OboToken, sizeof OboToken);
  121.     OboToken.Type = OBO_USER;
  122.  
  123.     if (AddOrRemove) {
  124.         if (Verbose) 
  125.             printf("Installing %ls...\n", Name);
  126.  
  127.         hr = pSetup->Install(
  128.                 Name,
  129.                 &OboToken,
  130.                 0, 0,
  131.                 NULL, NULL,
  132.                 &pIComp);
  133.  
  134.         if(SUCCEEDED(hr) && Verbose)
  135.             printf("The install was successful");
  136.  
  137.         if (pIComp != NULL) {
  138.             GUID AdapterGuid;
  139.  
  140.             if ((ConnectionName != NULL) &&    SUCCEEDED(pIComp->GetInstanceGuid(&AdapterGuid))) {
  141.                 if (Verbose) 
  142.                     printf("Succeeded to this point\n");
  143.                 RenameAdapter(&AdapterGuid, ConnectionName);
  144.             }
  145.             pIComp->Release();
  146.         }
  147.     }
  148.     else {
  149.         //
  150.         // Need to remove the component.
  151.         // Find it first.
  152.         //
  153.         hr = pINetCfg->FindComponent(Name, &pIComp);
  154.  
  155.         if (SUCCEEDED(hr)) {
  156.             if(Verbose)
  157.                 printf("Uninstalling %ls...\n", Name);
  158.  
  159.             hr = pSetup->DeInstall(pIComp, &OboToken, NULL);
  160.  
  161.             pIComp->Release();
  162.         }
  163.         else {
  164.             printf("%ls is not installed.\n", Name);
  165.         }
  166.     }
  167.  
  168.     if (SUCCEEDED(hr)) {
  169.         if (hr == NETCFG_S_REBOOT) {
  170.             hr = S_OK;
  171.             printf("A reboot is required to complete this action.\n");
  172.         }
  173.         else {
  174.             if (Verbose) 
  175.                 printf("Succeeded.\n");
  176.         }
  177.     }
  178.     else {
  179.         printf("Failed to complete the action.\n");
  180.  
  181.         if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) {
  182.             hr = S_OK;
  183.             printf("The INF file for VirtualWiFi could not be found.\n");
  184.         }
  185.         else if (hr == NETCFG_E_NEED_REBOOT) {
  186.             printf("A reboot is required before any further changes can be made.\n");
  187.         }
  188.         else {
  189.             printf("Error %x\n", hr);
  190.         }
  191.     }
  192.  
  193.     pSetup->Release();
  194.  
  195.     return hr;
  196. }
  197.  
  198. void
  199. ausage(void)
  200. {
  201.     fprintf(stderr, "You do not have local Administrator privileges.\n");
  202.     exit(1);
  203. }
  204.  
  205. static HRESULT
  206. InternalAddOrRemove(bool AddOrRemove)
  207. {
  208.     INetCfg *pINetCfg;
  209.     HRESULT hr;
  210.  
  211.     hr = HrCreateINetCfg(TRUE, &pINetCfg);
  212.     if (! SUCCEEDED(hr)) {
  213.         if (hr == NETCFG_E_NO_WRITE_LOCK) {
  214.             fprintf(stderr, "VirtualWiFi: Could not acquire config write lock\n");
  215.             exit(1);
  216.         }
  217.         else if (hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED)) {
  218.             ausage();
  219.         }
  220.         else {
  221.             fprintf(stderr, "VirtualWiFi: HrCreateINetCfg -> %x\n", hr);
  222.             exit(1);
  223.         }
  224.     }
  225.  
  226.     hr = AddOrRemoveComponent(pINetCfg, &GUID_DEVCLASS_NETTRANS,
  227.                          L"MS_VWiFiP", NULL, AddOrRemove);
  228.     //AddOrRemoveComponent(pINetCfg, &GUID_DEVCLASS_NET,
  229.     //                     L"MS_VWiFiMP", L"VirtualWiFi Miniport Driver2", AddOrRemove);
  230.     pINetCfg->Release();
  231.     return hr;
  232. }
  233.  
  234. static void
  235. InstallInf(const char *SetupKitDir, const char *InfName)
  236. {
  237.     char InfFile[MAX_PATH];
  238.     BOOL ok;
  239.  
  240.     if (FAILED(StringCchCopyA(InfFile, MAX_PATH, SetupKitDir)) ||
  241.         FAILED(StringCchCatA(InfFile, MAX_PATH, "\\")) ||
  242.         FAILED(StringCchCatA(InfFile, MAX_PATH, InfName))) {
  243.  
  244.         fprintf(stderr, "VirtualWiFi: directory name too long\n");
  245.         exit(1);
  246.     }
  247.  
  248.     ok = SetupCopyOEMInfA(InfFile,
  249.                           SetupKitDir,
  250.                           SPOST_PATH,
  251.                           0, // CopyStyle
  252.                           NULL, 0, NULL, NULL);
  253.     if (! ok) {
  254.         fprintf(stderr, "VirtualWiFi: SetupCopyOEMInfA(%s) -> %d\n",
  255.                 InfFile, GetLastError());
  256.         exit(1);
  257.     }
  258.  
  259. }
  260.  
  261. extern HRESULT
  262. AddOrRemoveVirtualWiFi(bool AddOrRemove, const char *SetupKitDir)
  263. {
  264.     HRESULT hr = S_OK;
  265.     boolean fInitCom = TRUE;
  266.  
  267.     if (SetupKitDir != NULL) {
  268.         InstallInf(SetupKitDir, "VWiFip.inf");
  269.         InstallInf(SetupKitDir, "VWiFi_mp.inf");
  270.     }
  271.  
  272.     //
  273.     // Initialize COM.
  274.     //
  275.     hr = CoInitializeEx(NULL,
  276.                         COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
  277.     if (hr == RPC_E_CHANGED_MODE) {
  278.         //
  279.         // If we changed mode, then we won't uninitialize COM when we are done.
  280.         //
  281.         hr = S_OK;
  282.         fInitCom = FALSE;
  283.     }
  284.  
  285.     if (! SUCCEEDED(hr)) {
  286.         fprintf(stderr, "VirtualWiFi: AddOrRemoveVirtualWiFi: could not initialize COM\n");
  287.         exit(1);
  288.     }
  289.  
  290.     hr = InternalAddOrRemove(AddOrRemove);
  291.  
  292.     if (fInitCom)
  293.         CoUninitialize();
  294.  
  295.    return hr;
  296. }
  297.