Results 1 to 5 of 5

Thread: Help me to understand something...

  1. #1

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Help me to understand something...

    When passing a struct into an API, I've noticed that many have a member that represents the size of the struct in bytes. I was just wondering why that would be needed. Shouldn't the size already be known by the definition of the struct?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Most likely for backwards compatibility. If you pass the size, then a newer function can expect both the old and new structures, and act accordingly
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Hmmm...*ponders a bit*

    Sounds good.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Parksie has it. But it really relates to MS approach to development of their OS 'families' Consumer windows (9X) is really sixteen bit - all of the api calls are translated 'down' to that environment.

    The NT family are actually 32 bit calls.

    So if you have code that should run both places there needs to be a way for the api interface (most of the stuff listed as api's are NOT - they merely pass the data to the real internal api - they are wrappers) to figure out what to do with the stuff it's given.

    Some native api's that are easy to find are any that start with Rtl
    like RtlZeroMemory() (calloc() calls this for example), or RtlMoveMemory().

    There are a LOT of native NT api's - in fact, there is a book on the subject:

    Gary Nebbet - Windows NT/2000 Native API Reference

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    As a practical example, say you have a struct:
    struct SOMETYPE {
    DWORD dwSize;
    int member;
    int thing;
    };

    sizeof(SOMETYPE) would be 12.

    There is a function that takes such a struct as an argument:
    void func(SOMETYPE *pST);

    In a later header file you exand the struct:
    struct SOMETYPE {
    DWORD dwSize;
    int member;
    int thing;
    long weebl;
    long bob;
    };

    sizeof(SOMETYPE) would be 20.

    The function still has the same prototype, but the code was replaced by newer code in the current DLL.

    Now up comes a user with a new version of the DLL but an old version of the header file and writes code using the old SOMETYPE struct, which is passed to the new func in the DLL. (happens all the time)
    Normally this would probably result in an access violation or worse in stack corruption. But if you write the new function so it uses the dwSize member it works:
    Code:
    struct OLD_SOMETYPE {
      DWORD dwSize;
      int member;
      int thing;
    };
    
    void func(SOMETYPE *pST)
    {
      if(pST->dwSize == sizeof(OLD_SOMETYPE)) {
        // old code
      } else if(pST->dwSize == sizeof(SOMETYPE)) {
        // new code
      } else {
        // error: unknown struct size
      }
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width