Automatically install the source code for uninstall drivers

xiaoxiao2021-03-06  55

Oh, I have hoped that I was developing to the hardware underlying driver. Originally, the major is the circuit. I have done some of the USB driver in the past two years. Although I can use it, I have been uncomfortable in my heart. I have a total of WDM. It feels a bit of a bit of a bit, so in the holidays in the holidays are realized, now I have a little bit, but I don't really write a thorough driver, always a little guilty, huh, huh, sticked a post, before BBS, I wrote it I wrote I don't know where to go, so I will pass it directly.

#include "stdafx.h" #include // Make all functions UNICODE safe. # include // for the API UpdateDriverForPlugAndPlayDevices (). # include // for SetupDiXxx functions . # include "install.h"

INT DisplayError (Tchar * ErrorName) {dWord Err = getLastError (); lpvoid lpMessageBuffer = NULL;

if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, Err, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) & lpMessageBuffer, 0, NULL)) NULL; // _ tprintf (TEXT ( "% s FAILURE:% s / n"), ErrorName (Tchar *) lpMessag

Ebuffer; Else Null; //_ TPrintf (text ("% s failure: (0x% 08x) / n"), erroorname, err;

IF (lpMessageBuffer) localfree (lpMessageBuffer); // free system buffer

SetLastError (ERR); Return False;}

Bool FINDEXISTINGDEVICE (HDEVINFO DeviceInfoset; sp_devinfo_data deviceinfodata; dword i, err; bool found;

/// Create a Device Information Set with all present devices. // deviceinfoset = setupdigetClassdevs (null, // all classes 0, 0, digcf_allclasses | Digcf_present); // All Devices Present On System

IF (DeviceInfoset == INVALID_HANDE_VALUE) {Return Displayerror (Text ("" GetClassDevs (All Present Devices)))

}

// _ TPrintf ("Search for Device ID: [% s] / n"), hardwareid; //////found = false; deviceInfodata.cbsize = sizeof (sp_devinfo_data); for (SP_DEVINFO_DATA); for I = 0; setUpdienumDeviceInfo (DeviceInfoseet, I, & DeviceInfodata); i ) {dword data; lptstr p, buffer = null; DWORD buffersize = 0;

/// We Won't know the size of the hardwareid buffer. So call it with a null to becom with and life. // use the required buffer size to alloc the netiCARY 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. //Iff (buffer) LocalFree; Buffer = (char *) Locala Lloc (lptr, buffersize);} else {////////// displayerror (Text ("getDeviceRegistryProperty)); goto cleanup_deviceinfo;}}

IF (getLastError () == error_INVALID_DATA) Continue; /// COMPARE EACH Entry In The Buff 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 (P)) {// _ TPrintf (Text ("Found! [% s] / n"), p); found = true; Break;}}

IF (buffer) localfree (buffer); found Break;}

IF (getLastError ()! = no_ERROR) {DisplayError (Text ("EnumDeviceInfo");}

// // cleanup. // cleanup_deviceinfo: err = getLastError (); setUpdidestroyDeviceInfolist (deviceinfoset); setLastError (ERR);

Return Err == NO_ERROR; / /? ???}

