Skip to content

Commit 5a4cada

Browse files
authored
0.1-beta - fixed WriteProcessMemory() method no writing
1 parent 9142104 commit 5a4cada

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

process.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#define WIN32_LEAN_AND_MEAN
22
#include <Windows.h>
33
#include <luart.h>
4-
//using namespace std; // don't need this garbage?
4+
#include <process.h>
5+
//using namespace std;
56

67
// process.FindWindow(windowName) method
78
// returns a userdata and the ID of the of the process
@@ -20,25 +21,41 @@ LUA_METHOD(process, FindWindow)
2021
}
2122
}
2223

24+
/*
25+
static int process_new(lua_State* L)
26+
{
27+
Process ** udata = (Process **)lua_newuserdata(L, sizeof(Process));
28+
*udata = new Process();
29+
luaL_getmetatable(L, "Process");
30+
lua_setmetatable(L, -1);
31+
return 1;
32+
}
33+
*/
34+
2335
// process.WriteProcessMemory(processID,address,string)
36+
// returns false if failed and true if succeded
2437
LUA_METHOD(process, WriteProcessMemory)
2538
{
2639
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);
40+
const int pID = lua_tonumber(L, 1);
41+
const int address = lua_tonumber(L,2);
42+
const char* bytes = lua_tostring(L,3);
43+
if (pID < 1) {
44+
luaL_error(L,"Invalid process ID!");
45+
}
3046
//char memory[sizeof(bytes)];
3147

32-
HANDLE pHandle = OpenProcess(PROCESS_VM_WRITE, 0, pID);
48+
HANDLE pHandle = OpenProcess(PROCESS_VM_WRITE | PROCESS_VM_OPERATION, 0, pID);
3349

34-
WriteProcessMemory(pHandle, (LPVOID)address, (LPCVOID)bytes, sizeof(bytes), NULL);
50+
VirtualProtectEx(pHandle, (LPVOID)address, strlen(bytes), PAGE_READWRITE, NULL); // removes protection so it can be written
51+
52+
int status = WriteProcessMemory(pHandle, (LPVOID)address, bytes, strlen(bytes), NULL); // and write it and return a status
53+
int lastErr = GetLastError();
3554
//ReadProcessMemory(pHandle, (LPVOID)address, &memory, sizeof(bytes), NULL);
3655

3756
CloseHandle(pHandle);
3857

3958
/*
40-
// checks if it was written with success
41-
// returns false if no and true if yes.
4259
if (memory == bytes) {
4360
lua_pushboolean(L,1); // returns true if the bytes were written correctly
4461
return 1;
@@ -48,7 +65,8 @@ LUA_METHOD(process, WriteProcessMemory)
4865
return 1;
4966
}
5067
*/
51-
return 0;
68+
lua_pushboolean(L,status);
69+
return 1;
5270
//lua_pushboolean(L,1);
5371
//return 1; // returns nothing
5472
}
@@ -62,7 +80,10 @@ LUA_METHOD(process, ReadProcessMemory)
6280
const int pID = lua_tonumber(L, 1);
6381
const int address = lua_tonumber(L,2);
6482
const int size = lua_tonumber(L,3);
65-
char memory[size]; // the string to be returned
83+
char memory[size];
84+
if (pID < 1) {
85+
luaL_error(L,"Invalid process ID!");
86+
}
6687

6788
HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pID);
6889

@@ -104,6 +125,7 @@ static const luaL_Reg processlib[] = {
104125

105126
int __declspec(dllexport) luaopen_process(lua_State *L)
106127
{
107-
lua_regmodulefinalize(L, process);
128+
//luaL_register(L, "MyObj", processlib);
129+
lua_regmodulefinalize(L, process);
108130
return 1;
109131
}

0 commit comments

Comments
 (0)