⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driverinstall.cpp

📁 自动安装驱动(驱动和INI文件放在该文件的所在目录)无需人工找驱动的软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    TCHAR *FName, *HWID;
    FName = InfName;
    HWID = HardwareID;

	

    if (FindFirstFile(FName,&FindFileData)==INVALID_HANDLE_VALUE)
    {
       // _tprintf(TEXT("  File not found.\n"));
       // _tprintf(TEXT("usage: install <INF_File> <Hardware_ID>\n"));
		DisplayError(TEXT("Find inf file erro"));
        return 2; // Install Failure
    }

	if (!UpdateDriverForPlugAndPlayDevices(0, // No Window Handle
            HWID, // Hardware ID
            FName, // FileName
            INSTALLFLAG_FORCE,
            &RebootRequired))
        {
           if (GetLastError()== ERROR_NO_SUCH_DEVINST)
		   {
			   return 5;
           }
		   else
            return 2; // Install Failure
        }

    //
    // Look to see if this device allready exists.
    /*
    if (FindExistingDevice(HWID))	
    {
        //
        // No Need to Create a Device Node, just call our API.
        //
        if (!UpdateDriverForPlugAndPlayDevices(0, // No Window Handle
            HWID, // Hardware ID
            FName, // FileName
            INSTALLFLAG_FORCE,
            &RebootRequired))
        {
            DisplayError(TEXT("UpdateDriverForPlugAndPlayDevices"));
            return 2; // Install Failure
        }
    }
    else
    {
		DisplayError(TEXT("FindExistingDevice erro"));

        if (GetLastError()!= ERROR_NO_MORE_ITEMS)
        {
            //
            // An unknown failure from FindExistingDevice()
            //
            //_tprintf(TEXT("(IERROR_NO_MORE_ITEMS)\n"));
            //_tprintf(TEXT("(Install Failure! Code = 2)\n"));
            return 2; // Install Failure
        }

        //
        // Driver Does not exist, Create and call the API.
        // HardwareID must be a multi-sz string, which argv[2] is.
        //
        if (!InstallRootEnumeratedDriver(HWID, // HardwareID
            FName, // FileName
            &RebootRequired))
        {
            _tprintf(TEXT("(InstallRootEnumeratedDriver Failure! Code = 2)\n"));
            return 2; // Install Failure
        }
    }

    //_tprintf(TEXT("Driver Installed successfully.\n"));

    if (RebootRequired)
    {
        //_tprintf(TEXT("(Reboot Required)\n"));
        return 1; // Install Success, reboot required.
    }
  */
    return 0; // Install Success, no reboot required.
}

int RemoveDriver(TCHAR *HardwareID)
{
    HDEVINFO DeviceInfoSet;
    SP_DEVINFO_DATA DeviceInfoData;
    DWORD i,err;

    DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
        0,
        0,
        DIGCF_ALLCLASSES | DIGCF_PRESENT ); // All devices present on system

    if (DeviceInfoSet == INVALID_HANDLE_VALUE)
    {
        DisplayError(TEXT("GetClassDevs(All Present Devices)"));
        return 1;
    }

    //
    //  Enumerate through all Devices.
    //
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    for (i=0;SetupDiEnumDeviceInfo(DeviceInfoSet,i,&DeviceInfoData);i++)
    {
        DWORD DataT;
        LPTSTR p,buffer = NULL;
        DWORD buffersize = 0;

        //
        // We won't know the size of the HardwareID buffer until we call
        // this function. So call it with a null to begin with, and then
        // use the required buffer size to Alloc the nessicary space.
        // Keep calling we have success or an unknown failure.
        //
        while (!SetupDiGetDeviceRegistryProperty(
            DeviceInfoSet,
            &DeviceInfoData,
            SPDRP_HARDWAREID,
            &DataT,
            (PBYTE)buffer,
            buffersize,
            &buffersize))
        {
            if (GetLastError() == ERROR_INVALID_DATA)
            {
                //
                // May be a Legacy Device with no HardwareID. Continue.
                //
                break;
            }
            else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
            {
                //
                // We need to change the buffer size.
                //
                if (buffer)
                    LocalFree(buffer);
                buffer = (LPTSTR)LocalAlloc(LPTR,buffersize);
            }
            else
            {
                //
                // Unknown Failure.
                //
                DisplayError(TEXT("GetDeviceRegistryProperty"));
                goto cleanup_DeviceInfo;
            }
        }

        if (GetLastError() == ERROR_INVALID_DATA)
            continue;

        //
        // Compare each entry in the buffer multi-sz list with our HardwareID.


        //
        for (p=buffer;*p&&(p<&buffer[buffersize]);p+=lstrlen(p)+sizeof(TCHAR))


        {
            //_tprintf(TEXT("Compare device ID: [%s]\n"),p);

            if (!_tcscmp(HardwareID,p))
            {
                //_tprintf(TEXT("Found! [%s]\n"),p);

                //
                // Worker function to remove device.
                //
                if (!SetupDiCallClassInstaller(DIF_REMOVE,
                    DeviceInfoSet,
                    &DeviceInfoData))
                {
                    DisplayError(TEXT("CallClassInstaller(REMOVE)"));
                }
                break;
            }
        }

        if (buffer) LocalFree(buffer);
    }

    if ((GetLastError()!=NO_ERROR)&&(GetLastError()!=ERROR_NO_MORE_ITEMS))
    {
        DisplayError(TEXT("EnumDeviceInfo"));
    }

    //
    //  Cleanup.
    //
