• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            struct在p/invoke中的轉化


            在msdn上發現很好的實例,與大家共享!

            enum調用實例:
            // Unions.cs

            using System;
            using System.Runtime.InteropServices;

            /*
            union MYUNION
            {
                
            int i;
                
            double d;
            };
            */

            [ StructLayout( LayoutKind.Explicit )]
            public struct MyUnion 
            {
                [ FieldOffset( 
            0 )]
                
            public int i;
                [ FieldOffset( 
            0 )]
                
            public double d;
            }

            /*
            union MYUNION2
            {
                
            int i;
                char str[
            128];
            };
            */

            [ StructLayout( LayoutKind.Explicit, Size
            =128 )]
            public struct MyUnion2_1 
            {    
                [ FieldOffset( 
            0 )]
                
            public int i;
            }

            [ StructLayout( LayoutKind.Sequential )]
            public struct MyUnion2_2 
            {    
                [ MarshalAs( UnmanagedType.ByValTStr, SizeConst
            =128 )] 
                
            public String str;
            }

            public class LibWrap
            {
                
            // void TestUnion(MYUNION u, int type)
                
                [ DllImport( 
            "..\\LIB\\PinvokeLib.dll" )]
                
            public static extern void TestUnion( MyUnion u, int type );
                
                
            // void TestUnion( MYUNION u, int type )
                
                [ DllImport( 
            "..\\LIB\\PinvokeLib.dll" )]
                
            public static extern void TestUnion2( MyUnion2_1 u, int type );
                
                
            // void TestUnion(MYUNION u, int type)
                
                [ DllImport( 
            "..\\LIB\\PinvokeLib.dll" )]
                
            public static extern void TestUnion2( MyUnion2_2 u, int type );        
            }

            public class App
            {
                
            public static void Main()
                {
                    MyUnion mu 
            = new MyUnion();
                    mu.i 
            = 99;
                    LibWrap.TestUnion( mu, 
            1 );
                    
                    mu.d 
            = 99.99;
                    LibWrap.TestUnion( mu, 
            2 );
                    
                    MyUnion2_1 mu2_1 
            = new MyUnion2_1();
                    mu2_1.i 
            = 99;
                    LibWrap.TestUnion2( mu2_1, 
            1 );
                    
                    MyUnion2_2 mu2_2 
            = new MyUnion2_2();
                    mu2_2.str 
            = "*** string ***";
                    LibWrap.TestUnion2( mu2_2, 
            2 );        
                }
            }

            struct調用實例:
            // Structs.cs

            using System;
            using System.Runtime.InteropServices;

            /*
            typedef struct _MYPERSON
            {
                char
            * first; 
                char
            * last; 
            } MYPERSON, 
            *LP_MYPERSON;
            */

            [ StructLayout( LayoutKind.Sequential, CharSet
            =CharSet.Ansi )]
            public struct MyPerson 
            {
                
            public String first; 
                
            public String last;
            }

            /*
            typedef struct _MYPERSON2
            {
                MYPERSON
            * person;
                
            int age; 
            } MYPERSON2, 
            *LP_MYPERSON2;
            */

            [ StructLayout( LayoutKind.Sequential )]
            public struct MyPerson2 
            {
                
            public IntPtr person;
                
            public int age;
            }

            /*
            typedef struct _MYPERSON3
            {
                MYPERSON person;
                
            int age; 
            } MYPERSON3;
            */

            [ StructLayout( LayoutKind.Sequential )]
            public struct MyPerson3 
            {
                
            public MyPerson person;
                
            public int age;
            }

            /*
            typedef struct _MYARRAYSTRUCT
            {
                bool flag;
                
            int vals[ 3 ]; 
            } MYARRAYSTRUCT;
            */

            [ StructLayout( LayoutKind.Sequential )]
            public struct MyArrayStruct 
            {
                
            public bool flag;
                [ MarshalAs( UnmanagedType.ByValArray, SizeConst
            =3 )] 
                
            public int[] vals;
            }

            public class LibWrap
            {
                
            // int TestStructInStruct(MYPERSON2* pPerson2);
                
                [ DllImport( 
            "..\\LIB\\PinvokeLib.dll" )]
                
            public static extern int TestStructInStruct( ref MyPerson2 person2 );
                
                
            // void TestStructInStruct3(MYPERSON3 person3)
                
                [ DllImport( 
            "..\\LIB\\PinvokeLib.dll" )]
                
            public static extern int TestStructInStruct3( MyPerson3 person3 );    
                
                
            // void TestArrayInStruct( MYARRAYSTRUCT* pStruct );
                
                [ DllImport( 
            "..\\LIB\\PinvokeLib.dll" )]
                
            public static extern int TestArrayInStruct( ref MyArrayStruct myStruct );    
            }

            public class App
            {
                
            public static void Main()
                {
                    
            // ******************* structure with pointer to other structure ************
                    MyPerson personName;
                    personName.first 
            = "Sue";
                    personName.last 
            = "Black";
                    
                    MyPerson2 personAll;
                    personAll.age 
            = 30;
                    
                    IntPtr buffer 
            = Marshal.AllocCoTaskMem( Marshal.SizeOf( personName ));
                    Marshal.StructureToPtr( personName, buffer, 
            false );
                    
                    personAll.person 
            = buffer;
                    
                    Console.WriteLine( 
            "\nPerson before call:" );
                    Console.WriteLine( 
            "first = {0}, last = {1}, age = {2}"
                        personName.first, personName.last, personAll.age ); 
                    
                    
            int res = LibWrap.TestStructInStruct( ref personAll );
                    
                    MyPerson personRes 
            = 
                        (MyPerson)Marshal.PtrToStructure( personAll.person, typeof( MyPerson ));
                    
                    Marshal.FreeCoTaskMem( buffer );
                    
                    Console.WriteLine( 
            "Person after call:" );
                    Console.WriteLine( 
            "first = {0}, last = {1}, age = {2}"
                        personRes.first, personRes.last, personAll.age );
                    
                    
            // ******************* structure with embedded structure ************    
                    MyPerson3 person3 
            = new MyPerson3();
                    person3.person.first 
            = "Marie";
                    person3.person.last 
            = "Claire";
                    person3.age 
            = 27;
                    
                    LibWrap.TestStructInStruct3( person3 );
                    
                    
            // ******************* structure with embedded array ************    
                    MyArrayStruct myStruct 
            = new MyArrayStruct();
                    
                    myStruct.flag 
            = false;
                    myStruct.vals 
            = new int3 ];
                    myStruct.vals[ 
            0 ] = 1;
                    myStruct.vals[ 
            1 ] = 4;
                    myStruct.vals[ 
            2 ] = 9;
                    
                    Console.WriteLine( 
            "\nStructure with array before call:" );
                    Console.WriteLine( myStruct.flag );
                    Console.WriteLine( 
            "{0} {1} {2}", myStruct.vals[ 0 ], 
                        myStruct.vals[ 
            1 ], myStruct.vals[ 2 ] );
                    
                    LibWrap.TestArrayInStruct( ref myStruct );
                    
                    Console.WriteLine( 
            "\nStructure with array after call:" );
                    Console.WriteLine( myStruct.flag );
                    Console.WriteLine( 
            "{0} {1} {2}", myStruct.vals[ 0 ], 
                        myStruct.vals[ 
            1 ], myStruct.vals[ 2 ] );        
                }
            }

            其他的API,string,數組的調用實例:請下載:PlatformInvoke實例。

            posted on 2007-06-04 23:34 夢在天涯 閱讀(2231) 評論(3)  編輯 收藏 引用 所屬分類: CPlusPlusC#/.NET

            評論

            # re: struct在p/invoke中的轉化 2007-06-04 23:35 夢在天涯

            DLL文件:http://msdn2.microsoft.com/zh-cn/library/as6wyhwt(VS.80).aspx  回復  更多評論   

            # re: struct在p/invoke中的轉化 2007-06-04 23:38 夢在天涯

            C#的調用文件:http://msdn2.microsoft.com/zh-cn/library/hk9wyw21(VS.80).aspx  回復  更多評論   

            # re: struct在p/invoke中的轉化 2007-06-04 23:50 夢在天涯

            更多其他的封裝的實例:可以msdn查找:封送類、結構和聯合

            http://msdn2.microsoft.com/zh-cn/library/eshywdt7(VS.80).aspx  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            国产成人综合久久久久久| 亚洲综合熟女久久久30p| 久久99国产精品99久久| 国产L精品国产亚洲区久久 | 久久精品无码av| 欧美一区二区久久精品| AV无码久久久久不卡网站下载| 久久精品国产影库免费看| 久久综合伊人77777麻豆| 日韩精品久久无码人妻中文字幕| 国产99精品久久| 亚洲欧洲久久av| 亚洲国产精品久久66| 伊人久久大香线蕉亚洲五月天| 久久精品国产亚洲欧美| 久久强奷乱码老熟女网站 | 69久久精品无码一区二区| 日韩十八禁一区二区久久| av无码久久久久久不卡网站| 久久天天婷婷五月俺也去| 蜜桃麻豆www久久| 久久精品人人做人人妻人人玩| 久久这里只有精品视频99| 日本久久久久久中文字幕| 区久久AAA片69亚洲| 久久噜噜久久久精品66| 91精品国产色综久久 | 日韩人妻无码一区二区三区久久 | 久久免费香蕉视频| 国内精品久久九九国产精品| 国产亚洲美女精品久久久2020| 久久无码人妻精品一区二区三区| 久久久久四虎国产精品| 97久久久久人妻精品专区| 看久久久久久a级毛片| 7777精品久久久大香线蕉 | 久久er国产精品免费观看2| 精品久久久久久无码专区| 久久久无码精品亚洲日韩蜜臀浪潮| 久久婷婷五月综合成人D啪| 怡红院日本一道日本久久 |