- Implement very basic uninstall functionality
- Move device action code into the DeviceView class

svn path=/trunk/; revision=68344
This commit is contained in:
Ged Murphy 2015-07-05 08:49:54 +00:00
parent 8fdb754153
commit e399ab4063
6 changed files with 180 additions and 148 deletions

View file

@ -344,6 +344,40 @@ CDeviceNode::EnableDevice(
return true;
}
bool
CDeviceNode::UninstallDevice()
{
if (CanUninstall() == false)
return false;
SP_REMOVEDEVICE_PARAMS RemoveDevParams;
RemoveDevParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
RemoveDevParams.ClassInstallHeader.InstallFunction = DIF_REMOVE;
RemoveDevParams.Scope = DI_REMOVEDEVICE_GLOBAL;
RemoveDevParams.HwProfile = 0;
//
// We probably need to walk all the siblings of this
// device and ask if they're happy with the uninstall
//
// Remove it
SetupDiSetClassInstallParamsW(m_hDevInfo,
&m_DevinfoData,
&RemoveDevParams.ClassInstallHeader,
sizeof(SP_REMOVEDEVICE_PARAMS));
SetupDiCallClassInstaller(DIF_REMOVE, m_hDevInfo, &m_DevinfoData);
// Clear the install params
SetupDiSetClassInstallParamsW(m_hDevInfo,
&m_DevinfoData,
NULL,
0);
}
/* PRIVATE METHODS ******************************************************/
void

View file

@ -38,6 +38,9 @@ public:
_Out_ bool &NeedsReboot
);
bool UninstallDevice(
);
private:
void Cleanup(
);

View file

