View mode: basic / threaded / horizontal-split · Log in · Help
June 02, 2005
Re: win32 and struct aligment
How to print the aligment in C? Also DMC headers miss the pager control so I
write it myself and I guess I will end up with the same problem in C. I've
downloaded some japanese windows api translation and it was done in the same way
I did it. I don't if they made it work.

align(1):

struct _16 {
NMHDR hdr;
WORD fwKeys;
RECT rcParent;
int iDir;
int iXpos;
int iYpos;
int iScroll;
}
alias _16 NMPGSCROLL;
alias _16* LPNMPGSCROLL;

In article <opsrrgecsx23k2f5@nrage.netwin.co.nz>, Regan Heath says...
>
>I've had a number of these problems, I think it's just none of those  
>things that happens when you port from C to D.
>
>The way I go about solving them is by printing the alignment, offset and  
>size of every member of the struct using D, then doing the same using C.
>
>One important point to note is that your D code is linking with the DMC  
>libraries so if the struct you are using comes from within a DMC library  
>you should really use DMC to compile your C. (I got bit by the differences  
>between findfirst in MSVC and DMC)
>
>Regan
>
>On Thu, 2 Jun 2005 20:35:31 +0000 (UTC), bobef <bobef_member@pathlink.com>  
>wrote:
>> struct RECT
>> {
>> LONG left;
>> LONG top;
>> LONG right;
>> LONG bottom;
>> }
>>
>> and LONG is alias for int
>>
>> rcParent.alignof says 4
>>
>> In article <d7np1t$26up$1@digitaldaemon.com>, Brad Beveridge says...
>>>
>>> bobef wrote:
>>>> no property 'offsetof' for type 'int'
>>>>
>>>> This is what I get....
>>>>
>>>>
>>>>> 1) What is the packing/alignment of RECT & does the align actually
>>>>> effect the packing of rcParent
>>>>
>>>>
>>> Hmm, all members of a struct should have the property "offsetof"
>>> http://digitalmars.com/d/struct.html
>>>
>>> What I was trying to say before - you have a type of RECT in your
>>> struct.  I think that RECT may be another structure.  I was trying to
>>> ask you to check that the RECT structure wasn't being padded out.
>>>
>>> For example, assume that RECT is align 4, and is
>>> struct RECT
>>> {
>>> char x,y;
>>> }
>>> There will be 2 bytes of padding after x & y.  Now, when you embed RECT
>>> inside of your other struct, does the align(1) directive also remove the
>>> padding from RECT?  I don't know, but I would hope that it does.
>>>
>>> Brad
>>
>>
>
June 02, 2005
Re: win32 and struct aligment
On Thu, 2 Jun 2005 21:20:49 +0000 (UTC), bobef <bobef_member@pathlink.com>  
wrote:
> How to print the aligment in C?

#include <stdlib.h>
#include <stdio.h>

#define print_member(base,x) { printf(#x"%*s = (%02d)  
0x%08x-0x%08x,%d\n",10-strlen(#x),"",(int)&x-(int)&base,&x,((int)&x)+sizeof(x)-1,sizeof(x));  
}

typedef struct test
{
	int a;
	short b;
	char c;
} TEST;

void main()
{
	TEST tmp;

	print_member(tmp,tmp.a);
	print_member(tmp,tmp.b);
	print_member(tmp,tmp.c);
}

replace TEST with the struct to test.

> Also DMC headers miss the pager control so I write it myself and I guess  
> I will end up with the same problem in C.

Wrote it in C? then compiled to a lib? or.. what did you compile it with?

> I've
> downloaded some japanese windows api translation and it was done in the  
> same way
> I did it. I don't if they made it work.
>
> align(1):
>
> struct _16 {
> NMHDR hdr;
> WORD fwKeys;
> RECT rcParent;
> int iDir;
> int iXpos;
> int iYpos;
> int iScroll;
> }
> alias _16 NMPGSCROLL;
> alias _16* LPNMPGSCROLL;

I would have preferred (not that it makes a difference):

struct NMPGSCROLL {
..etc..
}
alias NMPGSCROLL* LPNMPGSCROLL;

