0

I understand PBYTE is unsigned char* from Windows Data Types.

I am calling a function which updates data and dataSize.

I print the data after I call the function, but once I print the vector that stores all the data, I am not getting the same value.

Is it possible that the data is getting corrupted?

This is a function written internally, not open-source.

SerializeEvent(__in_z PCWSTR EventName,
     __out_bcount(DataSize)  PBYTE & Data,
    __out                   ULONG & DataSize){}

This function takes in a field and serializes it and data is basically the buffer where the serialize event name is stored, and dataSize is the size for the same.

PBYTE data=NULL;
ULONG dataSize=0;

int main(){
    vector<PBYTE> dataVector;
    vector<ULONG> dataSizeVector;
    
    for(int i=0;i<10;i++){
    
        serializeData(event,data,dataSize);
        printf("%02x ",data); ///----->a
        dataVector.push_back(data);
        dataSizeVector.push_back(dataSize);

    }
    //just want to print the contents of this vector to verify
    for(int i=0;i<dataVector.size();i++){
        printf("%02x ",dataVector.at(i)); ----> b
    }
    
}

The print from the first for loop is not matching the print from the second for loop. Am I missing something?

But doing the same for dataSizeVector is printing similar values...

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ccoding
  • 1
  • 2
  • 8
  • Hi @Someprogrammerdude. The serialize function requires PBYTE &data, so that;s a double pointer? – ccoding Oct 15 '20 at 23:10
  • Before your next question, please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And always include a proper [mcve]. – Some programmer dude Oct 15 '20 at 23:17

1 Answers1

1

Both loops are printing out incorrect data.

printf("%02x ", ...); is being given a BYTE* pointer as the input value for %x, but %x expects an integer not a pointer.

As I already showed you in your previous question, you can print out individual bytes using %x, and print out pointers using %p instead.

Try something more like this:

int main() {
    vector<PBYTE> dataVector;
    vector<ULONG> dataSizeVector;
    
    for(int i = 0; i < 10; ++i) {
        PBYTE data = NULL;
        ULONG dataSize = 0;
        SerializeEvent(event, data, dataSize);
        printf("%p", data);
        for(ULONG j = 0; j < dataSize; ++j) {
            printf(" %02x", data[j]);
        }
        printf("\n");
        dataVector.push_back(data);
        dataSizeVector.push_back(dataSize);
    }

    //just want to print the contents of this vector to verify
    for(size_t i = 0; i < dataVector.size(); ++i) {
        PBYTE data = dataVector[i];
        ULONG dataSize = dataSizeVector[i];
        printf("%p", data);
        for(ULONG j = 0; j < dataSize; ++j) {
            printf(" %02x", data[j]);
        }
        printf("\n");
    }    
}

I suggest you read this printf() reference on how printf() actually works.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Hi @Remy I tried what you suggested, thanks for the elaborate reply :) While the pointer values are the same(so I understand they are pointing to the same address.), the data isn't.. :( – ccoding Oct 15 '20 at 23:25
  • 93 b6 53 for the first and 70 8e ce ad ..isn't that strange? – ccoding Oct 15 '20 at 23:26
  • @ccoding If the data is changing it's likely due to an issue within `SerializeData`. What does the pointer assigned to its `Data` parameter point to? How is that pointed-to thing allocated? – Miles Budnek Oct 15 '20 at 23:49
  • @ccoding If the pointers are the same in both loops, then the data should be the same, assuming the serialize is allocating and filling memory properly. So you are likely just still printing the data wrong. [It works fine for me](https://ideone.com/EqTd32). As you can see, both loops produce the same output. – Remy Lebeau Oct 16 '20 at 00:02