Esempio n. 1
0
    public static int Main(string[] args)
    {
        string strManaged = "Managed";
        string strRet = "a";

        StringBuilder strBRet = new StringBuilder("a", 1);
        string strNative = " Native";
        StringBuilder strBNative = new StringBuilder(" Native", 7);

        Console.WriteLine("[Calling PInvokeDef.Marshal_InOut]");
        //since the out attributes doesnt work for string, so i dont check the out value.
        string strPara2 = new string(strManaged.ToCharArray());
        string strRet2 = PInvokeDef.Marshal_InOut(strPara2);
        if (!strRet2.Equals(strRet))
        {
            ReportFailure("Method PInvokeDef.Marshal_InOut[Managed Side],The Return string is wrong", strRet, strRet2);
        }

        //TestMethod3
        Console.WriteLine("[Calling PInvokeDef.Marshal_Out]");
        string strPara3 = strManaged;
        string strRet3 = PInvokeDef.Marshal_Out(strPara3);
        if (!strRet.Equals(strRet3))
        {
            ReportFailure("Method PInvokeDef.Marshal_Out[Managed Side],The Return string is wrong", strRet, strRet3);
        }
        if (!strManaged.Equals(strPara3))
        {
            ReportFailure("Method PInvokeDef.Marshal_Out[Managed Side],The Parameter string is Changed", strManaged, strPara3);
        }
        
        //TestMethod5
        Console.WriteLine("[Calling PInvokeDef.MarshalPointer_InOut]");
        string strPara5 = strManaged;
        string strRet5 = PInvokeDef.MarshalPointer_InOut(ref strPara5);
        if (!strRet5.Equals(strRet))
        {
            ReportFailure("Method PInvokeDef.MarshalPointer_InOut[Managed Side],The Return string is wrong", strRet, strRet5);
        }
        if (!strPara5.Equals(strNative))
        {
            ReportFailure("Method PInvokeDef.MarshalPointer_InOut[Managed Side],The Passed string is wrong", strNative, strPara5);
        }

        //TestMethod6
        Console.WriteLine("[Calling PInvokeDef.MarshalPointer_Out]");
        string strPara6;// = String.Copy(strManaged);
        string strRet6 = PInvokeDef.MarshalPointer_Out(out strPara6);
        if (!strRet6.Equals(strRet))
        {
            ReportFailure("Method PInvokeDef.MarshalPointer_Out[Managed Side],The Return string is wrong", strRet, strRet6);
        }
        if (!strPara6.Equals(strNative))
        {
            ReportFailure("Method PInvokeDef.MarshalPointer_Out[Managed Side],The Passed string is wrong", strNative, strPara6);
        }


        //TestMethod7
        Console.WriteLine("[Calling PInvokeDef.MarshalStrB_InOut]");
        StringBuilder strPara7 = new StringBuilder(strManaged);
        StringBuilder strRet7 = PInvokeDef.MarshalStrB_InOut(strPara7);

        if (!IsEqual(strRet7,strBRet))
        {
            ReportFailure("Method PInvokeDef.MarshalStrB_InOut[Managed Side],The Return string is wrong", strRet, strRet7.ToString());
        }
        if (!strPara7.Equals(new StringBuilder(strNative)))
        {
            ReportFailure("Method PInvokeDef.MarshalStrB_InOut[Managed Side],The Passed string is wrong", strNative, strPara7.ToString());
        }

        //TestMethod8
        Console.WriteLine("[Calling PInvokeDef.MarshalStrB_Out]");
        StringBuilder strPara8;// = new StringBuilder(strManaged);
        StringBuilder strRet8 = PInvokeDef.MarshalStrB_Out(out strPara8);
        if (!IsEqual(strRet8, strBRet))
        {
            ReportFailure("Method PInvokeDef.MarshalStrB_Out[Managed Side],The Return string is wrong", strRet, strRet8.ToString());
        }
        if (!IsEqual(strPara8,strBNative))
        {
            ReportFailure("Method PInvokeDef.MarshalStrB_Out[Managed Side],The Passed string is wrong", strNative, strPara8.ToString());
        }
        
        //TestMethod9
        Console.WriteLine("[Calling PInvokeDef.MarshalStrWB_InOut]");
        StringBuilder strPara9 = new StringBuilder(strManaged, strManaged.Length);
        StringBuilder strRet9 = PInvokeDef.MarshalStrWB_InOut(strPara9);
        
        if (!IsEqual(strRet9, strBRet))
        {
            ReportFailure("Method PInvokeDef.MarshalStrWB_InOut[Managed Side],The Return string is wrong", strRet, strRet9.ToString());
        }
        if (!IsEqual(strPara9,strBNative))
        {
            ReportFailure("Method PInvokeDef.MarshalStrWB_InOut[Managed Side],The Passed string is wrong", strNative, strPara9.ToString());
        }

        #region DelegatePinvoke
        //TestMethod11
        Del_MarshalPointer_InOut d1 = new Del_MarshalPointer_InOut(PInvokeDef.MarshalPointer_InOut);
        string strPara11 = new string(strManaged.ToCharArray());
        string strRet11 = d1(ref strPara11);
        if (!strRet11.Equals(strRet))
        {
            ReportFailure("Method Del_MarshalPointer_InOut[Managed Side],The Return string is wrong", strRet, strRet11);
        }
        if (!strPara11.Equals(strNative))
        {
            ReportFailure("Method Del_MarshalPointer_InOut[Managed Side],The Passed string is wrong", strNative, strPara11);
        }

        Del_Marshal_Out d2 = new Del_Marshal_Out(PInvokeDef.Marshal_Out);
        string strPara12 = strManaged;
        string strRet12 = d2(strPara12);
        if (!strRet.Equals(strRet12))
        {
            ReportFailure("Method Del_Marshal_Out[Managed Side],The Return string is wrong", strRet, strRet12);
        }
        if (!strManaged.Equals(strPara12))
        {
            ReportFailure("Method Del_Marshal_Out[Managed Side],The Parameter string is Changed", strManaged, strPara12);
        }
        #endregion

        #region ReversePInvoke
        Del_MarshalStrB_InOut d3 = new Del_MarshalStrB_InOut(Call_Del_MarshalStrB_InOut);
        if (!PInvokeDef.ReverseP_MarshalStrB_InOut(d3, "ă"))
        {
            ReportFailure("Method ReverseP_MarshalStrB_InOut[Managed Side],return value is false");
        }

        Del_MarshalStrB_Out d4 = new Del_MarshalStrB_Out(Call_Del_MarshalStrB_Out);
        if (!PInvokeDef.ReverseP_MarshalStrB_Out(d4))
        {
            ReportFailure("Method ReverseP_MarshalStrB_Out[Managed Side],return value is false");
        }

        #endregion

        return ExitTest();
     }
