Example #1
0
        static void Main(string[] args)
        {
            print_line("Hello PInvoke!!!");
            Console.WriteLine();

            // unmanaged に配列を渡して壊してみる
            var buf = new StringBuilder(5);
            var bufCanary = new StringBuilder(5);
            get_string(buf, buf.Capacity + 5);  // StringBuilder は壊せない

            Console.WriteLine("buf:" + buf + "[EOB] " + buf.Capacity);
            Console.WriteLine("canary:" + bufCanary + "[EOB] " + bufCanary.Capacity);

            var intBuf = new int[5];
            var intBufCanary = new int[5];
            takes_an_int_array(intBuf, intBuf.Length + 5);  // 壊せる

            for (int i = 0; i < intBuf.Length; i++)
            {
                Console.WriteLine("intBuf:" + intBuf[i] + "[EOB] " + intBuf.Length);
            }
            for (int i = 0; i < intBufCanary.Length; i++)
            {
                Console.WriteLine("canary:" + intBufCanary[i] + "[EOB] " + intBufCanary.Length);
            }

            // class や struct を native に渡して、アドレスとか中身を見てみる
            var managedClass = new ManagedClass();
            managed_ptr_class(managedClass);
            var managedClass2 = new ManagedClass() { A = 1, B = 0xFF, };
            managed_ptr_class(managedClass2);
            var managedClass3 = new ManagedClass() { A = 0xFF, B = 0xFF, };
            managed_ptr_class(managedClass3);

            var managedStruct = new ManagedStruct();
            managed_ptr_struct(ref managedStruct);
            var managedStruct2 = new ManagedStruct() { A = 1, B = 0xEE, };
            managed_ptr_struct(ref managedStruct2);
            var managedStruct3 = new ManagedStruct() { A = 0xFF, B = 0xFFFF, };
            managed_ptr_struct(ref managedStruct3);

            // Boxsing っぷりをみたかったけど、msw.ManagedStruct をそのままは渡せないのかも?
            var msw = new ManagedStructWrapper();
            var ms = new ManagedStruct()  { A = 1, B = 2, };
            msw.ManagedStruct = ms;
            managed_ptr_struct(ref ms);
            var ms2 = (ManagedStruct)msw.ManagedStruct;
            managed_ptr_struct(ref ms2);
            managed_ptr_if(msw.ManagedStruct);

            Console.WriteLine("\n\nPress any key...");
            Console.ReadKey();
        }
Example #2
0
 private static extern void managed_ptr_struct(ref ManagedStruct managedObject);