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?