Пример #1
0
        static void Main(string[] args)
        {
            object continueEvent;

            //Console.Write("Press any key to continue... ");
            //Console.ReadKey(true);
            //Console.WriteLine("OK");

            processesList = new SortedList<Int32, string>();

            remoteBridge = new NktRemoteBridge();
            if (remoteBridge == null)
            {
                Console.Write("Error: NktRemoteBridge not registered.");
                return;
            }
            remoteBridge.OnCreateProcessCall += new DNktRemoteBridgeEvents_OnCreateProcessCallEventHandler(OnCreateProcessCall);
            remoteBridge.OnComInterfaceCreated += new DNktRemoteBridgeEvents_OnComInterfaceCreatedEventHandler(OnComInterfaceCreated);
            remoteBridge.OnProcessUnhooked += new DNktRemoteBridgeEvents_OnProcessUnhookedEventHandler(OnProcessUnhooked);

            try
            {
                string s;

                s = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\IEXPLORE.EXE", "", null) as string;
                if (s == null)
                {
                    Console.WriteLine("failed.");
                    return;
                }
                Console.Write("Launching & hooking IEXPLORE.EXE... ");
                s = "\"" + s + "\" http://www.bbc.co.uk";
                iePid = remoteBridge.CreateProcess(s, true, out continueEvent);
                if (iePid == 0)
                {
                    Console.WriteLine("failed.");
                    return;
                }
                lock (processesList)
                {
                    processesList.Add(iePid, "");
                }

                remoteBridge.Hook(iePid, eNktHookFlags.flgDebugPrintInterfaces);
                remoteBridge.WatchComInterface(iePid, CLSID_HTMLDocument, IID_IUnknown);
                remoteBridge.ResumeProcess(iePid, continueEvent);
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(ex.ToString());
                return;
            }

            while (true)
            {
                bool b;

                lock (processesList)
                {
                    b = (processesList.Count > 0);
                }
                if (b == 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;
            }
            return;
        }
Пример #2
0
        static void Main(string[] args)
        {
            object continueEvent;

            //Console.Write("Press any key to continue... ");
            //Console.ReadKey(true);
            //Console.WriteLine("OK");

            processesList = new SortedList <Int32, string>();

            remoteBridge = new NktRemoteBridge();
            if (remoteBridge == null)
            {
                Console.Write("Error: NktRemoteBridge not registered.");
                return;
            }
            remoteBridge.OnCreateProcessCall   += new DNktRemoteBridgeEvents_OnCreateProcessCallEventHandler(OnCreateProcessCall);
            remoteBridge.OnComInterfaceCreated += new DNktRemoteBridgeEvents_OnComInterfaceCreatedEventHandler(OnComInterfaceCreated);
            remoteBridge.OnProcessUnhooked     += new DNktRemoteBridgeEvents_OnProcessUnhookedEventHandler(OnProcessUnhooked);

            try
            {
                string s;

                s = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\IEXPLORE.EXE", "", null) as string;
                if (s == null)
                {
                    Console.WriteLine("failed.");
                    return;
                }
                Console.Write("Launching & hooking IEXPLORE.EXE... ");
                s     = "\"" + s + "\" http://www.bbc.co.uk";
                iePid = remoteBridge.CreateProcess(s, true, out continueEvent);
                if (iePid == 0)
                {
                    Console.WriteLine("failed.");
                    return;
                }
                lock (processesList)
                {
                    processesList.Add(iePid, "");
                }

                remoteBridge.Hook(iePid, eNktHookFlags.flgDebugPrintInterfaces);
                remoteBridge.WatchComInterface(iePid, CLSID_HTMLDocument, IID_IUnknown);
                remoteBridge.ResumeProcess(iePid, continueEvent);
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR");
                Console.WriteLine(ex.ToString());
                return;
            }

            while (true)
            {
                bool b;

                lock (processesList)
                {
                    b = (processesList.Count > 0);
                }
                if (b == 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;
                }
            }
            return;
        }
Пример #3
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);
        }
