1

I'm trying to do an automatic install for the NDIS filter driver. Kernel debugging is enabled on my machine so driver signing is not required.

P.s. some of the code, I took from this question, but it's still doesn't work. It gives me this dialog, where the default path to dir wrong. Also, I watch this topic, but links there aren't workable.

How I can set a default path to .sys file?

Thanks.

....

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);
buridan
  • 11
  • 2

1 Answers1

0

You can use below code for installing protocol driver. I had created a win32 application for installing protocol driver and assumed that inf and driver file are at same location where executable binary is present. Sometime there are chances that driver file does not get copied so copy it via code. I had used this and it is working perfectly.

#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;
}
Chandra
  • 471
  • 2
  • 10