public static bool RunTests(int pid, NktRemoteBridge remoteBridge)
        {
            NktJavaObject testObj;

            Console.Write("  EventCallbackTests[DefineClass] test... ");
            try
            {
                byte[] byteCode;
                string s;

                s = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                s += "\\..\\Samples\\Demos4Test\\Java\\Test\\InjectTestWithCallbacks.class";
                byteCode = System.IO.File.ReadAllBytes(s);
                remoteBridge.DefineJavaClass(pid, "InjectTestWithCallbacks", byteCode);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return false;
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[Instatiate] test... ");
            try
            {
                testObj = remoteBridge.CreateJavaObject(pid, "InjectTestWithCallbacks", null);
                if (testObj  == null)
                {
                    Console.WriteLine("Error: Cannot create object");
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return false;
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[NoParametersReturningInt] test... ");
            try
            {
                object res;

                res = testObj.InvokeMethod("Test_NoParametersReturningInt", null);
                if (res.GetType() != typeof(int))
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return false;
                }
                if ((int)res != 10)
                {
                    Console.WriteLine("Error: Returned data mismatch");
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return false;
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[NoParametersReturningJavaLangDoubleObject] test... ");
            try
            {
                object res;
                NktJavaObject javaObj;

                res = testObj.InvokeMethod("Test_NoParametersReturningJavaLangDoubleObject", null);
                javaObj = res as NktJavaObject;
                if (javaObj == null)
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return false;
                }
                res = javaObj.get_Field("value"); //accessing private field
                if (res.GetType() != typeof(double))
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return false;
                }
                if ((double)res != 10.0)
                {
                    Console.WriteLine("Error: Returned data mismatch");
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return false;
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[WithParametersReturningInt] test... ");
            try
            {
                object res;

                res = testObj.InvokeMethod("Test_WithParametersReturningInt", null);
                if (res.GetType() != typeof(int))
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return false;
                }
                if ((int)res != 124)
                {
                    Console.WriteLine("Error: Returned data mismatch");
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return false;
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[WithParametersReturningJavaLangDoubleObject] test... ");
            try
            {

                object res;
                NktJavaObject javaObj;

                res = testObj.InvokeMethod("Test_WithParametersReturningJavaLangDoubleObject", null);
                javaObj = res as NktJavaObject;
                if (javaObj == null)
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return false;
                }
                res = javaObj.get_Field("value"); //accessing private field
                if (res.GetType() != typeof(double))
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return false;
                }
                if ((double)res != 124.0)
                {
                    Console.WriteLine("Error: Returned data mismatch");
                    return false;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return false;
            }
            Console.WriteLine("OK");
            return true;
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            object         continueEv;
            INktJavaObject topLevelFrame;
            int            verMajor, verMinor;

            Console.WriteLine("This example launches the Java Development Kit Notepad demo application and...");
            Console.WriteLine("   a) Changes the main frame caption.");
            Console.WriteLine("   b) Creates a JTextArea with a text and inserts it at the bottom.");
            Console.WriteLine("   c) Sends System.exit() on exit to gracefully close Notepad if still running.");

            remoteBridge = new NktRemoteBridge();
            if (remoteBridge == null)
            {
                Console.Write("Error: NktRemoteBridge not registered.");
                return;
            }

            try
            {
                String s;
                int    pid;

                Console.Write("Launching NOTEPAD.JAR... ");
                s = FindJavaLauncher(args);
                if (s == "")
                {
                    Console.WriteLine("Cannot locate a registered jarfile launcher.");
                    return;
                }
                //create command line
                s = "\"" + s + "\" ";
                if (DoVerboseJNI(args) != false)
                {
                    s += "-Xcheck:jni -verbose:jni ";
                }
                s += "-jar \"" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                s += "\\..\\Samples\\Demos4Test\\Java\\Notepad\\notepad.jar\"";
                //create process
                pid = remoteBridge.CreateProcess(s, (DoDelayedHook(args) != false) ? false : true, out continueEv);
                if (pid == 0)
                {
                    Console.WriteLine("failed.");
                    return;
                }
                procNotepad = Process.GetProcessById(pid);
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return;
            }

            //---------------- INJECT

            try
            {
                Console.Write("Injecting... ");
                remoteBridge.Hook(procNotepad.Id, eNktHookFlags.flgDebugPrintInterfaces);
                if (continueEv != null)
                {
                    remoteBridge.ResumeProcess(procNotepad.Id, continueEv);
                }
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return;
            }

            //---------------- WAIT UNTIL JVM INITIALIZATION

            Console.Write("Waiting for JVM initialization... ");
            try
            {
                while (remoteBridge.IsJVMAttached(procNotepad.Id, out verMajor, out verMinor) == false)
                {
                    if (procNotepad.HasExited != false)
                    {
                        Console.WriteLine("");
                        Console.WriteLine("Hooked process has ended. Quitting.");
                        return;
                    }
                    if (Console.KeyAvailable != false)
                    {
                        Char ch = Console.ReadKey(true).KeyChar;
                        if (ch == 27)
                        {
                            Console.WriteLine("");
                            Console.WriteLine("Exiting by user request.");
                            //close java notepad if still open
                            QuitJavaApp(true);
                            return;
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(10);
                    }
                }
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                //close java notepad if still open
                QuitJavaApp(true);
                return;
            }

            //---------------- PERFORM TASKS

            Console.Write("Obtaining main JFrame... ");
            topLevelFrame = null;
            while (topLevelFrame == null)
            {
                object frames;

                if (procNotepad.HasExited != false)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Hooked process has ended. Quitting.");
                    return;
                }
                if (Console.KeyAvailable != false)
                {
                    Char ch = Console.ReadKey(true).KeyChar;
                    if (ch == 27)
                    {
                        Console.WriteLine("");
                        Console.WriteLine("Exiting by user request.");
                        //close java notepad if still open
                        QuitJavaApp(true);
                        return;
                    }
                }
                //get top level frame
                try
                {
                    frames = remoteBridge.InvokeJavaStaticMethod(procNotepad.Id, "javax.swing.JFrame", "getFrames", null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("failed.");
                    Console.WriteLine(ex.ToString());
                    QuitJavaApp(true);
                    return;
                }
                if (frames is Array)
                {
                    if (((object[])frames).Length > 0)
                    {
                        topLevelFrame = ((object[])frames)[0] as INktJavaObject;
                    }
                }
                if (topLevelFrame == null)
                {
                    System.Threading.Thread.Sleep(100);
                }
            }
            Console.WriteLine("OK");

            Console.Write("Waiting until visible... ");
            while ((bool)(topLevelFrame.InvokeMethod("isVisible", null)) == false)
            {
                System.Threading.Thread.Sleep(10);
            }
            Console.WriteLine("OK");

            //set new caption
            topLevelFrame.InvokeMethod("setTitle", "Nektra - Notepad");

            //get main frame content pane
            INktJavaObject cntPane = topLevelFrame.InvokeMethod("getContentPane", null) as INktJavaObject;

            //add a new JTextArea at the bottom
            INktJavaObject newTxArea = remoteBridge.CreateJavaObject(procNotepad.Id, "javax.swing.JTextArea", "Nektra Demo");

            cntPane.InvokeMethod("add", new object[] { "South", newTxArea });

            //resize window in order to force a re-layout
            topLevelFrame.InvokeMethod("setSize", new object[] { 500, 601 });

            //free resources
            Marshal.ReleaseComObject(cntPane);
            Marshal.ReleaseComObject(newTxArea);
            Marshal.ReleaseComObject(topLevelFrame);

            //---------------- WAIT FOR EXIT

            Console.WriteLine("Press 'ESC' key to quit");
            while (true)
            {
                if (procNotepad.HasExited != false)
                {
                    Console.WriteLine("Hooked process has ended. Quitting.");
                    break;
                }
                if (Console.KeyAvailable == false)
                {
                    System.Threading.Thread.Sleep(10);
                    continue;
                }
                Char ch = Console.ReadKey(true).KeyChar;
                if (ch == 27)
                {
                    break;
                }
            }

            //---------------- CLOSE JAVA APP IF STILL RUNNING

            QuitJavaApp(false);
        }
Beispiel #3
0
        static private void Test_OnJavaCustomNativeCall(int procId, string className, string methodName, NktJavaObject objectOrClass, ref object[] parameters, out object retValue)
        {
            retValue = null;
            if (className == "InjectTestWithCallbacks")
            {
                if (methodName == "noParametersReturningInt")
                {
                    if (parameters.Length == 0)
                    {
                        retValue = 10;
                    }
                }
                else if (methodName == "noParametersReturningJavaLangDoubleObject")
                {
                    if (parameters.Length == 0)
                    {
                        try
                        {
                            retValue = remoteBridge.CreateJavaObject(procJavaTest.Id, "java/lang/Double", 10.0);
                        }
                        catch (System.Exception /*ex*/)
                        {
                            retValue = null;
                        }
                    }
                }
                else if (methodName == "withParametersReturningInt")
                {
                    int res;

                    if (parameters.Length == 5)
                    {
                        try
                        {
                            if ((string)parameters[0] == "Test_WithParametersReturningInt")
                            {
                                NktJavaObject javaObj = parameters[1] as NktJavaObject;
                                res = (int)javaObj.get_Field("value"); //accessing private field

                                int[,] intArrayParam = parameters[2] as int[, ];
                                for (int i = 0; i < 2; i++)
                                {
                                    for (int j = 0; j < 3; j++)
                                    {
                                        res += intArrayParam[i, j];
                                    }
                                }
                                res += (int)(double)parameters[3];
                                float[] flt = parameters[4] as float[];
                                for (int i = 0; i < 2; i++)
                                {
                                    res += (int)flt[i];
                                }
                                retValue = res;
                            }
                        }
                        catch (System.Exception /*ex*/)
                        {
                            retValue = null;
                        }
                    }
                }
                else if (methodName == "withParametersReturningJavaLangDoubleObject")
                {
                    double res;

                    if (parameters.Length == 5)
                    {
                        try
                        {
                            if ((string)parameters[0] == "Test_WithParametersReturningJavaLangDoubleObject")
                            {
                                NktJavaObject javaObj = parameters[1] as NktJavaObject;
                                res = (double)(int)javaObj.get_Field("value"); //accessing private field

                                int[,] intArrayParam = parameters[2] as int[, ];
                                for (int i = 0; i < 2; i++)
                                {
                                    for (int j = 0; j < 3; j++)
                                    {
                                        res += (double)intArrayParam[i, j];
                                    }
                                }
                                res += (double)parameters[3];
                                float[] flt = parameters[4] as float[];
                                for (int i = 0; i < 2; i++)
                                {
                                    res += (double)flt[i];
                                }
                                retValue = remoteBridge.CreateJavaObject(procJavaTest.Id, "java/lang/Double", res);
                            }
                        }
                        catch (System.Exception /*ex*/)
                        {
                            retValue = null;
                        }
                    }
                }
            }
            return;
        }
        public static bool RunTests(int pid, NktRemoteBridge remoteBridge)
        {
            NktJavaObject testObj;

            Console.Write("  EventCallbackTests[DefineClass] test... ");
            try
            {
                byte[] byteCode;
                string s;

                s        = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                s       += "\\..\\Samples\\Demos4Test\\Java\\Test\\InjectTestWithCallbacks.class";
                byteCode = System.IO.File.ReadAllBytes(s);
                remoteBridge.DefineJavaClass(pid, "InjectTestWithCallbacks", byteCode);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return(false);
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[Instatiate] test... ");
            try
            {
                testObj = remoteBridge.CreateJavaObject(pid, "InjectTestWithCallbacks", null);
                if (testObj == null)
                {
                    Console.WriteLine("Error: Cannot create object");
                    return(false);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return(false);
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[NoParametersReturningInt] test... ");
            try
            {
                object res;

                res = testObj.InvokeMethod("Test_NoParametersReturningInt", null);
                if (res.GetType() != typeof(int))
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return(false);
                }
                if ((int)res != 10)
                {
                    Console.WriteLine("Error: Returned data mismatch");
                    return(false);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return(false);
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[NoParametersReturningJavaLangDoubleObject] test... ");
            try
            {
                object        res;
                NktJavaObject javaObj;

                res     = testObj.InvokeMethod("Test_NoParametersReturningJavaLangDoubleObject", null);
                javaObj = res as NktJavaObject;
                if (javaObj == null)
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return(false);
                }
                res = javaObj.get_Field("value"); //accessing private field
                if (res.GetType() != typeof(double))
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return(false);
                }
                if ((double)res != 10.0)
                {
                    Console.WriteLine("Error: Returned data mismatch");
                    return(false);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return(false);
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[WithParametersReturningInt] test... ");
            try
            {
                object res;

                res = testObj.InvokeMethod("Test_WithParametersReturningInt", null);
                if (res.GetType() != typeof(int))
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return(false);
                }
                if ((int)res != 124)
                {
                    Console.WriteLine("Error: Returned data mismatch");
                    return(false);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return(false);
            }
            Console.WriteLine("OK");

            Console.Write("  EventCallbackTests[WithParametersReturningJavaLangDoubleObject] test... ");
            try
            {
                object        res;
                NktJavaObject javaObj;

                res     = testObj.InvokeMethod("Test_WithParametersReturningJavaLangDoubleObject", null);
                javaObj = res as NktJavaObject;
                if (javaObj == null)
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return(false);
                }
                res = javaObj.get_Field("value"); //accessing private field
                if (res.GetType() != typeof(double))
                {
                    Console.WriteLine("Error: Invalid returned type");
                    return(false);
                }
                if ((double)res != 124.0)
                {
                    Console.WriteLine("Error: Returned data mismatch");
                    return(false);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return(false);
            }
            Console.WriteLine("OK");
            return(true);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            object continueEv;
            INktJavaObject topLevelFrame;
            int verMajor, verMinor;

            Console.WriteLine("This example launches the Java Development Kit Notepad demo application and...");
            Console.WriteLine("   a) Changes the main frame caption.");
            Console.WriteLine("   b) Creates a JTextArea with a text and inserts it at the bottom.");
            Console.WriteLine("   c) Sends System.exit() on exit to gracefully close Notepad if still running.");

            remoteBridge = new NktRemoteBridge();
            if (remoteBridge == null)
            {
                Console.Write("Error: NktRemoteBridge not registered.");
                return;
            }

            try
            {
                String s;
                int pid;

                Console.Write("Launching NOTEPAD.JAR... ");
                s = FindJavaLauncher(args);
                if (s == "")
                {
                    Console.WriteLine("Cannot locate a registered jarfile launcher.");
                    return;
                }
                //create command line
                s = "\"" + s + "\" ";
                if (DoVerboseJNI(args) != false)
                    s += "-Xcheck:jni -verbose:jni ";
                s += "-jar \"" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                s += "\\..\\Samples\\Demos4Test\\Java\\Notepad\\notepad.jar\"";
                //create process
                pid = remoteBridge.CreateProcess(s, (DoDelayedHook(args) != false) ? false : true, out continueEv);
                if (pid == 0)
                {
                    Console.WriteLine("failed.");
                    return;
                }
                procNotepad = Process.GetProcessById(pid);
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return;
            }

            //---------------- INJECT

            try
            {
                Console.Write("Injecting... ");
                remoteBridge.Hook(procNotepad.Id, eNktHookFlags.flgDebugPrintInterfaces);
                if (continueEv != null)
                    remoteBridge.ResumeProcess(procNotepad.Id, continueEv);
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                return;
            }

            //---------------- WAIT UNTIL JVM INITIALIZATION

            Console.Write("Waiting for JVM initialization... ");
            try
            {
                while (remoteBridge.IsJVMAttached(procNotepad.Id, out verMajor, out verMinor) == false)
                {
                    if (procNotepad.HasExited != false)
                    {
                        Console.WriteLine("");
                        Console.WriteLine("Hooked process has ended. Quitting.");
                        return;
                    }
                    if (Console.KeyAvailable != false)
                    {
                        Char ch = Console.ReadKey(true).KeyChar;
                        if (ch == 27)
                        {
                            Console.WriteLine("");
                            Console.WriteLine("Exiting by user request.");
                            //close java notepad if still open
                            QuitJavaApp(true);
                            return;
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(10);
                    }
                }
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                //close java notepad if still open
                QuitJavaApp(true);
                return;
            }

            //---------------- PERFORM TASKS

            Console.Write("Obtaining main JFrame... ");
            topLevelFrame = null;
            while (topLevelFrame == null)
            {
                object frames;

                if (procNotepad.HasExited != false)
                {
                    Console.WriteLine("");
                    Console.WriteLine("Hooked process has ended. Quitting.");
                    return;
                }
                if (Console.KeyAvailable != false)
                {
                    Char ch = Console.ReadKey(true).KeyChar;
                    if (ch == 27)
                    {
                        Console.WriteLine("");
                        Console.WriteLine("Exiting by user request.");
                        //close java notepad if still open
                        QuitJavaApp(true);
                        return;
                    }
                }
                //get top level frame
                try
                {
                    frames = remoteBridge.InvokeJavaStaticMethod(procNotepad.Id, "javax.swing.JFrame", "getFrames", null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("failed.");
                    Console.WriteLine(ex.ToString());
                    QuitJavaApp(true);
                    return;
                }
                if (frames is Array)
                {
                    if (((object[])frames).Length > 0)
                        topLevelFrame = ((object[])frames)[0] as INktJavaObject;
                }
                if (topLevelFrame == null)
                    System.Threading.Thread.Sleep(100);
            }
            Console.WriteLine("OK");

            Console.Write("Waiting until visible... ");
            while ((bool)(topLevelFrame.InvokeMethod("isVisible", null)) == false)
            {
                System.Threading.Thread.Sleep(10);
            }
            Console.WriteLine("OK");

            //set new caption
            topLevelFrame.InvokeMethod("setTitle", "Nektra - Notepad");

            //get main frame content pane
            INktJavaObject cntPane = topLevelFrame.InvokeMethod("getContentPane", null) as INktJavaObject;

            //add a new JTextArea at the bottom
            INktJavaObject newTxArea = remoteBridge.CreateJavaObject(procNotepad.Id, "javax.swing.JTextArea", "Nektra Demo");
            cntPane.InvokeMethod("add", new object[] { "South", newTxArea });

            //resize window in order to force a re-layout
            topLevelFrame.InvokeMethod("setSize", new object[] { 500, 601 });

            //free resources
            Marshal.ReleaseComObject(cntPane);
            Marshal.ReleaseComObject(newTxArea);
            Marshal.ReleaseComObject(topLevelFrame);

            //---------------- WAIT FOR EXIT

            Console.WriteLine("Press 'ESC' key to quit");
            while (true)
            {
                if (procNotepad.HasExited != false)
                {
                    Console.WriteLine("Hooked process has ended. Quitting.");
                    break;
                }
                if (Console.KeyAvailable == false)
                {
                    System.Threading.Thread.Sleep(10);
                    continue;
                }
                Char ch = Console.ReadKey(true).KeyChar;
                if (ch == 27)
                    break;
            }

            //---------------- CLOSE JAVA APP IF STILL RUNNING

            QuitJavaApp(false);
        }