Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I am trying to get a large char array allocated from the heap. From the stack, this code:

C++
unsigned char buf[] = { 0x48, 0x33, 0x65, .... };


works fine but if the bytecode is over 200,000, WriteProcessMemory will NOT copy more than 12224 bytes! There is indeed some kind of limit with these buffer arrays, Ive tested this too many times. So then I tried to get it from the heap but can't actually figure out how to initialize it

C++
unsigned char *buf = new unsigned char[256000];
buf = { 0x48, 0x33, 0x65, .... };


^ this throws an error buf does not name a type.

How can I simply declare an unsigned char array and initialize it at the same time. I've tried a one liner together, doesn't work. C++ seems like a very specific programming language. It's nothing compared to C. Thank you.

What I have tried:

tried to initialize it on the heap
Posted
Updated 19-May-22 20:08pm
v5
Comments
0x01AA 17-May-22 14:30pm    
You like to tell that you list here buf = { 0x48, 0x33, 0x65, .... }; 256'000 characters in your cpp file?
In case yes, I suggest it to put that into a binary resource and load it from there.

1 solution

I am not sure why you are having a problem and I am fairly certain that there is no limit of 12224 bytes. I have a structure initialized by including a header file that is over 512KB and it compiles just fine. In my case it is a structure with three members and there are almost 4700 of them and one is a text string of varying length. FWIW, it is a table of windows error codes and message strings. As I mentioned I put them all in a file that is included by the code. The code that does that looks like this :
C++
werrmsg Messages[] =
{
    #include "WindowsErrorList.h"
};
My point is this is a statically defined structure with a lot of members and over 512KB of data. If you take that approach with your data I think you can make it work. I wrapped mine in a local namespace and I recommend that for your data but it is not required.

You could do something like this :
C++
namespace // the anonymous namespace
{
const UCHAR BufferData[] =
{
#include "BufferData.h"
};

} // namespace
and the file with the data could look like this :
C++
  0x48
, 0x33
, 0x67
// and so on
 
Share this answer
 
v2
Comments
0x01AA 17-May-22 16:34pm    
Quote: 'there is no limit of 12224 bytes.'
I think OP means this limit happens when data is on stack and therefor I can imagine a limit. Usually one can configure max stack size in c++ compilers.
Rick York 17-May-22 16:50pm    
There might be limit for data on the stack but in windows programs the stack usually defaults to 2MB so a limit of 12KB makes little sense. The OP has amended their post to state that WriteProcessMemory has a limit. That was not there originally and I seriously doubt that statement also. The documentation for that function does not state any limit and I think it is because there isn't one. I think the OP hasn't posted enough code for anyone else to make sense of. I know for a fact that what I showed WILL work because I have done it several times.
0x01AA 17-May-22 17:00pm    
Yep. It is something confusing. Among others, that an edited question does not longer show the history...
A 5 for your help.
Franz Schweizer 18-May-22 20:23pm    
edited

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900