-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoveonreboot.cpp
More file actions
210 lines (176 loc) · 5.39 KB
/
Copy pathmoveonreboot.cpp
File metadata and controls
210 lines (176 loc) · 5.39 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "common.h"
#include "Support\\LWAnsiString\\LWAnsiString.h"
#include "osver.h"
#include "IAT_ENV.H"
#include "IAT_FILE.H"
void LegacyMarkForDelete(const char* name, LWAnsiString* vebal)
{
if (! (IAT_DynamicLink_FileApi(IAT_FILE_LINKING_IMPORTS_ANIS) & IAT_FILE_LINKING_IMPORTS_ANIS) == IAT_FILE_LINKING_IMPORTS_ANIS)
{
return;
}
if (!(IAT_DynamicLink_Environment(IAT_ENV_API_ALL_A) & IAT_ENV_API_ALL_A) == IAT_ENV_API_ALL_A)
{
return;
}
else
{
LWAnsiString* WinDir = LWAnsiString_CreateString(MAX_PATH);
LWAnsiString_MarkLenDirty(WinDir);
DWORD size = IAT_GetWindowsDirectoryA(WinDir->AnsiData, MAX_PATH);
if (size > MAX_PATH)
{
LWAnsiString_Reserve(WinDir, size + 1);
SetLastError(0);
size = IAT_GetWindowsDirectoryA(WinDir->AnsiData, size + 1);
if (!LWAnsiString_EndsWith(WinDir, "\\", false))
{
LWAnsiString_AppendA(WinDir, "\\");
}
}
else
{
if (!LWAnsiString_EndsWith(WinDir, "\\", false))
{
LWAnsiString_AppendA(WinDir, "\\");
}
}
// now the windir
LWAnsiString_AppendA(WinDir, "wininit.ini");
{
DWORD ShortPath = IAT_GetShortPathA(name, NULL, 0);
if (ShortPath != 0)
{
LWAnsiString* EntryLine = LWAnsiString_CreateString(1);
LWAnsiString_AppendA(EntryLine, "NUL=");
LWAnsiString_AddReserve(EntryLine, ShortPath + 1);
char* offset = (char*)LWAnsiString_EndingOffset(EntryLine);
if (IAT_GetShortPathA(name, offset, ShortPath) != 0)
{
LWAnsiString_MarkLenDirty(EntryLine);
LWAnsiString_AppendNewLine(EntryLine);
HANDLE file = IAT_CreateFileA(LWAnsiString_ToCStr(WinDir), GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (file != INVALID_HANDLE_VALUE)
{
DWORD wrote = 0;
if (GetLastError() != ERROR_ALREADY_EXISTS)
{
WriteFile(file, "[rename]\r\n", 10, &wrote, 0);
}
SetLastError(0);
if ((IAT_SetFilePointer(file, 0, 0, FILE_END) != INVALID_SET_FILE_POINTER) && (GetLastError() == NO_ERROR))
{
if (!WriteFile(file, LWAnsiString_ToCStr(EntryLine), EntryLine->Length * sizeof(char), &wrote, 0))
{
LWAnsiString_AppendWithNewLineA(vebal, "Failed to write the new entry to delete on reboot");
}
else
{
LWAnsiString_AppendA(vebal, "");
LWAnsiString_AppendA(vebal, LWAnsiString_ToCStr(EntryLine));
LWAnsiString_AppendA(vebal, "----> registered for deletion in ");
LWAnsiString_AppendA(vebal, LWAnsiString_ToCStr(WinDir));
LWAnsiString_AppendWithNewLineA(vebal, " OK ");
}
}
else
{
LWAnsiString_AppendWithNewLineA(vebal, "Failed to seek to end of wininit.ini to write the pending delete on reboot");
}
CloseHandle(file);
}
else
{
LWAnsiString_AppendA(vebal, "Failed to Create / Open wininit.ini in the expected location of ");
LWAnsiString_AppendA(vebal, LWAnsiString_ToCStr(WinDir));
LWAnsiString_AppendNewLineA(vebal);
LWAnsiString_AppendA(vebal, "Error code in attempt is ");
LWAnsiString_AppendNumberA(GetLastError(), vebal, 0);
LWAnsiString_AppendNewLineA(vebal);
}
}
else
{
LWAnsiString_AppendWithNewLineA(vebal, "Failed to get the short (8.3 DOS naming) size for the pending delete.");
}
LWAnsiString_FreeString(EntryLine);
}
else
{
LWAnsiString_AppendWithNewLineA(vebal, "Failed to get the short (8.3 DOS naming) size for the pending delete.");
}
}
// free
LWAnsiString_FreeString(WinDir);
}
}
bool DeleteOnReboot(int* result, const char** message_result, const char* argv[], int argc)
{
MyOSVERSIONINFO osvi;
bool UnicodeVer = FALSE;
FetchVersionInfo(&osvi, &UnicodeVer);
LWAnsiString* vebal = LWAnsiString_CreateString(1);
BOOL LegacyOS = FALSE;
if (argc < 2)
{
LWAnsiString_FreeString(vebal);
return false;
}
else
{
if ((result == 0 || (message_result == 0)))
{
LWAnsiString_FreeString(vebal);
return false;
}
else
{
if (((IAT_DynamicLink_FileApi(IAT_FILE_LINKING_IMPORTS_ANIS) & (IAT_FILE_LINKING_IMPORTS_ANIS))) != (IAT_FILE_LINKING_IMPORTS_ANIS))
{
LWAnsiString_FreeString(vebal);
return false;
}
if ((osvi.A.dwMajorVersion < 5) || (osvi.A.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS))
{
LWAnsiString_AppendWithNewLineA(vebal, "Warning: Windows verison reports OS possibly older than Windows 2000. Modern register for delete might not work ");
LegacyOS = TRUE;
}
if (IAT_MoveFileExA == 0)
{
if (LegacyOS)
{
LWAnsiString_AppendWithNewLineA(vebal, "Failed to get MoveFileExA routine. Falling back to legacy register. Note. This will NOT work on NT based OS.");
for (int i = 2; i < argc; i++)
{
LegacyMarkForDelete(argv[i], vebal);
}
}
else
{
LWAnsiString_AppendWithNewLineA(vebal, "Failed to get MoveFileExA routine. No files registered for deletion. ");
}
}
else
{
for (int i = 2; i < argc; i++)
{
SetLastError(0);
if (IAT_MoveFileExA(argv[i], NULL, MOVEFILE_DELAY_UNTIL_REBOOT))
{
LWAnsiString_AppendA(vebal, argv[i]);
LWAnsiString_AppendWithNewLineA(vebal, " Registered for deletion. ");
}
else
{
LWAnsiString_AppendA(vebal, argv[i]);
LWAnsiString_AppendWithNewLineA(vebal, " Failed to Register for Deletion.");
}
}
}
}
WriteStdout(vebal->AnsiData);
LWAnsiString_FreeString(vebal);
}
SetLastError(0);
return true;
}