HRESULT HrGetComponentEnum (INetCfg* pnc, IN const GUID* pguidClass, OUT IEnumNetCfgComponent **ppencc) { INetCfgClass *pncclass; HRESULT hr; *ppencc = NULL; // // Get the class reference. // hr = pnc->QueryNetCfgClass( pguidClass, IID_INetCfgClass, (PVOID *)&pncclass ); if ( hr == S_OK ) { // // Get the enumerator reference. // hr = pncclass->EnumComponents( ppencc ); // // We don't need the class reference any more. // ReleaseRef( pncclass ); } return hr; }
HRESULT CMuxVirtualMiniport::DeInstall (VOID) { INetCfgClass *pncClass; INetCfgClassSetup *pncClassSetup; INetCfgComponent *pnccMiniport; HRESULT hr; LPWSTR *pmszwRefs=NULL; OBO_TOKEN *pOboToken=NULL; TraceMsg( L"-->CMuxVirtualMiniport::DeInstall.\n" ); hr = m_pnc->QueryNetCfgClass( &GUID_DEVCLASS_NET, IID_INetCfgClass, (void **)&pncClass ); if ( hr == S_OK ) { hr = pncClass->QueryInterface( IID_INetCfgClassSetup, (void **)&pncClassSetup ); if ( hr == S_OK ) { hr = HrFindInstance( m_pnc, m_guidMiniport, &pnccMiniport ); if ( hr == S_OK ) { TraceMsg( L" Found the miniport instance to uninstall.\n" ); hr = pncClassSetup->DeInstall( pnccMiniport, pOboToken, pmszwRefs ); ReleaseObj( pnccMiniport ); } else { TraceMsg( L" Didn't find the miniport instance to uninstall.\n" ); } ReleaseObj( pncClassSetup ); } else { TraceMsg( L" QueryInterface failed.\n" ); } ReleaseObj( pncClass ); } else { TraceMsg( L" QueryNetCfgClass failed.\n" ); } TraceMsg( L"<--CMuxVirtualMiniport::DeInstall(HRESULT = %x).\n", hr ); return hr; }
//+--------------------------------------------------------------------------- // // Function: HrUninstallNetComponent // // Purpose: Uninstall the specified component. // // Arguments: // pnc [in] pointer to INetCfg object // szComponentId [in] component to uninstall // // Returns: S_OK or NETCFG_S_REBOOT on success, otherwise an error code // // Notes: // HRESULT HrUninstallNetComponent(IN INetCfg* pnc, IN PCWSTR szComponentId) { HRESULT hr=S_OK; OBO_TOKEN OboToken; INetCfgComponent* pncc; GUID guidClass; INetCfgClass* pncClass; INetCfgClassSetup* pncClassSetup; // OBO_TOKEN specifies the entity on whose behalf this // component is being uninstalld // set it to OBO_USER so that szComponentId will be uninstalld // On-Behalf-Of "user" ZeroMemory (&OboToken, sizeof(OboToken)); OboToken.Type = OBO_USER; // see if the component is really installed hr = pnc->FindComponent(szComponentId, &pncc); if (S_OK == hr) { // yes, it is installed. obtain INetCfgClassSetup and DeInstall hr = pncc->GetClassGuid(&guidClass); if (S_OK == hr) { hr = pnc->QueryNetCfgClass(&guidClass, IID_INetCfgClass, (void**)&pncClass); if (SUCCEEDED(hr)) { hr = pncClass->QueryInterface(IID_INetCfgClassSetup, (void**)&pncClassSetup); if (SUCCEEDED(hr)) { hr = pncClassSetup->DeInstall (pncc, &OboToken, NULL); ReleaseObj (pncClassSetup); } ReleaseObj(pncClass); } } ReleaseObj(pncc); } 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: HrShowNetComponents // // Purpose: Display the list of installed components of the // specified class. // // Arguments: // pnc [in] pointer to INetCfg object // pguidClass [in] pointer to class GUID // // Returns: S_OK on success, otherwise an error code // // Notes: // HRESULT HrShowNetComponents(IN INetCfg* pnc, IN const GUID* pguidClass) { HRESULT hr=S_OK; PWSTR szInfId; PWSTR szDisplayName; DWORD dwcc; INetCfgComponent* pncc; INetCfgClass* pncclass; IEnumNetCfgComponent* pencc; ULONG celtFetched; hr = pnc->QueryNetCfgClass(pguidClass, IID_INetCfgClass, (void**)&pncclass); if (SUCCEEDED(hr)) { // get IEnumNetCfgComponent so that we can enumerate hr = pncclass->EnumComponents(&pencc); ReleaseObj(pncclass); while (SUCCEEDED(hr) && (S_OK == (hr = pencc->Next(1, &pncc, &celtFetched)))) { if (pguidClass == &GUID_DEVCLASS_NET) { // we are interested only in physical netcards // hr = pncc->GetCharacteristics(&dwcc); if (FAILED(hr) || !(dwcc & NCF_PHYSICAL)) { hr = S_OK; ReleaseObj(pncc); continue; } } hr = pncc->GetId(&szInfId); if (S_OK == hr) { hr = pncc->GetDisplayName(&szDisplayName); if (SUCCEEDED(hr)) { LogPrintf(_T("%-26s %s\n"), szInfId, szDisplayName); CoTaskMemFree(szDisplayName); } CoTaskMemFree(szInfId); } // we dont want to stop enumeration just because 1 component // failed either GetId or GetDisplayName, therefore reset hr to S_OK hr = S_OK; ReleaseObj(pncc); } ReleaseObj(pencc); } if (S_FALSE == hr) { hr = S_OK; } return hr; }
// Install the NDIS protocol driver bool UninstallNdisProtocolDriver(wchar_t *id, UINT lock_timeout) { bool ret = false; HRESULT hr; INetCfg *pNetCfg; // Validate arguments if (id == NULL) { return false; } hr = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER, IID_INetCfg, (void **)&pNetCfg); if (SUCCEEDED(hr)) { INetCfgLock *pLock; hr = pNetCfg->QueryInterface(IID_INetCfgLock, (PVOID*)&pLock); if (SUCCEEDED(hr)) { LPWSTR locked_by; hr = pLock->AcquireWriteLock(lock_timeout, L"SoftEther VPN", &locked_by); if (SUCCEEDED(hr)) { hr = pNetCfg->Initialize(NULL); if (SUCCEEDED(hr)) { INetCfgComponent *pncc = NULL; hr = pNetCfg->FindComponent(id, &pncc); if (pncc == NULL || hr == S_FALSE) { hr = E_FAIL; } if (SUCCEEDED(hr)) { INetCfgClass *pncClass; hr = pNetCfg->QueryNetCfgClass(&GUID_DEVCLASS_NETTRANS, IID_INetCfgClass, (void **)&pncClass); if (SUCCEEDED(hr)) { INetCfgClassSetup *pncClassSetup; hr = pncClass->QueryInterface(IID_INetCfgClassSetup, (void **)&pncClassSetup); if (SUCCEEDED(hr)) { OBO_TOKEN obo; wchar_t *c = NULL; Zero(&obo, sizeof(obo)); obo.Type = OBO_USER; hr = pncClassSetup->DeInstall(pncc, &obo, &c); if (SUCCEEDED(hr)) { hr = pNetCfg->Apply(); if (SUCCEEDED(hr)) { ret = true; } else { WHERE; Debug("0x%x\n", hr); } } else { WHERE; Debug("0x%x\n", hr); } pncClassSetup->Release(); } else { WHERE; } pncClass->Release(); } else { WHERE; } pncc->Release(); } else { WHERE; } } else { WHERE; } pLock->ReleaseWriteLock(); } else { WHERE; } pLock->Release(); } pNetCfg->Release(); } else { WHERE; } return ret; }
HRESULT CMuxVirtualMiniport::Install (VOID) { INetCfgClass *pncClass; INetCfgClassSetup *pncClassSetup; INetCfgComponent *pnccMiniport; HRESULT hr; LPWSTR *pmszwRefs=NULL; OBO_TOKEN *pOboToken=NULL; DWORD dwSetupFlags=0; LPCWSTR pszwAnswerFile=NULL; LPCWSTR pszwAnswerSections=NULL; TraceMsg( L"-->CMuxVirtualMiniport::Install.\n" ); hr = m_pnc->QueryNetCfgClass( &GUID_DEVCLASS_NET, IID_INetCfgClass, (void **)&pncClass ); if ( hr == S_OK ) { hr = pncClass->QueryInterface( IID_INetCfgClassSetup, (void **)&pncClassSetup ); if ( hr == S_OK ) { hr = pncClassSetup->Install( c_szMuxMiniport, pOboToken, dwSetupFlags, 0, pszwAnswerFile, pszwAnswerSections, &pnccMiniport ); if ( hr == S_OK ) { hr = pnccMiniport->GetInstanceGuid( &m_guidMiniport ); if ( hr != S_OK ) { TraceMsg( L" Failed to get the instance guid, uninstalling " L" the miniport.\n" ); pncClassSetup->DeInstall( pnccMiniport, pOboToken, pmszwRefs ); } ReleaseObj( pnccMiniport ); } else { TraceMsg( L" Failed to install the miniport.\n" ); } ReleaseObj( pncClassSetup ); } else { TraceMsg( L" QueryInterface failed.\n" ); } ReleaseObj( pncClass ); } else { TraceMsg( L" QueryNetCfgClass failed.\n" ); } TraceMsg( L"<--CMuxVirtualMiniport::Install(HRESULT = %x).\n", hr ); return hr; }
HRESULT HrUninstallNetComponent( IN INetCfg* pnc, IN LPCTSTR szComponentId ) { INetCfgComponent *pncc = NULL; INetCfgClass *pncClass = NULL; INetCfgClassSetup *pncClassSetup = NULL; OBO_TOKEN OboToken; GUID guidClass; HRESULT hr = S_OK; // // OBO_TOKEN specifies on whose behalf this // component is being installed. // Set it to OBO_USER so that szComponentId will be installed // on behalf of the user. // ZeroMemory(&OboToken, sizeof(OboToken)); OboToken.Type = OBO_USER; // // Get the component's reference. // hr = pnc->FindComponent(szComponentId, &pncc); if (S_OK == hr) { // // Get the component's class GUID. // hr = pncc->GetClassGuid(&guidClass); if (hr == S_OK) { // // Get component's class reference. // hr = pnc->QueryNetCfgClass(&guidClass, IID_INetCfgClass, (void**)&pncClass); if (hr == S_OK) { // // Get Setup reference. // hr = pncClass->QueryInterface(IID_INetCfgClassSetup, (void**)&pncClassSetup); if (hr == S_OK) { hr = pncClassSetup->DeInstall(pncc, &OboToken, NULL); if (hr == S_OK) { // // Apply the changes // hr = pnc->Apply(); } ReleaseRef(pncClassSetup); } ReleaseRef(pncClass); } } ReleaseRef(pncc); } return hr; }