tags:

views:

126

answers:

1

Hi,

I have an unmanaged struct I'd like to marshal to c# that looks basically like this:

struct DateTimeStruct{
   double datetimestamp;
};   

struct MyStruct{
   char firstname[40];
   char lastname[40];
   DateTimeStruct bday;
   unsigned integer bool1;
   int val1;
};

What is the the correct c# declaration?

A: 

The struct isn't a problem, it will marshal correctly as-is.

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    struct MyStruct{
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40]
        string firstname;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40]
        string lastname;
        DateTimeStruct bday;
        uint bool1;
        int val1;
    }

Of course, it will be up to you to convert the double to a matching DateTime value. How it is encoded is unguessable from your question.

Hans Passant
Thank you. The problem is that I created the c structs from a document that describes the dll and have no idea how the DateTimeStruct looks other than this line in a table in the MyStruct documentation: Element: BirthdayType: DateTimeStruct (Double)
Wouter Roux
I know C well, "DateTimeStruct(Double)" is not valid C. How do you know that what you've got isn't actually correct? Contact the vendor if you want to be sure.
Hans Passant
Thanks I will have to follow up. My c knowledge is dangerous.
Wouter Roux