-3

I want to do WriteProcessMemory In C++ using Dword or Int, without storing it in a Variable i found one way to do this but i can only do it with bytes. does anyone know how to do this?? this one works using bytes.

WriteProcessMemory(hProcess, (void*)(BasePointer + 0x728),"\x90\x90", 4, NULL);

Thanks for the help everyone i made a function and its working really good

void WriteMemory(DWORD Address,DWORD NewValue, int NewValueSize)
{
    WriteProcessMemory(hProcess, (void*)Address, (void*)&NewValue, NewValueSize, NULL);
}

int main()
{
    srand(time(0));
    GetProcess();
    WriteMemory((BasePointer + 0x6F8),2+rand()%65500,2);
    CloseHandle(hProcess);
    return 0;
}
Tprice88
  • 651
  • 2
  • 7
  • 18
  • 3
    What do you have against variables? – James McNellis Apr 03 '12 at 19:55
  • it makes shorter code for me im gonna do like 300 different writes – Tprice88 Apr 03 '12 at 19:56
  • `&"100"` is a pointer to the character string you know... So I don't understand why you're casting it to a `DWORD*` – Mike Kwan Apr 03 '12 at 19:57
  • 2
    @Tprice88, for short code with 300 writes you might want to make a macro... – Roman R. Apr 03 '12 at 20:02
  • 1
    "I'm just testing whatever lets me compile without using bytes to write or declaring anything." This is a bad idea. If you want to learn C++, pick up [a good introductory book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Randomly applying different combinations of operators and punctuators can only end in tears. – James McNellis Apr 03 '12 at 20:02
  • i ben using c++ about 2 months now, i can do sockets and more. – Tprice88 Apr 03 '12 at 20:03

2 Answers2

2

Put the data into an array, and have a small loop get each item from the array, write it to the target process, then move to the next:

struct data {
    DWORD offset;
    DWORD length;
    char data[256];
};

data items[] = {
    {0x728, 4, "\x90\x90"},
    // ...
};

for (int i=0; i<elements(items); i++)
    WriteProcessMemory(hProcess, (void *)(BasePointer + items[i].offset), items[i].data, items[i].length, NULL);
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • thats worse then declaring, and i doubt itll work for int values. – Tprice88 Apr 03 '12 at 20:06
  • @Tprice88: I'm not sure what "worse than declaring" is supposed to mean. For int values, you'd have to break the int down into individual bytes (non-trivial, but certainly possible). – Jerry Coffin Apr 03 '12 at 20:08
  • This table driven approach is by far the best way to solve this problem. Writing `WriteProcessMemory` 300 times is just form. Don't repeat yourself (DRY). – David Heffernan Apr 03 '12 at 20:23
  • C++ is more powerful anyways i think im getting close to solving this. WriteProcessMemory(hProcess, (void*)(BasePointer + 0x728),"&"+DWORD("500"), 4, NULL); – Tprice88 Apr 03 '12 at 20:48
  • 2
    @Tprice88: Can you explain what you think the expression `"&"+DWORD("500")` does? – Blastfurnace Apr 03 '12 at 21:12
  • btw what is elements its not declared. – Tprice88 Apr 07 '12 at 22:39
2

The reason your code "works" with bytes is that you're using a string literal. A string literal is an array of char, and an array of char automatically converts to a pointer to the first element if the context calls for it, as it does when you try to pass one as the third argument of WriteProcessMemory.

You can write any value you want as a string literal, including a four-byte DWord, as long as you're willing to express it one byte at a time. For example, "\x70\x71\x72\x73". On Windows, that's equivalent to a pointer to the DWord value 0x73727170. You probably won't want to do that, though; expressing numbers like that is tedious.

C++ doesn't offer any facility for having literal arrays of non-char type. There's just not much demand for it. Demand for literal char arrays is high because everyone deals with text, so we want easy ways of expressing it in our code. Although everyone also works with numbers, we rarely have need to express blobs of numerical data in our code, especially not mid-expression.

You haven't given a practical problem to be solved by your question. You're just asking whether something is possible to do. I'm sorry to be the bearer of bad news, but the answer is that what you're asking for cannot be done in C++. You'll just have to do like everyone else and declare a variable. Variables are cheap; feel free to use them whenever the need arises. Nonetheless, you've been shown ways to keep your code concise by using subroutines. Macros can also help shorten your code, if that's your goal.

Please also note that the string literal in your code is an array of three characters — the two between quotation marks, plus the nul character the compiler automatically includes at the end of all string literals. You're telling the function that you've provided a pointer to a block of four bytes, which is false. the fourth byte that the function writes into the other process will have an unspecified value.

Community
  • 1
  • 1
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467