C + +/CLI-managed + + Fabric package (memory aligned #pragma pack) "9"

Source: Internet
Author: User

When the CLI encapsulates a structure that involves memory alignment, it uses StructLayoutAttribute, which specifies that the Pack is a property of 1, 4, 8, and so on .


"1" C + + export function, using 1-bit alignment

#pragma pack (push) #pragma pack (1) typedef struct _TESTSTRU2{INTIVAL;CHARCVAL;__INT64 llval;} TESTSTRU2; #pragma pack (POP)
4.2 struct boundary alignment exportdll_class void Struct_packn (TestStru2 *pstru) {if (NULL = = Pstru) {return;} Pstru->ival = 1;pstru->cval = ' a ';p stru->llval = 2;wprintf (L "struct_packn \ n");}

CLI Encapsulation:

[Serializable] [StructLayoutAttribute (Layoutkind::sequential, pack=1)]public ref struct test2{    int32ival;    Sbytecval;    Int64   Llval;    void Unmanagedptr2managedstru (IntPtr ptr)    {        TestStru2 *ptstru = Static_cast<teststru2 *> (ptr. ToPointer ());        if (NULL = = Ptstru)            return;        Ival = ptstru->ival;        Cval = ptstru->cval;        Llval= ptstru->llval;    }};    
void Exportcli::structcls::structpackn (test2^%pstru) {    if (Pstru = = nullptr)    {        return;    }    TESTSTRU2  strt;    Strt.ival = pstru->ival;    Strt.cval = pstru->cval;    Strt.llval = pstru->llval;    Struct_packn (&STRT);    Pstru->unmanagedptr2managedstru (INTPTR (&STRT));}

CLI class declaration:

<summary>///3 Structural Test class//</summary>public ref class Structcls{public:///<summary>//    3.1 Structure as input/output parameter///</summary>//    <param name= "Pstru" > Structure </param>      static void Structchange (test1^%pstru);    <summary>///    3.2 structure Boundary alignment///</summary>//    <param name= "Pstru" > Structure </param>      static  void Structpackn (test2^%pstru);    <summary>////    3.3 structure contains an array of built-in data types///</summary>//    <param name= "Pstru" > struct </param>      static  void Structchangearr (test3^%pstru);    <summary>//    3.4 union type contains structure///</summary>//    <param name= "Pstru" > struct </ param>      static  void Structunion (test4^%pstru);    <summary>////    3.5 struct Array as parameter///</summary>//    <param name= "Pstru" > struct array </ param>      static  void Structstruarr (list<test5^>^ pstru);

C # test program:

You can see that the length of the struct is 13.

Test2 tStru2 = new Test2 (); Tstru2.ival  = 0;tstru2.cval  = 0;tstru2.llval = 0;int size = marshal.sizeof (Tstru2.gett Ype ()); STRUCTCLS.STRUCTPACKN (ref TSTRU2);

Source Address:

Https://coding.net/u/aoshilangCode/p/CLI/git


C + +/CLI-managed + + Fabric package (memory aligned #pragma pack) "9"

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.