typecasting

Can someone help me understand this code

lparam is passed by reference in this case as
(LPARAM)&DlghWnd)

then there is this
HWND *hw=(HWND*)lParam;
*hw=hWnd;

I don't understand what the typecasting (HWND*) is doing in this case. I've seen pointers created as, datatype * variable_name = address, such as HWND * hw = &lparam.i dont understand why the rhs of the equation is (HWND*)lparam instead of &lparam. Please explain why this is typecasted
(LPARAM)&DlghWnd) This is casting the address (aka a pointer to DlghWnd) to a LPARAM. DlghWnd judging by it's name is a HWND.

HWND *hw=(HWND*)lParam; This is casting LPARAM back to a pointer to HWND.
Last edited on
I wrote this code to help me understand what is going on

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <windows.h>
#include <iostream>


void CALLBACK arguments (LPARAM c);

int main()
{
    int c = 2;
    int * a = &c ;
    char b;
    std::cout<<" "<<&c<<" "<<a<<" "<<*a<<" "<<&a<<std::endl;
    arguments((LPARAM)&a);
    std::cin>>b;
    return 0;
 }
void CALLBACK arguments (LPARAM c)
{
     int * d =(int*)c;
     std::cout<<d<<" "<<*d<<std::endl;     
}


the output is

0x22ff74 0x22ff74 2 0x22ff70
0x22ff70 2293620

I don't get why the last output is 2293620. After int* d = (int*)c, d = &a. So then shouldn't *d = 2 ? so the last output should be 2 and not 2293620.
d is assigned the address of a, which is the address of a pointer to int.
dereferencing d gives you the address of c which is 2293620 decimal / 0x22ff74 hex.

See how easy it is to get it wrong when you bypass the type system?
Last edited on
closed account (S6k9GNh0)
arguments((LPARAM)&a); is wrong. Your passing the location of int c in memory.
Try arguments((LPARAM)a); instead.

If that's wrong, it's because I don't know what type an LPARAM is...ffs.

Topic archived. No new replies allowed.