Write in another process' memory with WriteProcessMemory and a Pointer
<p>There are probably several post that explain my problem in several ways... But I have been searching in google and stackoverflow searchbox and I didn't found anything. So here I go.</p>

<p>I want to Write in a Process Memory a String Changing it in c++, but I don't even know clearly how it work so..</p>

<p>I have this pointer:
<a href="http://i.stack.imgur.com/jQMds.png" rel="nofollow">Image of the pointer</a>
Please, can someone help me doing it?</p>

<p>I've tried it but it's not working..</p>

<pre><code>#include &lt;windows.h&gt; 
#include &lt;iostream&gt; 

int main() {
    HWND hWnd = FindWindow(0, "WindowName");
    if (hWnd == 0) {
        std::cout &lt;&lt; "Cannot find window." &lt;&lt; std::endl;
    }
    DWORD pId;
    GetWindowThreadProcessId(hWnd, &amp;pId);
    HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pId);
    DWORD baseAddress = 0x009B03D0;
    DWORD offset = 0xA7;
    DWORD ptrAddress;
    char *newString = "newvalue";
    ReadProcessMemory(hProc, (void*)baseAddress, &amp;ptrAddress, sizeof(DWORD), 0);
    WriteProcessMemory(hProc, (void*)(ptrAddress + offset), newString, strlen(newString), 0);
    std::cout &lt;&lt; "Done. " &lt;&lt; &amp;ptrAddress &lt;&lt; std::endl;
    std::getchar();
}
</code></pre>

<p>I should get the pointer and jumpt to the last one because I only have one offset.. But I'm not getting the correct one..</p>

<hr>

<p>Edit:</p>

<p>Here is my new code, it works until the WriteProcessMemory function.. What can be wrong?</p>

<p>CODE THAT ACTUALLY WORKS:</p>

<pre><code>int main()
{
    unsigned long Pointer;   /* to hold the final value */
    unsigned long temp;      /* hold the temp values    */
    unsigned long address = 0x009B03D0;
    unsigned long offset = 0xA7;
    unsigned long newString = 0;
    DWORD pid;
    HWND hwnd;
    hwnd = FindWindow(0, TEXT("NewWindow"));
    if (!hwnd)
    {
        cout &lt;&lt; "No!\n";
        cin.get();
    }
    else
    {
        GetWindowThreadProcessId(hwnd, &amp;pid);
        HANDLE phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
        if (!phandle)
        {
            cout &lt;&lt; "None!\n";
            cin.get();
        }
        else
        {
            while (1)
            {

                ReadProcessMemory(phandle, reinterpret_cast&lt;LPVOID&gt;(address), &amp;temp, sizeof(temp), 0);
                Pointer = temp + offset;
                //Good
                ReadProcessMemory(phandle, reinterpret_cast&lt;LPVOID&gt;(Pointer), &amp;newString, 16, 0);
                cout &lt;&lt; reinterpret_cast&lt;LPVOID&gt;(Pointer) &lt;&lt; " en " &lt;&lt; newString;
                Sleep(1000);
            }
            return 0;
        }
    }
}
</code></pre>

<p>CODE THAT NOT WORK:</p>

<pre><code>int main()
{
    unsigned int Pointer;   /* to hold the final value */
    unsigned int temp;      /* hold the temp values    */
    unsigned int address = 0x009B03D0;
    unsigned int offset = 0xA7;
    unsigned int newString = 1768060259;
    DWORD pid;
    HWND hwnd;
    hwnd = FindWindow(0, TEXT("NewWindow"));
    if (!hwnd)
    {
        cout &lt;&lt; "NO\n";
        cin.get();
    }
    else
    {
        GetWindowThreadProcessId(hwnd, &amp;pid);
        HANDLE phandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
        if (!phandle)
        {
            cout &lt;&lt; "NONE\n";
            cin.get();
        }
        else
        {
            while (1)
            {

                ReadProcessMemory(phandle, reinterpret_cast&lt;LPVOID&gt;(address), &amp;temp, sizeof(temp), 0);
                Pointer = temp + offset;
                //Good
                if (!WriteProcessMemory(phandle, reinterpret_cast&lt;LPVOID&gt;(Pointer), &amp;newString, sizeof(newString), 0))
                    std::cerr &lt;&lt; "Couldn't write process memory:" &lt;&lt; GetLastError() &lt;&lt; std::endl;
                cout &lt;&lt; reinterpret_cast&lt;LPVOID&gt;(Pointer) &lt;&lt; " en " &lt;&lt; newString;
                Sleep(1000);
            }
            return 0;
        }
    }
}
</code></pre>