//+--------------------------------------------------------------------------- // // Function: HrShowNetComponents // // Purpose: Display installed net components. // // Arguments: None // // Returns: S_OK on success, otherwise an error code // // Notes: // HRESULT HrShowNetComponents() { HRESULT hr=S_OK; PCWSTR szClassName; static const PCWSTR c_aszClassNames[] = { L"Network Adapters", L"Network Protocols", L"Network Services", L"Network Clients" }; INetCfg* pnc; // get INetCfg interface hr = HrGetINetCfg(FALSE, &pnc); if (SUCCEEDED(hr)) { for (int i=0; i<4; i++) { LogPrintf(_T("\n%s\n-----------------\n"), c_aszClassNames[i]); (void) HrShowNetComponents(pnc, c_aguidClass[i]); } // release INetCfg hr = HrReleaseINetCfg(FALSE, pnc); } return hr; }
//+--------------------------------------------------------------------------- // // Function: HrUninstallNetComponent // // Purpose: Initialize INetCfg and uninstall a component // // Arguments: // szComponentId [in] InfId of component to uninstall (e.g. MS_TCPIP) // // Returns: S_OK or NETCFG_S_REBOOT on success, otherwise an error code // // Notes: // HRESULT HrUninstallNetComponent(IN PCWSTR szComponentId) { HRESULT hr=S_OK; INetCfg* pnc = NULL ; LogPrintf(_T("Trying to uninstall '%s'...\n"), szComponentId); // get INetCfg interface hr = HrGetINetCfg(TRUE, &pnc); if (SUCCEEDED(hr)) { // uninstall szComponentId hr = HrUninstallNetComponent(pnc, szComponentId); if (S_OK == hr) { // Apply the changes hr = pnc->Apply(); } else if (S_FALSE == hr) { LogPrintf(_T("'%s' is not installed\n"), szComponentId); } // release INetCfg (void) HrReleaseINetCfg(TRUE, pnc); } // show success/failure message ShowHrMessage(hr); return hr; }
//+--------------------------------------------------------------------------- // // Function: HrUninstallNetComponent // // Purpose: Initialize INetCfg and uninstall a component // // Arguments: // szComponentId [in] InfId of component to uninstall (e.g. MS_TCPIP) // // Returns: S_OK or NETCFG_S_REBOOT on success, otherwise an error code // // Notes: // HRESULT HrUninstallNetComponent(IN PCWSTR szComponentId) { HRESULT hr=S_OK; INetCfg* pnc; // get INetCfg interface hr = HrGetINetCfg(TRUE, &pnc); if (SUCCEEDED(hr)) { // uninstall szComponentId hr = HrUninstallNetComponent(pnc, szComponentId); if (S_OK == hr) { // Apply the changes hr = pnc->Apply(); } // release INetCfg (void) HrReleaseINetCfg(TRUE, pnc); } // show success/failure message ShowHrMessage(hr); return hr; }
//+--------------------------------------------------------------------------- // // Function: HrShowBindingPathsBelowComponent // // Purpose: Display all binding paths that start with // the specified component // // Arguments: // szComponentId [in] id of given component (e.g. MS_TCPIP) // // Returns: S_OK on success, otherwise an error code // // Notes: // HRESULT HrShowBindingPathsOfComponent(IN PCWSTR szComponentId) { HRESULT hr=S_OK; INetCfg* pnc=NULL; INetCfgComponent* pncc=NULL; INetCfgBindingPath* pncbp=NULL; // get INetCfg interface hr = HrGetINetCfg(FALSE, &pnc); if (SUCCEEDED(hr)) { // get INetCfgComponent for szComponentId hr = pnc->FindComponent(szComponentId, &pncc); if (S_OK == hr) { LogPrintf(_T("Binding paths starting with '%s'\n\n"), szComponentId); while (S_OK == (hr = HrGetNextBindingPath(pncc, EBP_BELOW, &pncbp))) { // display the binding path hr = HrShowBindingPath(pncbp); ReleaseObj(pncbp); } LogPrintf(_T("Binding paths ending with '%s'\n\n"), szComponentId); while (S_OK == (hr = HrGetNextBindingPath(pncc, EBP_ABOVE, &pncbp))) { // display the binding path hr = HrShowBindingPath(pncbp); ReleaseObj(pncbp); } ReleaseObj(pncc); } // release INetCfg hr = HrReleaseINetCfg(FALSE, pnc); } return hr; }
HRESULT InstallSpecifiedComponent(LPTSTR lpszInfFile, LPTSTR lpszPnpID, const GUID* pguidClass) { INetCfg* pnc; LPTSTR lpszApp; HRESULT hr; hr = HrGetINetCfg(TRUE, APP_NAME, &pnc, &lpszApp); if (hr == S_OK) { // // Install the network component. // hr = HrInstallNetComponent(pnc, lpszPnpID, pguidClass, lpszInfFile); if ((hr == S_OK) || (hr == NETCFG_S_REBOOT)) { hr = pnc->Apply(); } else { if (hr != HRESULT_FROM_WIN32(ERROR_CANCELLED)) { ErrMsg(hr, L"Couldn't install the network component."); } } HrReleaseINetCfg(pnc, TRUE); } else { if ((hr == NETCFG_E_NO_WRITE_LOCK) && lpszApp) { ErrMsg(hr, L"%s currently holds the lock, try later.", lpszApp); CoTaskMemFree(lpszApp); } else { ErrMsg(hr, L"Couldn't the get notify object interface."); } } return hr; }
//+--------------------------------------------------------------------------- // // Function: HrIsComponentInstalled // // Purpose: Find out if a component is installed // // Arguments: // szComponentId [in] id of component to search // // Returns: S_OK if installed, // S_FALSE if not installed, // otherwise an error code // // Author: kumarp 11-February-99 // // Notes: // HRESULT HrIsComponentInstalled(IN PCWSTR szComponentId) { HRESULT hr=S_OK; INetCfg* pnc; INetCfgComponent* pncc; hr = HrGetINetCfg(FALSE, &pnc); if (S_OK == hr) { hr = pnc->FindComponent(szComponentId, &pncc); if (S_OK == hr) { ReleaseObj(pncc); } (void) HrReleaseINetCfg(FALSE, pnc); } return hr; }
//+--------------------------------------------------------------------------- // // Function: HrInstallNetComponent // // Purpose: Install the specified net component // // Arguments: // szComponentId [in] component to install // nc [in] class of the component // szInfFullPath [in] full path to primary INF file // required if the primary INF and other // associated files are not pre-copied to // the right destination dirs. // Not required when installing MS components // since the files are pre-copied by // Windows NT Setup. // // Returns: S_OK or NETCFG_S_REBOOT on success, otherwise an error code // // Notes: // HRESULT HrInstallNetMonProtocol() { HRESULT hr=S_OK; INetCfg* pnc; hr = HrGetINetCfg(TRUE, &pnc); if (SUCCEEDED(hr)) { // install szComponentId hr = HrInstallNetComponent(pnc, L"MS_NetMon", &GUID_DEVCLASS_NETTRANS); if (SUCCEEDED(hr)) { // Apply the changes hr = pnc->Apply(); } // release INetCfg (void) HrReleaseINetCfg(TRUE, pnc); } return hr; }
//+--------------------------------------------------------------------------- // // Function: HrUninstallNetComponent // // Purpose: Initialize INetCfg and uninstall a component // // Arguments: // szComponentId [in] InfId of component to uninstall (e.g. MS_TCPIP) // // Returns: S_OK or NETCFG_S_REBOOT on success, otherwise an error code // // Notes: // HRESULT HrUninstallNetMonProtocol() { HRESULT hr=S_OK; INetCfg* pnc; // get INetCfg interface hr = HrGetINetCfg(TRUE, &pnc); if (SUCCEEDED(hr)) { // uninstall szComponentId hr = HrUninstallNetComponent(pnc, L"MS_NetMon"); if (S_OK == hr) { // Apply the changes hr = pnc->Apply(); } // release INetCfg (void) HrReleaseINetCfg(TRUE, pnc); } return hr; }
DWORD UninstallDriver() { TRACE_ENTER("UninstallDriver"); //_tprintf( _T("Uninstalling %s...\n"), NDISPROT_FRIENDLY_NAME ); // int nResult = MessageBox(NULL, _T("Uninstalling Driver..."), NDISPROT_FRIENDLY_NAME, MB_OKCANCEL | MB_ICONINFORMATION); // // if (nResult != IDOK) // { // return 0; // } INetCfg* pnc; INetCfgComponent* pncc; INetCfgClass* pncClass; INetCfgClassSetup* pncClassSetup; LPTSTR lpszApp; GUID guidClass; OBO_TOKEN obo; HRESULT hr; hr = HrGetINetCfg(TRUE, APP_NAME, &pnc, &lpszApp); if (hr == S_OK) { // // Get a reference to the network component to uninstall. // hr = pnc->FindComponent(NDISLWF_SERVICE_PNP_DEVICE_ID, &pncc); if (hr == S_OK) { // // Get the class GUID. // hr = pncc->GetClassGuid(&guidClass); if (hr == S_OK) { // // Get a reference to component's class. // hr = pnc->QueryNetCfgClass(&guidClass, IID_INetCfgClass, (PVOID *)&pncClass); if (hr == S_OK) { // // Get the setup interface. // hr = pncClass->QueryInterface(IID_INetCfgClassSetup, (LPVOID *)&pncClassSetup); if (hr == S_OK) { // // Uninstall the component. // ZeroMemory(&obo, sizeof(OBO_TOKEN)); obo.Type = OBO_USER; hr = pncClassSetup->DeInstall(pncc, &obo, NULL); if ((hr == S_OK) || (hr == NETCFG_S_REBOOT)) { hr = pnc->Apply(); if ((hr != S_OK) && (hr != NETCFG_S_REBOOT)) { ErrMsg(hr, L"Couldn't apply the changes after" L" uninstalling %s.", NDISLWF_SERVICE_PNP_DEVICE_ID); } else { TRACE_EXIT("UninstallDriver"); return 1; } } else { ErrMsg(hr, L"Failed to uninstall %s.", NDISLWF_SERVICE_PNP_DEVICE_ID); } ReleaseRef(pncClassSetup); } else { ErrMsg(hr, L"Couldn't get an interface to setup class."); } ReleaseRef(pncClass); } else { ErrMsg(hr, L"Couldn't get a pointer to class interface " L"of %s.", NDISLWF_SERVICE_PNP_DEVICE_ID); } } else { ErrMsg(hr, L"Couldn't get the class guid of %s.", NDISLWF_SERVICE_PNP_DEVICE_ID); } ReleaseRef(pncc); } else { ErrMsg(hr, L"Couldn't get an interface pointer to %s.", NDISLWF_SERVICE_PNP_DEVICE_ID); } HrReleaseINetCfg(pnc, TRUE); } else { if ((hr == NETCFG_E_NO_WRITE_LOCK) && lpszApp) { ErrMsg(hr, L"%s currently holds the lock, try later.", lpszApp); CoTaskMemFree(lpszApp); } else { ErrMsg(hr, L"Couldn't get the notify object interface."); } } TRACE_EXIT("UninstallDriver"); return 0; }
//+--------------------------------------------------------------------------- // // Function: HrInstallNetComponent // // Purpose: Install the specified net component // // Arguments: // szComponentId [in] component to install // nc [in] class of the component // szInfFullPath [in] full path to primary INF file // required if the primary INF and other // associated files are not pre-copied to // the right destination dirs. // Not required when installing MS components // since the files are pre-copied by // Windows NT Setup. // // Returns: S_OK or NETCFG_S_REBOOT on success, otherwise an error code // // Notes: // HRESULT HrInstallNetComponent(IN PCTSTR szComponentId, IN enum NetClass nc, IN PCTSTR szInfFullPath) { HRESULT hr=S_OK; INetCfg* pnc; // cannot install net adapters this way. they have to be // enumerated/detected and installed by PnP if ((nc == NC_NetProtocol) || (nc == NC_NetService) || (nc == NC_NetClient)) { LogPrintf(_T("Trying to install '%s'...\n"), szComponentId); // if full path to INF has been specified, the INF // needs to be copied using Setup API to ensure that any other files // that the primary INF copies will be correctly found by Setup API // size_t cchPath; StringCchLength(szInfFullPath, MAX_PATH, &cchPath); if (szInfFullPath && cchPath) { TCHAR szInfNameAfterCopy[MAX_PATH+1]; if (SetupCopyOEMInf( szInfFullPath, NULL, // other files are in the // same dir. as primary INF SPOST_PATH, // first param. contains path to INF 0, // default copy style szInfNameAfterCopy, // receives the name of the INF // after it is copied to %windir%\inf MAX_PATH, // max buf. size for the above NULL, // receives required size if non-null NULL)) // optionally retrieves filename // component of szInfNameAfterCopy { LogPrintf(_T("...%s was copied to %s\n"), szInfFullPath, szInfNameAfterCopy); } else { DWORD dwError = GetLastError(); LogPrintf(_T("Error on SetupCopyOEMInf, error %d"), dwError); hr = HRESULT_FROM_WIN32(dwError); } } if (S_OK == hr) { // get INetCfg interface hr = HrGetINetCfg(TRUE, &pnc); if (SUCCEEDED(hr)) { // install szComponentId LogPrintf(_T("Installing %s\n"), szComponentId); hr = HrInstallNetComponent(pnc, szComponentId, c_aguidClass[nc]); if (SUCCEEDED(hr)) { LogPrintf(_T("Installed successfully\n")); // Apply the changes LogPrintf(_T("Applying changes\n")); hr = pnc->Apply(); LogPrintf(_T("Applied successfully\n")); } else { LogPrintf(_T("%s installation failed.\n")); } // release INetCfg (void) HrReleaseINetCfg(TRUE, pnc); } } // show success/failure message ShowHrMessage(hr); } return hr; }