Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

quick syntax question

Status
Not open for further replies.

Brewman76

Programmer
Mar 27, 2001
50
US
I have this line of code:

memcpy(&x, (BYTE*)lParam, sizeof(x));

lParam is the LPARAM datatype, this is in a message callback.

My question is what exactly is (BYTE*)lParam doing? I am trying to do this same thing in a VB app and I'm not sure of the VB equivalent. Saying CByte(lParam) doesn't seem to be giving me the expected results.

 
>> what exactly is (BYTE*)lParam doing?

it casts the unsigned long value to a memory address of type unsigned char[]

does that help?
-pete
 
The BYTE* is casting it as a single byte. I am pretty sure that it is not required because the BYTE* will be made into a void*. The last argument for size is the real issue. If that should exceed the "SIZE OF" x, you will write into unknown memory. In this case, it is not an issue. So, however big x is, its allocated memory will be set to exactly what lParam's memory is. LPARAM is a 32 bit value so if x is not at least 4 bytes, you wont get all the data.

HOWEVER, if x exceeds the "SIZE OF" lParam, then you will be copying unknown data into x.

I hope this made sense.

Matt
 
doh.. and it is needed... cause it needs to be a pointer. The value is the actual memory address (i think) so casting the value as a pointer makes it THAT memory address and not the address of lParam.

Phew.

Matt
 
Thanks for the fast reply.

If I understand memcpy correctly, we are taking sizeof(x) bytes starting at the address pointed to by lParam and copying the bytes to the memory address &x and following.

Can you tell me why you would want to cast this memory address as a byte? I thought memory addresses were 32 bits.

Would you happen to know the VB equivalent of (BYTE*)lParam. Perhaps VarPtr(cbyte(lParam))?
 
what is really happeing is as follows

LPARAM lParam = (int)somePtr;

// integer value set into lparam

memcpy(&x,(BYTE*)lParam,sizeof(x))

// if somePtr's address is 12345
// this happens, thus really copying the
// contents of somePtr's memory
memcpy(&x,(BYTE*)12345,sizeof(x));


// If this were to happen, the value put into x
// is the memory ADDRESS of somePtr, not its data
memcpy(&x,&lParam,sizeof(x));


Does that help?

Matt
 
Thanks for the responses. Don't worry about the sizeof thing. I'm somewhat "paraphrasing" from the actual code. The length parameter is actually a const unsigned int which is the sizeof the struct that x is. But I digress.

Zyrenthian, if I understand your last post correctly, (BYTE*)lParam is giving me a pointer to wherever lParam is pointing? I'm getting lost on the concept of why a memory address would be casted as a byte.
 
Build a quick console app and paste this in

int* ptr = new int;
*ptr = 45678;
LPARAM lParam = (int)ptr;
int x,y;
memcpy(&x,&lParam,sizeof(x));
memcpy(&y,(BYTE*)ptr,sizeof(y));

cout<<x<<endl<<y<<endl;

Matt
 
>> Can you tell me why you would want to cast this memory
>> address as a byte?

because the signature of memcpy requires a &quot;void*&quot; in for that argument. The code could have been memcpy(&x, (void*)lParam, ...);

>> I thought memory addresses were 32 bits.

They are

 
Zyrenthian thanks. Your last post somewhat clears it up. So we are passing to memcpy a pointer to lParam. Correct?
 
>> So we are passing to memcpy a pointer to lParam.
>> Correct?

NO, the value of lParam is a memory address.

-pete
 
here are the equivalences in the code

y == *somePtr;
x == (int)lParam;

lParam = (int)ptr;

so because lParam is actually a value and that value is REALLY a memory address, lParam is cast BACK TO the pointer value.


Does that help any?
 
Zyrenthian,


You probably know it right, but you did not explain it right:


>So, however big x is, its allocated memory will be set to exactly what lParam's memory is.
Not true: since it's sizeof(x) that's being used as a size indicator, exactly sizeof(x) bytes are being copied. If sizeof(x) exceeds the memory buffer pointed to by lParam, then rubbish will be included in the copy (since more bytes are being copied than are actual part of the source buffer). If sizeof(x) is smaller than the source buffer, then not all bytes will be copied into the destination buffer.


>LPARAM is a 32 bit value so if x is not at least 4 bytes, you wont get all the data..
LPARAM is a 32 bit value, no doubt about that. But since it's a memory pointer, it could very well be pointing to a one-byte size buffer.

Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top