Пример #4
0
        static void Main(string[] args)
        {
            object continueEv;
            INktJavaObject topLevelFrame;
            int verMajor, verMinor;

            Console.WriteLine("This example performs some tests on RemoteBridge's Java functionality.");

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

            try
            {
                String s;
                int pid;

                Console.Write("Launching JAVATEST.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\\Test\\JavaTest.jar\"";
                //create process
                pid = remoteBridge.CreateProcess(s, (DoDelayedHook(args) != false) ? false : true, out continueEv);
                if (pid == 0)
                {
                    Console.WriteLine("failed.");
                    return;
                }
                procJavaTest = Process.GetProcessById(pid);
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                QuitJavaApp(true);
                return;
            }

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

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

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

            Console.Write("Waiting for JVM initialization... ");
            try
            {
                while (remoteBridge.IsJVMAttached(procJavaTest.Id, out verMajor, out verMinor) == false)
                {
                    if (procJavaTest.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 test if still open
                            QuitJavaApp(false);
                            return;
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(10);
                    }
                }
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                QuitJavaApp(true);
                return;
            }

            //---------------- FIND TOPLEVEL WINDOW

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

                if (procJavaTest.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 test if still open
                        QuitJavaApp(false);
                        return;
                    }
                }
                //get top level frame
                try
                {
                    frames = remoteBridge.InvokeJavaStaticMethod(procJavaTest.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(10);
            }
            Console.WriteLine("OK");

            //---------------- RUN TESTS

            Console.Write("Running tests...");
            for (int testNo=1; testNo<=12; testNo++)
            {
                if (procJavaTest.HasExited != false)
                {
                    Console.WriteLine("Hooked process has ended. Quitting.");
                    return;
                }
                if (Console.KeyAvailable != false)
                {
                    Char ch = Console.ReadKey(true).KeyChar;
                    if (ch == 27)
                    {
                        Console.WriteLine("Exiting by user request.");
                        //close java test if still open
                        QuitJavaApp(false);
                        return;
                    }
                }
                //run test
                bool b = false;
                switch (testNo)
                {
                    case 1:
                        b = BooleanTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 2:
                        b = ByteTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 3:
                        b = ShortTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 4:
                        b = CharTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 5:
                        b = IntTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 6:
                        b = LongTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 7:
                        b = FloatTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 8:
                        b = DoubleTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 9:
                        b = DateTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 10:
                        b = BigDecimalTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 11:
                        b = StringTests.RunTests(procJavaTest.Id, remoteBridge);
                        break;
                    case 12:
                        remoteBridge.OnJavaCustomNativeCall += Test_OnJavaCustomNativeCall;
                        b = EventCallbackTests.RunTests(procJavaTest.Id, remoteBridge);
                        remoteBridge.OnJavaCustomNativeCall -= Test_OnJavaCustomNativeCall;
                        break;
                }
                if (b == false)
                {
                    QuitJavaApp(true);
                    return;
                }
            }

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

            QuitJavaApp(true);
        }
Пример #5
0
        //static string IID_IUnknown = "{00000000-0000-0000-C000-000000000046}";
        static void Main(string[] args)
        {
            NktRemoteBridge remoteBridge;
            object continueEv;
            Process procNotepad;

            if (Environment.OSVersion.Version.Major < 6)
            {
                Console.Write("Error: This application requires Windows Vista or later to work.");
                return;
            }

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

            try
            {
                int pid;

                Console.Write("Launching NOTEPAD.EXE... ");
                pid = remoteBridge.CreateProcess("notepad.exe", (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;
            }

            //----------------

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

            //----------------

            Console.WriteLine("Ready.");
            Console.WriteLine("Usage:");
            Console.WriteLine("  This demo launches a Windows' Notepad application and scans 'File Open' dialog boxes.");
            Console.WriteLine("  When a file open dialog box is created, you can take the following actions:");
            Console.WriteLine("    1) Press the 'F' key to retrieve the typed file name in the dialog box.");
            Console.WriteLine("    2) Press the 'C' key to close the dialog box window using the OK button.");
            Console.WriteLine("    3) Press 'ESC' key to quit this demo!");
            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;
                switch (ch)
                {
                    case 'f':
                    case 'F':
                        DoGetOpenFileDialogFilename(remoteBridge, procNotepad);
                        break;

                    case 'c':
                    case 'C':
                        DoCloseOpenFileDialog(remoteBridge, procNotepad);
                        break;
                }
            }
            return;
        }
Пример #6
0
        //static string IID_IUnknown = "{00000000-0000-0000-C000-000000000046}";
        static void Main(string[] args)
        {
            NktRemoteBridge remoteBridge;
            object continueEv;
            Process procNotepad;

            if (Environment.OSVersion.Version.Major < 6)
            {
                Console.Write("Error: This application requires Windows Vista or later to work.");
                return;
            }

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

            try
            {
                int pid;

                Console.Write("Launching NOTEPAD.EXE... ");
                pid = remoteBridge.CreateProcess("notepad.exe", (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;
            }

            //----------------

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

            //----------------

            Console.WriteLine("Ready.");
            Console.WriteLine("Usage:");
            Console.WriteLine("  This demo launches a Windows' Notepad application and scans 'File Open' dialog boxes.");
            Console.WriteLine("  When a file open dialog box is created, you can take the following actions:");
            Console.WriteLine("    1) Press the 'F' key to retrieve the typed file name in the dialog box.");
            Console.WriteLine("    2) Press the 'C' key to close the dialog box window using the OK button.");
            Console.WriteLine("    3) Press 'ESC' key to quit this demo!");
            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;
                switch (ch)
                {
                    case 'f':
                    case 'F':
                        DoGetOpenFileDialogFilename(remoteBridge, procNotepad);
                        break;

                    case 'c':
                    case 'C':
                        DoCloseOpenFileDialog(remoteBridge, procNotepad);
                        break;
                }
            }
            return;
        }
Пример #7
0
        static void Main(string[] args)
        {
            object         continueEv;
            INktJavaObject topLevelFrame;
            int            verMajor, verMinor;

            Console.WriteLine("This example performs some tests on RemoteBridge's Java functionality.");

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

            try
            {
                String s;
                int    pid;

                Console.Write("Launching JAVATEST.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\\Test\\JavaTest.jar\"";
                //create process
                pid = remoteBridge.CreateProcess(s, (DoDelayedHook(args) != false) ? false : true, out continueEv);
                if (pid == 0)
                {
                    Console.WriteLine("failed.");
                    return;
                }
                procJavaTest = Process.GetProcessById(pid);
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                QuitJavaApp(true);
                return;
            }

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

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

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

            Console.Write("Waiting for JVM initialization... ");
            try
            {
                while (remoteBridge.IsJVMAttached(procJavaTest.Id, out verMajor, out verMinor) == false)
                {
                    if (procJavaTest.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 test if still open
                            QuitJavaApp(false);
                            return;
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(10);
                    }
                }
                Console.WriteLine("OK");
            }
            catch (Exception ex)
            {
                Console.WriteLine("failed.");
                Console.WriteLine(ex.ToString());
                QuitJavaApp(true);
                return;
            }

            //---------------- FIND TOPLEVEL WINDOW

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

                if (procJavaTest.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 test if still open
                        QuitJavaApp(false);
                        return;
                    }
                }
                //get top level frame
                try
                {
                    frames = remoteBridge.InvokeJavaStaticMethod(procJavaTest.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(10);
                }
            }
            Console.WriteLine("OK");

            //---------------- RUN TESTS

            Console.Write("Running tests...");
            for (int testNo = 1; testNo <= 12; testNo++)
            {
                if (procJavaTest.HasExited != false)
                {
                    Console.WriteLine("Hooked process has ended. Quitting.");
                    return;
                }
                if (Console.KeyAvailable != false)
                {
                    Char ch = Console.ReadKey(true).KeyChar;
                    if (ch == 27)
                    {
                        Console.WriteLine("Exiting by user request.");
                        //close java test if still open
                        QuitJavaApp(false);
                        return;
                    }
                }
                //run test
                bool b = false;
                switch (testNo)
                {
                case 1:
                    b = BooleanTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 2:
                    b = ByteTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 3:
                    b = ShortTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 4:
                    b = CharTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 5:
                    b = IntTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 6:
                    b = LongTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 7:
                    b = FloatTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 8:
                    b = DoubleTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 9:
                    b = DateTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 10:
                    b = BigDecimalTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 11:
                    b = StringTests.RunTests(procJavaTest.Id, remoteBridge);
                    break;

                case 12:
                    remoteBridge.OnJavaCustomNativeCall += Test_OnJavaCustomNativeCall;
                    b = EventCallbackTests.RunTests(procJavaTest.Id, remoteBridge);
                    remoteBridge.OnJavaCustomNativeCall -= Test_OnJavaCustomNativeCall;
                    break;
                }
                if (b == false)
                {
                    QuitJavaApp(true);
                    return;
                }
            }

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

            QuitJavaApp(true);
        }
Пример #8
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);
        }