BOOLInstallRootEnumeratedDriver (IN LPTSTR HardwareId, IN LPTSTR INFFile, OUT PBOOL RebootRequired OPTIONAL) {HDEVINFO DeviceInfoSet = 0; SP_DEVINFO_DATA DeviceInfoData; GUID ClassGUID; TCHAR ClassName [MAX_CLASS_NAME_LEN]; DWORD err;

/////////////Iff (! Setupdigetinfclass (inffile, & classguid, classname, sizeof (classname), 0))

{Return DisplayError (Text ("getInfclass");

// // Create The Container for the to-be-create DEvice Information Element.

// deviceInfoset = setupdicreateDeviceInfolist (& classguid, 0); if (deviceinfoset == invalid_handle_value) {Return DisplayError (Text ("createDeviceInfolist");}

// // Now create the element // Use the Class GUID and Name from the INF file // DeviceInfoData.cbSize = sizeof (SP_DEVINFO_DATA);..! If (SetupDiCreateDeviceInfo (DeviceInfoSet, ClassName, & ClassGUID, NULL, 0, DICD_GENERATE_ID, & DeviceInfoData)) {DisplayError (TEXT ( "CreateDeviceInfo")); goto cleanup_DeviceInfo;} // // Add the HardwareID to the Device's HardwareID property // if (SetupDiSetDeviceRegistryProperty (DeviceInfoSet, & DeviceInfoData, SPDRP_HARDWAREID, (LPBYTE.!) HardwareId, ( Lstrlen (hardwareid) 1 1) * sizeof (tchar))) {DisplayError (Text ("setDeviceRegistryProperty)); goto cleanup_deviceinfo;}

// // Transform the registry element into an actual devnode // in the PnP HW tree // if {DisplayError (TEXT ( "CallClassInstaller (REGISTERDEVICE)")). (SetupDiCallClassInstaller (DIF_REGISTERDEVICE, DeviceInfoSet, & DeviceInfoData)!); Goto cleanup_DeviceInfo }

//////// // device sale diff_remove, if we encounter any failure from now on. //

// // Install the Driver // if (UpdateDriverForPlugAndPlayDevices (0, HardwareId, INFFile, INSTALLFLAG_FORCE, RebootRequired)!) {DWORD err = GetLastError ();. DisplayError (TEXT ( "UpdateDriverForPlugAndPlayDevices"));

if {DisplayError (TEXT ( "CallClassInstaller (REMOVE)"));} SetLastError (err);} // // Cleanup // cleanup_DeviceInfo (SetupDiCallClassInstaller (DIF_REMOVE, DeviceInfoSet, & DeviceInfoData)!):. err = GetLastError (); SetupDiDestroyDeviceInfoList DeviceInfoset; setLastError (ERR);

Return Err == NO_ERROR;

int InstallDriver (_TCHAR * InfName, _TCHAR * HardwareID) {WIN32_FIND_DATA FindFileData; BOOL RebootRequired = 0;. // Must be cleared _TCHAR * FName, * HWID; FName = InfName; HWID = HardwareID;

IF (Findfirstfile (FNAME, & FINDFILEDATA) == Invalid_Handle_Value) {//_TPrintf (Text ("File Not Found./N")); // _ TPrintf (Text ("Usage: Install / n") Return 2; // Install Failure}

// // Look to see if this device allready exissrs. //Iff (FINDEXISTINGDEVICE (HWID)) {/// no need to create a device node, just call oor api. //Iff (! UpdatedriverforPlugandPlayDevices (0, / / No Window Handle HWID, // Hardware ID FName, // FileName INSTALLFLAG_FORCE, & RebootRequired)) {DisplayError (TEXT ( "UpdateDriverForPlugAndPlayDevices")); return 2; // Install Failure}!} else {if (GetLastError () = ERROR_NO_MORE_ITEMS ) {// // AN UNKNOWN FAILURE FROM FINDEXISTINGDEVICE () / / / _ TPRINTF ("(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 (! InstallrootenumeredDrive R (hwid, // hardwareid fname, // filename & rebootRequired) {//_TPrintf (text ("(" ("(InstallRootenumeredDriver Failure! Code = 2) / n")); Return 2; // Install failure}}

//_ TPrintf (Text ("Driver Installed Success,));

IF (RebootRequired) {//_ tprintf (text ("(" ("(" (Reboot Required) / N "))))))); RETURN 1; // Install success, reboot required.

Return 0; // install success, no reboot required.

Int RemoveDriver {HDevinfo DeviceInfoset; sp_devinfo_data deviceinfodata; dword i, err;

DeviceInfoSet = SetupDiGetClassDevs (NULL, // All Classes 0, 0, DIGCF_ALLCLASSES | DIGCF_PRESENT); // All devices present on systemif (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. So call it with a null to becom with and life. // use the required buffer size to alloc the netiCARY 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. //Iff (buffer) LocalFree; Buffer = (char *) Locala lloc (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 (P)) {//_TPrintf (Text ("Found! [% S] / N"), P);

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

IF ((GetLastError () && (getLastError ()! = error_no_more_items) {DisplayError (Text ("EnumDeviceInfo");}

// // cleanup. // cleanup_deviceInfo: err = getLastError (); setUpdidestroyDeviceInfolist (deviceinfoseet);

Return Err;

转载请注明原文地址:https://www.9cbs.com/read-117864.html

New Post(0)