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); }
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); }
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); }
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); }