Reading souce code of WINAPI functions | Casting

Is there a way that I can read the source code of WINAPI functions? Such as SendMessage? I want to understand how casting works. For example:

SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) szString) ;

I don't understand how it's programmed that szString can easily transform into a LPARAM.
That doesn't have anything to do with the implementation (source code) of the SendMessage() function. That is simply you (the coder of the Windows application) telling the compiler to reinterpret the TCHAR pointer as of type LPARAM.

In layman's terms: "Compiler, I know this is a pointer to a TCHAR, but I need you to believe it is of type LPARAM for this particular call. Don't worry about potential data access issues because the function is quite smart and won't mess up. Thank you!"
But how does the function easily work with TCHAR? Does it have a copy of itself that accepts TCHAR in its LPARAM field?
Last edited on
Maybe it would make more sence if you look at what LPARAM is? It's just a pointer to some memory
typedef LONG_PTR LPARAM;

LPARAMS get passed from function to function until they get to the function that deals with that message (in your case LB_ADDSTRING), and the code there will cast the LPARAM to whatever is needed. It dosn't matter what's passed around as the LPARAM is just a pointer to some memory. Does that help?
Nope. The SendMessage() function actually calls the window's window procedure. It is the window procedure that ultimately work with the string in the lparam parameter.

I have no access to the Windows source code, but I imagine SendMessage() queries the window for the pointer to the window procedure, and then simply call it.

1
2
3
4
5
6
7
8
9
LRESULT WINAPI SendMessage(HWND hWnd, UINT msg, WPARAM wParam LPARAM lParam)
{
    WNDPROCCALLBACK fnWndProc = (WNDPROCCALLBACK)GetWindowLongPtr(hWnd, GWLP_WNDPROC);
    if (fnWndProc)
    {
        return (*fnWndProc)(hWnd, msg, wParam, lParam);
    }
    return 0;
}


On the other hand, the window procedure for the ListBox control might be programmed like this:

1
2
3
4
5
6
7
8
9
10
11
12
LRESULT WINAPI CALLBACK ListBoxWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case LB_ADDSTRING:
        //Because the message intends to add a string, we know for sure that lParam is really a pointer to TCHAR.
        TCHAR *szTheNewString = (TCHAR*)lParam;
        //and now add a copy of this string to the internal structure of the listbox as a new element.
        ...
        break;
    }
}


NOTE: I didn't look up the right signatures for the SendMessage() function or the window procedure. I just posed this as an example and the return values may be wrong. I may also be wrong about the type of a window proceudre function pointer. Look it up if you want to know for sure.
Thanks for all the info everyone, I think I understand now.

Its called typecasting. When you put a different type of something in front of another variable in parenthesis it Typecasts it into that other type. Nothing unusual and it is very useful if you ever need to implement it in your own code.

For future reference. What you can do if you are using dev-c++ is call the function in your program but leave a parameter empty. The compiler we see this error then display it and open up the header file highlighting the incorrectly used function. I discovered this by accident when compiling a program. But you could use it to find out exactly which header file the function is in then have it highlight it assuming you couldn't find it through another method.
Topic archived. No new replies allowed.