【问题标题】:auto install NDIS filter driver自动安装 NDIS 过滤器驱动程序
【发布时间】:2020-03-19 11:56:43
【问题描述】:

我正在尝试自动安装 NDIS 过滤器驱动程序。 我的机器上启用了内核调试,因此不需要驱动程序签名。

附:我从this 问题中获取了一些代码,但它仍然不起作用。 它给了我这个dialog,其中 dir 的默认路径错误。 另外,我看this topic,但那里的链接不可用。

如何设置 .sys 文件的默认路径?

谢谢。

....

DWORD size = 0;
isCopied = SetupCopyOEMInfA(pathToInf, // ( C:\[SomeDirs]\[driverInfFile.inf] )
                            pathToBin, // ( C:\[SomeDirs]\ ) here is driverSysFile.sys
                            SPOST_PATH,
                            SP_COPY_NEWER,
                            NULL,
                            0,
                            &size,
                            NULL);
....

INetCfg      *pnc = NULL;
INetCfgClassSetup   *pncClassSetup = NULL;
HRESULT      hr;

....

hr = CoCreateInstance( CLSID_CNetCfg,
                       NULL, CLSCTX_INPROC_SERVER,
                       IID_INetCfg,
                       (void**)&pnc );

....

INetCfgLock *pncfglock = NULL;
pnc->QueryInterface(IID_INetCfgLock, (LPVOID*)&pncfglock);
pncfglock->AcquireWriteLock(5000, L"MY CLIENT", &szwrClient)

....

hr = pnc->QueryNetCfgClass ( &GUID_DEVCLASS_NETSERVICE,
                             IID_INetCfgClassSetup,
                             (void**)&pncClassSetup );
....

OBO_TOKEN           OboToken;
ZeroMemory( &OboToken, sizeof(OboToken) );
OboToken.Type = OBO_USER;
INetCfgComponent* NDIS_Component;
//
// I read, that this 2 param both need for automatic setup, and if one is set,
// the second must be setted too.
// But the second[pszwAnswerSections] need to be list of sections in the inf file.
// And it not so cool to parse inf file manually, why OS cant do this???
LPCWSTR  pszwAnswerFile = NULL;
LPCWSTR  pszwAnswerSections = NULL;
//
// this call fails:
hr = pncClassSetup->Install(COMPONENT_ID,
                            &OboToken,
                            NSF_POSTSYSINSTALL,
                            0,
                            pszwAnswerFile,
                            pszwAnswerSections ,
                            &NDIS_Component);

【问题讨论】:

    标签: windows installation driver ndis


    【解决方案1】:

    您可以使用以下代码安装协议驱动程序。我创建了一个用于安装协议驱动程序的 win32 应用程序,并假设 inf 和驱动程序文件位于可执行二进制文件所在的同一位置。有时驱动程序文件可能不会被复制,因此请通过代码复制它。我用过这个,效果很好。

    #define NDISPROT_SERVICE_PNP_DEVICE_ID_A      "PROTOCOL DEVICE NAME"
    HRESULT InstallSpecifiedComponent(LPTSTR lpszInfFile, LPTSTR lpszPnpID, const GUID *pguidClass)
    {
        INetCfg         *pnc                    = NULL;
        LPTSTR          lpszApp                 = NULL;
        HRESULT         hr                      = S_OK;
    
        hr = HrGetINetCfg(TRUE, APP_NAME, &pnc, &lpszApp);
        if(S_OK == hr)
        {
        //
        // Install the network component.
        //
            PrintMsg(NULL, L"InstallSpecifiedComponent : HrGetINetCfg success.\n");
            hr = HrInstallNetComponent(pnc, lpszPnpID, pguidClass, lpszInfFile);
            if((S_OK == hr) || (NETCFG_S_REBOOT == hr))
            {
                PrintMsg(NULL, L"InstallSpecifiedComponent : HrInstallNetComponent success.\n");
                hr = pnc->Apply();
                if (S_OK == hr)
                {
                    PrintMsg(NULL, L"InstallSpecifiedComponent : Apply success.\n");
                }
                else
                {
                    PrintMsg(NULL, L"InstallSpecifiedComponent : Apply fail with error code %d.\n", GetLastError());
                }
            }
            else
            {
                PrintMsg(NULL, L"InstallSpecifiedComponent : HrInstallNetComponent fail with error code %d.\n", GetLastError());
                if(HRESULT_FROM_WIN32(ERROR_CANCELLED) != hr)
                {
                    PrintMsg(hr, L"InstallSpecifiedComponent : Couldn't install the network component.");
                }
            }
    
            HrReleaseINetCfg(pnc, TRUE);
        }
        else
        {
            PrintMsg(NULL, L"InstallSpecifiedComponent : HrGetINetCfg fail with error code %d.\n", GetLastError());
            if((NETCFG_E_NO_WRITE_LOCK == hr) && lpszApp )
            {
                PrintMsg(hr, L"InstallSpecifiedComponent : %s currently holds the lock, try later.", lpszApp);
                CoTaskMemFree(lpszApp);
            }
            else
            {
                PrintMsg(hr, L"InstallSpecifiedComponent : Couldn't the get notify object interface.");
            }
        }
    
       PrintMsg(NULL, L"InstallSpecifiedComponent : InstallSpecifiedComponent exit.\n");
    
        return hr;
    }
    
    DWORD InstallDriver()
    {
        DWORD           nResult                     = 0;
        HRESULT         hr                          = S_OK;
    
        memset(g_szInfFileFullPath, 0, _MAX_PATH * sizeof(TCHAR));
    
        // Get Path to Service INF File 
    // The INF file is assumed to be in the same folder as this application.    
    // Below function returns full path for inf file present in same folder
    nResult = GetInfFilePath(g_szInfFileFullPath, MAX_PATH);
    if(0 == nResult)
    {
        return nResult;
    }
    
    hr = InstallSpecifiedComponent(g_szInfFileFullPath, NDISPROT_SERVICE_PNP_DEVICE_ID, &GUID_DEVCLASS_NETTRANS);
    
    if(S_OK == hr)
    {
        PrintMsg(NULL, L"InstallDriver : InstallSpecifiedComponent success.\n");
        nResult = 1;
    }
    else
    {
        PrintMsg(hr, L"InstallDriver : InstallSpecifiedComponent fail with error code %d.\n", GetLastError());
    }
    
    PrintMsg(NULL, L"InstallDriver : InstallDriver exit.\n");
    
    return nResult;
    }
    
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    int nRetCode = 0;
    //Driver Installation.
    nRetCode = InstallDriver();
    if(1 == nRetCode)
    {
        Sleep(1000 * 2);
        // Sometimes driver file does not get copied into systme32\drivers folder, so just for precaution copy it using code
        if(CopyDrvFile())
        {
            system("net start ekaprot6");
        }   
    }
    return nRetCode;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-05
      • 2016-02-23
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      • 2020-06-23
      • 2014-04-06
      • 2013-08-20
      • 2017-06-21
      相关资源
      最近更新 更多