Re: Questions about a Windows struct



"Chris Saunders" <evas@xxxxxxxxxxxxxxxxx> writes:
Here is the C declaration of a struct from Windows:


typedef struct _TXFS_READ_BACKUP_INFORMATION_OUT {
union {

//
// Used to return the required buffer size if return code is
STATUS_BUFFER_OVERFLOW
//

DWORD BufferLength;

//
// On success the data is copied here.
//

BYTE Buffer[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKUP_INFORMATION_OUT, *PTXFS_READ_BACKUP_INFORMATION_OUT;

Can I declare a pointer to Buffer[0] as BYTE ** ptr;

No. A BYTE** is a pointer to a pointer to a BYTE. There has to be a
pointer-to-BYTE object for it to point to. There is no
pointer-to-BYTE object in the code you've shown us.

Given an object obj of type TXFS_READ_BACKUP_INFORMATION_OUT, the
expression obj.DUMMYUNIONNAME.Buffer, which is an array name,
evaluates (in most contexts) to a *value* of type pointer-to-BYTE, but
there's no object of type pointer-to-BYTE.

Buffer is an array. If you want to point to the array object, you
need something of type BYTE(*)[1], i.e., a pointer to an array of one
BYTE. But that's almost certainly not what you want. Instead, you
probably want a pointer to *the first element* of Buffer; for that,
you just need a BYTE*.

In general, a FOO* that points to the first element of an array of FOO
is the most common way to access the elements of the array.

It looks like this code probably does something very similar to the
struct hack (see www.c-faq.com question 2.6), in addition to using a
union to overlay the buffer length (on input?) with the buuffer itself
(on output?).

and if I have a pointer to some allocated memory BYTE * p;
can I assign it like this: Buffer[0] = p;

No. Buffer[0] is of type BYTE; p is of type BYTE*.

You can write Buffer[0] = *p to copy a single BYTE. To copy multiple
bytes, use a loop or memcpy() -- but make sure there's enough space
allocated to hold it.

I'm actually accessing this C struct from another language and
my C is quite rusty so sorry for the dumb question.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: pointer( array ) <- invalid typecast BLEH
    ... > (except when the array is empty, in which case the latter is an error). ... I wrote a little program that demonstrates the "problems" with delphi arrays ... StaticBuffer in C means the address of the first character of the buffer. ... (there is no pointer! ...
    (alt.comp.lang.borland-delphi)
  • Re: How to cast a buffer pointer into a byte array
    ... allocates a buffer in memory. ... After filling this buffer (with bitmap ... array ) because this is the type the OCX display control it ... Is there a way to send a pointer to the buffer ...
    (microsoft.public.vb.syntax)
  • Re: dynamic array, access violation example.
    ... > {Suppose a programmer wants to use the dynamic array variable ... De buffer could be anything, a record, ... > and array, or just some memory in this example. ... as many pointer types. ...
    (alt.comp.lang.borland-delphi)
  • Re: buffer...
    ... In the case of that function, the buffer can be anything ... Note that the name of an array variable with no is a pointer to the first ... meaning to the bytes stored in a particular memory block. ...
    (microsoft.public.vc.mfc)
  • Re: Zero length array declaration
    ... If you want "to have the structure above point to a message buffer", ... should use a pointer to a buffer containing a message. ... be an array of strings, ... > compilers may let you get away with it. ...
    (comp.lang.c)