-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecyclinbin.cpp
More file actions
97 lines (92 loc) · 2.56 KB
/
Copy pathRecyclinbin.cpp
File metadata and controls
97 lines (92 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "common.h"
typedef int (WINAPI* SHEmptyRecycleBinA_PTR)(
HWND hwnd,
LPCWSTR pszRootPath,
DWORD dwFlags
);
extern "C" {
#define SHELL32_FAIL -1
#define SHELL32_NO_EMPTYBINA -2
/// <summary>
/// Empty the recycle bin using SHEmptyRecycleBinA. If SILENCE is true, it will not show any UI
/// </summary>
/// <param name="result">if not null, will have a negative value on error and 0 on ok.</param>
/// <param name="message_result">If not null, will be pointed to a read only ANSI string stating what happened.</param>
/// <param name="argv">part of the tool protocol. not actually used currently with this. yes pass the argv from main()</param>
/// <param name="argc">part of the tool protocol. Yes pass argc from main()</param>
/// <returns>True if it worked and false if something went bad. Message is set to something suitable for console/stdout. Result too.</returns>
/// <remarks> if the return value is not -1, -2, lookup the return values for SHEmptyRecycleBinA.</remarks>
bool EmptyBin(int* result, const char** message_result, const char* argv[], int argc)
{
SHEmptyRecycleBinA_PTR WideCode = nullptr;
// load shell32 and handle failure stuff
HMODULE hModule = LoadLibraryA("shell32.dll");
if (message_result != nullptr)
{
*message_result = nullptr; // reset the message result to null
}
if (result != nullptr)
{
*result = 0;
}
if (hModule == NULL)
{
if (message_result != nullptr)
{
*message_result = Message_CantLoadShell32;
}
if (result != nullptr)
{
*result = SHELL32_FAIL;
}
return false;
}
// get ShEmptyRecycleBinW handle failure
WideCode = (SHEmptyRecycleBinA_PTR)GetProcAddress(hModule, "SHEmptyRecycleBinA");
if (WideCode == nullptr)
{
if (message_result != nullptr)
{
*message_result = GetProc_FAIL_ON_ShEmptyBinW;
}
if (result != nullptr)
{
*result = SHELL32_NO_EMPTYBINA;
}
// NOTE WE DON'T free the library here as we do assume the workflow of run a tool and exit vs persist
return false;
}
int r = 0;
// call the function without noise
if (SILENCE)
{
r = WideCode(0, 0, SHERB_NOCONFIRMATION | SHERB_NOPROGRESSUI);
}
else
{
r = WideCode(0, 0, 0);
}
// assisn the result
if (result != nullptr)
{
*result = r;
}
/// set the yay message or boo message
if (r == S_OK)
{
if (message_result != nullptr)
{
*message_result = Message_RecycleBin_Empty_Success;
}
return true;
}
else
{
if (message_result != nullptr)
{
*message_result = Message_RecycleBin_Empty_Failure;
}
return false;
}
}
}