Sizeof and structure and managed code

Source: Internet
Author: User
I just submitted a case to Ms at noon. The problem is described as follows:
(This is an open-source Excel file. Code It was originally written in VB.net, and I wrote it again in C. The API used in VB.net: fileput does not solve this problem. I wrote this myself, but it does not work .)

The followingMarshal. sizeof (VT)In this line of codeSizeIt should be2 + 2 + 1Instead of memory alignment6. How can I get5This value?

 

Private VoidFileput (system. valuetype VT)

{

IntSize = 0;

Size = marshal. sizeof (VT );

Intptr P = marshal. allochglobal (size );

Marshal. structuretoptr (Vt, P,True);

Byte[] Buf =New Byte[Size];

Marshal. Copy (p, Buf, 0, size );

FS. Write (BUF, 0, Buf. Length );

}

 

//Call code:

L = aformat [lindex]. length;

Cformat_record.opcode = 0x1e;

Cformat_record.length = (Short) (L + 1 );

Cformat_record.formatlength = (Byte) (L );

Fileput (cformat_record );

 

//Structure example

[Structlayoutattribute (layoutkind. Sequential, charset = charset. Auto)]

StructFormat_record

{

Public ShortOpcode;// 0x1e

Public ShortLength;// 1 + Len (Format)

[Financialas (unmanagedtype. u1, sizeconst = 1)]Public ByteFormatlength;// Len (Format)

}// Followed by the format-picture

After Ms replies to me, I will post the results to share with you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.