473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Questions about a Windows struct

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

Aug 29 '08 #1
5 1486
"Chris Saunders" <ev**@mountaincable.netwrites:
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) ks***@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"
Aug 29 '08 #2
"Chris Saunders" <ev**@mountaincable.netwrites:
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).

--
Ben.
Aug 29 '08 #3
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

Aug 29 '08 #4
Chris Saunders wrote:
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.

--
Ian Collins.
Aug 29 '08 #5
My thanks to all who replied. I'm starting to get this.

Regards
Chris Saunders
Aug 29 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
15
by: Marc Thrun | last post by:
Hello, I've got a few questions: 1) Given the two structs struct A { int x; }; and
3
by: p988 | last post by:
Learning C# is much tougher than I expected...Please help me by answering the following questions! Thank you in advance! 1. Are all Enumerations type Value type? 2. The line, RegistryKey...
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
9
by: Patrick.O.Ige | last post by:
I have a code below and its a PIE & BAR CHART. The values now are all static but I want to be able to pull the values from a database. Can you guys give me some ideas to do this? Thanks ...
28
by: WaterWalk | last post by:
Hi, I'm haunted by 2 questions about struct copy. Though I searched the net, but still in confusion. 1. Does struct assignment copies every member including array members? For example, struct...
25
by: Syam | last post by:
Fully answered interview questions in c and c++ from http://www.faiqs.com
8
by: active | last post by:
I have this and it seems to be working OK but now I wonder if the Pack value shouldn't be 1. What do you think? <StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=CharSet.Auto)_ Public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.