//+--------------------------------------------------------------------------- // // Function: HrReleaseINetCfg // // Purpose: Uninitialize INetCfg, release write lock (if present) // and uninitialize COM. // // Arguments: // fHasWriteLock [in] whether write lock needs to be released. // pnc [in] pointer to INetCfg object // // Returns: S_OK on success, otherwise an error code // // Notes: // HRESULT HrReleaseINetCfg(BOOL fHasWriteLock, INetCfg* pnc) { HRESULT hr = S_OK; // uninitialize INetCfg hr = pnc->Uninitialize(); // if write lock is present, unlock it if (SUCCEEDED(hr) && fHasWriteLock) { INetCfgLock* pncLock; // Get the locking interface hr = pnc->QueryInterface(IID_INetCfgLock, (LPVOID *)&pncLock); if (SUCCEEDED(hr)) { hr = pncLock->ReleaseWriteLock(); ReleaseObj(pncLock); } } ReleaseObj(pnc); CoUninitialize(); return hr; }
HRESULT HrReleaseINetCfg( IN INetCfg* pnc, IN BOOL fHasWriteLock ) { INetCfgLock *pncLock = NULL; HRESULT hr = S_OK; // // Uninitialize INetCfg // hr = pnc->Uninitialize(); // // If write lock is present, unlock it // if (hr == S_OK && fHasWriteLock) { // // Get the locking reference // hr = pnc->QueryInterface(IID_INetCfgLock, (LPVOID *)&pncLock); if (hr == S_OK) { hr = pncLock->ReleaseWriteLock(); ReleaseRef(pncLock); } } ReleaseRef(pnc); // // Uninitialize COM. // CoUninitialize(); return hr; }
bool ReleaseINetCfg(BOOL bHasWriteLock, INetCfg* pNetCfg) { // If write lock is present, unlock it if (SUCCEEDED(pNetCfg->Uninitialize()) && bHasWriteLock) { INetCfgLock* pNetCfgLock; // Get the locking interface if (SUCCEEDED(pNetCfg->QueryInterface(IID_INetCfgLock, (LPVOID *)&pNetCfgLock))) { pNetCfgLock->ReleaseWriteLock(); ReleaseObj(pNetCfgLock); } } ReleaseObj(pNetCfg); CoUninitialize(); return true; }
bool ChangeNicBindingOrder() { HRESULT hr = S_OK; INetCfg *pNetCfg = NULL; INetCfgBindingPath *pNetCfgPath; INetCfgComponent *pNetCfgComponent = NULL; INetCfgComponentBindings *pNetCfgBinding = NULL; INetCfgLock *pNetCfgLock = NULL; IEnumNetCfgBindingPath *pEnumNetCfgBindingPath = NULL; PWSTR szLockedBy; if (!SUCCEEDED(CoInitialize(NULL))) { return false; } if (S_OK != CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER, IID_INetCfg, (void**)&pNetCfg)) { return false; } if (!SUCCEEDED(pNetCfg->QueryInterface(IID_INetCfgLock, (LPVOID *)&pNetCfgLock))) { return false; } static const ULONG c_cmsTimeout = 5000; static const WCHAR c_szSampleNetcfgApp[] = L"TapRebinder (TapRebinder.exe)"; if (!SUCCEEDED(pNetCfgLock->AcquireWriteLock(c_cmsTimeout, c_szSampleNetcfgApp, &szLockedBy))) { wprintf(L"Could not lock INetcfg, it is already locked by '%s'", szLockedBy); return false; } if (!SUCCEEDED(pNetCfg->Initialize(NULL))) { if (pNetCfgLock) { pNetCfgLock->ReleaseWriteLock(); } ReleaseObj(pNetCfgLock); return false; } ReleaseObj(pNetCfgLock); if (S_OK != pNetCfg->FindComponent(L"ms_tcpip", &pNetCfgComponent)) { return false; } if (S_OK != pNetCfgComponent->QueryInterface(IID_INetCfgComponentBindings, (LPVOID *)&pNetCfgBinding)) { return false; } if (S_OK != pNetCfgBinding->EnumBindingPaths(EBP_BELOW, &pEnumNetCfgBindingPath)) { return false; } while (S_OK == hr) { hr = pEnumNetCfgBindingPath->Next(1, &pNetCfgPath, NULL); LPWSTR pszwPathToken; pNetCfgPath->GetPathToken(&pszwPathToken); if (wcscmp(pszwPathToken, wDeviceInstanceId) == 0) { wprintf(L" Moving adapter to the first position: %s.\n", pszwPathToken); pNetCfgBinding->MoveBefore(pNetCfgPath, NULL); pNetCfg->Apply(); CoTaskMemFree(pszwPathToken); ReleaseObj(pNetCfgPath); break; } CoTaskMemFree(pszwPathToken); ReleaseObj(pNetCfgPath); } ReleaseObj(pEnumNetCfgBindingPath); ReleaseObj(pNetCfgBinding); ReleaseObj(pNetCfgComponent); ReleaseINetCfg(TRUE, pNetCfg); return true; }
//+--------------------------------------------------------------------------- // // Function: HrGetINetCfg // // Purpose: Initialize COM, create and initialize INetCfg. // Obtain write lock if indicated. // // Arguments: // fGetWriteLock [in] whether to get write lock // ppnc [in] pointer to pointer to INetCfg object // // Returns: S_OK on success, otherwise an error code // // Notes: // HRESULT HrGetINetCfg(IN BOOL fGetWriteLock, INetCfg** ppnc) { HRESULT hr=S_OK; // Initialize the output parameters. *ppnc = NULL; // initialize COM hr = CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED ); if (SUCCEEDED(hr)) { // Create the object implementing INetCfg. // INetCfg* pnc; hr = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER, IID_INetCfg, (void**)&pnc); if (SUCCEEDED(hr)) { INetCfgLock * pncLock = NULL; if (fGetWriteLock) { // Get the locking interface hr = pnc->QueryInterface(IID_INetCfgLock, (LPVOID *)&pncLock); if (SUCCEEDED(hr)) { // Attempt to lock the INetCfg for read/write static const ULONG c_cmsTimeout = 15000; static const WCHAR c_szSampleNetcfgApp[] = L"Sample Netcfg Application (netcfg.exe)"; PWSTR szLockedBy; hr = pncLock->AcquireWriteLock(c_cmsTimeout, c_szSampleNetcfgApp, &szLockedBy); if (S_FALSE == hr) { hr = NETCFG_E_NO_WRITE_LOCK; } } } if (SUCCEEDED(hr)) { // Initialize the INetCfg object. // hr = pnc->Initialize(NULL); if (SUCCEEDED(hr)) { *ppnc = pnc; pnc->AddRef(); } else { // initialize failed, if obtained lock, release it if (pncLock) { pncLock->ReleaseWriteLock(); } } } ReleaseObj(pncLock); ReleaseObj(pnc); } if (FAILED(hr)) { CoUninitialize(); } } return hr; }
// Install the NDIS protocol driver bool InstallNdisProtocolDriver(wchar_t *inf_path, wchar_t *id, UINT lock_timeout) { bool ret = false; HRESULT hr; INetCfg *pNetCfg; HINSTANCE hSetupApiDll = NULL; BOOL (WINAPI *_SetupCopyOEMInfW)(PCWSTR, PCWSTR, DWORD, DWORD, PWSTR, DWORD, PDWORD, PWSTR *) = NULL; BOOL (WINAPI *_SetupUninstallOEMInfW)(PCWSTR, DWORD, PVOID) = NULL; // Validate arguments if (inf_path == NULL || id == NULL) { return false; } hSetupApiDll = LoadLibraryA("setupapi.dll"); if (hSetupApiDll == NULL) { WHERE; goto LABEL_CLEANUP; } _SetupCopyOEMInfW = (UINT (__stdcall *)(PCWSTR,PCWSTR,DWORD,DWORD,PWSTR,DWORD,PDWORD,PWSTR *)) GetProcAddress(hSetupApiDll, "SetupCopyOEMInfW"); _SetupUninstallOEMInfW = (UINT (__stdcall *)(PCWSTR,DWORD,PVOID)) GetProcAddress(hSetupApiDll, "SetupUninstallOEMInfW"); if (_SetupCopyOEMInfW == NULL || _SetupUninstallOEMInfW == NULL) { WHERE; goto LABEL_CLEANUP; } 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)) { wchar_t inf_dir[MAX_PATH]; GetDirNameFromFilePathW(inf_dir, sizeof(inf_dir), inf_path); if (_SetupCopyOEMInfW(inf_path, inf_dir, SPOST_PATH, 0, NULL, 0, NULL, 0)) { INetCfgClassSetup *pSetup; hr = pNetCfg->QueryNetCfgClass(&GUID_DEVCLASS_NETTRANS, IID_INetCfgClassSetup, (void **)&pSetup); if (SUCCEEDED(hr)) { OBO_TOKEN token; INetCfgComponent *pComponent; Zero(&token, sizeof(token)); token.Type = OBO_USER; hr = pSetup->Install(id, &token, 0, 0, NULL, NULL, &pComponent); if (SUCCEEDED(hr)) { pNetCfg->Apply(); ret = true; } else { WHERE; Debug("0x%x\n", hr); } pSetup->Release(); } else { WHERE; } if (ret == false) { wchar_t dst_inf_name[MAX_PATH]; DWORD dst_inf_name_size = MAX_PATH; if (_SetupCopyOEMInfW(inf_path, inf_dir, SPOST_PATH, SP_COPY_REPLACEONLY, dst_inf_name, dst_inf_name_size, &dst_inf_name_size, NULL) == false && GetLastError() == ERROR_FILE_EXISTS) { _SetupUninstallOEMInfW(dst_inf_name, 0, NULL); } } } else { WHERE; } } else { WHERE; } pLock->ReleaseWriteLock(); } else { WHERE; } pLock->Release(); } pNetCfg->Release(); } else { WHERE; } LABEL_CLEANUP: if (hSetupApiDll != NULL) { FreeLibrary(hSetupApiDll); } return ret; }
// 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; }
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 *pComponent; hr = pNetCfg->FindComponent(id, &pComponent); if (SUCCEEDED(hr)) { INetCfgClassSetup *pSetup; hr = pNetCfg->QueryNetCfgClass(&GUID_DEVCLASS_NETTRANS, IID_INetCfgClassSetup, (void **)&pSetup); if (SUCCEEDED(hr)) { OBO_TOKEN token; SeZero(&token, sizeof(token)); token.Type = OBO_USER; hr = pSetup->DeInstall(pComponent, &token, NULL); if (SUCCEEDED(hr)) { pNetCfg->Apply(); ret = true; } pSetup->Release(); } } } pLock->ReleaseWriteLock(); } pLock->Release(); } pNetCfg->Release(); } return ret; }
bool InstallNdisProtocolDriver(char *inf_path, wchar_t *id, UINT lock_timeout) { bool ret = false; HRESULT hr; INetCfg *pNetCfg; // Validate arguments if (inf_path == NULL || 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)) { char inf_dir[MAX_PATH]; SeGetDirNameFromFilePath(inf_dir, sizeof(inf_dir), inf_path); if (SetupCopyOEMInfA(inf_path, inf_dir, SPOST_PATH, 0, NULL, 0, NULL, 0)) { INetCfgClassSetup *pSetup; hr = pNetCfg->QueryNetCfgClass(&GUID_DEVCLASS_NETTRANS, IID_INetCfgClassSetup, (void **)&pSetup); if (SUCCEEDED(hr)) { OBO_TOKEN token; INetCfgComponent *pComponent; SeZero(&token, sizeof(token)); token.Type = OBO_USER; hr = pSetup->Install(id, &token, 0, 0, NULL, NULL, &pComponent); if (SUCCEEDED(hr)) { pNetCfg->Apply(); ret = true; } pSetup->Release(); } if (ret == false) { char dst_inf_name[MAX_PATH]; DWORD dst_inf_name_size = MAX_PATH; if (SetupCopyOEMInfA(inf_path, inf_dir, SPOST_PATH, SP_COPY_REPLACEONLY, dst_inf_name, dst_inf_name_size, &dst_inf_name_size, NULL) == false && GetLastError() == ERROR_FILE_EXISTS) { SetupUninstallOEMInfA(dst_inf_name, 0, NULL); } } } } pLock->ReleaseWriteLock(); } pLock->Release(); } pNetCfg->Release(); } return ret; }
HRESULT HrGetINetCfg( IN BOOL fGetWriteLock, IN LPCTSTR lpszAppName, OUT INetCfg** ppnc, OUT LPTSTR *lpszLockedBy ) { INetCfg *pnc = NULL; INetCfgLock *pncLock = NULL; HRESULT hr = S_OK; // // Initialize the output parameters. // *ppnc = NULL; if (lpszLockedBy) { *lpszLockedBy = NULL; } // // Initialize COM // hr = CoInitialize(NULL); if (hr == S_OK) { // // Create the object implementing INetCfg. // hr = CoCreateInstance(CLSID_CNetCfg, NULL, CLSCTX_INPROC_SERVER, IID_INetCfg, (void**)&pnc); if (hr == S_OK) { if (fGetWriteLock) { // // Get the locking reference // hr = pnc->QueryInterface(IID_INetCfgLock, (LPVOID *)&pncLock); if (hr == S_OK) { // // Attempt to lock the INetCfg for read/write // hr = pncLock->AcquireWriteLock(LOCK_TIME_OUT, lpszAppName, lpszLockedBy); if (hr == S_FALSE) { hr = NETCFG_E_NO_WRITE_LOCK; } } } if (hr == S_OK) { // // Initialize the INetCfg object. // hr = pnc->Initialize(NULL); if (hr == S_OK) { *ppnc = pnc; pnc->AddRef(); } else { // // Initialize failed, if obtained lock, release it // if (pncLock) { pncLock->ReleaseWriteLock(); } } } ReleaseRef(pncLock); ReleaseRef(pnc); } // // In case of error, uninitialize COM. // if (hr != S_OK) { CoUninitialize(); } } return hr; }