Exemple #1
0
    /// <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!");
    }
Exemple #2
0
 public static extern bool SHStructWithObjectFldParam(StructWithObjFld s, Int32 shValue, Int32 shfld1Value, Int32 shfld2Value, String objtype);