int __stdcall NDInstallNetComp(PCWSTR szNetComp, UINT nc, PCWSTR szInfFullPath) { HRESULT hr ; int iRet; LogPrintf(TEXT("Entering NDInstallNetProtocol(%s, %d, %s)..."), szNetComp, nc, szInfFullPath); hr = HrInstallNetComponent(szNetComp, (NetClass) nc, szInfFullPath) ; LogNetCfgError(hr); if(SUCCEEDED(hr)) { if (hr == NETCFG_S_REBOOT) iRet = NDS_REBOOT_REQUIRED; else iRet = NDS_SUCCESS; } else if(hr == NETCFG_E_NEED_REBOOT) { LogPrintf(TEXT("LpxInstall(), A system reboot is required before the component can be installed.")); iRet = NDS_PREBOOT_REQUIRED; SetLastError(HRESULT_CODE(hr)); } else { SetLastError(HRESULT_CODE(hr)); iRet = NDS_FAIL; } LogPrintf(TEXT("Leaving NDInstallNetProtocol()... %d"), iRet); return iRet; }
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: 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: 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; }
int netcomp(int argc, LPTSTR *argv) { typedef enum { NET_INSTALL, NET_REMOVE, NET_FIND } COMMAND_TYPE; struct { COMMAND_TYPE type; LPCTSTR cmd; int argc; BOOL op; } opts[] = { { NET_INSTALL, L"install", 3, FALSE}, { NET_REMOVE, L"remove", 1, FALSE}, { NET_FIND, L"find", 1, FALSE}, }; COMMAND_TYPE CmdType; const DWORD nopts = RTL_NUMBER_OF(opts); DWORD i = 0; for (; i < nopts; ++i) { if (lstrcmpi(opts[i].cmd, argv[0]) == 0) { if ((argc - 1) != opts[i].argc) { _ftprintf(stderr, _T("Invalid command arguments.\n")); return 254; } CmdType = opts[i].type; break; } } if (i == nopts) { _ftprintf(stderr, _T("Invalid command.\n")); return 254; } if (NET_INSTALL == CmdType) { NetClass nc; if (0 == lstrcmpi(_T("protocol"), argv[2])) { nc = NC_NetProtocol; } else if (0 == lstrcmpi(_T("adapter"), argv[2])) { nc = NC_NetAdapter; } else if (0 == lstrcmpi(_T("client"), argv[2])) { nc = NC_NetClient; } else if (0 == lstrcmpi(_T("service"), argv[2])) { nc = NC_NetService; } else { _ftprintf(stderr, _T("Invalid network component.\n")); return 253; } TCHAR szCopiedInfPath[MAX_PATH] = {0}; HRESULT hr = HrInstallNetComponent( argv[3], nc, argv[1], szCopiedInfPath, MAX_PATH, NULL, NULL); if (FAILED(hr)) { _tprintf(_T("Installation of %s failed with error %s\n"), argv[3], pNetCfgHrString(hr)); printErrorText(stderr, hr); return 255; } else if (S_OK != hr) { _tprintf(_T("%s installed successfully. INF=%s\n"), argv[3], szCopiedInfPath); _tprintf(_T("HResult = %s\n"), pNetCfgHrString(hr)); printErrorText(stderr, hr); return 1; } else { _tprintf(_T("%s installed successfully. INF=%s\n"), argv[3], szCopiedInfPath); return 0; } } else if (NET_REMOVE == CmdType) { HRESULT hr = HrUninstallNetComponent(argv[1]); if (FAILED(hr)) { _tprintf(_T("Uninstallation of %s failed with error %s\n"), argv[3], pNetCfgHrString(hr)); return 255; } else if (S_OK != hr) { _tprintf(_T("%s uninstalled successfully.\n"), argv[1]); _tprintf(_T("HResult = %s\n"), pNetCfgHrString(hr)); printErrorText(stderr, hr); return 1; } else { _tprintf(_T("%s uninstalled successfully.\n"), argv[1]); return 0; } } else if (NET_FIND == CmdType) { HRESULT hr = HrIsNetComponentInstalled(argv[1]); if (FAILED(hr)) { _tprintf(_T("Finding %s failed with error %s\n"), argv[1], pNetCfgHrString(hr)); printErrorText(stderr, hr); return 255; } else { if (S_FALSE == hr) { _tprintf(_T("Component %s not found.\n"), argv[1]); return 1; } else if (S_OK == hr) { _tprintf(_T("Component %s is installed.\n"), argv[1]); return 0; } else { _tprintf(_T("Component %s not found. (%s)\n"), argv[1], pNetCfgHrString(hr)); printErrorText(stderr, hr); return 1; } } } else { _ftprintf(stderr, _T("Unknown command.\n")); return 254; } return 0; }