I was trying to run some drivers coded for 32-bit vista (x86) on 64-bit win7 (amd64) and it was not running. After a lot of debugging, and hit-and-trial, I made it to work on the latter, but I don’t know the reason why it’s working. This is what I did:
At many places, buffer pointers pointed to an array of structures(different at different places), and to increment them, at some places this type of statement was used:
ptr = (PVOID)((PCHAR)ptr + offset);
And at some places:
ptr = (PVOID)((ULONG)ptr + offset);
The 2nd one was returning garbage, so I changed them all to 1st one. But I found many sample drivers on the net following the second one. My questions:
- Where are these macros
defined(google didn’t help much)? - I understand all the P_ macros are
pointers, why was a pointer casted
to ULONG? How does this work on
32-bit? - PCHAR obviously changes the
width according to the environment. Do you know any place to find documentation for this?
can be converted back and forth to ULONG without loss – but not so on a 64-bit system
(where casting the value will truncate it). People cast to ULONG to get byte-base pointer
arithmetic (even though this has undefined behavior, as you found out)