Regan

> In article <opsrrgecsx23k2f5@nrage.netwin.co.nz>, Regan Heath says...
>>
>> I've had a number of these problems, I think it's just none of those
>> things that happens when you port from C to D.
>>
>> The way I go about solving them is by printing the alignment, offset and
>> size of every member of the struct using D, then doing the same using C.
>>
>> One important point to note is that your D code is linking with the DMC
>> libraries so if the struct you are using comes from within a DMC library
>> you should really use DMC to compile your C. (I got bit by the  
>> differences
>> between findfirst in MSVC and DMC)
>>
>> Regan
>>
>> On Thu, 2 Jun 2005 20:35:31 +0000 (UTC), bobef  
>> <bobef_member@pathlink.com>
>> wrote:
>>> struct RECT
>>> {
>>> LONG left;
>>> LONG top;
>>> LONG right;
>>> LONG bottom;
>>> }
>>>
>>> and LONG is alias for int
>>>
>>> rcParent.alignof says 4
>>>
>>> In article <d7np1t$26up$1@digitaldaemon.com>, Brad Beveridge says...
>>>>
>>>> bobef wrote:
>>>>> no property 'offsetof' for type 'int'
>>>>>
>>>>> This is what I get....
>>>>>
>>>>>
>>>>>> 1) What is the packing/alignment of RECT & does the align actually
>>>>>> effect the packing of rcParent
>>>>>
>>>>>
>>>> Hmm, all members of a struct should have the property "offsetof"
>>>> http://digitalmars.com/d/struct.html
>>>>
>>>> What I was trying to say before - you have a type of RECT in your
>>>> struct.  I think that RECT may be another structure.  I was trying to
>>>> ask you to check that the RECT structure wasn't being padded out.
>>>>
>>>> For example, assume that RECT is align 4, and is
>>>> struct RECT
>>>> {
>>>> char x,y;
>>>> }
>>>> There will be 2 bytes of padding after x & y.  Now, when you embed  
>>>> RECT
>>>> inside of your other struct, does the align(1) directive also remove  
>>>> the
>>>> padding from RECT?  I don't know, but I would hope that it does.
>>>>
>>>> Brad
>>>
>>>
>>
>
>
June 02, 2005
Re: win32 and struct aligment
I also noted in the MSVC header richedit.h, this:

#ifdef _WIN32
#	define	_WPAD	/##/
#else
#	define	_WPAD	WORD
#endif

where _WPAD is used in the:

typedef struct _nmhdr
{
	HWND	hwndFrom;
	_WPAD	_wPad1;
	UINT	idFrom;
	_WPAD	_wPad2;
	UINT	code;
	_WPAD	_wPad3;
} NMHDR;

struct.

Regan

On Thu, 2 Jun 2005 21:20:49 +0000 (UTC), bobef <bobef_member@pathlink.com>  
wrote:

