C++ WriteProcessMemory Without Variables

Go To StackoverFlow.com

-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;
}
2012-04-03 19:52
by Tprice88
What do you have against variables - James McNellis 2012-04-03 19:55
it makes shorter code for me im gonna do like 300 different write - Tprice88 2012-04-03 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 2012-04-03 19:57
@Tprice88, for short code with 300 writes you might want to make a macro.. - Roman R. 2012-04-03 20:02
"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. Randomly applying different combinations of operators and punctuators can only end in tears - James McNellis 2012-04-03 20:02
i ben using c++ about 2 months now, i can do sockets and more - Tprice88 2012-04-03 20:03


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.

2012-04-03 22:39
by Rob Kennedy
Thanks for the help your answer was most useful to me - Tprice88 2012-04-03 23:26


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);
2012-04-03 20:03
by Jerry Coffin
thats worse then declaring, and i doubt itll work for int values - Tprice88 2012-04-03 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 2012-04-03 20:08
i allready have bytes without declaring if u look above - Tprice88 2012-04-03 20:17
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 2012-04-03 20:23
David this is C+ - Tprice88 2012-04-03 20:41
@Tprice88: And so - Jerry Coffin 2012-04-03 20:44
C++ is more powerful anyways i think im getting close to solving this. WriteProcessMemory(hProcess, (void*)(BasePointer + 0x728),"&"+DWORD("500"), 4, NULL) - Tprice88 2012-04-03 20:48
@Tprice88: Can you explain what you think the expression "&"+DWORD("500") does - Blastfurnace 2012-04-03 21:12
btw what is elements its not declared - Tprice88 2012-04-07 22:39
Ads