public static bool PosTest1() { string s = "before"; string changedValue = "after"; bool retval = true; Sstr p = new Sstr(0, false, s); TestFramework.BeginScenario("Test #1 (Roundtrip of a simple structre by reference. Verify that values updated on unmanaged side reflect on managed side)"); //Direct pinvoke //cdecl calling convention. try { TestFramework.LogInformation(" Case 2: Direct p/invoke cdecl calling convention"); retval = DoCdeclSimpleStructByRef(ref p); if ((p.a != 100) || (!p.b) || (!p.str.Equals(changedValue))) { Console.WriteLine("\nExpected values:\n SimpleStruct->a=" + 100 + "\nSimpleStruct->b=TRUE\n" + "SimpleStruct->str=after\n"); Console.WriteLine("\nActual values:\n SimpleStruct->a=" + p.a + "\nSimpleStruct->b=" + p.b + "\nSimpleStruct->str=" + p.str + "\n"); TestFramework.LogError("03", "PInvokeTests->PosTest1 : Returned values are different from expected values"); retval = false; } } catch (Exception e) { TestFramework.LogError("04", "Unexpected exception: " + e.ToString()); retval = false; } //Delegate pinvoke //cdecl try { TestFramework.LogInformation(" Case 4: Delegate p/invoke - cdecl calling convention"); CdeclSimpleStructByRefDelegate std = GetFptrCdeclSimpleStructByRef(14); retval = std(ref p); if ((p.a != 100) || (!p.b) || (!p.str.Equals(changedValue))) { Console.WriteLine("\nExpected values:\n SimpleStruct->a=" + 100 + "\nSimpleStruct->b=TRUE\n" + "SimpleStruct->str=after\n"); Console.WriteLine("\nActual values:\n SimpleStruct->a=" + p.a + "\nSimpleStruct->b=" + p.b + "\nSimpleStruct->str=" + p.str + "\n"); TestFramework.LogError("01", "PInvokeTests->PosTest1 : Returned values are different from expected values"); retval = false; } } catch (Exception e) { TestFramework.LogError("02", "Unexpected exception: " + e.ToString()); retval = false; } return(retval); }
public static bool PosTest2() { string s = "Before"; bool retval = true; double d = 3.142; Sstr p = new Sstr(100, false, s); TestFramework.BeginScenario("\n\nTest #2 (Roundtrip of a simple structre by value. Verify that values updated on unmanaged side reflect on managed side)"); //direct pinvoke // //cdecl calling convention try { TestFramework.LogInformation(" Case 2: Direct p/invoke cdecl calling convention"); Sstr_simple simple = new Sstr_simple(100, false, d); simple = DoCdeclSimpleStruct(simple, ref retval); if (retval == false) { TestFramework.LogError("01", "PInvokeTests->PosTest2 : values of passed in structure not matched with expected once on unmanaged side."); return(false); } if ((simple.a != 101) || (!simple.b) || (simple.c != 10.11)) { Console.WriteLine("\nExpected values:\n SimpleStruct->a=101\nSimpleStruct->b=TRUE\nSimpleStruct->c=10.11\n"); Console.WriteLine("\nActual values:\n SimpleStruct->a=" + simple.a + "\nSimpleStruct->b=" + simple.b + "\nSimpleStruct->c=" + simple.c + "\n"); TestFramework.LogError("02", "PInvokeTests->PosTest2 : Returned values are different from expected values"); retval = false; } } catch (Exception e) { TestFramework.LogError("03", "Unexpected exception: " + e.ToString()); retval = false; } // //delegate pinvoke // //cdecl calling convention try { TestFramework.LogInformation(" Case 4: Delegate p/invoke cdecl calling convention"); Sstr_simple simple = new Sstr_simple(100, false, d); CdeclSimpleStructDelegate std = GetFptrCdeclSimpleStruct(16); IntPtr st = std(simple, ref retval); simple = (Sstr_simple)Marshal.PtrToStructure(st, typeof(Sstr_simple)); if (retval == false) { TestFramework.LogError("01", "PInvokeTests->PosTest2 : values of passed in structure not matched with expected once on unmanaged side."); return(false); } if ((simple.a != 101) || (!simple.b) || (simple.c != 10.11)) { Console.WriteLine("\nExpected values:\n SimpleStruct->a=101\nSimpleStruct->b=TRUE\nSimpleStruct->c=10.11\n"); Console.WriteLine("\nActual values:\n SimpleStruct->a=" + simple.a + "\nSimpleStruct->b=" + simple.b + "\nSimpleStruct->c=" + simple.c + "\n"); TestFramework.LogError("02", "PInvokeTests->PosTest2 : Returned values are different from expected values"); retval = false; } } catch (Exception e) { TestFramework.LogError("03", "Unexpected exception: " + e.ToString()); retval = false; } return(retval); }
public static bool DoCdeclSimpleStructByRef(ref Sstr p) { return(CdeclSimpleStructByRef(ref p)); }
private static extern bool CdeclSimpleStructByRef(ref Sstr p);
public static bool PosTest2() { string s = "Before"; bool retval = true; double d = 3.142; Sstr p = new Sstr(100, false, s); TestFramework.BeginScenario("\n\nTest #2 (Roundtrip of a simple structre by value. Verify that values updated on unmanaged side reflect on managed side)"); //direct pinvoke // //cdecl calling convention try { TestFramework.LogInformation(" Case 2: Direct p/invoke cdecl calling convention"); Sstr_simple simple = new Sstr_simple(100, false, d); simple = DoCdeclSimpleStruct(simple, ref retval); if (retval == false) { TestFramework.LogError("01", "PInvokeTests->PosTest2 : values of passed in structure not matched with expected once on unmanaged side."); return false; } if ((simple.a != 101) || (!simple.b) || (simple.c != 10.11)) { Console.WriteLine("\nExpected values:\n SimpleStruct->a=101\nSimpleStruct->b=TRUE\nSimpleStruct->c=10.11\n"); Console.WriteLine("\nActual values:\n SimpleStruct->a=" + simple.a + "\nSimpleStruct->b=" + simple.b + "\nSimpleStruct->c=" + simple.c + "\n"); TestFramework.LogError("02", "PInvokeTests->PosTest2 : Returned values are different from expected values"); retval = false; } } catch (Exception e) { TestFramework.LogError("03", "Unexpected exception: " + e.ToString()); retval = false; } // //delegate pinvoke // //cdecl calling convention try { TestFramework.LogInformation(" Case 4: Delegate p/invoke cdecl calling convention"); Sstr_simple simple = new Sstr_simple(100, false, d); CdeclSimpleStructDelegate std = GetFptrCdeclSimpleStruct(16); IntPtr st = std(simple, ref retval); simple = Marshal.PtrToStructure<Sstr_simple>(st); if (retval == false) { TestFramework.LogError("01", "PInvokeTests->PosTest2 : values of passed in structure not matched with expected once on unmanaged side."); return false; } if ((simple.a != 101) || (!simple.b) || (simple.c != 10.11)) { Console.WriteLine("\nExpected values:\n SimpleStruct->a=101\nSimpleStruct->b=TRUE\nSimpleStruct->c=10.11\n"); Console.WriteLine("\nActual values:\n SimpleStruct->a=" + simple.a + "\nSimpleStruct->b=" + simple.b + "\nSimpleStruct->c=" + simple.c + "\n"); TestFramework.LogError("02", "PInvokeTests->PosTest2 : Returned values are different from expected values"); retval = false; } } catch (Exception e) { TestFramework.LogError("03", "Unexpected exception: " + e.ToString()); retval = false; } return retval; }
public static bool PosTest1() { string s = "before"; string changedValue = "after"; bool retval = true; Sstr p = new Sstr(0, false, s); TestFramework.BeginScenario("Test #1 (Roundtrip of a simple structre by reference. Verify that values updated on unmanaged side reflect on managed side)"); //Direct pinvoke //cdecl calling convention. try { TestFramework.LogInformation(" Case 2: Direct p/invoke cdecl calling convention"); retval = DoCdeclSimpleStructByRef(ref p); if ((p.a != 100) || (!p.b) || (!p.str.Equals(changedValue))) { Console.WriteLine("\nExpected values:\n SimpleStruct->a=" + 100 + "\nSimpleStruct->b=TRUE\n" + "SimpleStruct->str=after\n"); Console.WriteLine("\nActual values:\n SimpleStruct->a=" + p.a + "\nSimpleStruct->b=" + p.b + "\nSimpleStruct->str=" + p.str + "\n"); TestFramework.LogError("03", "PInvokeTests->PosTest1 : Returned values are different from expected values"); retval = false; } } catch (Exception e) { TestFramework.LogError("04", "Unexpected exception: " + e.ToString()); retval = false; } //Delegate pinvoke //cdecl try { TestFramework.LogInformation(" Case 4: Delegate p/invoke - cdecl calling convention"); CdeclSimpleStructByRefDelegate std = GetFptrCdeclSimpleStructByRef(14); retval = std(ref p); if ((p.a != 100) || (!p.b) || (!p.str.Equals(changedValue))) { Console.WriteLine("\nExpected values:\n SimpleStruct->a=" + 100 + "\nSimpleStruct->b=TRUE\n" + "SimpleStruct->str=after\n"); Console.WriteLine("\nActual values:\n SimpleStruct->a=" + p.a + "\nSimpleStruct->b=" + p.b + "\nSimpleStruct->str=" + p.str + "\n"); TestFramework.LogError("01", "PInvokeTests->PosTest1 : Returned values are different from expected values"); retval = false; } } catch (Exception e) { TestFramework.LogError("02", "Unexpected exception: " + e.ToString()); retval = false; } return retval; }
public static bool DoCdeclSimpleStructByRef(ref Sstr p) { return CdeclSimpleStructByRef(ref p); }