I recently sent a request for help because ReadFile
kept crashing for me. The moderator sent me some code
which cured my problem. My next step was to
experiment to see what I had originally done wrong.
(For further info see ReadFile crashes by toolmanx.)
I started by replacing each item back to the way I
had originally written it to make sure dwRead was my
only problem since the moderator's code made several
code changes to mine. dwRead was my only problem. I
initialized with several different int values
finally even using my original "0". It still worked.
The only change that caused failure was using LPDWORD
as written in Win32.HLP instead of plain DWORD. That
apparently was my mistake. I copied Win32.HLP
verbatum. My understanding, correct me if I'm wrong,
is, LPDWORD indicates to the compiler that dwRead is
a pointer, not just a variable.
The moderator had also added an "&" in front of
dwRead in the function. This tells me that I am asking
for the address of dwRead, not what is in the location of
dwRead. That made sense to me and was reinforced
because BCC32 would error if I sent a pointer in
the function without the ampersand.
My next step was to try to find out why LPDWORD wouldn't
work. The compiler would not accept
"LPDWORD dwRead = 0;" or "LPDWORD dwRead = 16;"
I was trying to put an int into a DWORD pointer. So
I tried a cast (DWORD*)16;(16 is the number of bytes in
my file I'm reading). The compiler accepted that
and gave me an exe. The exe crashes just like in the
old days.
I've read a lot of pointer tutorials but
I'm missing something here. Also, why I couldn't use
ReadFile as written in Win32.HLP bothers me too. Is
this a problem in more than one API? I'll be experimenting
using many other API's in the future as part of my
learning process. Do I have to watch out for this
every time I try to use a new API? Obviously I need
to understand what is happening when using LPDWORD
otherwise I'll be blindly trying DWORD in place of
LPDWORD everytime I get a failure on new functions.
Lastly so I don't force a moderator to write a
long and tedious explanation, let me note what
I think I know about pointers. Pointers point to
an address in memory. When the API uses LPDWORD,
it wants a pointer. Since dwRead works only using
DWORd, that tells me that it wants a variable, at
least that's what works. Why does the API make it
look like it wants a pointer when a variable is
what it wants?