Esempio n. 2
0
 public static extern bool ReverseP_MarshalStrB_InOut(Del_MarshalStrB_InOut d, [MarshalAs(UnmanagedType.LPTStr)]string s);
Esempio n. 3
0
    public static int Main(string[] args)
    {
#if BUG750509
        if ((args != null) && (args.Length == 1) && (args[0] == "/mb"))
        {
            MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
        }
#endif

#pragma warning disable 0219
        string strManaged = "Managed";
        string strRet = "a";
        StringBuilder strBRet = new StringBuilder("a", 1);
        string strNative = " Native";
        StringBuilder strBNative = new StringBuilder(" Native", 7);
#pragma warning restore 0219

        PInvokeDef.Writeline("Format i=%d c=%c d=%f, s=%d, u=%d", 100, 'A', 3.1415926, 32767, 32768);

        //since the out attributes doesnt work for string, so i dont check the out value.
        string strPara2 = strManaged;
        string strRet2 = PInvokeDef.Marshal_InOut(strPara2);
        if (!strRet2.Equals(strRet))
        {
            ReportFailure("Method PInvokeDef.Marshal_InOut[Managed Side],The Return string is wrong", strRet, strRet2);
        }
        if (!strPara2.Equals(strManaged))
        {
            ReportFailure("Method PInvokeDef.Marshal_InOut[Managed Side],The Parameter string is Changed", strManaged, strPara2);
        }

        //TestMethod3
        string strPara3 = strManaged;
        string strRet3 = PInvokeDef.Marshal_Out(strPara3);
        if (!strRet.Equals(strRet3))
        {
            ReportFailure("Method PInvokeDef.Marshal_Out[Managed Side],The Return string is wrong", strRet, strRet3);
        }
        if (!strPara3.Equals(strManaged))
        {
            ReportFailure("Method PInvokeDef.Marshal_Out[Managed Side],The Parameter string is not Changed", strManaged, strPara3);
        }

        //TestMethod5
        string strPara5 = strManaged;
        string strRet5 = PInvokeDef.MarshalPointer_InOut(ref strPara5);

        if (!strRet5.Equals(strRet))
        {
            ReportFailure("Method PInvokeDef.MarshalPointer_InOut[Managed Side],The Return string is wrong", strRet, strRet5);
        }
        if (!strPara5.Equals(strNative))
        {
            ReportFailure("Method PInvokeDef.MarshalPointer_InOut[Managed Side],The Passed string is wrong", strNative, strPara5);
        }


        //TestMethod6
        string strPara6 = strManaged;
        string strRet6 = PInvokeDef.MarshalPointer_Out(out strPara6);
        if (!strRet6.Equals(strRet))
        {
            ReportFailure("Method PInvokeDef.MarshalPointer_Out[Managed Side],The Return string is wrong", strRet, strRet6);
        }
        if (!strPara6.Equals(strNative))
        {
            ReportFailure("Method PInvokeDef.MarshalPointer_Out[Managed Side],The Passed string is wrong", strNative, strPara6);
        }

        //TestMethod7
        StringBuilder strPara7 = new StringBuilder(strManaged, strManaged.Length);
        StringBuilder strRet7 = PInvokeDef.MarshalStrB_InOut(strPara7);

        if (!strRet7.ToString().Equals(strBRet.ToString()))
        {
            ReportFailure("Method PInvokeDef.MarshalStrB_InOut[Managed Side],The Return string is wrong", strRet, strRet7.ToString());
        }
        if (!strPara7.ToString().Equals(strBNative.ToString()))
        {
            ReportFailure("Method PInvokeDef.MarshalStrB_InOut[Managed Side],The Passed string is wrong", strNative, strPara7.ToString());
        }

        //TestMethod8
        StringBuilder strPara8;// = new StringBuilder(strManaged);
        StringBuilder strRet8 = PInvokeDef.MarshalStrB_Out(out strPara8);
        if (!strRet8.ToString().Equals(strBRet.ToString()))
        {
            ReportFailure("Method PInvokeDef.MarshalStrB_Out[Managed Side],The Return string is wrong", strRet, strRet8.ToString());
        }
        if (!strPara8.ToString().Equals(strBNative.ToString()))
        {
            ReportFailure("Method PInvokeDef.MarshalStrB_Out[Managed Side],The Passed string is wrong", strNative, strPara8.ToString());
        }

        #region ReversePinvoke
        DelMarshal_InOut d1 = new DelMarshal_InOut(Call_DelMarshal_InOut);
        if (!PInvokeDef.RPinvoke_DelMarshal_InOut(d1, "ň"))
        {
            ReportFailure("Method RPinvoke_DelMarshal_InOut[Managed Side],Return value is false");
        }

        DelMarshalPointer_Out d2 = new DelMarshalPointer_Out(Call_DelMarshalPointer_Out);
        if (!PInvokeDef.RPinvoke_DelMarshalPointer_Out(d2))
        {
            ReportFailure("Method RPinvoke_DelMarshal_Out[Managed Side],Return value is false");
        }

        #endregion
        #region DelegatePInvoke

        Del_Marshal_InOut d3 = new Del_Marshal_InOut(PInvokeDef.Marshal_InOut);
        string strPara9 = strManaged;
        string strRet9 = d3(strPara9);
        if (!strRet9.Equals(strRet))
        {
            ReportFailure("Method Del_Marshal_InOut[Managed Side],The Return string is wrong", strRet, strRet9);
        }
        if (!strPara9.Equals(strManaged))
        {
            ReportFailure("Method Del_Marshal_InOut[Managed Side],The Parameter string is Changed", strManaged, strPara9);
        }

        Del_MarshalPointer_Out d4 = new Del_MarshalPointer_Out(PInvokeDef.MarshalPointer_Out);
        string strPara10 = strManaged;
        string strRet10 = d4(out strPara10);
        if (!strRet10.Equals(strRet))
        {
            ReportFailure("Method Del_MarshalPointer_Out[Managed Side],The Return string is wrong", strRet, strRet10);
        }
        if (!strPara10.Equals(strNative))
        {
            ReportFailure("Method Del_MarshalPointer_Out[Managed Side],The Passed string is wrong", strNative, strPara10);
        }

        Del_MarshalStrB_InOut d5 = new Del_MarshalStrB_InOut(Call_Del_MarshalStrB_InOut);

        if (!PInvokeDef.ReverseP_MarshalStrB_InOut(d5, new string('a', 1)))
        {
            ReportFailure("Method ReverseP_MarshalStrB_InOut[Managed Side],return value is false");
        }
        #endregion
        return 100;
    }