refact: win, idd control (#7789)

* refact: win, idd control

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* refact: win device control, better addr of

Signed-off-by: fufesou <shuanglongchen@yeah.net>

* refact: simple refact

Signed-off-by: fufesou <shuanglongchen@yeah.net>

---------

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2024-04-22 10:37:08 +08:00
committed by GitHub
parent ad062486ff
commit 4f47d4482b
19 changed files with 657 additions and 181 deletions

View File

@@ -13,3 +13,4 @@ bool MyStopServiceW(LPCWSTR serviceName);
std::wstring ReadConfig(const std::wstring& filename, const std::wstring& key);
void UninstallDriver(LPCWSTR hardwareId, BOOL &rebootRequired);

View File

@@ -70,7 +70,7 @@ UINT __stdcall RemoveInstallFolder(
}
LExit:
ReleaseStr(installFolder);
ReleaseStr(pwzData);
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
@@ -598,57 +598,14 @@ UINT __stdcall RemoveAmyuniIdd(
HRESULT hr = S_OK;
DWORD er = ERROR_SUCCESS;
int nResult = 0;
LPWSTR installFolder = NULL;
LPWSTR pwz = NULL;
LPWSTR pwzData = NULL;
WCHAR workDir[1024] = L"";
DWORD fileAttributes = 0;
HINSTANCE hi = 0;
SYSTEM_INFO si;
LPCWSTR exe = NULL;
BOOL rebootRequired = FALSE;
hr = WcaInitialize(hInstall, "RemoveAmyuniIdd");
ExitOnFailure(hr, "Failed to initialize");
hr = WcaGetProperty(L"CustomActionData", &pwzData);
ExitOnFailure(hr, "failed to get CustomActionData");
pwz = pwzData;
hr = WcaReadStringFromCaData(&pwz, &installFolder);
ExitOnFailure(hr, "failed to read database key from custom action data: %ls", pwz);
hr = StringCchPrintfW(workDir, 1024, L"%lsusbmmidd_v2", installFolder);
ExitOnFailure(hr, "Failed to compose a resource identifier string");
fileAttributes = GetFileAttributesW(workDir);
if (fileAttributes == INVALID_FILE_ATTRIBUTES) {
WcaLog(LOGMSG_STANDARD, "Amyuni idd dir \"%ls\" is out found, %d", workDir, fileAttributes);
goto LExit;
}
GetNativeSystemInfo(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
exe = L"deviceinstaller64.exe";
} else {
// No need to check if is other architecture.
// Because the driver is only for x86 and x64. It will not work at on other architectures.
exe = L"deviceinstaller.exe";
}
WcaLog(LOGMSG_STANDARD, "Remove amyuni idd %ls in %ls", exe, workDir);
hi = ShellExecuteW(NULL, L"open", exe, L"remove usbmmidd", workDir, SW_HIDE);
// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew
if ((int)hi <= 32) {
WcaLog(LOGMSG_STANDARD, "Failed to remove amyuni idd : %d, last error: %d", (int)hi, GetLastError());
}
else {
WcaLog(LOGMSG_STANDARD, "Amyuni idd is removed");
}
UninstallDriver(L"usbmmidd", rebootRequired);
LExit:
ReleaseStr(installFolder);
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}

View File

@@ -60,6 +60,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="CustomActions.cpp" />
<ClCompile Include="DeviceUtils.cpp" />
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="FirewallRules.cpp" />
<ClCompile Include="pch.cpp">
@@ -81,4 +82,4 @@
<Error Condition="!Exists('..\packages\WixToolset.DUtil.4.0.5\build\WixToolset.DUtil.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\WixToolset.DUtil.4.0.5\build\WixToolset.DUtil.props'))" />
<Error Condition="!Exists('..\packages\WixToolset.WcaUtil.4.0.5\build\WixToolset.WcaUtil.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\WixToolset.WcaUtil.4.0.5\build\WixToolset.WcaUtil.props'))" />
</Target>
</Project>
</Project>

View File

@@ -0,0 +1,84 @@
#include "pch.h"
#include <Windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <cfgmgr32.h>
#pragma comment(lib, "SetupAPI.lib")
void UninstallDriver(LPCWSTR hardwareId, BOOL &rebootRequired)
{
HDEVINFO deviceInfoSet = SetupDiGetClassDevsW(&GUID_DEVCLASS_DISPLAY, NULL, NULL, DIGCF_PRESENT);
if (deviceInfoSet == INVALID_HANDLE_VALUE)
{
WcaLog(LOGMSG_STANDARD, "Failed to get device information set, last error: %d", GetLastError());
return;
}
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
devInfoListDetail.cbSize = sizeof(SP_DEVINFO_LIST_DETAIL_DATA);
if (!SetupDiGetDeviceInfoListDetailW(deviceInfoSet, &devInfoListDetail))
{
SetupDiDestroyDeviceInfoList(deviceInfoSet);
WcaLog(LOGMSG_STANDARD, "Failed to call SetupDiGetDeviceInfoListDetail, last error: %d", GetLastError());
return;
}
SP_DEVINFO_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DWORD dataType;
WCHAR deviceId[MAX_DEVICE_ID_LEN] = { 0, };
DWORD deviceIndex = 0;
while (SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, &deviceInfoData))
{
if (!SetupDiGetDeviceRegistryPropertyW(deviceInfoSet, &deviceInfoData, SPDRP_HARDWAREID, &dataType, (PBYTE)deviceId, MAX_DEVICE_ID_LEN, NULL))
{
WcaLog(LOGMSG_STANDARD, "Failed to get hardware id, last error: %d", GetLastError());
deviceIndex++;
continue;
}
if (wcscmp(deviceId, hardwareId) != 0)
{
deviceIndex++;
continue;
}
SP_REMOVEDEVICE_PARAMS remove_device_params;
remove_device_params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
remove_device_params.ClassInstallHeader.InstallFunction = DIF_REMOVE;
remove_device_params.Scope = DI_REMOVEDEVICE_GLOBAL;
remove_device_params.HwProfile = 0;
if (!SetupDiSetClassInstallParamsW(deviceInfoSet, &deviceInfoData, &remove_device_params.ClassInstallHeader, sizeof(SP_REMOVEDEVICE_PARAMS)))
{
WcaLog(LOGMSG_STANDARD, "Failed to set class install params, last error: %d", GetLastError());
deviceIndex++;
continue;
}
if (!SetupDiCallClassInstaller(DIF_REMOVE, deviceInfoSet, &deviceInfoData))
{
WcaLog(LOGMSG_STANDARD, "ailed to uninstall driver, last error: %d", GetLastError());
deviceIndex++;
continue;
}
SP_DEVINSTALL_PARAMS deviceParams;
if (SetupDiGetDeviceInstallParamsW(deviceInfoSet, &deviceInfoData, &deviceParams))
{
if (deviceParams.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT))
{
rebootRequired = true;
}
}
WcaLog(LOGMSG_STANDARD, "Driver uninstalled successfully");
deviceIndex++;
}
SetupDiDestroyDeviceInfoList(deviceInfoSet);
}

View File

@@ -29,7 +29,6 @@
<CustomAction Id="SetPropertyServiceStop.SetParam.ConfigKey" Return="check" Property="ConfigKey" Value="stop-service" />
<CustomAction Id="SetPropertyServiceStop.SetParam.PropertyName" Return="check" Property="PropertyName" Value="STOP_SERVICE" />
<CustomAction Id="TryDeleteStartupShortcut.SetParam" Return="check" Property="ShortcutName" Value="$(var.Product) Tray" />
<CustomAction Id="RemoveAmyuniIdd.SetParam" Return="check" Property="RemoveAmyuniIdd" Value="[INSTALLFOLDER]" />
<InstallExecuteSequence>
<Custom Action="SetPropertyIsServiceRunning" After="InstallInitialize" Condition="Installed" />
@@ -74,7 +73,6 @@
<Custom Action="TerminateBrokers" Before="RemoveInstallFolder"/>
<Custom Action="TerminateBrokers.SetParam" Before="TerminateBrokers"/>
<Custom Action="RemoveAmyuniIdd" Before="RemoveInstallFolder"/>
<Custom Action="RemoveAmyuniIdd.SetParam" Before="RemoveAmyuniIdd"/>
</InstallExecuteSequence>
<!-- Shortcuts -->