ReactOS 0.4.16-dev-125-g798ea90
eject.c
Go to the documentation of this file.
1/*
2 * PROJECT: Safely Remove Hardware Applet
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Device removal support
5 * COPYRIGHT: Copyright 2023 Thamatip Chitpong <thamatip.chitpong@reactos.org>
6 */
7
8#include "hotplug.h"
9
10#include <strsafe.h>
11
14 _In_ PHOTPLUG_DATA pHotplugData)
15{
16 HTREEITEM hItem, hParentItem;
17 TVITEMW tvItem;
18
19 hItem = TreeView_GetSelection(pHotplugData->hwndDeviceTree);
20 if (!hItem)
21 return 0;
22
23 /* Find root item */
24 hParentItem = TreeView_GetParent(pHotplugData->hwndDeviceTree, hItem);
25 while (hParentItem)
26 {
27 hItem = hParentItem;
28 hParentItem = TreeView_GetParent(pHotplugData->hwndDeviceTree, hItem);
29 }
30
31 ZeroMemory(&tvItem, sizeof(tvItem));
32 tvItem.mask = TVIF_PARAM;
33 tvItem.hItem = hItem;
34
35 TreeView_GetItem(pHotplugData->hwndDeviceTree, &tvItem);
36
37 return tvItem.lParam;
38}
39
40static
41VOID
43 _In_ HWND hwndCfmDeviceList,
44 _In_ PHOTPLUG_DATA pHotplugData)
45{
46 LVCOLUMNW lvColumn;
47
48 ZeroMemory(&lvColumn, sizeof(lvColumn));
49 lvColumn.mask = LVCF_FMT;
50 lvColumn.fmt = LVCFMT_LEFT | LVCFMT_IMAGE;
51 ListView_InsertColumn(hwndCfmDeviceList, 0, &lvColumn);
52
53 CfmListEnumDevices(hwndCfmDeviceList, pHotplugData);
54
56}
57
58static
59VOID
61 _In_ DEVINST DevInst,
62 _In_opt_ HWND hwndDlg)
63{
65 CONFIGRET cr;
66
67 cr = CM_Request_Device_EjectW(DevInst, &VetoType, NULL, 0, 0);
68 if (cr != CR_SUCCESS && VetoType == PNP_VetoTypeUnknown)
69 {
70 LPCWSTR pszFormat = L"";
71 WCHAR szError[64];
72
73 /* NOTES: IDS_EJECT_ERROR_FORMAT resource has to be explicitly NULL-terminated
74 * so we can use the string directly without having to make a copy of it. */
76 StringCbPrintfW(szError, sizeof(szError), pszFormat, cr);
77
78 MessageBoxW(hwndDlg, szError, NULL, MB_ICONEXCLAMATION | MB_OK);
79 }
80}
81
85 _In_ HWND hwndDlg,
86 _In_ UINT uMsg,
89{
90 PHOTPLUG_DATA pHotplugData;
91
92 pHotplugData = (PHOTPLUG_DATA)GetWindowLongPtrW(hwndDlg, DWLP_USER);
93
94 switch (uMsg)
95 {
96 case WM_INITDIALOG:
97 {
98 HWND hwndDevList;
99
100 pHotplugData = (PHOTPLUG_DATA)lParam;
101 SetWindowLongPtrW(hwndDlg, DWLP_USER, (LONG_PTR)pHotplugData);
102
103 hwndDevList = GetDlgItem(hwndDlg, IDC_CONFIRM_STOP_DEVICE_LIST);
104
105 ListView_SetImageList(hwndDevList,
106 pHotplugData->ImageListData.ImageList,
108
109 FillConfirmDeviceList(hwndDevList, pHotplugData);
110
111 return TRUE;
112 }
113
114 case WM_COMMAND:
115 {
116 switch (LOWORD(wParam))
117 {
118 case IDOK:
119 SafeRemoveDevice(GetDeviceInstForRemoval(pHotplugData), hwndDlg);
120 EndDialog(hwndDlg, LOWORD(wParam));
121 break;
122
123 case IDCANCEL:
124 EndDialog(hwndDlg, LOWORD(wParam));
125 break;
126 }
127
128 break;
129 }
130
131 case WM_DESTROY:
133 break;
134 }
135
136 return FALSE;
137}
@ PNP_VetoTypeUnknown
Definition: cfg.h:180
enum _PNP_VETO_TYPE PNP_VETO_TYPE
DWORD DEVINST
Definition: cfgmgr32.h:76
RETURN_TYPE CONFIGRET
Definition: cfgmgr32.h:74
#define CR_SUCCESS
Definition: cfgmgr32.h:842
CONFIGRET WINAPI CM_Request_Device_EjectW(_In_ DEVINST dnDevInst, _Out_opt_ PPNP_VETO_TYPE pVetoType, _Out_writes_opt_(ulNameLength) LPWSTR pszVetoName, _In_ ULONG ulNameLength, _In_ ULONG ulFlags)
Definition: cfgmgr.c:7580
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
HINSTANCE hApplet
Definition: access.c:17
VOID CfmListEnumDevices(_In_ HWND hwndCfmDeviceList, _In_ PHOTPLUG_DATA pHotplugData)
Definition: enum.c:272
#define IDC_CONFIRM_STOP_DEVICE_LIST
Definition: resource.h:18
#define IDS_EJECT_ERROR_FORMAT
Definition: resource.h:29
#define CALLBACK
Definition: compat.h:35
DEVINST GetDeviceInstForRemoval(_In_ PHOTPLUG_DATA pHotplugData)
Definition: eject.c:13
INT_PTR CALLBACK ConfirmRemovalDlgProc(_In_ HWND hwndDlg, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam)
Definition: eject.c:84
static VOID FillConfirmDeviceList(_In_ HWND hwndCfmDeviceList, _In_ PHOTPLUG_DATA pHotplugData)
Definition: eject.c:42
static VOID SafeRemoveDevice(_In_ DEVINST DevInst, _In_opt_ HWND hwndDlg)
Definition: eject.c:60
struct _HOTPLUG_DATA * PHOTPLUG_DATA
#define _In_
Definition: ms_sal.h:308
#define _In_opt_
Definition: ms_sal.h:309
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
unsigned int UINT
Definition: ndis.h:50
#define L(x)
Definition: ntvdm.h:50
#define LOWORD(l)
Definition: pedump.c:82
#define LVSIL_SMALL
Definition: commctrl.h:2304
#define LVCFMT_IMAGE
Definition: commctrl.h:2607
#define ListView_InsertColumn(hwnd, iCol, pcol)
Definition: commctrl.h:2641
#define ListView_SetImageList(hwnd, himl, iImageList)
Definition: commctrl.h:2309
#define TreeView_GetParent(hwnd, hitem)
Definition: commctrl.h:3474
#define ListView_SetColumnWidth(hwnd, iCol, cx)
Definition: commctrl.h:2653
#define TreeView_GetSelection(hwnd)
Definition: commctrl.h:3478
#define TreeView_GetItem(hwnd, pitem)
Definition: commctrl.h:3495
#define LVCF_FMT
Definition: commctrl.h:2591
#define LVCFMT_LEFT
Definition: commctrl.h:2603
#define TVIF_PARAM
Definition: commctrl.h:3273
#define LVSCW_AUTOSIZE_USEHEADER
Definition: commctrl.h:2650
STRSAFEAPI StringCbPrintfW(STRSAFE_LPWSTR pszDest, size_t cbDest, STRSAFE_LPCWSTR pszFormat,...)
Definition: strsafe.h:557
HTREEITEM hItem
Definition: commctrl.h:3322
LPARAM lParam
Definition: commctrl.h:3330
UINT mask
Definition: commctrl.h:3321
HTREEITEM hItem
Definition: treelist.h:37
int32_t INT_PTR
Definition: typedefs.h:64
#define ZeroMemory
Definition: winbase.h:1712
LONG_PTR LPARAM
Definition: windef.h:208
UINT_PTR WPARAM
Definition: windef.h:207
#define DWLP_USER
Definition: winuser.h:875
#define GetWindowLongPtrW
Definition: winuser.h:4832
#define IDCANCEL
Definition: winuser.h:834
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
#define WM_COMMAND
Definition: winuser.h:1743
#define WM_INITDIALOG
Definition: winuser.h:1742
int WINAPI MessageBoxW(_In_opt_ HWND hWnd, _In_opt_ LPCWSTR lpText, _In_opt_ LPCWSTR lpCaption, _In_ UINT uType)
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
#define IDOK
Definition: winuser.h:833
#define MB_ICONEXCLAMATION
Definition: winuser.h:788
#define MB_OK
Definition: winuser.h:793
#define WM_DESTROY
Definition: winuser.h:1612
#define SetWindowLongPtrW
Definition: winuser.h:5358
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185