> How to print the aligment in C? Also DMC headers miss the pager control  
> so I
> write it myself and I guess I will end up with the same problem in C.  
> I've
> downloaded some japanese windows api translation and it was done in the  
> same way
> I did it. I don't if they made it work.
>
> align(1):
>
> struct _16 {
> NMHDR hdr;
> WORD fwKeys;
> RECT rcParent;
> int iDir;
> int iXpos;
> int iYpos;
> int iScroll;
> }
> alias _16 NMPGSCROLL;
> alias _16* LPNMPGSCROLL;
>
> In article <opsrrgecsx23k2f5@nrage.netwin.co.nz>, Regan Heath says...
>>
>> I've had a number of these problems, I think it's just none of those
>> things that happens when you port from C to D.
>>
>> The way I go about solving them is by printing the alignment, offset and
>> size of every member of the struct using D, then doing the same using C.
>>
>> One important point to note is that your D code is linking with the DMC
>> libraries so if the struct you are using comes from within a DMC library
>> you should really use DMC to compile your C. (I got bit by the  
>> differences
>> between findfirst in MSVC and DMC)
>>
>> Regan
>>
>> On Thu, 2 Jun 2005 20:35:31 +0000 (UTC), bobef  
>> <bobef_member@pathlink.com>
>> wrote:
>>> struct RECT
>>> {
>>> LONG left;
>>> LONG top;
>>> LONG right;
>>> LONG bottom;
>>> }
>>>
>>> and LONG is alias for int
>>>
>>> rcParent.alignof says 4
>>>
>>> In article <d7np1t$26up$1@digitaldaemon.com>, Brad Beveridge says...
>>>>
>>>> bobef wrote:
>>>>> no property 'offsetof' for type 'int'
>>>>>
>>>>> This is what I get....
>>>>>
>>>>>
>>>>>> 1) What is the packing/alignment of RECT & does the align actually
>>>>>> effect the packing of rcParent
>>>>>
>>>>>
>>>> Hmm, all members of a struct should have the property "offsetof"
>>>> http://digitalmars.com/d/struct.html
>>>>
>>>> What I was trying to say before - you have a type of RECT in your
>>>> struct.  I think that RECT may be another structure.  I was trying to
>>>> ask you to check that the RECT structure wasn't being padded out.
>>>>
>>>> For example, assume that RECT is align 4, and is
>>>> struct RECT
>>>> {
>>>> char x,y;
>>>> }
>>>> There will be 2 bytes of padding after x & y.  Now, when you embed  
>>>> RECT
>>>> inside of your other struct, does the align(1) directive also remove  
>>>> the
>>>> padding from RECT?  I don't know, but I would hope that it does.
>>>>
>>>> Brad
>>>
>>>
>>
>
>
June 02, 2005
Re: win32 and struct aligment
"bobef" <bobef_member@pathlink.com> wrote in message
news:d7nk3e$21qj$1@digitaldaemon.com...
> I have the folloing problem: in win32 headers NMPGSCROLL structure is
defined as
> align(1). So I defined it this way
>
> align(1) struct NMPGSCROLL
> {
> NMHDR hdr;
> WORD fwKeys;
> RECT rcParent;
> int  iDir;
> int  iXpos;
> int  iYpos;
> int  iScroll;
> }
>
> but when I have
>
> NMPGSCROLL *s=(cast(NMPGSCROLL*)lParam);
>
> s.iScroll is messed up no matter if align is 1 and  s.alignof says it is
4. Also
> if I have like that
>
> NMPGSCROLL s=*(cast(NMPGSCROLL*)lParam);
>
> s.alignof is 1 but values are still messed up.
>
> Any suggestions what may be wrong?

If you want the *members* to be aligned a certain way, rather than the
struct itself, put the align directive inside the struct:

struct NMPGSCROLL
{
align(1):
NMHDR hdr;
WORD fwKeys;
RECT rcParent;
int  iDir;
int  iXpos;
int  iYpos;
int  iScroll;
}
June 03, 2005
Re: win32 and struct aligment
"Walter" <newshound@digitalmars.com> wrote in message 
news:d7nv6c$2cgp$1@digitaldaemon.com...
>
> "bobef" <bobef_member@pathlink.com> wrote in message
> news:d7nk3e$21qj$1@digitaldaemon.com...
>> I have the folloing problem: in win32 headers NMPGSCROLL structure is
> defined as
>> align(1). So I defined it this way
>>
>> align(1) struct NMPGSCROLL
>> {
>> NMHDR hdr;
>> WORD fwKeys;
>> RECT rcParent;
>> int  iDir;
>> int  iXpos;
>> int  iYpos;
>> int  iScroll;
>> }
>>
>> but when I have
>>
>> NMPGSCROLL *s=(cast(NMPGSCROLL*)lParam);
>>
>> s.iScroll is messed up no matter if align is 1 and  s.alignof says it is
> 4. Also
>> if I have like that
>>
>> NMPGSCROLL s=*(cast(NMPGSCROLL*)lParam);
>>
>> s.alignof is 1 but values are still messed up.
>>
>> Any suggestions what may be wrong?
>
> If you want the *members* to be aligned a certain way, rather than the
> struct itself, put the align directive inside the struct:
>
> struct NMPGSCROLL
> {
> align(1):
> NMHDR hdr;
> WORD fwKeys;
> RECT rcParent;
> int  iDir;
> int  iXpos;
> int  iYpos;
> int  iScroll;
> }
>
>