cleanup_DeviceInfo:
    err = GetLastError();
    SetupDiDestroyDeviceInfoList(DeviceInfoSet);

    return err;
}

/*****************************************************************
 判断是否正在安装函数

******************************************************************/
BOOL
IsDeviceInstallInProgress (VOID)
{
    HMODULE hModule;
    CMP_WAITNOPENDINGINSTALLEVENTS_PROC pCMP_WaitNoPendingInstallEvents;

    hModule = GetModuleHandle(TEXT("setupapi.dll"));
    if(!hModule)
    {
        // Should never happen since we're linked to SetupAPI, but...
        return FALSE;
    }

    pCMP_WaitNoPendingInstallEvents = 
        (CMP_WAITNOPENDINGINSTALLEVENTS_PROC)GetProcAddress(hModule,
                                             "CMP_WaitNoPendingInstallEvents");
    if(!pCMP_WaitNoPendingInstallEvents)
    {
        // We're running on a release of the operating system that doesn't supply this function.
        // Trust the operating system to suppress Autorun when appropriate.
        return FALSE;
    }
    return (pCMP_WaitNoPendingInstallEvents(0) == WAIT_TIMEOUT);
}



int APIENTRY WinMain (HINSTANCE hInst,
                      HINSTANCE hPrevInstance, 
                      LPSTR lpCmdLine,         
                      int nCmdShow )           
{
    //
    // Make sure common control DLL is loaded.
    //
	HRESULT     hr;	
	 LPWSTR     lpszApp;
	LPWSTR      lpszPnpID;
	LPWSTR      lpszInfFile;
	int         Result;


   // hInstance = hInst;	

	if(IsDeviceInstallInProgress()) {
        //
        // We don't want to start right now.  Instead, our 
        // device co-installer will invoke this application 
        // (if necessary) during finish-install processing.
        //
        return -1;
    }

	int argc;


	ULONG      nLenOfCommd;
	PWSTR pwcCommd=NULL, pwcPath=NULL;

	TCHAR  *pF= _T("d:\\work\\card.inf");
	TCHAR  *pID=_T("PCI\\VEN_1171&DEV_0205");

	nLenOfCommd = MultiByteToWideChar( CP_ACP, 0, lpCmdLine, -1, NULL, 0 );
	pwcCommd = (PWSTR)malloc( nLenOfCommd * sizeof(WCHAR) );
	if( !pwcCommd) return 0;
	MultiByteToWideChar( CP_ACP, 0, lpCmdLine, -1, pwcCommd, nLenOfCommd );

	LPWSTR* argv = CommandLineToArgvW(pwcCommd, &argc); 

	Result = InstallDriver(pF,pID);
	if (Result ==0)  
	{
		MessageBoxW( NULL,
			        L"安装  成功",
                    L"installdriver",
                    MB_ICONINFORMATION | MB_OK );
		
		return 0;
	}
	if (Result != 5)
	{
		MessageBoxW( NULL,
			        L"安装  失败",
                    L"installdriver",
                    MB_ICONINFORMATION | MB_OK );
		return 1;
	}
	else
		MessageBoxW( NULL,
			        L"请插入卡后再安装",
                    L"installdriver",
                    MB_ICONINFORMATION | MB_OK );




	return 0;

//	EnableDriverSigning(FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -