473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Questions about a Windows struct

Here is the C declaration of a struct from Windows:
typedef struct _TXFS_READ_BACK UP_INFORMATION_ OUT {
union {

//
// Used to return the required buffer size if return code is
STATUS_BUFFER_O VERFLOW
//

DWORD BufferLength;

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

BYTE Buffer[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKU P_INFORMATION_O UT, *PTXFS_READ_BAC KUP_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 1503
"Chris Saunders" <ev**@mountainc able.netwrites:
Here is the C declaration of a struct from Windows:
typedef struct _TXFS_READ_BACK UP_INFORMATION_ OUT {
union {

//
// Used to return the required buffer size if return code is
STATUS_BUFFER_O VERFLOW
//

DWORD BufferLength;

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

BYTE Buffer[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKU P_INFORMATION_O UT, *PTXFS_READ_BAC KUP_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_BACKU P_INFORMATION_O UT, the
expression obj.DUMMYUNIONN AME.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_Keit h) 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**@mountainc able.netwrites:
Here is the C declaration of a struct from Windows:

typedef struct _TXFS_READ_BACK UP_INFORMATION_ OUT {
union {

//
// Used to return the required buffer size if return code is
STATUS_BUFFER_O VERFLOW
//

DWORD BufferLength;

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

BYTE Buffer[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKU P_INFORMATION_O UT, *PTXFS_READ_BAC KUP_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_BACK UP_INFORMATION_ OUT {
union {

//
// Used to return the required buffer size if return code is
STATUS_BUFFER_O VERFLOW
//

DWORD BufferLength;

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

BYTE Buffer[1];
} DUMMYUNIONNAME;
} TXFS_READ_BACKU P_INFORMATION_O UT, *PTXFS_READ_BAC KUP_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
2194
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 http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
4
2797
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
1967
by: Marc Thrun | last post by:
Hello, I've got a few questions: 1) Given the two structs struct A { int x; }; and
3
1433
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 Reg = new RegistryKey();
28
3005
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
9
3061
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 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Declare your object variables
28
25486
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 A { int n; int m;
25
2748
by: Syam | last post by:
Fully answered interview questions in c and c++ from http://www.faiqs.com
8
5633
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 Structure CHARFORMAT2 Public cbSize As Integer
0
7496
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7452
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7784
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6014
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5354
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.