@ -229,6 +229,68 @@ CDeviceView::Refresh(
if (hThread) CloseHandle(hThread);
}
LRESULT
CDeviceView::OnAction(
_In_ UINT Action
)
{
switch (Action)
{
case IDC_PROPERTIES:
{
DisplayPropertySheet();
break;
}
case IDC_SCAN_HARDWARE:
{
Refresh(GetCurrentView(),
true,
true,
NULL);
break;
}
case IDC_ENABLE_DRV:
{
bool NeedsReboot;
if (EnableSelectedDevice(true, NeedsReboot) &&
NeedsReboot)
{
MessageBox(m_hMainWnd, L"Rebooting", L"Enable", MB_OK);
}
break;
}
case IDC_DISABLE_DRV:
{
bool NeedsReboot;
EnableSelectedDevice(false, NeedsReboot);
break;
}
case IDC_UPDATE_DRV:
{
MessageBox(m_hMainWnd, L"Not yet implemented", L"Update Driver", MB_OK);
break;
}
case IDC_UNINSTALL_DRV:
{
UninstallSelectedDevice();
break;
}
case IDC_ADD_HARDWARE:
{
MessageBox(m_hMainWnd, L"Not yet implemented", L"Add Hardware", MB_OK);
break;
}
}
return 0;
}
void
CDeviceView::DisplayPropertySheet()
{
@ -286,77 +348,14 @@ CDeviceView::CreateActionMenu(
return false;
}
bool
CDeviceView::HasProperties(
_In_ LPTV_ITEMW TvItem
)
CNode*
CDeviceView::GetSelectedNode()
{
CNode *Node = GetNode(TvItem);
if (Node)
{
return Node->HasProperties();
}
return false;
TV_ITEM TvItem;
TvItem.hItem = TreeView_GetSelection(m_hTreeView);
return GetNode(&TvItem);
}
bool
CDeviceView::IsDisabled(
_In_ LPTV_ITEMW TvItem
)
{
CDeviceNode *Node = dynamic_cast<CDeviceNode *>(GetNode(TvItem));
if (Node)
{
return Node->IsDisabled();
}
return false;
}
bool
CDeviceView::CanDisable(
_In_ LPTV_ITEMW TvItem
)
{
CDeviceNode *Node = dynamic_cast<CDeviceNode *>(GetNode(TvItem));
if (Node)
{
return Node->CanDisable();
}
return false;
}
bool
CDeviceView::EnableSelectedDevice(
_In_ bool Enable,
_Out_ bool &NeedsReboot
)
{
CDeviceNode *Node = dynamic_cast<CDeviceNode *>(GetSelectedNode());
if (Node == nullptr) return false;
if (Enable == false)
{
CAtlStringW str;
if (str.LoadStringW(g_hInstance, IDS_CONFIRM_DISABLE))
{
if (MessageBoxW(m_hMainWnd,
str,
Node->GetDisplayName(),
MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2) != IDYES)
{
return false;
}
}
}
if (Node->EnableDevice(Enable, NeedsReboot))
{
Refresh(m_ViewType, true, true, Node->GetDeviceId());
return true;
}
return false;
}
// PRIVATE METHODS *******************************************/
@ -709,6 +708,49 @@ CDeviceView::RecurseChildDevices(
return true;
}
bool
CDeviceView::EnableSelectedDevice(
_In_ bool Enable,
_Out_ bool &NeedsReboot
)
{
CDeviceNode *Node = dynamic_cast<CDeviceNode *>(GetSelectedNode());
if (Node == nullptr) return false;
if (Enable == false)
{
CAtlStringW str;
if (str.LoadStringW(g_hInstance, IDS_CONFIRM_DISABLE))
{
if (MessageBoxW(m_hMainWnd,
str,
Node->GetDisplayName(),
MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2) != IDYES)
{
return false;
}
}
}
if (Node->EnableDevice(Enable, NeedsReboot))
{
Refresh(m_ViewType, true, true, Node->GetDeviceId());
return true;
}
return false;
}
bool
CDeviceView::UninstallSelectedDevice(
)
{
CDeviceNode *Node = dynamic_cast<CDeviceNode *>(GetSelectedNode());
if (Node == nullptr) return false;
return Node->UninstallDevice();
}
bool
CDeviceView::GetChildDevice(
_In_ DEVINST ParentDevInst,
@ -1034,13 +1076,6 @@ CNode* CDeviceView::GetNode(
return NULL;
}
CNode* CDeviceView::GetSelectedNode()
{
TV_ITEM TvItem;
TvItem.hItem = TreeView_GetSelection(m_hTreeView);
return GetNode(&TvItem);
}
void
CDeviceView::EmptyLists()
{

View file

@ -51,6 +51,10 @@ public:
_In_ LPARAM lParam
);
LRESULT OnAction(
UINT Action
);
VOID Refresh(
_In_ ViewType Type,
_In_ bool ScanForChanges,
@ -73,20 +77,7 @@ public:
_In_ bool MainMenu
);
bool HasProperties(
_In_ LPTV_ITEMW TvItem
);
//bool SelDeviceIsHidden();
bool CanDisable(
_In_ LPTV_ITEMW TvItem
);
bool IsDisabled(
_In_ LPTV_ITEMW TvItem
);
bool EnableSelectedDevice(
_In_ bool Enable,
_Out_ bool &NeedsReboot
CNode* GetSelectedNode(
);
bool SelDeviceIsStarted();
@ -117,6 +108,14 @@ private:
_In_ HTREEITEM hParentTreeItem
);
bool EnableSelectedDevice(
_In_ bool Enable,
_Out_ bool &NeedsReboot
);
bool UninstallSelectedDevice(
);
bool GetChildDevice(
_In_ DEVINST ParentDevInst,
_Out_ PDEVINST DevInst
@ -153,7 +152,6 @@ private:
CNode* GetNode(
_In_ LPTV_ITEMW TvItem
);
CNode* GetSelectedNode();
CClassNode* GetClassNode(
_In_ LPGUID ClassGuid

View file

@ -234,17 +234,6 @@ CMainWindow::RefreshView(ViewType Type)
return TRUE;
}
bool
CMainWindow::ScanForHardwareChanges()
{
// Refresh the cache and and display
m_DeviceView->Refresh(m_DeviceView->GetCurrentView(),
true,
true,
NULL);
return true;
}
bool
CMainWindow::CreateToolBar()
{
@ -323,12 +312,14 @@ CMainWindow::CreateStatusBar()
return bRet;
}
void CMainWindow::UpdateUiContext(_In_ LPTV_ITEMW TvItem)
void CMainWindow::UpdateToolbar(_In_ LPTV_ITEMW TvItem)
{
WORD State;
CNode *Node = m_DeviceView->GetSelectedNode();
// properties button
if (m_DeviceView->HasProperties(TvItem))
if (Node->HasProperties())
{
State = TBSTATE_ENABLED;
}
@ -340,8 +331,11 @@ void CMainWindow::UpdateUiContext(_In_ LPTV_ITEMW TvItem)
SendMessageW(m_hToolBar, TB_SETSTATE, IDC_UPDATE_DRV, MAKELPARAM(State, 0)); //hack
SendMessageW(m_hToolBar, TB_SETSTATE, IDC_UNINSTALL_DRV, MAKELPARAM(State, 0)); // hack
// enable driver button
if (m_DeviceView->IsDisabled(TvItem))
if (Node->GetNodeType() == DeviceNode &&
dynamic_cast<CDeviceNode *>(Node)->IsDisabled())
{
State = TBSTATE_ENABLED;
}
@ -352,7 +346,9 @@ void CMainWindow::UpdateUiContext(_In_ LPTV_ITEMW TvItem)
SendMessageW(m_hToolBar, TB_SETSTATE, IDC_ENABLE_DRV, MAKELPARAM(State, 0));
// disable driver button
if (m_DeviceView->CanDisable(TvItem) && !m_DeviceView->IsDisabled(TvItem))
if (Node->GetNodeType() == DeviceNode &&
dynamic_cast<CDeviceNode *>(Node)->CanDisable() &&
!dynamic_cast<CDeviceNode *>(Node)->IsDisabled())
{
State = TBSTATE_ENABLED;
}
@ -411,7 +407,10 @@ CMainWindow::OnCreate(HWND hwnd)
if (m_DeviceView->Initialize())
{
// Do the initial scan
ScanForHardwareChanges();
m_DeviceView->Refresh(m_DeviceView->GetCurrentView(),
true,
true,
NULL);
// Display the window according to the user request
ShowWindow(hwnd, m_CmdShow);
@ -468,7 +467,7 @@ CMainWindow::OnNotify(LPARAM lParam)
case TVN_SELCHANGED:
{
LPNMTREEVIEW NmTreeView = (LPNMTREEVIEW)lParam;
UpdateUiContext(&NmTreeView->itemNew);
UpdateToolbar(&NmTreeView->itemNew);
break;
}
@ -543,50 +542,14 @@ CMainWindow::OnCommand(WPARAM wParam,
switch (Msg)
{
case IDC_PROPERTIES:
{
m_DeviceView->DisplayPropertySheet();
break;
}
case IDC_SCAN_HARDWARE:
{
ScanForHardwareChanges();
break;
}
case IDC_ENABLE_DRV:
{
bool NeedsReboot;
if (m_DeviceView->EnableSelectedDevice(true, NeedsReboot) &&
NeedsReboot)
{
MessageBox(m_hMainWnd, L"Rebooting", L"Enable", MB_OK);
}
break;
}
case IDC_DISABLE_DRV:
{
bool NeedsReboot;
m_DeviceView->EnableSelectedDevice(false, NeedsReboot);
break;
}
case IDC_UPDATE_DRV:
{
MessageBox(m_hMainWnd, L"Not yet implemented", L"Update Driver", MB_OK);
break;
}
case IDC_UNINSTALL_DRV:
{
MessageBox(m_hMainWnd, L"Not yet implemented", L"Uninstall Driver", MB_OK);
break;
}
case IDC_ADD_HARDWARE:
{
MessageBox(m_hMainWnd, L"Not yet implemented", L"Add Hardware", MB_OK);
m_DeviceView->OnAction(Msg);
break;
}

View file

@ -45,7 +45,9 @@ private:
bool CreateToolBar();
bool CreateStatusBar();
void UpdateUiContext(_In_ LPTV_ITEMW TvItem);
void UpdateToolbar(
_In_ LPTV_ITEMW TvItem
);
bool StatusBarLoadString(
HWND hStatusBar,
@ -68,8 +70,5 @@ private:
bool RefreshView(
ViewType Type
);
bool ScanForHardwareChanges(
);
};