#include <fstream>\r
#include <stdio.h>\r
+#include <stdint.h>\r
#include <string>\r
#include <time.h>\r
#include <windows.h>\r
\r
string get_text() {\r
int len = SendMessageA(hwnd_edit, WM_GETTEXTLENGTH, 0, 0);\r
- char *buffer = new char[len + 1];\r
- int read = SendMessageA(hwnd_edit, WM_GETTEXT, len + 1, (LPARAM)buffer);\r
+ string result;\r
+ result.resize(len + 1);\r
+ int read = SendMessageA(hwnd_edit, WM_GETTEXT, len + 1, (LPARAM)result.data());\r
if (read != len)\r
puts("???");\r
- buffer[read] = 0;\r
- string result(buffer);\r
- delete[] buffer;\r
+ result.data()[read] = 0;\r
return result;\r
}\r
\r
}\r
\r
int get_pos(int x, int y) {\r
- return (y + 1) * (WIDTH + 4) + x + 1;\r
+ return (y + 1) * (WIDTH + 3) + x + 1;\r
}\r
\r
char get_block(int x, int y) {\r
ifs.close();\r
buffer[length] = 0;\r
std::string result(buffer);\r
- delete buffer;\r
+ delete[] buffer;\r
return result;\r
}\r
\r
STARTUPINFOA si;\r
PROCESS_INFORMATION pi;\r
\r
-BOOL CALLBACK ew_cb(HWND hwnd, LPARAM lp) {\r
- DWORD pid;\r
- DWORD tid = GetWindowThreadProcessId(hwnd, &pid);\r
- if (pi.dwProcessId == pid && pi.dwThreadId == tid) {\r
- hwnd_notepad = hwnd;\r
- return false;\r
- }\r
- return true;\r
-}\r
-BOOL CALLBACK ecw_cb(HWND child, LPARAM in) {\r
- char buffer[1024 + 1];\r
- int len = GetClassNameA(child, buffer, 1024);\r
- buffer[len] = 0;\r
- if (strcmp(buffer, "Edit") == 0) {\r
- hwnd_edit = child;\r
- return false;\r
- }\r
- return true;\r
+bool check_class(HWND hwnd, const char *name) {\r
+ char b[64];\r
+ int n = GetClassNameA(hwnd, b, 64);\r
+ return (n && strncmp(b, name, n) == 0);\r
}\r
\r
void start_notepad() {\r
ZeroMemory(&pi, sizeof(pi));\r
\r
if (!CreateProcessA(NULL, // No module name (use command line)\r
- "notepad.exe", // Command line\r
+ "notepad.exe lvl/0.txt", // Command line\r
NULL, // Process handle not inheritable\r
NULL, // Thread handle not inheritable\r
FALSE, // Set handle inheritance to FALSE\r
) {\r
printf("CreateProcess failed (%d).\n", GetLastError());\r
}\r
- Sleep(100);\r
- EnumWindows(ew_cb, 0);\r
- EnumChildWindows(hwnd_notepad, ecw_cb, 0);\r
+ Sleep(1000);\r
+ EnumWindows([](HWND hwnd, LPARAM lp)->BOOL{\r
+ if (check_class(hwnd, "Notepad")) {\r
+ *(HWND*)lp = hwnd;\r
+ return false;\r
+ }\r
+ return true;\r
+ }, (LPARAM)&hwnd_notepad);\r
+ EnumChildWindows(hwnd_notepad, [](HWND hwnd, LPARAM lp)->BOOL{\r
+ printf("looking at %p\n", hwnd);\r
+ if (check_class(hwnd, "NotepadTextBox")) {\r
+ EnumChildWindows(hwnd, [](HWND hwnd, LPARAM lp)->BOOL{\r
+ printf(" looking at %p\n", hwnd);\r
+ const size_t size = (WIDTH+4)*(HEIGHT+2);\r
+ char b[size];\r
+ DWORD_PTR dwResult;\r
+ b[0] = 0;\r
+ SendMessageTimeoutA(hwnd, WM_GETTEXT, size, (LPARAM)b,\r
+ SMTO_ABORTIFHUNG, 100, &dwResult);\r
+ if (b[0] == '+') {\r
+ // TODO: check lvl\r
+ printf("%.*s.\n", size, b);\r
+ *(HWND*)lp = hwnd;\r
+ return false;\r
+ }\r
+ return true;\r
+ }, lp);\r
+ if (lp) return false;\r
+ }\r
+ return true;\r
+ }, (LPARAM)&hwnd_edit);\r
SendMessage(hwnd_edit, EM_SETREADONLY, TRUE, NULL);\r
}\r
void close_notepad() {\r
- TerminateProcess(pi.hProcess, 0);\r
- CloseHandle(pi.hProcess);\r
- CloseHandle(pi.hThread);\r
+ SendMessage(hwnd_edit, EM_SETREADONLY, FALSE, NULL);\r
+ //TerminateProcess(pi.hProcess, 0);\r
+ //CloseHandle(pi.hProcess);\r
+ //CloseHandle(pi.hThread);\r
}\r
\r
// Keys\r
static int progress = 0;\r
switch (progress) {\r
case 0:\r
- print_text(4, 2, "Move with left/right.", text_speed);\r
+ print_text(4, 2, "Move with A/D.", text_speed);\r
\r
progress++;\r
break;\r
close_notepad();\r
\r
return 0;\r
-}
\ No newline at end of file
+}\r