1+ #define WIN32_LEAN_AND_MEAN
2+ #include <Windows.h>
3+ #include <luart.h>
4+ //using namespace std;
5+
6+ // process.FindWindow(windowName) method
7+ // returns a userdata and the ID of the of the process
8+ LUA_METHOD (process , FindWindow )
9+ {
10+ if (LUA_TSTRING == lua_type (L , 1 )) {
11+ const char * windowName = lua_tostring (L , -1 ); // lua string to char
12+
13+ HWND win = FindWindowA (windowName ,NULL ); // find the window
14+ DWORD pID = 0 ; // the window ID
15+ GetWindowThreadProcessId (win , & pID ); // get the window ID
16+
17+ lua_pushlightuserdata (L , win ); // returns the userdata
18+ lua_pushinteger (L , pID ); // and the ID
19+ return 2 ;
20+ }
21+ }
22+
23+ // process.WriteProcessMemory(processID,address,string)
24+ LUA_METHOD (process , WriteProcessMemory )
25+ {
26+ if (LUA_TNUMBER == lua_type (L , 1 ) && LUA_TNUMBER == lua_type (L , 2 ) && LUA_TSTRING == lua_type (L , 3 )) {
27+ const int pID = lua_tonumber (L , -1 );
28+ const int address = lua_tonumber (L ,-2 );
29+ const char * bytes = lua_tostring (L ,-3 );
30+ //char memory[sizeof(bytes)];
31+
32+ HANDLE pHandle = OpenProcess (PROCESS_VM_WRITE , 0 , pID );
33+
34+ WriteProcessMemory (pHandle , (LPVOID )address , (LPCVOID )bytes , sizeof (bytes ), NULL );
35+ //ReadProcessMemory(pHandle, (LPVOID)address, &memory, sizeof(bytes), NULL);
36+
37+ CloseHandle (pHandle );
38+
39+ /*
40+ if (memory == bytes) {
41+ lua_pushboolean(L,1); // returns true if the bytes were written correctly
42+ return 1;
43+ }
44+ else {
45+ lua_pushboolean(L,0); // returns false if the bytes wasn't written correctly
46+ return 1;
47+ }
48+ */
49+ return 0 ;
50+ //lua_pushboolean(L,1);
51+ //return 1; // returns nothing
52+ }
53+ }
54+
55+ // process.ReadProcessMemory(processID,address,size)
56+ // return the readed memory
57+ LUA_METHOD (process , ReadProcessMemory )
58+ {
59+ if (LUA_TNUMBER == lua_type (L , 1 ) && LUA_TNUMBER == lua_type (L , 2 ) && LUA_TNUMBER == lua_type (L , 3 )) {
60+ const int pID = lua_tonumber (L , 1 );
61+ const int address = lua_tonumber (L ,2 );
62+ const int size = lua_tonumber (L ,3 );
63+ char memory [size ];
64+
65+ HANDLE pHandle = OpenProcess (PROCESS_ALL_ACCESS , 0 , pID );
66+
67+ ReadProcessMemory (pHandle , (LPVOID )address , & memory , size , NULL );
68+
69+ CloseHandle (pHandle );
70+
71+ lua_pushstring (L ,memory );
72+ return 1 ; // returns nothing
73+ }
74+ else {
75+ luaL_error (L ,"Bad argument at method ReadProcessMemory" );
76+ }
77+ }
78+
79+
80+
81+ //--------------- The finalizer function called when the module is garbage collected
82+ int process_finalize (lua_State * L )
83+ {
84+ //--- finalizer functions returns nothing
85+ return 0 ;
86+ }
87+
88+
89+
90+ static const luaL_Reg process_properties [] = {
91+ {NULL , NULL }
92+ };
93+
94+ static const luaL_Reg processlib [] = {
95+ {"FindWindow" , process_FindWindow }, // FindWindow method
96+ {"WriteProcessMemory" , process_WriteProcessMemory }, // WriteProcessMemory method
97+ {"ReadProcessMemory" , process_ReadProcessMemory }, // ReadProcessMemory
98+ {NULL , NULL }
99+ };
100+
101+
102+
103+ int __declspec(dllexport ) luaopen_process (lua_State * L )
104+ {
105+ lua_regmodulefinalize (L , process );
106+ return 1 ;
107+ }
0 commit comments