Questions about a Windows struct

C

Chris Saunders

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;

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

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

Regards
Chris Saunders
 
K

Keith Thompson

Chris Saunders said:
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.
 
B

Ben Bacarisse

Chris Saunders said:
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. Buffer[0] is a BYTE so a pointer to it is of type BYTE *.
and if I have a pointer to some allocated memory BYTE * p;
can I assign it like this: Buffer[0] = p;

Woah... back up. This looks like the "struct hack" (GIYF). When the
last member of a struct is a one-element array, it is often that case
that we are to assume that the whole struct has been "over
allocated". Rather than allocating just the room for the declared
members, the programmer will have allocated that size + some more for
the buffer at the end.

Now, to get to your question, no, you can't allocate memory to a
buffer by assigning to Buffer[0]. For one thing, all the types are
wrong. What usually happens is that memory obtained from some place
is simply treated as if it were one of these structs, the buffer being
accessed in the normal way but with indexes (or pointers) beyond the
otherwise rather pointless single declared element.

This is about as much as C has to say on the matter (other than that
this trick is, strictly speaking, not permitted).
 
C

Chris Saunders

First, thanks very much for the reply Keith. I had a look at the FAQ but I
still don't see how to allocate some memory and assign it to Buffer[0].
Here is the struct again:

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;

Regards
Chris Saunders
 
I

Ian Collins

Chris said:
First, thanks very much for the reply Keith.

Which you should have quoted!
I had a look at the FAQ
but I still don't see how to allocate some memory and assign it to
Buffer[0].
You can't and you wouldn't want to. buffer[0] is a single BYTE.
 
C

Chris Saunders

My thanks to all who replied. I'm starting to get this.

Regards
Chris Saunders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top