⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jnativetester.java

📁 一个开源的组件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			class Inline extends Thread {
				final File f;

				public Inline(File f, int id) {
					this.f = f;
					setName("Inline " + id);
				}

				public void run() {
					try {
						JNative b = new JNative(f.getCanonicalPath(),
								"g_random_int_range", false);
						b.setRetVal(Type.INT);
						b.setParameter(0, 0);
						b.setParameter(1, 255);
						for (int i = 0; i < 1000; i++) {
							b.invoke();
							System.err
									.println(Thread.currentThread().getName()
											+ " : Random number from g_random_int in /usr/lib/libglib-2.0.so: "
											+ b.getRetVal());
							yield();
						}
						b.dispose();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			int numThreads = 10;
			List<Inline> threads = new ArrayList<Inline>();
			for (int i = 0; i < numThreads; i++) {
				threads.add(new Inline(f, i));
			}
			/*
			for (int i = 0; i < numThreads; i++) {
				threads.get(i).start();
			}
			for (int i = 0; i < numThreads; i++) {
				threads.get(i).join();
			}
*/
			LinuxCallback.runit();

//			JNative gtk_main_quit = new JNative("/usr/lib/libbonoboui-2.so.0", "gtk_main_quit");
//			gtk_main_quit.invoke();
			

			
		} else if (System.getProperty("os.name").toLowerCase().indexOf(
				"windows") != -1) {
			User32.MessageBox(0,
					"Demonstrates JNative in action with many Win32 calls",
					"Welcome to JNative", 0);

			User32
					.MessageBox(
							0,
							"Demarrage de la calculatrice,\nATTENTION : ne cliquez pas sur OK\nsi vous avez ouvert la calculatrice\navec des donnees non sauvegardees : elles seraient perdues",
							"Tuer un precessus par son nom", 0);
			User32
					.MessageBox(
							0,
							"Starting calculator,\nCAUTION : DO NOT click OK\nif you have calc open\nwith unsaved data : they will be lost",
							"Kill a process by name", 0);
			try {
				Runtime.getRuntime().exec("calc.exe");
				Runtime.getRuntime().exec("calc.exe");
				Runtime.getRuntime().exec("calc.exe");
				Runtime.getRuntime().exec("calc.exe");
				Thread.sleep(3000);
				if (0 != new KillProcess("calc.exe", true).killProcess())
					User32.MessageBox(0, "Process not found", "Error", 0);
			} catch (IOException e) {
				e.printStackTrace();
				User32.MessageBox(0, e.getMessage(), "Error launching notepad",
						0);
			}

			Pointer p = new Pointer(MemoryBlockFactory.createMemoryBlock(500));
			p.setStringAt(0, "This is a test");
			System.err.println("Serching in >" + p.getAsString() + "<");
			System.err.println("Java    found test at offset "
					+ p.getAsString().indexOf("test"));
			System.err.println("JNative found test at offset "
					+ JNative.searchNativePattern(p, "test".getBytes(), 500));
			if (JNative.searchNativePattern(p, "toto".getBytes(), 50) < 0) {
				System.err.println("toto not found");
			}

			try {
				User32.MessageBox(0,
						Kernel32.globalMemoryStatusEx().toString(),
						"GlobalMemoryStatusEx", 0);
				System.out.println();
				String[] ss = JNative.getDLLFileExports(System.getenv("WINDIR")
						+ "\\system32\\user32.dll");

				String me = "";
				for (String s : ss) {
					if (s.toLowerCase().indexOf("proc") != -1)
						me += s + "\n";
				}
				User32
						.MessageBox(
								0,
								me,
								"Exported functions of User32.dll containing the word proc",
								0);
				try {
					ss = JNative.getDLLFileExports("c:/windows/twain_32.dll");
					for (String s : ss) {
						System.err.println(s);
					}
				} catch (NativeException e) {
				}
			} catch (Exception e) {
				e.printStackTrace();
				User32.MessageBox(0, "Error #" + Kernel32.getLastError(),
						"GlobalMemoryStatusEx failed", 0);
			}

			// if(true) return;
			System.err.println(User32.MessageBox(0, Kernel32
					.getDiskFreeSpaceEx("c:").toString(), "Free space on "
					+ Kernel32.getComputerName(), 0x33));
			System.err.println("Module : " + JNative.getCurrentModule());
			HWND hwnd = new HWND(User32.CreateWindowEx(0, "Button", "TATA",
					WS_OVERLAPPEDWINDOW, 20, 30, 200, 300, 0, 0, JNative
							.getCurrentModule(), 0));
			/*
			 * JNative.registerWindowProc(hwnd, new WindowProc() {
			 * 
			 * /** Method windowProc
			 * 
			 * @param hwnd an int [in] Handle to the window. @param uMsg an int
			 * [in] Specifies the message. @param wParam an int [in] Specifies
			 * additional message information. The contents of this parameter
			 * depend on the value of the uMsg parameter. @param lParam an int
			 * Specifies additional message information. The contents of this
			 * parameter depend on the value of the uMsg parameter. @return an
			 * int The return value is the result of the message processing and
			 * depends on the message sent.
			 */
			/*
			 * public int windowProc(int hwnd, int uMsg, int wParam, int lParam) {
			 * System.err.println(hwnd + " " + uMsg + " " + wParam + " " +
			 * lParam); try { if (uMsg == WM.WM_CREATE.getValue()) // Initialize
			 * the window. return 0;
			 * 
			 * else if (uMsg == WM.WM_PAINT.getValue()) // Paint the window's
			 * client area. return 1;
			 * 
			 * else if (uMsg == WM.WM_SIZE.getValue()) // Set the size and
			 * position of the window. return 0;
			 * 
			 * else if (uMsg == WM.WM_DESTROY.getValue()) // Clean up
			 * window-specific data objects. return 0; // // Process other
			 * messages. //
			 * 
			 * else // return 1; return User32.defWindowProc(new HWND(hwnd), new
			 * UINT(uMsg), new WPARAM(wParam), new LPARAM(lParam)).getValue(); }
			 * catch (Exception e) { e.printStackTrace(); return 0; } }
			 * 
			 * });
			 */
			User32.ShowWindow(hwnd, SW_SHOW);
			User32.UpdateWindow(hwnd);

			MSG msg = new MSG();
			boolean lQuit = false;
			while (!lQuit) {
				switch (User32.GetMessage(msg, hwnd, 0, 0)) {
				case -1:
					System.err.println("Error occured");
					lQuit = true;
					break;
				case 0:
					System.err.println("WM_QUIT received");
					lQuit = true;
					break;
				default:
				}
				User32.TranslateMessage(msg);
				User32.DispatchMessage(msg);
			}

			System.err.println(hwnd.getValue());
			System.err.println(Kernel32.getLastError());
			Thread.sleep(6000);

			try {
				TestCallback.runIt();
			} catch (Exception e) {
				User32.MessageBox(0, e.toString(), e.getClass().getName(), 0);
			}
		}
		System.exit(0);
	}

	public static void loadLib() throws IOException {
		final File f;
		if (JNative.isLinux()) {
			f = new File("libJNativeCpp.so");
		} else if (JNative.isWindows()) {
			f = new File("JNativeCpp.dll");
		} else {
			throw new IllegalStateException("This OS is acctually not supported, please contact jnative@free.fr if you want it supported!");
		}
		if (f.exists()) {
			JNative.getLogger().log(SEVERITY.INFO, "Loading native lib "+f.getAbsolutePath());
			System.load(f.getCanonicalPath());
		} else {
			JFileChooser jfc = new JFileChooser(new File("."));
			jfc.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {

				@Override
				public boolean accept(File f) {
					return f.isDirectory() || f.getName().toLowerCase().endsWith(JNative.isWindows() ? ".dll" : ".so");
				}

				@Override
				public String getDescription() {
					return JNative.isWindows() ? "DLL : Dynamic Link Library" : " .so : Shared library";
				}
				
			});
			
			if(JFileChooser.APPROVE_OPTION == jfc.showOpenDialog(null)) {
				JNative.getLogger().log(SEVERITY.INFO, "Loading native lib "+jfc.getSelectedFile().getCanonicalPath());
				System.load(jfc.getSelectedFile().getCanonicalPath());
			} else {
				return;
			}
		}
	}

	private static void MessageBoxGnome(String text, String type, String[] buttons) throws NativeException, IllegalAccessException {
		JNative MessageBox = new JNative("/usr/lib/libgnomeui-2.so.0", "gnome_message_box_new");
		MessageBox.setRetVal(Type.INT);
		MessageBox.setParameter(0, text);
		MessageBox.setParameter(1, type /* SEE GNOME_MESSAGE_BOX_INFO*/);
		/* SEE GNOME_STOCK_BUTTON_OK*/
		int i = 2;
		for(String button : buttons) {
			MessageBox.setParameter(i++, button);
		}
		MessageBox.setParameter(i, NullPointer.NULL);
		MessageBox.invoke();
		int gtkWidget = MessageBox.getRetValAsInt();

		JNative gtk_widget_show = new JNative("/usr/lib/libgnomeui-2.so.0", "gtk_widget_show");
		gtk_widget_show.setParameter(0, gtkWidget);
		gtk_widget_show.invoke();
		MessageBox.dispose();
		gtk_widget_show.dispose();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -