0

I have a c-library which which I can access through Python, Excel and C#. In order to use vba types I need to use the pragma pack directive to get some sort of struct alignment correct.

I'm not all that familiar with using c directives but I know enough to know that in order to use it with excel (which is 32 bits on my computer) I need to enclose my 'outgoing' types with pragma pack(4) like so

#pragma pack(4)

typedef struct
{
    double myDouble;
    int myInt;
} Mystruct;

....

#pragma pack()

In the receiving end (in for instance VBA) I can now do this

Type MyVBAStruct
    myDouble as Double
    myInt as Long
End Type

For C# i define my structs

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 4)]
public struct myCSharpStruct
{ 
    public double myDouble;
    public int myInt;
}

And this all works great.

However, on certain occasions I also have to build 64-bits, in which case I have to use pragma pack(8) .... pragma pack(). Currently I have to do the switch manually. This is of course not a great solution. Ideally I would like to be able to write

#pragma pack(PACK_SIZE)

typedef struct
{
    double myDouble;
    int myInt;
} Mystruct;

....

#pragma pack()

where PACK_SIZE is a macro that somehow automatically detects the bitness of the build.

Ideally a similar thing would happen for C# (the types are contained in the same solution).

Is there a way to achieve this?

mortysporty
  • 2,749
  • 6
  • 28
  • 51
  • Your compiler, which you didn't mention, probably has preprocessor defines that you can use to determine whether you are compiling for 32 or 64 bit. Assuming you're using Visual Studio this may help: https://stackoverflow.com/questions/8672887/is-there-a-define-for-64-bit-in-visual-studio-2010 – Retired Ninja Oct 27 '17 at 13:39
  • Yes I'm using Visual Studio. Thanks for the tip, I'll check it out. I'm not sure on the compiler. I'm guessing its the default compiler. Can't remember its name. – mortysporty Oct 27 '17 at 13:41
  • Looks promising btw... I edited my question while you answered. Do you know how to do the same trick in C#? – mortysporty Oct 27 '17 at 13:44
  • https://stackoverflow.com/questions/266082/how-do-i-tell-if-my-application-is-running-as-a-32-bit-or-64-bit-application Since C# isn't strictly compiled for any particular architecture you can only tell at runtime. I don't know if that information can be used for your particular case. – Retired Ninja Oct 27 '17 at 13:49

1 Answers1

0

You might not realize it, but what you are actually trying to do is not to fix alignment, but probably just to get rid of struct padding.

Because the correct alignment of the struct is already handled automatically by the C compiler. Likely you get 16 when you run it on a 64 bit PC. Why this isn't good for VBA etc, I have no idea, but I'll assume that it doesn't like the padding, since #pragma pack(4) would give you 12 instead of 16.

Therefore, you could perhaps solve the problem by getting rid of all padding no matter the size of an int. This is done with #pragma pack(1) and will work no matter if 32 or 64 bits.

Please note that #pragma pack isn't standard C and may behave differently on different compilers.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Yes. Padding is the correct term. I really want to keep the option of using `pragma pack(4)` because I can then use VBA. This is the only way I'm able to call VBA (believe me I have tried other ways :|). – mortysporty Oct 27 '17 at 14:14