/// <summary> ///passing structures (with SH subclass fields) as parameters in various combinations and forms; ///it uses the PInvoke signatures defined above it ///1- passing structures (In, out, ref) (with SH subclass fields) individually in separate methods ///2- passing structures (In, out, ref) (with SH subclass fields) in combination in the same method /// </summary> public static void RunSHStructParamTests() { Console.WriteLine("\nRunSHStructParamTests():"); Console.WriteLine("SHStructParam_In"); StructWithSHFld s = new StructWithSHFld(); //initialize a new StructWithSHFld s.hnd = Helper.NewSFH(); //get a new SH Int32 hndInt32 = Helper.SHInt32(s.hnd); //get the 32-bit value associated with s.hnd Assert.IsTrue(SHStructParam_In(s, hndInt32), "SHStructParam_In did not receive param as expected."); Assert.IsTrue(!Helper.IsChanged(s.hnd), "SHStructParam_In did not return param as expected.");//check that the value of the HANDLE field did not change Console.WriteLine("SHStructParam_Out"); Assert.Throws <NotSupportedException>(() => SHStructParam_Out(out s), "SHStructParam_Out"); Console.WriteLine("SHStructParam_OutRetVal"); //mangling the unmanaged signature and returning the structure should cause this exception since PInvoke does not support returning //non-blittable value classes Assert.Throws <MarshalDirectiveException>(() => SHStructParam_OutRetVal(), "SHStructParam_OutRetVal"); Console.WriteLine("SHStructParam_Ref1"); //Testing SHStructParam_Ref1 (does not change value of handle field) s.hnd = Helper.NewSFH(); //get a new SH hndInt32 = Helper.SHInt32(s.hnd); //get the 32-bit value associated with s.hnd Assert.IsTrue(SHStructParam_Ref1(ref s, hndInt32), "SHStructParam_Ref1 did not receive param as expected."); Assert.IsTrue(!Helper.IsChanged(s.hnd), "SHStructParam_Ref1 did not return param as expected."); //check that the value of the HANDLE field is not changed Console.WriteLine("SHStructParam_Ref2"); Assert.Throws <NotSupportedException>(() => SHStructParam_Ref2(ref s, hndInt32), "SHStructParam_Ref2"); /// ///2- passing structures (In, out, ref) (with SH subclass fields) in combination in the same method /// StructWithSHFld s1 = new StructWithSHFld();//initialize parameters s1.hnd = Helper.NewSFH(); Int32 hnd1Int32 = Helper.SHInt32(s1.hnd); //get the 32-bit value associated with s1.hnd StructWithSHFld s2; //out parameter StructWithSHFld s3 = new StructWithSHFld(); s3.hnd = Helper.NewSFH(); Int32 hnd3Int32 = Helper.SHInt32(s3.hnd); //get the 32-bit value associated with s3.hnd Console.WriteLine("SHStructParam_Multiple1: takes an out struct as one of the params and so is expected to result in an exception"); Assert.Throws <NotSupportedException>(() => SHStructParam_Multiple1(s1, out s2, ref s3, hnd1Int32, hnd3Int32), "SHStructParam_Multiple1"); Console.WriteLine("SHStructParam_Multiple2:takes a ref struct as one of the params"); s3.hnd = Helper.NewSFH(); hnd3Int32 = Helper.SHInt32(s3.hnd); //get the 32-bit value associated with s3.hnd Assert.IsTrue(SHStructParam_Multiple2(s1, ref s3, hnd1Int32, hnd3Int32), "SHStructParam_Multiple2 did not receive parameter(s) as expected."); Assert.IsTrue(!Helper.IsChanged(s1.hnd), "SHStructParam_Multiple2 did not return s1.hnd as expected."); Assert.IsTrue(!Helper.IsChanged(s3.hnd), "SHStructParam_Multiple2 did not return s3.hnd as expected."); Console.WriteLine("SHStructParam_Multiple3:takes a ref struct as one of the params aand changes its handle field and so is expected to result in an exception"); Assert.Throws <NotSupportedException>(() => SHStructParam_Multiple3(s1, ref s3, hnd1Int32, hnd3Int32), "SHStructParam_Multiple3"); }
/// <summary> ///passing structures (with SH subclass fields) as parameters in various combinations and forms; ///it uses the PInvoke signatures defined above it ///1- passing structures (In, out, ref) (with SH subclass fields) individually in separate methods ///2- passing structures (In, out, ref) (with SH subclass fields) in combination in the same method /// </summary> public static void RunSHStructParamTests() { Console.WriteLine("\nRunSHStructParamTests():"); ///1- passing structures (In, out, ref) (with SH subclass fields) individually in separate methods /// /// //initialize a new StructWithSHFld StructWithSHFld s = new StructWithSHFld(); s.hnd = Helper.NewSFH(); //get a new SH Int32 hndInt32 = Helper.SHInt32(s.hnd); //get the 32-bit value associated with s.hnd Console.WriteLine("Testing SHStructParam_In..."); SHStructParam_In(s, hndInt32); CheckCleanUp(s.hnd); s.hnd = Helper.NewSFH(); //get a new SH SafeHandle hnd_ref_backup = s.hnd; hndInt32 = Helper.SHInt32(s.hnd); //get the 32-bit value associated with s.hnd Console.WriteLine("Testing SHStructParam_Ref1 (does not change value of handle field)..."); SHStructParam_Ref1(ref s, hndInt32); CheckCleanUp(hnd_ref_backup); ///2- passing structures (In, out, ref) (with SH subclass fields) in combination in the same method /// /// //initialize parameters StructWithSHFld s1 = new StructWithSHFld(); s1.hnd = Helper.NewSFH(); Int32 hnd1Int32 = Helper.SHInt32(s1.hnd); //get the 32-bit value associated with s1.hnd StructWithSHFld s3 = new StructWithSHFld(); s3.hnd = Helper.NewSFH(); Int32 hnd3Int32 = Helper.SHInt32(s3.hnd); //get the 32-bit value associated with s3.hnd SafeHandle hnd3_ref_backup = s3.hnd; Console.WriteLine("Testing SHStructParam_Multiple2 (takes a ref struct as one of the params)..."); SHStructParam_Multiple2(s1, ref s3, hnd1Int32, hnd3Int32); CheckCleanUp(s1.hnd); CheckCleanUp(hnd3_ref_backup); }
/// <summary> ///runs all other miscellaneous tests; ///it uses the PInvoke signatures defined above it ///1-passing arrays of SHs as parameters ///2-passing arrays of structures (with SH subclass fields) as parameters ///3-returning SHs from unmanaged code as pure return values ///4-returning structures (with SH subclass fields) from unmanaged code as pure return values ///5-passing nested structures (with the nested structure having a SH subclass field) ///6-passing structures with SH Array fields ///7-passing mixed params (SH, SH subclass, subclass of SH subclass) ///8-passing struct params that have many handle fields [in, ref (with and without changes to flds)] ///9-passing SH subclass in Dispatch\UnknownWrapper, expecting a VARIANT (of type VT_DISPATCH or ///VT_UNKNOWN) on the managed side; as params and as fields /// </summary> public static void RunSHMiscTests() { Console.WriteLine("\nRunSHMiscTests():"); SafeFileHandle[] hndArray = new SafeFileHandle[Helper.N]; //the following array will contain the 32-bit values corresponding to hndArray's elements Int32[] hndArrayInt32s = new Int32[Helper.N]; //2-passing arrays of structures (with SH subclass fields) as parameters StructWithSHFld[] structArray = new StructWithSHFld[Helper.N]; //the following array will contain the 32-bit values corresponding to structArray's elements Int32[] structArrayInt32s = new Int32[Helper.N]; for (int i = 0; i < Helper.N; i++) { structArray[i] = new StructWithSHFld(); structArray[i].hnd = Helper.NewSFH(); structArrayInt32s[i] = Helper.SHInt32(structArray[i].hnd); } Console.WriteLine("Testing SHStructArrayParam..."); Assert.Throws <InvalidOperationException>(() => SHStructArrayParam(structArray, structArrayInt32s, Helper.N), "FAILED! Expected Exception Not Thrown!"); //3-returning SHs from unmanaged code as pure return values SFH_NoCloseHandle hnd; Console.WriteLine("Testing SHReturn..."); hnd = SHReturn(); Assert.IsTrue(Helper.IsChanged(hnd), "FAILED! SHReturn did not return hnd as expected."); //5-passing nested structures (with the nested structure having a SH subclass field) StructNestedParent sn = new StructNestedParent(); sn.snOneDeep = new StructNestedOneDeep(); sn.snOneDeep.s = new StructWithSHFld(); sn.snOneDeep.s.hnd = Helper.NewSFH(); Int32 hndInt32 = Helper.SHInt32(sn.snOneDeep.s.hnd); Console.WriteLine("Testing SHStructNestedParam..."); Assert.IsTrue(SHStructNestedParam(sn, hndInt32), "FAILED! SHStructNestedParam did not receive param as expected."); //check that the value of the HANDLE field did not change Assert.IsFalse(Helper.IsChanged(sn.snOneDeep.s.hnd), "FAILED! SHStructNestedParam did not return param as expected."); //7-passing mixed params (SH, SH subclass, subclass of SH subclass) SafeHandle sh1 = Helper.NewSFH(); SFH_NoCloseHandle sh2; ChildSFH_NoCloseHandle sh3 = Helper.NewChildSFH_NoCloseHandle(); StructWithBaseSHFld s1 = new StructWithBaseSHFld(); s1.hnd = Helper.NewSFH(); StructWithSHFld s2 = new StructWithSHFld(); s2.hnd = Helper.NewSFH(); StructWithChildSHFld s3 = new StructWithChildSHFld(); s3.hnd = Helper.NewChildSFH(); Int32 sh1Value = Helper.SHInt32(sh1); Int32 sh3Value = Helper.SHInt32(sh3); Int32 s1fldValue = Helper.SHInt32(s1.hnd); Int32 s2fldValue = Helper.SHInt32(s2.hnd); Int32 s3fldValue = Helper.SHInt32(s3.hnd); Console.WriteLine("Testing SHMixedParam1..."); Assert.IsTrue(SHMixedParam1(sh1, out sh2, ref sh3, s1, s2, ref s3, sh1Value, sh3Value, s1fldValue, s2fldValue, s3fldValue), "FAILED! SHMixedParam1 did not receive params as expected."); //check the values after the call Assert.IsFalse(Helper.IsChanged(sh1) || !Helper.IsChanged(sh2) || Helper.IsChanged(sh3) || Helper.IsChanged(s1.hnd) || Helper.IsChanged(s2.hnd) || Helper.IsChanged(s3.hnd), "FAILED! SHMixedParam1 did not return params as expected."); //8-passing struct params that have many handle fields [in, ref (with and without changes to flds)] //initialize a new StructWithManySHFlds Int32[] arrInt32s = null; StructWithManySHFlds s = Helper.NewStructWithManySHFlds(ref arrInt32s); Console.WriteLine("Testing SHStructWithManySHFldsParam_In..."); Assert.IsTrue(SHStructWithManySHFldsParam_In(s, arrInt32s), "FAILED! SHStructWithManySHFldsaram_In did not receive param as expected."); //check that the value of the HANDLE fields did not change Assert.IsFalse(Helper.IsChangedStructWithManySHFlds(s, arrInt32s), "FAILED! SHStructWithManySHFldsParam_In did not return param as expected."); Console.WriteLine("Testing SHStructWithManySHFldsParam_Ref1..."); Assert.IsTrue(SHStructWithManySHFldsParam_Ref1(ref s, arrInt32s), "FAILED! SHStructWithManySHFldsaram_Ref1 did not receive param as expected."); //check that the value of the HANDLE fields did not change Assert.IsFalse(Helper.IsChangedStructWithManySHFlds(s, arrInt32s), "FAILED! SHStructWithManySHFldsParam_Ref1 did not return param as expected."); Console.WriteLine("Testing SHStructWithManySHFldsParam_Ref2..."); Assert.Throws <NotSupportedException>(() => SHStructWithManySHFldsParam_Ref2(ref s, arrInt32s), "FAILED! Expected Exception Not Thrown!"); //9-passing SH subclass in Dispatch\UnknownWrapper, expecting a VARIANT (of type VT_DISPATCH or //VT_UNKNOWN) on the managed side SafeFileHandle sfh = Helper.NewSFH(); //SafeFileHandle sfh.shfld1 = Helper.NewSFH(); sfh.shfld2 = Helper.NewSFH(); Int32 shValue = Helper.SHInt32(sfh); Int32 shfld1Value = Helper.SHInt32(sfh.shfld1); Int32 shfld2Value = Helper.SHInt32(sfh.shfld2); SafeFileHandle sfh2 = Helper.NewSFH(); //SafeFileHandle sfh2.shfld1 = Helper.NewSFH(); sfh2.shfld2 = Helper.NewSFH(); Int32 sh2Value = Helper.SHInt32(sfh2); Int32 sh2fld1Value = Helper.SHInt32(sfh2.shfld1); Int32 sh2fld2Value = Helper.SHInt32(sfh2.shfld2); //re-initialize sfh = Helper.NewSFH(); //SafeFileHandle sfh.shfld1 = Helper.NewSFH(); sfh.shfld2 = Helper.NewSFH(); shValue = Helper.SHInt32(sfh); shfld1Value = Helper.SHInt32(sfh.shfld1); shfld2Value = Helper.SHInt32(sfh.shfld2); String sfhstr = "SafeFileHandle"; Console.WriteLine("Testing SHObjectParam with SFH..."); Assert.Throws <ArgumentException>(() => SHObjectParam(sfh, shValue, shfld1Value, shfld2Value, sfhstr), "FAILED! Expected Exception Not Thrown!"); //re-initialize SH's that will be wrapped for the structure fields sfh = Helper.NewSFH(); //SafeFileHandle sfh.shfld1 = Helper.NewSFH(); sfh.shfld2 = Helper.NewSFH(); shValue = Helper.SHInt32(sfh); shfld1Value = Helper.SHInt32(sfh.shfld1); shfld2Value = Helper.SHInt32(sfh.shfld2); sfh2 = Helper.NewSFH(); //SafeFileHandle sfh2.shfld1 = Helper.NewSFH(); sfh2.shfld2 = Helper.NewSFH(); sh2Value = Helper.SHInt32(sfh2); sh2fld1Value = Helper.SHInt32(sfh2.shfld1); sh2fld2Value = Helper.SHInt32(sfh2.shfld2); //re-initialize sfh = Helper.NewSFH(); //SafeFileHandle sfh.shfld1 = Helper.NewSFH(); sfh.shfld2 = Helper.NewSFH(); shValue = Helper.SHInt32(sfh); shfld1Value = Helper.SHInt32(sfh.shfld1); shfld2Value = Helper.SHInt32(sfh.shfld2); StructWithObjFld sWithSFHFld = new StructWithObjFld(); sWithSFHFld.obj = sfh; Console.WriteLine("Testing SHStructWithObjectFldParam with sWithSFHFld..."); Assert.Throws <ArgumentException>(() => SHStructWithObjectFldParam(sWithSFHFld, shValue, shfld1Value, shfld2Value, sfhstr), "FAILED! Expected Exception Not Thrown!"); }
public static extern bool SHMixedParam1(SafeHandle sh1, out SFH_NoCloseHandle sh2, ref ChildSFH_NoCloseHandle sh3, StructWithBaseSHFld s1, StructWithSHFld s2, ref StructWithChildSHFld s3, Int32 sh1Value, Int32 sh3Value, Int32 s1fldValue, Int32 s2fldValue, Int32 s3fldValue);
public static extern bool SHStructParam_Multiple3([In] StructWithSHFld sh1, ref StructWithSHFld sh2, Int32 sh1fldValue, Int32 sh2fldValue);
public static extern bool SHStructParam_Ref2(ref StructWithSHFld s, Int32 shfldValue);
public static extern bool SHStructParam_Out(out StructWithSHFld s);
public static extern bool SHStructParam_In([In] StructWithSHFld s, Int32 shfldValue);