📄 misc.c
字号:
return Ret;
}
BOOL
EnableDevice(IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DevInfoData OPTIONAL,
IN BOOL bEnable,
IN DWORD HardwareProfile OPTIONAL,
OUT BOOL *bNeedReboot OPTIONAL)
{
SP_PROPCHANGE_PARAMS pcp;
SP_DEVINSTALL_PARAMS dp;
DWORD LastErr;
BOOL Ret = FALSE;
pcp.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
pcp.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
pcp.HwProfile = HardwareProfile;
if (bEnable)
{
/* try to enable the device on the global profile */
pcp.StateChange = DICS_ENABLE;
pcp.Scope = DICS_FLAG_GLOBAL;
/* ignore errors */
LastErr = GetLastError();
if (SetupDiSetClassInstallParams(DeviceInfoSet,
DevInfoData,
&pcp.ClassInstallHeader,
sizeof(SP_PROPCHANGE_PARAMS)))
{
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,
DeviceInfoSet,
DevInfoData);
}
SetLastError(LastErr);
}
/* try config-specific */
pcp.StateChange = (bEnable ? DICS_ENABLE : DICS_DISABLE);
pcp.Scope = DICS_FLAG_CONFIGSPECIFIC;
if (SetupDiSetClassInstallParams(DeviceInfoSet,
DevInfoData,
&pcp.ClassInstallHeader,
sizeof(SP_PROPCHANGE_PARAMS)) &&
SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,
DeviceInfoSet,
DevInfoData))
{
dp.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
if (SetupDiGetDeviceInstallParams(DeviceInfoSet,
DevInfoData,
&dp))
{
if (bNeedReboot != NULL)
{
*bNeedReboot = ((dp.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT)) != 0);
}
Ret = TRUE;
}
}
return Ret;
}
BOOL
GetDeviceTypeString(IN PSP_DEVINFO_DATA DeviceInfoData,
OUT LPWSTR szBuffer,
IN DWORD BufferSize)
{
BOOL Ret = FALSE;
if (!SetupDiGetClassDescription(&DeviceInfoData->ClassGuid,
szBuffer,
BufferSize,
NULL))
{
szBuffer[0] = L'\0';
if (LoadString(hDllInstance,
IDS_UNKNOWN,
szBuffer,
BufferSize))
{
Ret = TRUE;
}
}
else
{
/* FIXME - check string for NULL termination! */
Ret = TRUE;
}
return Ret;
}
BOOL
GetDeviceDescriptionString(IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DeviceInfoData,
OUT LPWSTR szBuffer,
IN DWORD BufferSize)
{
DWORD RegDataType;
BOOL Ret = FALSE;
if ((SetupDiGetDeviceRegistryProperty(DeviceInfoSet,
DeviceInfoData,
SPDRP_FRIENDLYNAME,
&RegDataType,
(PBYTE)szBuffer,
BufferSize * sizeof(WCHAR),
NULL) ||
SetupDiGetDeviceRegistryProperty(DeviceInfoSet,
DeviceInfoData,
SPDRP_DEVICEDESC,
&RegDataType,
(PBYTE)szBuffer,
BufferSize * sizeof(WCHAR),
NULL)) &&
RegDataType == REG_SZ)
{
/* FIXME - check string for NULL termination! */
Ret = TRUE;
}
else
{
szBuffer[0] = L'\0';
if (LoadString(hDllInstance,
IDS_UNKNOWNDEVICE,
szBuffer,
BufferSize))
{
Ret = TRUE;
}
}
return Ret;
}
BOOL
FindCurrentDriver(IN HDEVINFO DeviceInfoSet,
IN PSP_DEVINFO_DATA DeviceInfoData,
OUT PSP_DRVINFO_DATA DriverInfoData)
{
HKEY hKey = INVALID_HANDLE_VALUE;
SP_DEVINSTALL_PARAMS InstallParams = {0};
SP_DRVINFO_DETAIL_DATA DriverInfoDetailData = {0};
WCHAR InfPath[MAX_PATH];
WCHAR InfSection[LINE_LEN];
DWORD dwType, dwLength;
DWORD i = 0;
LONG rc;
BOOL Ret = FALSE;
/* Steps to find the right driver:
* 1) Get the device install parameters
* 2) Open the driver registry key
* 3) Read the .inf file name
* 4) Update install params, by setting DI_ENUMSINGLEINF and .inf file name
* 5) Build class driver list
* 6) Read inf section and inf section extension from registry
* 7) Enumerate drivers
* 8) Find the one who is in the same section as current driver?
*/
/* 1) Get the install params */
InstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
if (!SetupDiGetDeviceInstallParams(DeviceInfoSet,
DeviceInfoData,
&InstallParams))
{
DPRINT1("SetupDiSetDeviceInstallParams() failed with error 0x%lx\n", GetLastError());
goto Cleanup;
}
#ifdef DI_FLAGSEX_INSTALLEDDRIVER
InstallParams.FlagsEx |= (DI_FLAGSEX_INSTALLEDDRIVER | DI_FLAGSEX_ALLOWEXCLUDEDDRVS);
if (SetupDiSetDeviceInstallParams(DeviceInfoSet,
DeviceInfoData,
&InstallParams))
{
if (SetupDiBuildDriverInfoList(DeviceInfoSet,
DeviceInfoData,
SPDIT_CLASSDRIVER) &&
SetupDiEnumDriverInfo(DeviceInfoSet,
DeviceInfoData,
SPDIT_CLASSDRIVER,
0,
DriverInfoData))
{
Ret = TRUE;
}
goto Cleanup;
}
InstallParams.FlagsEx &= ~(DI_FLAGSEX_INSTALLEDDRIVER | DI_FLAGSEX_ALLOWEXCLUDEDDRVS);
#endif
/* 2) Open the driver registry key */
hKey = SetupDiOpenDevRegKey(DeviceInfoSet,
DeviceInfoData,
DICS_FLAG_GLOBAL,
0,
DIREG_DRV,
KEY_QUERY_VALUE);
if (hKey == INVALID_HANDLE_VALUE)
{
DPRINT1("SetupDiOpenDevRegKey() failed with error 0x%lx\n", GetLastError());
goto Cleanup;
}
/* 3) Read the .inf file name */
dwLength = (sizeof(InfPath) / sizeof(InfPath[0])) - 1;
rc = RegQueryValueEx(hKey,
REGSTR_VAL_INFPATH,
0,
&dwType,
(LPBYTE)InfPath,
&dwLength);
if (rc != ERROR_SUCCESS)
{
SetLastError(rc);
DPRINT1("RegQueryValueEx() failed with error 0x%lx\n", GetLastError());
goto Cleanup;
}
else if (dwType != REG_SZ)
{
SetLastError(ERROR_GEN_FAILURE);
DPRINT1("Expected registry type REG_SZ (%lu), got %lu\n", REG_SZ, dwType);
goto Cleanup;
}
InfPath[(dwLength / sizeof(WCHAR)) - 1] = L'\0';
/* 4) Update install params, by setting DI_ENUMSINGLEINF and .inf file name */
InstallParams.Flags |= DI_ENUMSINGLEINF;
InstallParams.FlagsEx |= DI_FLAGSEX_ALLOWEXCLUDEDDRVS;
wcscpy(InstallParams.DriverPath, InfPath);
if (!SetupDiSetDeviceInstallParams(DeviceInfoSet,
DeviceInfoData,
&InstallParams))
{
DPRINT1("SetupDiSetDeviceInstallParams() failed with error 0x%lx\n", GetLastError());
goto Cleanup;
}
/* 5) Build class driver list */
if (!SetupDiBuildDriverInfoList(DeviceInfoSet,
DeviceInfoData,
SPDIT_CLASSDRIVER))
{
DPRINT1("SetupDiBuildDriverInfoList() failed with error 0x%lx\n", GetLastError());
goto Cleanup;
}
/* 6) Read inf section and from registry */
dwLength = (sizeof(InfSection) / sizeof(InfSection[0])) - 1;
rc = RegQueryValueEx(hKey,
REGSTR_VAL_INFSECTION,
0,
&dwType,
(LPBYTE)InfSection,
&dwLength);
if (rc != ERROR_SUCCESS)
{
SetLastError(rc);
DPRINT1("RegQueryValueEx() failed with error 0x%lx\n", GetLastError());
goto Cleanup;
}
else if (dwType != REG_SZ)
{
SetLastError(ERROR_GEN_FAILURE);
DPRINT1("Expected registry type REG_SZ (%lu), got %lu\n", REG_SZ, dwType);
goto Cleanup;
}
InfPath[(dwLength / sizeof(WCHAR)) - 1] = L'\0';
/* 7) Enumerate drivers */
DriverInfoData->cbSize = sizeof(SP_DRVINFO_DATA);
DriverInfoDetailData.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
while (SetupDiEnumDriverInfo(DeviceInfoSet,
DeviceInfoData,
SPDIT_CLASSDRIVER,
i,
DriverInfoData))
{
/* 8) Find the one who is in the same section as current driver */
if (!SetupDiGetDriverInfoDetail(DeviceInfoSet,
DeviceInfoData,
DriverInfoData,
&DriverInfoDetailData,
DriverInfoDetailData.cbSize,
NULL) &&
GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
DPRINT1("SetupDiGetDriverInfoDetail() failed with error 0x%lx\n", GetLastError());
goto Cleanup;
}
if (!wcsicmp(DriverInfoDetailData.SectionName,
InfSection) != 0)
{
/* We have found the right driver */
Ret = TRUE;
goto Cleanup;
}
i++;
}
if (GetLastError() != ERROR_NO_MORE_ITEMS)
{
DPRINT1("SetupDiEnumDriverInfo() failed with error 0x%lx\n", GetLastError());
goto Cleanup;
}
SetLastError(ERROR_NO_DRIVER_SELECTED);
Cleanup:
if (hKey != INVALID_HANDLE_VALUE)
RegCloseKey(hKey);
return Ret;
}
HINSTANCE
LoadAndInitComctl32(VOID)
{
typedef VOID (WINAPI *PINITCOMMONCONTROLS)(VOID);
PINITCOMMONCONTROLS pInitCommonControls;
HINSTANCE hComCtl32;
hComCtl32 = LoadLibrary(L"comctl32.dll");
if (hComCtl32 != NULL)
{
/* initialize the common controls */
pInitCommonControls = (PINITCOMMONCONTROLS)GetProcAddress(hComCtl32,
"InitCommonControls");
if (pInitCommonControls == NULL)
{
FreeLibrary(hComCtl32);
return NULL;
}
pInitCommonControls();
}
return hComCtl32;
}
BOOL
STDCALL
DllMain(IN HINSTANCE hinstDLL,
IN DWORD dwReason,
IN LPVOID lpvReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hinstDLL);
hDllInstance = hinstDLL;
break;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -