Search code examples
c++pointersdereference

Dereferencing pointer C++


I just managed to write a global CBT hook in c++, usable with c#. It may sound stupid but my knowledge of pointers and dereferencing of those is very bad due to my work with c#.

I'm not able to dereference a pointer inside a struct pointed to by the lParam.

It looks like this: the lParam is a long Pointer to a CBT_CREATEWND struct, which in turn holds a member "lpcs", and a pointer to a CREATESTRUCT. This struct contains a member "x", which I want to read. I tried the following, but I get invalid values for x:

CREATESTRUCT str = *(LPCREATESTRUCT)(((LPCBT_CREATEWND)lParam)->lpcs);
int normal = str.x;
PostMessage(FindWindow(NULL, L"Process Watcher Window"), RegisterWindowMessage(L"ALEXX_SEINE_WNDMESSAGE_C"), wParam, normal);

could someone please help me?


Solution

  • Your syntax seems to check out, even though it is perhaps a bit unreadable, and the LPCREATESTRUCT cast is obviously unnecessary.

    You mentioned that you get invalid values for x, perhaps lParam is not really a pointer to a CBT_CREATEWND structure? Are you checking that nCode of the callback function is equal to HCBT_CREATEWND before casting lParam?