This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinutils.c
194 lines (180 loc) · 6.41 KB
/
winutils.c
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
#include "./include/winutils.h"
#define GET_WINDOWS_HEAP_COUNT 1024
errno_t checkErrorExit(HWND *hWindow){
if (MessageBoxW(*hWindow,
L"エラーが発生しました。続行した場合の動作は保証しません。終了しますか?",
L"警告", MB_YESNO | MB_ICONWARNING) == IDYES &&
MessageBoxW(*hWindow, L"本当に終了しますか?", L"終了の確認",
MB_YESNO | MB_ICONWARNING) == IDYES) {
if(DestroyWindow(*hWindow)==0){
return (errno_t)GetLastError();
}else{
return 0;
}
} else {
return 0; // continue;
}
}
errno_t getProcesses(DWORD **all_processes, unsigned int *all_processes_count) {
DWORD allProcesses_size;
DWORD neededSize;
DWORD *allProcesses_tmp = NULL;
for (allProcesses_size = 4096; allProcesses_size <= 65536;
allProcesses_size <<= 1) {
allProcesses_tmp = (DWORD *)realloc(allProcesses_tmp, allProcesses_size);
neededSize = 0;
if (EnumProcesses(allProcesses_tmp, allProcesses_size, &neededSize) ==
0) {
MessageBoxA(NULL, "any error", "", MB_OK);
*all_processes = NULL;
*all_processes_count = 0;
free(allProcesses_tmp);
return (errno_t)GetLastError();
} else {
if (neededSize >= allProcesses_size) {
continue; // メモリを増やす
}
break;
}
}
*all_processes = allProcesses_tmp;
*all_processes_count = neededSize / sizeof(DWORD);
return EXIT_SUCCESS;
}
errno_t getProcessName(DWORD processID, wchar_t **processName) {
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, processID);
if (hProcess != NULL) {
DWORD fileName_size;
wchar_t *fileName = NULL;
for (fileName_size = 256; fileName_size <= 65536; fileName_size <<= 1) {
fileName =
(wchar_t *)realloc(fileName, sizeof(wchar_t) * fileName_size);
DWORD result_name_size = fileName_size;
if (QueryFullProcessImageNameW(hProcess, 0, fileName,
&result_name_size) == 0) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
continue; // 文字を増やす
} else {
CloseHandle(hProcess);
return (errno_t)GetLastError();
}
} else {
*processName = fileName;
break;
}
}
CloseHandle(hProcess);
return EXIT_SUCCESS;
} else {
*processName = NULL;
return (errno_t)GetLastError();
}
}
// 共有はしない
BOOL CALLBACK EnumWindowsProc(HWND hWindow, LPARAM lParam);
bool isWindowAppearingInTaskbar(HWND hWindow, DWORD windowStyle,
DWORD windowExStyle);
struct EnumWindowProcData {
HWND *hWindows;
size_t hWindows_count;
size_t heap_count;
errno_t error;
bool filter;
};
errno_t getWindowHandles(HWND **hWindows, size_t *hWindows_countP,
bool filter) {
struct EnumWindowProcData enumWindowProcData;
enumWindowProcData.hWindows =
(HWND *)calloc(sizeof(HWND) * GET_WINDOWS_HEAP_COUNT, sizeof(HWND));
if (enumWindowProcData.hWindows == NULL) {
return (errno_t)ERROR_INSUFFICIENT_BUFFER;
} else {
enumWindowProcData.hWindows_count = 0;
enumWindowProcData.heap_count = GET_WINDOWS_HEAP_COUNT;
enumWindowProcData.error = 0;
enumWindowProcData.filter=filter;
WINBOOL enunWindowError =
EnumWindows(EnumWindowsProc, (LPARAM)&enumWindowProcData);
*hWindows = enumWindowProcData.hWindows;
*hWindows_countP = enumWindowProcData.hWindows_count;
if (enunWindowError == 0) {
if (enumWindowProcData.error > 0) {
return enumWindowProcData.error;
} else {
return (errno_t)GetLastError();
}
} else {
return EXIT_SUCCESS;
}
}
}
BOOL CALLBACK EnumWindowsProc(HWND hWindow, LPARAM lParam) {
static int count = 0;
static HWND *hWindows;
struct EnumWindowProcData *enumWindowProcDataP =
(struct EnumWindowProcData *)lParam;
int heap_count = enumWindowProcDataP->heap_count;
hWindows = enumWindowProcDataP->hWindows;
// memory check
if (count > heap_count) {
heap_count += 1024;
enumWindowProcDataP->heap_count = heap_count;
hWindows = realloc(hWindows, heap_count);
if (hWindows == NULL) {
enumWindowProcDataP->error = ERROR_INSUFFICIENT_BUFFER;
return FALSE;
} else {
enumWindowProcDataP->hWindows = hWindows;
}
}
// memory check end
// window check
if (hWindow != NULL) {
if (enumWindowProcDataP->filter == true) {
const DWORD windowStyle = (DWORD)GetWindowLongW(hWindow, GWL_STYLE);
const DWORD windowExStyle =
(DWORD)GetWindowLongW(hWindow, GWL_EXSTYLE);
// condition
if (isWindowAppearingInTaskbar(hWindow, windowStyle, windowExStyle) ==
true) {
hWindows[count++] = hWindow;
enumWindowProcDataP->hWindows_count = count;
return TRUE;
} else {
return TRUE; // not in Taskbar. continue
}
}else{
hWindows[count++] = hWindow;
enumWindowProcDataP->hWindows_count = count;
return TRUE;
}
} else {
return TRUE; // continue
}
};
errno_t getHWindowPID(HWND hWindow, DWORD *processID) {
if (GetWindowThreadProcessId(hWindow, processID) == 0) {
return (errno_t)GetLastError();
} else {
return EXIT_SUCCESS;
}
}
bool isWindowAppearingInTaskbar(HWND hWindow, DWORD windowStyle,
DWORD windowExStyle) {
if ((windowStyle & WS_VISIBLE) == 0) return false;
if ((windowExStyle & WS_EX_TOOLWINDOW) != 0) return false;
#if (WINVER >= 0x0602)
if ((windowExStyle & WS_EX_NOREDIRECTIONBITMAP) != 0) return false;
#endif
HWND hOwnerWindow = GetWindow(hWindow, GW_OWNER);
if (hOwnerWindow != NULL && (((windowExStyle & WS_EX_APPWINDOW) == 0) ||
(IsIconic(hOwnerWindow) == TRUE))) {
return false;
} else {
return true;
}
}
errno_t searchWindow(wchar_t searchName,HWND *hWindowP){
//
}