[Help] Injecting into Memory

1
Hello guys! :D
I've been trying to mess with injection and I'm building a general DLL that will injects anything I want into an existing process. The objective is currently to achieve a custom screen size for the Conquer Online Client.

I currently have this piece of code:

Code: Select all

DWORD idProcess = GetCurrentProcessId();
        HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, idProcess);
        if (hProcess) 
        {
            const LPVOID BASE_W_ADDR = (LPVOID) (0x00469DEF + 6);
            const LPVOID BASE_H_ADDR = (LPVOID) (0x00469DFC + 6);

            int width = GetPrivateProfileIntA("GameResolution", "Width", 1024, CONFIG_FILE);
            int height = GetPrivateProfileIntA("GameResolution", "Height", 768, CONFIG_FILE);

            int read = 0;
            SIZE_T bytes_read = 0, bytes_written = 0;
            DWORD error = 0;
            if (ReadProcessMemory(hProcess, BASE_W_ADDR, &read, 4, &bytes_read) || GetLastError() == ERROR_PARTIAL_COPY) {
                if (bytes_read == 0)
                    MessageBoxA(NULL, "Could not get width memory offset.", "ReadProcessMemory error", MB_OK);
                
                if (!WriteProcessMemory(hProcess, BASE_W_ADDR, (LPCVOID)width, 4, &bytes_written))
                    MessageBoxA(NULL, "Could not write to width memory offset.", "WriteProcessMemory error", MB_OK);

                error = GetLastError();
                if (error != 0)
                {
                    sprintf_s(msg, "Error writing to memory! %d" , error);
                    MessageBoxA(NULL, msg, "WriteProcessMemory error", MB_OK);
                }
                else 
                {
                    sprintf_s(msg, "Width edited with success! :D Old X: %d", read);
                    MessageBoxA(NULL, msg, "Success?", MB_OK);
                }
                error = 0;
            }

            if (ReadProcessMemory(hProcess, BASE_H_ADDR, &read, 4, &bytes_read) || GetLastError() == ERROR_PARTIAL_COPY) {
                if (bytes_read == 0)
                    MessageBoxA(NULL, "Could not get height memory offset.", "ReadProcessMemory error", MB_OK);
                if (!WriteProcessMemory(hProcess, BASE_H_ADDR, (LPCVOID)height, 4, &bytes_written))
                    MessageBoxA(NULL, "Could not write to height memory offset.", "WriteProcessMemory error", MB_OK);
                
                error = GetLastError();
                if (error != 0)
                {
                    sprintf_s(msg, "Error writing to memory! %d", error);
                    MessageBoxA(NULL, msg, "WriteProcessMemory error", MB_OK);
                }
                else 
                {
                    sprintf_s(msg, "Height edited with success! :D Old X: %d", read);
                    MessageBoxA(NULL, msg, "Success?", MB_OK);
                }                
            }
        }
It will give me an error related to ERROR_FILE_NOT_FOUND on width change and will give no error on height change, but neither work. I have tried a lot of solutions from Stack Overflow and etc but idk what's wrong there. The process can currently ReadProcessMemory with success, it will return the current screen resolution at that memory pointer but I can't set the one in my ini file.

Any lights on this?

Re: [Help] Injecting into Memory

3
You shouldn't be using GetLastError unless you know there is an error (i.e ReadProcessMemory returns false).
WriteProcessMemory and ReadProcessMemory expect a buffer/pointer to be passed in, and casting the variable to LPCVOID doesn't turn it into a pointer, use the & sign instead (i.e &width).
when you are using OpenProcess on the same process you are injected to you should use the handle from GetCurrentProcess and cast it to DWORD and use GetCurrentProcess when asked for a handle to read or write memory.

Re: [Help] Injecting into Memory

4
Spirited wrote: Thu Dec 17, 2020 8:44 am Is the injected library the same architecture as Conquer? I think Conquer only runs in 32-bit.
Yes, I'm building it as x86 and the injector too.
Diab wrote: You shouldn't be using GetLastError unless you know there is an error (i.e ReadProcessMemory returns false).
WriteProcessMemory and ReadProcessMemory expect a buffer/pointer to be passed in, and casting the variable to LPCVOID doesn't turn it into a pointer, use the & sign instead (i.e &width).
when you are using OpenProcess on the same process you are injected to you should use the handle from GetCurrentProcess and cast it to DWORD and use GetCurrentProcess when asked for a handle to read or write memory.
I changed the code and now it works! :D

Code: Select all

int read = 0;
            SIZE_T bytes_read = 0, bytes_written = 0;
            if (ReadProcessMemory(hProcess, BASE_W_ADDR, &read, 4, &bytes_read) || GetLastError() == ERROR_PARTIAL_COPY) {
                if (bytes_read == 0)
                    MessageBoxA(NULL, "Could not get width memory offset.", "ReadProcessMemory error", MB_OK);
                
                if (!WriteProcessMemory(hProcess, BASE_W_ADDR, &width, 4, &bytes_written))
                {
                    sprintf_s(msg, "Error writing to memory! %d", GetLastError());
                    MessageBoxA(NULL, msg, "WriteProcessMemory error", MB_OK);
                }

                /*sprintf_s(msg, "Width edited with success! :D NewX: %d Old X: %d", width, read);
                MessageBoxA(NULL, msg, "Success?", MB_OK);*/
            }
This made the job :] Thank you
cron