Thread: Getting a structure from a pointer

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Getting a structure from a pointer

    I use something like this to get the structure out of lParam (in the low-level keyboard hook).
    But it only returns a pointer.
    Code:
    KBDLLHOOKSTRUCT *meep = (KBDLLHOOKSTRUCT *) lParam;
    If I use something like this to get a variable from this structure, I get memory addresses:
    Code:
    KBDLLHOOKSTRUCT *meep = (KBDLLHOOKSTRUCT *) lParam;
    char buffer[256];
    MessageBox(HWND_DESKTOP,atoi(meep->vkCode,buffer,10),"Button is:",MB_OK);
    it will give the memory address to that specific variable inside the structure. How can I get the value of vkCode?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Maybe *meep->vkCode
    If you understand what you're doing, you're not learning anything.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Then I get:
    Code:
    error: invalid type argument of `unary *'

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The problem is in your call to the MessageBox api function - the second paramater should be a string, not a number which atoi returns.
    Code:
    char buffer[256]='\0';
    wsprintf(buffer,"%d",meep->vkCode);
    MessageBox(HWND_DESKTOP,buffer,"Button is:",MB_OK);
    I'm surprised you didn't get an incompatible type error from the MessageBox call. Also, atoi only takes one parameter so that, too, should have fired off an error.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I am actually using itoa, just accidentally wrote the wrong one here.
    atoi has only one parameter, so I would've got many different errors.
    And that meep->vkCode is passed through a function as an int and then used with itoa.

    I am not getting any error. I am getting the memory address of meep->vkCode
    Last edited by maxorator; 09-21-2006 at 07:17 AM.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    I see - easily done.

    Just as a test does the snippet I provide produce the same result? The reason I ask, is because the syntax you are using to access the struct member variable via its pointer is correct and you should therefore get the actual value of the 'vkCode' and not the garbage you're reporting (you may just be getting the rubbish that 'buffer' contains).

    edit:

    >>And that meep->vkCode is passed through a function as an int and then used with itoa<<

    It would probably be better to show exactly how you're doing this as the code you've posted - typos aside - doesn't appear to contain any errors.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Just as a test does the snippet I provide produce the same result? The reason I ask, is because the syntax you are using to access the struct member variable via its pointer is correct and you should therefore get the actual value of the 'vkCode' and not the garbage you're reporting (you may just be getting the rubbish that 'buffer' contains).
    I don't get garbage. If I use meep->vkCode, lets say I get 2200340, when I use meep->scanCode, I get 2200344 and when I use meep->flags, I get 2200348.

    I am not at my home computer at the moment, but my program looks something like this:
    DLL file, LowLevelKeyboardProc, case HC_ACTION:
    Code:
    PostMessage(guiapplication,WM_KEYBDHOOK,wParam,lParam);
    GUI application, WindowProcedure, case WM_KEYBDHOOK:
    Code:
    KBDLLHOOKSTRUCT *meep = (KBDLLHOOKSTRUCT *) lParam;
    NewItem(meep->vkCode,0,0,0);
    NewItem function (ints):
    Code:
    counter++;
    data[counter].a=paramone;
    data[counter].b=paramtwo;
    data[counter].c=paramthree;
    data[counter].d=paramfour;
    And then, an other function shows the whole data array in a list view control. Uses itoa() to convert the integer to a string.
    Last edited by maxorator; 09-21-2006 at 07:46 AM.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I don't get garbage.<<

    It seems possible that you're passing the address and not the value of the DWORD, vkCode, struct member variable in one of these helper functions you're using.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Oh no... I am passing this:
    Code:
    PostMessage(guiapplication,WM_KEYBDHOOK,wParam,lParam);
    But shouldn't lParam be passed with "&" or "*"??
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 06:35 PM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM