// Returns true if the two params have the same fields public static bool ValidateOUTER3(OUTER3 outer1, OUTER3 InnerArrayExplicit, string methodName) { for (int i = 0; i < Common.NumArrElements; i++) { if (outer1.arr[i].f1 != InnerArrayExplicit.arr[i].f1 || outer1.arr[i].f2 != InnerArrayExplicit.arr[i].f2 || outer1.arr[i].f3 != InnerArrayExplicit.arr[i].f3) { Console.WriteLine("\tFAILED! " + methodName + "did not receive result as expected."); Console.WriteLine("\tThe Actual is..."); Console.WriteLine("\t{0}.arr[{1}].f1 = {2}", outer1.ToString(), i, outer1.arr[i].f1); Console.WriteLine("\t{0}.arr[{1}].f2 = {2}", outer1.ToString(), i, outer1.arr[i].f2); Console.WriteLine("\t{0}.arr[{1}].f3 = {2}", outer1.ToString(), i, outer1.arr[i].f3); Console.WriteLine("\tThe Expected is..."); Console.WriteLine("\t{0}.arr[{1}].f1 = {2}", InnerArrayExplicit.ToString(), i, InnerArrayExplicit.arr[i].f1); Console.WriteLine("\t{0}.arr[{1}].f2 = {2}", InnerArrayExplicit.ToString(), i, InnerArrayExplicit.arr[i].f2); Console.WriteLine("\t{0}.arr[{1}].f3 = {2}", InnerArrayExplicit.ToString(), i, InnerArrayExplicit.arr[i].f3); return(false); } } if (outer1.f4 != InnerArrayExplicit.f4) { Console.WriteLine("\tFAILED! " + methodName + "did not receive result as expected."); Console.WriteLine("\tThe Actual f4 field is..."); Console.WriteLine("\t{0}.f4 = {1}", outer1.ToString(), outer1.f4); Console.WriteLine("\tThe Expected f4 field is..."); Console.WriteLine("\t{0}.f4 = {1}", InnerArrayExplicit.ToString(), InnerArrayExplicit.f4); return(false); } Console.WriteLine("\tPASSED!"); return(true); }
// Prints OUTER3 public static void PrintOUTER3(OUTER3 outer, string name) { for (int i = 0; i < Common.NumArrElements; i++) { Console.WriteLine("\t{0}.arr[{1}].f1 = {2}", name, i, outer.arr[i].f1); Console.WriteLine("\t{0}.arr[{1}].f2 = {2}", name, i, outer.arr[i].f2); Console.WriteLine("\t{0}.arr[{1}].f3 = {2}", name, i, outer.arr[i].f3); } Console.WriteLine("\t{0}.f4 = {1}", name, outer.f4); }
// Returns new OUTER3 instance; the params are the fields of INNER; // all the INNER elements have the same field values public static OUTER3 NewOUTER3(int f1, float f2, string f3, string f4) { OUTER3 outer = new OUTER3(); outer.arr = new InnerSequential[Common.NumArrElements]; for (int i = 0; i < Common.NumArrElements; i++) { outer.arr[i].f1 = f1; outer.arr[i].f2 = f2; outer.arr[i].f3 = f3; } outer.f4 = f4; return(outer); }
public static bool TestMethod_DoCallBack_MarshalStructByVal_OUTER3_Stdcall(OUTER3 outer3) { Console.WriteLine("Reverse,Pinvoke,By Val,Stdcall"); OUTER3 sourceOUTER3 = Helper.NewOUTER3(1, 1.0F, "some string", "some string"); Assert.True(Helper.ValidateOUTER3(sourceOUTER3, outer3, "TestMethod_DoCallBack_MarshalStructByVal_OUTER3_Stdcall")); //changed the value for (int i = 0; i < Common.NumArrElements; i++) { outer3.arr[i].f1 = 1; outer3.arr[i].f2 = 1.0F; outer3.arr[i].f3 = "changed string"; } outer3.f4 = "changed string"; return true; }
public static bool TestMethod_DoCallBack_MarshalStructByRef_OUTER3_Cdecl(ref OUTER3 outer3) { Console.WriteLine("Reverse,Pinvoke,By Ref,Cdecl"); OUTER3 sourceOUTER3 = Helper.NewOUTER3(77, 77.0F, "Native", "Native"); Assert.IsTrue(Helper.ValidateOUTER3(sourceOUTER3, outer3, "TestMethod_DoCallBack_MarshalStructByRef_OUTER3_Cdecl")); //changed the value for (int i = 0; i < Common.NumArrElements; i++) { outer3.arr[i].f1 = 1; outer3.arr[i].f2 = 1.0F; outer3.arr[i].f3 = "some string"; } outer3.f4 = "some string"; return(true); }
// Returns true if the two params have the same fields public static bool ValidateOUTER3(OUTER3 outer1, OUTER3 InnerArrayExplicit, string methodName) { for (int i = 0; i < Common.NumArrElements; i++) { if (outer1.arr[i].f1 != InnerArrayExplicit.arr[i].f1 || outer1.arr[i].f2 != InnerArrayExplicit.arr[i].f2 || outer1.arr[i].f3 != InnerArrayExplicit.arr[i].f3) { Console.WriteLine("\tFAILED! " + methodName + "did not recieve result as expected."); Console.WriteLine("\tThe Actual is..."); Console.WriteLine("\t{0}.arr[{1}].f1 = {2}", outer1.ToString(), i, outer1.arr[i].f1); Console.WriteLine("\t{0}.arr[{1}].f2 = {2}", outer1.ToString(), i, outer1.arr[i].f2); Console.WriteLine("\t{0}.arr[{1}].f3 = {2}", outer1.ToString(), i, outer1.arr[i].f3); Console.WriteLine("\tThe Expected is..."); Console.WriteLine("\t{0}.arr[{1}].f1 = {2}", InnerArrayExplicit.ToString(), i, InnerArrayExplicit.arr[i].f1); Console.WriteLine("\t{0}.arr[{1}].f2 = {2}", InnerArrayExplicit.ToString(), i, InnerArrayExplicit.arr[i].f2); Console.WriteLine("\t{0}.arr[{1}].f3 = {2}", InnerArrayExplicit.ToString(), i, InnerArrayExplicit.arr[i].f3); return false; } } if (outer1.f4 != InnerArrayExplicit.f4) { Console.WriteLine("\tFAILED! " + methodName + "did not recieve result as expected."); Console.WriteLine("\tThe Actual f4 field is..."); Console.WriteLine("\t{0}.f4 = {1}", outer1.ToString(), outer1.f4); Console.WriteLine("\tThe Expected f4 field is..."); Console.WriteLine("\t{0}.f4 = {1}", InnerArrayExplicit.ToString(), InnerArrayExplicit.f4); return false; } Console.WriteLine("\tPASSED!"); return true; }
// Returns new OUTER3 instance; the params are the fields of INNER; // all the INNER elements have the same field values public static OUTER3 NewOUTER3(int f1, float f2, string f3, string f4) { OUTER3 outer = new OUTER3(); outer.arr = new InnerSequential[Common.NumArrElements]; for (int i = 0; i < Common.NumArrElements; i++) { outer.arr[i].f1 = f1; outer.arr[i].f2 = f2; outer.arr[i].f3 = f3; } outer.f4 = f4; return outer; }
static extern bool MarshalStructAsParam_AsExpByRefOutOUTER3(out OUTER3 str1);
static extern bool MarshalStructAsParam_AsExpByValOUTER3(OUTER3 str1);
private static void TestMethod_DelegatePInvoke_MarshalByRef_Cdecl(StructID structid) { Console.WriteLine("Delegate,Pinvoke,By Ref,Cdecl"); switch (structid) { case StructID.INNER2Id: INNER2 sourceINNER2 = Helper.NewINNER2(1, 1.0F, "some string"); INNER2 changeINNER2 = Helper.NewINNER2(77, 77.0F, "changed string"); DelegateCdeclByRef_INNER2 caller_INNER2 = Get_MarshalStructAsParam_AsExpByRefINNER2_Cdecl_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByRefINNER2_Cdecl_FuncPtr..."); Assert.IsTrue(caller_INNER2(ref sourceINNER2)); Assert.IsTrue(Helper.ValidateINNER2(sourceINNER2, changeINNER2, "Get_MarshalStructAsParam_AsExpByRefINNER2_Cdecl_FuncPtr")); break; case StructID.InnerExplicitId: InnerExplicit sourceInnerExplicit = new InnerExplicit(); sourceInnerExplicit.f1 = 1; sourceInnerExplicit.f3 = "some string"; InnerExplicit changeInnerExplicit = new InnerExplicit(); changeInnerExplicit.f1 = 77; changeInnerExplicit.f3 = "changed string"; DelegateCdeclByRef_InnerExplicit caller_InnerExplicit = Get_MarshalStructAsParam_AsExpByRefInnerExplicit_Cdecl_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByRefInnerExplicit_Cdecl_FuncPtr..."); Assert.IsTrue(caller_InnerExplicit(ref sourceInnerExplicit)); Assert.IsTrue(Helper.ValidateInnerExplicit(sourceInnerExplicit, changeInnerExplicit, "Get_MarshalStructAsParam_AsExpByRefInnerExplicit_Cdecl_FuncPtr")); break; case StructID.InnerArrayExplicitId: InnerArrayExplicit sourceInnerArrayExplicit = Helper.NewInnerArrayExplicit(1, 1.0F, "some string1", "some string2"); InnerArrayExplicit changeInnerArrayExplicit = Helper.NewInnerArrayExplicit(77, 77.0F, "change string1", "change string2"); DelegateCdeclByRef_InnerArrayExplicit caller_InnerArrayExplicit = Get_MarshalStructAsParam_AsExpByRefInnerArrayExplicit_Cdecl_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByRefInnerArrayExplicit_Cdecl_FuncPtr..."); Assert.IsTrue(caller_InnerArrayExplicit(ref sourceInnerArrayExplicit)); Assert.IsTrue(Helper.ValidateInnerArrayExplicit(sourceInnerArrayExplicit, changeInnerArrayExplicit, "Get_MarshalStructAsParam_AsExpByRefInnerArrayExplicit_Cdecl_FuncPtr")); break; case StructID.OUTER3Id: OUTER3 sourceOUTER3 = Helper.NewOUTER3(1, 1.0F, "some string", "some string"); OUTER3 changeOUTER3 = Helper.NewOUTER3(77, 77.0F, "changed string", "changed string"); DelegateCdeclByRef_OUTER3 caller_OUTER3 = Get_MarshalStructAsParam_AsExpByRefOUTER3_Cdecl_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByRefOUTER3_Cdecl_FuncPtr..."); Assert.IsTrue(caller_OUTER3(ref sourceOUTER3)); Assert.IsTrue(Helper.ValidateOUTER3(sourceOUTER3, changeOUTER3, "Get_MarshalStructAsParam_AsExpByRefOUTER3_Cdecl_FuncPtr")); break; case StructID.UId: U sourceU = Helper.NewU(Int32.MinValue, UInt32.MaxValue, new IntPtr(-32), new UIntPtr(32), short.MinValue, ushort.MaxValue, byte.MinValue, sbyte.MaxValue, long.MinValue, ulong.MaxValue, 32.0F, 3.2); U changeU = Helper.NewU(Int32.MaxValue, UInt32.MinValue, new IntPtr(-64), new UIntPtr(64), short.MaxValue, ushort.MinValue, byte.MaxValue, sbyte.MinValue, long.MaxValue, ulong.MinValue, 64.0F, 6.4); DelegateCdeclByRef_U caller_U = Get_MarshalStructAsParam_AsExpByRefU_Cdecl_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByRefU_Cdecl_FuncPtr..."); Assert.IsTrue(caller_U(ref sourceU)); Assert.IsTrue(Helper.ValidateU(sourceU, changeU, "Get_MarshalStructAsParam_AsExpByRefU_Cdecl_FuncPtr")); break; case StructID.ByteStructPack2ExplicitId: ByteStructPack2Explicit source_bspe = Helper.NewByteStructPack2Explicit(32, 32); ByteStructPack2Explicit change_bspe = Helper.NewByteStructPack2Explicit(64, 64); DelegateCdeclByRef_ByteStructPack2Explicit caller_ByteStructPack2Explicit = Get_MarshalStructAsParam_AsExpByRefByteStructPack2Explicit_Cdecl_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByRefByteStructPack2Explicit_Cdecl_FuncPtr..."); Assert.IsTrue(caller_ByteStructPack2Explicit(ref source_bspe)); Assert.IsTrue(Helper.ValidateByteStructPack2Explicit(source_bspe, change_bspe, "Get_MarshalStructAsParam_AsExpByRefByteStructPack2Explicit_Cdecl_FuncPtr")); break; case StructID.ShortStructPack4ExplicitId: ShortStructPack4Explicit source_sspe = Helper.NewShortStructPack4Explicit(32, 32); ShortStructPack4Explicit change_sspe = Helper.NewShortStructPack4Explicit(64, 64); DelegateCdeclByRef_ShortStructPack4Explicit caller_ShortStructPack4Explicit = Get_MarshalStructAsParam_AsExpByRefShortStructPack4Explicit_Cdecl_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByRefShortStructPack4Explicit_Cdecl_FuncPtr..."); Assert.IsTrue(caller_ShortStructPack4Explicit(ref source_sspe)); Assert.IsTrue(Helper.ValidateShortStructPack4Explicit(source_sspe, change_sspe, "Get_MarshalStructAsParam_AsExpByRefShortStructPack4Explicit_Cdecl_FuncPtr")); break; case StructID.IntStructPack8ExplicitId: IntStructPack8Explicit source_ispe = Helper.NewIntStructPack8Explicit(32, 32); IntStructPack8Explicit change_ispe = Helper.NewIntStructPack8Explicit(64, 64); DelegateCdeclByRef_IntStructPack8Explicit caller_IntStructPack8Explicit = Get_MarshalStructAsParam_AsExpByRefIntStructPack8Explicit_Cdecl_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByRefIntStructPack8Explicit_Cdecl_FuncPtr..."); Assert.IsTrue(caller_IntStructPack8Explicit(ref source_ispe)); Assert.IsTrue(Helper.ValidateIntStructPack8Explicit(source_ispe, change_ispe, "Get_MarshalStructAsParam_AsExpByRefIntStructPack8Explicit_Cdecl_FuncPtr")); break; case StructID.LongStructPack16ExplicitId: LongStructPack16Explicit source_lspe = Helper.NewLongStructPack16Explicit(32, 32); LongStructPack16Explicit change_lspe = Helper.NewLongStructPack16Explicit(64, 64); DelegateCdeclByRef_LongStructPack16Explicit caller_LongStructPack16Explicit = Get_MarshalStructAsParam_AsExpByRefLongStructPack16Explicit_Cdecl_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByRefLongStructPack16Explicit_Cdecl_FuncPtr..."); Assert.IsTrue(caller_LongStructPack16Explicit(ref source_lspe)); Assert.IsTrue(Helper.ValidateLongStructPack16Explicit(source_lspe, change_lspe, "Get_MarshalStructAsParam_AsExpByRefLongStructPack16Explicit_Cdecl_FuncPtr")); break; default: Assert.Fail("TestMethod_DelegatePInvoke_MarshalByRef_Cdecl:The structid (Managed Side) is wrong"); break; } }
private static void TestMethod_DelegatePInvoke_MarshalByVal_Stdcall(StructID structid) { Console.WriteLine("Delegate,Pinvoke,By Val,Stdcall"); switch (structid) { case StructID.INNER2Id: INNER2 sourceINNER2 = Helper.NewINNER2(1, 1.0F, "some string"); INNER2 cloneINNER2 = Helper.NewINNER2(1, 1.0F, "some string"); DelegateStdcallByVal_INNER2 caller_INNER2 = Get_MarshalStructAsParam_AsExpByValINNER2_Stdcall_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByValINNER2_Stdcall_FuncPtr..."); Assert.True(caller_INNER2(sourceINNER2)); Assert.True(Helper.ValidateINNER2(sourceINNER2, cloneINNER2, "Get_MarshalStructAsParam_AsExpByValINNER2_Stdcall_FuncPtr")); break; case StructID.InnerExplicitId: InnerExplicit sourceInnerExplicit = new InnerExplicit(); sourceInnerExplicit.f1 = 1; sourceInnerExplicit.f3 = "some string"; InnerExplicit cloneInnerExplicit = new InnerExplicit(); cloneInnerExplicit.f1 = 1; cloneInnerExplicit.f3 = "some string"; DelegateStdcallByVal_InnerExplicit caller_InnerExplicit = Get_MarshalStructAsParam_AsExpByValInnerExplicit_Stdcall_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByValInnerExplicit_Stdcall_FuncPtr..."); Assert.True(caller_InnerExplicit(sourceInnerExplicit)); Assert.True(Helper.ValidateInnerExplicit(sourceInnerExplicit, cloneInnerExplicit, "Get_MarshalStructAsParam_AsExpByValInnerExplicit_Stdcall_FuncPtr")); break; case StructID.InnerArrayExplicitId: InnerArrayExplicit sourceInnerArrayExplicit = Helper.NewInnerArrayExplicit(1, 1.0F, "some string1", "some string2"); InnerArrayExplicit cloneInnerArrayExplicit = Helper.NewInnerArrayExplicit(1, 1.0F, "some string1", "some string2"); DelegateStdcallByVal_InnerArrayExplicit caller_InnerArrayExplicit = Get_MarshalStructAsParam_AsExpByValInnerArrayExplicit_Stdcall_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByValInnerArrayExplicit_Stdcall_FuncPtr..."); Assert.True(caller_InnerArrayExplicit(sourceInnerArrayExplicit)); Assert.True(Helper.ValidateInnerArrayExplicit(sourceInnerArrayExplicit, cloneInnerArrayExplicit, "Get_MarshalStructAsParam_AsExpByValInnerArrayExplicit_Stdcall_FuncPtr")); break; case StructID.OUTER3Id: OUTER3 sourceOUTER3 = Helper.NewOUTER3(1, 1.0F, "some string", "some string"); OUTER3 cloneOUTER3 = Helper.NewOUTER3(1, 1.0F, "some string", "some string"); DelegateStdcallByVal_OUTER3 caller_OUTER3 = Get_MarshalStructAsParam_AsExpByValOUTER3_Stdcall_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByValOUTER3_Stdcall_FuncPtr..."); Assert.True(caller_OUTER3(sourceOUTER3)); Assert.True(Helper.ValidateOUTER3(sourceOUTER3, cloneOUTER3, "Get_MarshalStructAsParam_AsExpByValOUTER3_Stdcall_FuncPtr")); break; case StructID.UId: U sourceU = Helper.NewU(Int32.MinValue, UInt32.MaxValue, new IntPtr(-32), new UIntPtr(32), short.MinValue, ushort.MaxValue, byte.MinValue, sbyte.MaxValue, long.MinValue, ulong.MaxValue, 32.0F, 3.2); U cloneU = Helper.NewU(Int32.MinValue, UInt32.MaxValue, new IntPtr(-32), new UIntPtr(32), short.MinValue, ushort.MaxValue, byte.MinValue, sbyte.MaxValue, long.MinValue, ulong.MaxValue, 32.0F, 3.2); DelegateStdcallByVal_U caller_U = Get_MarshalStructAsParam_AsExpByValU_Stdcall_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByValU_Stdcall_FuncPtr..."); Assert.True(caller_U(sourceU)); Assert.True(Helper.ValidateU(sourceU, cloneU, "Get_MarshalStructAsParam_AsExpByValU_Stdcall_FuncPtr")); break; case StructID.ByteStructPack2ExplicitId: ByteStructPack2Explicit source_bspe = Helper.NewByteStructPack2Explicit(32, 32); ByteStructPack2Explicit clone_bspe = Helper.NewByteStructPack2Explicit(32, 32); DelegateStdcallByVal_ByteStructPack2Explicit caller_ByteStructPack2Explicit = Get_MarshalStructAsParam_AsExpByValByteStructPack2Explicit_Stdcall_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByValByteStructPack2Explicit_Stdcall_FuncPtr..."); Assert.True(caller_ByteStructPack2Explicit(source_bspe)); Assert.True(Helper.ValidateByteStructPack2Explicit(source_bspe, clone_bspe, "Get_MarshalStructAsParam_AsExpByValByteStructPack2Explicit_Stdcall_FuncPtr")); break; case StructID.ShortStructPack4ExplicitId: ShortStructPack4Explicit source_sspe = Helper.NewShortStructPack4Explicit(32, 32); ShortStructPack4Explicit clone_sspe = Helper.NewShortStructPack4Explicit(32, 32); DelegateStdcallByVal_ShortStructPack4Explicit caller_ShortStructPack4Explicit = Get_MarshalStructAsParam_AsExpByValShortStructPack4Explicit_Stdcall_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByValShortStructPack4Explicit_Stdcall_FuncPtr..."); Assert.True(caller_ShortStructPack4Explicit(source_sspe)); Assert.True(Helper.ValidateShortStructPack4Explicit(source_sspe, clone_sspe, "Get_MarshalStructAsParam_AsExpByValShortStructPack4Explicit_Stdcall_FuncPtr")); break; case StructID.IntStructPack8ExplicitId: IntStructPack8Explicit source_ispe = Helper.NewIntStructPack8Explicit(32, 32); IntStructPack8Explicit clone_ispe = Helper.NewIntStructPack8Explicit(32, 32); DelegateStdcallByVal_IntStructPack8Explicit caller_IntStructPack8Explicit = Get_MarshalStructAsParam_AsExpByValIntStructPack8Explicit_Stdcall_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByValIntStructPack8Explicit_Stdcall_FuncPtr..."); Assert.True(caller_IntStructPack8Explicit(source_ispe)); Assert.True(Helper.ValidateIntStructPack8Explicit(source_ispe, clone_ispe, "Get_MarshalStructAsParam_AsExpByValIntStructPack8Explicit_Stdcall_FuncPtr")); break; case StructID.LongStructPack16ExplicitId: LongStructPack16Explicit source_lspe = Helper.NewLongStructPack16Explicit(32, 32); LongStructPack16Explicit clone_lspe = Helper.NewLongStructPack16Explicit(32, 32); DelegateStdcallByVal_LongStructPack16Explicit caller_LongStructPack16Explicit = Get_MarshalStructAsParam_AsExpByValLongStructPack16Explicit_Stdcall_FuncPtr(); Console.WriteLine("Calling Get_MarshalStructAsParam_AsExpByValLongStructPack16Explicit_Stdcall_FuncPtr..."); Assert.True(caller_LongStructPack16Explicit(source_lspe)); Assert.True(Helper.ValidateLongStructPack16Explicit(source_lspe, clone_lspe, "Get_MarshalStructAsParam_AsExpByValLongStructPack16Explicit_Stdcall_FuncPtr")); break; default: Assert.True(false, "TestMethod_DelegatePInvoke_MarshalByRef_Stdcall:The structid (Managed Side) is wrong"); break; } }