unsafe static void CompareWithUTF8Encoding() { // Compare results with UTF8Encoding UnmanagedStruct ums; ums.psz[0] = 0xFF; ums.psz[1] = (byte)'a'; ums.psz[2] = (byte)'b'; ums.psz[3] = (byte)'c'; ums.psz[4] = (byte)'d'; ums.psz[5] = 0; IntPtr ptr = (IntPtr)(&ums); ManagedStruct ms = Marshal.PtrToStructure <ManagedStruct>(ptr); string actual = ms.str; UTF8Encoding uTF8Encoding = new UTF8Encoding(); byte [] b = new byte[5]; b[0] = 0xFF; b[1] = (byte)'a'; b[2] = (byte)'b'; b[3] = (byte)'c'; b[4] = (byte)'d'; string expected = uTF8Encoding.GetString(b); if (actual != expected) { Console.WriteLine("Actual:" + actual + " Length:" + actual.Length); Console.WriteLine("Expected:" + expected + " Length:" + expected.Length); throw new Exception("UTF8Encoding.GetString doesn't match with Utf8 String Marshaller result"); } }
/// <summary> /// Gets the address of the UScriptStruct for the given struct type (defined in managed code) /// </summary> /// <param name="type">The type of the struct</param> /// <returns>The address of the UScriptStruct for the given type</returns> public static IntPtr GetStructAddress(Type type) { ManagedStruct managedStruct = GetManagedStruct(type); return(managedStruct != null ? managedStruct.Address : IntPtr.Zero); }