Good one!
June 03, 2005
Re: win32 and struct aligment
In article <d7ob83$2li3$1@digitaldaemon.com>, Andrew Fedoniouk says...
>
>
>"Walter" <newshound@digitalmars.com> wrote in message 
>news:d7nv6c$2cgp$1@digitaldaemon.com...
>>
>> "bobef" <bobef_member@pathlink.com> wrote in message
>> news:d7nk3e$21qj$1@digitaldaemon.com...
>>> I have the folloing problem: in win32 headers NMPGSCROLL structure is
>> defined as
>>> align(1). So I defined it this way
>>>
>>> align(1) struct NMPGSCROLL
>>> {
>>> NMHDR hdr;
>>> WORD fwKeys;
>>> RECT rcParent;
>>> int  iDir;
>>> int  iXpos;
>>> int  iYpos;
>>> int  iScroll;
>>> }
>>>
>>> but when I have
>>>
>>> NMPGSCROLL *s=(cast(NMPGSCROLL*)lParam);
>>>
>>> s.iScroll is messed up no matter if align is 1 and  s.alignof says it is
>> 4. Also
>>> if I have like that
>>>
>>> NMPGSCROLL s=*(cast(NMPGSCROLL*)lParam);
>>>
>>> s.alignof is 1 but values are still messed up.
>>>
>>> Any suggestions what may be wrong?
>>
>> If you want the *members* to be aligned a certain way, rather than the
>> struct itself, put the align directive inside the struct:
>>
>> struct NMPGSCROLL
>> {
>> align(1):
>> NMHDR hdr;
>> WORD fwKeys;
>> RECT rcParent;
>> int  iDir;
>> int  iXpos;
>> int  iYpos;
>> int  iScroll;
>> }
>>
>>
>
>Good one! 
>
>


Good one but it still works the same (wrong) way...
Can't I realign it myself?
something like 

char[4] a=cast(char[4])s.iScroll
and then change bytes positions?

I don't have any idea what this aligment thing is and what is it's use except it
is ruining my code :)
June 03, 2005
Re: win32 and struct aligment
"bobef" <bobef_member@pathlink.com> wrote in message
news:d7p8fh$9qe$1@digitaldaemon.com...
> Good one but it still works the same (wrong) way...

It works (i.e. produces the same result) as the C compiler's alignment
directives on the same struct.

> Can't I realign it myself?
> something like
>
> char[4] a=cast(char[4])s.iScroll
> and then change bytes positions?

???

> I don't have any idea what this aligment thing is and what is it's use
except it
> is ruining my code :)

Why are you using it? Exactly what are you trying to achieve?
June 04, 2005
Re: win32 and struct aligment
Walter wrote:
> "bobef" <bobef_member@pathlink.com> wrote in message
> 
>>I don't have any idea what this aligment thing is and what is it's use
> 
> except it
> 
>>is ruining my code :)
> 
> 
> Why are you using it? Exactly what are you trying to achieve?
> 
> 
If the windows header doesn't specify something like pragma(pack...), 
then that structure won't be tightly packed I don't think - in which 
case, telling D to pack the structure may be a bit broken :)

Brad
June 04, 2005
Re: win32 and struct aligment
In article <d7qql5$1j2t$1@digitaldaemon.com>, Walter says...
>
>> I don't have any idea what this aligment thing is and what is it's use
>except it
>> is ruining my code :)
>
>Why are you using it? Exactly what are you trying to achieve?
>
>

I am not using it because it doesn't work or at least I don't know how to make
it work, but I need it because I need a Pager Control and definition of
NMPGSCROLL, or whatever it was, in the ms headers have #include <pshpack1.h> and
#include <poppack.h> around it, which says #pragma(pack,1), which I believe,
should be equivalent of D's align(1). This is why. But all numbers are messed
up! damn.
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home