Connecting Tech Pros Worldwide Help | Site Map

Questions about a Windows struct

  #1  
Old August 29th, 2008, 11:15 PM
Chris Saunders
Guest
 
Posts: n/a
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

  #2  
Old August 29th, 2008, 11:35 PM
Keith Thompson
Guest
 
Posts: n/a

re: Questions about a Windows struct


"Chris Saunders" <evas@mountaincable.netwrites:
Quote:
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?).
Quote:
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.
Quote:
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@mib.org <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"
  #3  
Old August 29th, 2008, 11:55 PM
Ben Bacarisse
Guest
 
Posts: n/a

re: Questions about a Windows struct


"Chris Saunders" <evas@mountaincable.netwrites:
Quote:
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 *.
Quote:
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).

--
Ben.
  #4  
Old August 29th, 2008, 11:55 PM
Chris Saunders
Guest
 
Posts: n/a

re: Questions about a Windows struct


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

  #5  
Old August 30th, 2008, 12:05 AM
Ian Collins
Guest
 
Posts: n/a

re: Questions about a Windows struct


Chris Saunders wrote:
Quote:
First, thanks very much for the reply Keith.
Which you should have quoted!
Quote:
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.

--
Ian Collins.
  #6  
Old August 30th, 2008, 12:25 AM
Chris Saunders
Guest
 
Posts: n/a

re: Questions about a Windows struct


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

Regards
Chris Saunders
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Questions about StructLayout Pack values active answers 8 July 21st, 2007 07:35 PM
questions about arrays copx answers 15 November 14th, 2005 08:31 PM
comp.lang.c Changes to Answers to Frequently Asked Questions (FAQ) Steve Summit answers 1 November 14th, 2005 11:18 AM
Paper sizes and HDEVMODE struct Carlos Kirkconnell answers 0 July 21st, 2005 12:30 PM