MsgHook Native library updates

MsgHook Native library works from native test application, still
introducing library to java side through jni.
This commit is contained in:
Edward Jakubowski
2014-05-23 06:50:49 -04:00
parent 1721d2a130
commit b5ede5e6cb
28 changed files with 304 additions and 52 deletions

View File

@@ -26,6 +26,8 @@ public class MessageHookFrame extends JFrame {
public static final int WH_GETMESSAGE = 3;
public static final int WH_KEYBOARD_LL = 13;
public static final int WM_COPYDATA = 74;
private JTextArea textArea;
private JButton btnSave;
private JButton btnStartMsgHook;
@@ -91,6 +93,8 @@ public class MessageHookFrame extends JFrame {
}
public void createMessageHook() {
/*
// Below set windows hook is called from inside the native DLL
if (hHook != null)
return; //hook already running don't add anymore
System.out.println("starting global message hook");
@@ -108,13 +112,17 @@ public class MessageHookFrame extends JFrame {
hHook = User32.INSTANCE.SetWindowsHookEx(WH_CALLWNDPROC, lpfn, hMod, threadId);
if (hHook == null)
return;
*/
MsgHook mh = new MsgHook();
//mh.setMessageHook(hwnd, threadId);
MSG msg = new MSG();
try {
while (!quit) {
User32.INSTANCE.PeekMessage(msg, null, 0, 0, 1);
if (msg.message == User32.WM_HOTKEY){ // && msg.wParam.intValue() == 1
if (msg.message == WM_COPYDATA){ // && msg.wParam.intValue() == 1
System.out.println("WM_COPYDATA");
msg = new MSG(); //must clear msg so it doesn't repeat
}
Thread.sleep(10);

View File

@@ -1,6 +1,84 @@
package org.synthuse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.swing.JOptionPane;
public class MsgHook {
static
{
String loadFailedMsg = "Failed to load MsgHook library.\n";
//System.out.println("SynthuseDlg.config.disableUiaBridge: " + SynthuseDlg.config.disableUiaBridge);
String archDataModel = System.getProperty("sun.arch.data.model");//32 or 64 bit
try {
loadNativeLibraryFromJar("/MsgHook" + archDataModel + ".dll");
} catch (Exception ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
System.out.println(sw.toString());
JOptionPane.showMessageDialog(null, loadFailedMsg + sw.toString() , "Native Library Load Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void loadNativeLibraryFromJar(String path) {
// Obtain filename from path
String[] parts = path.split("/");
String filename = (parts.length > 1) ? parts[parts.length - 1] : null;
// Split filename to prexif and suffix (extension)
String prefix = "";
String suffix = null;
if (filename != null) {
parts = filename.split("\\.", 2);
prefix = parts[0];
suffix = (parts.length > 1) ? "."+parts[parts.length - 1] : null;
}
File temp = null;
try {
// Prepare temporary file
temp = File.createTempFile(prefix, suffix);
temp.deleteOnExit();
} catch(Exception e) {
e.printStackTrace();
}
if (!temp.exists()) { //some reason the temp file wasn't create so abort
System.out.println("File " + temp.getAbsolutePath() + " does not exist.");
return;
}
// Prepare buffer for data copying
byte[] buffer = new byte[1024];
int readBytes;
// Open and check input stream
InputStream is = MsgHook.class.getResourceAsStream(path);
if (is == null) { //check if valid
System.out.println("File " + path + " was not found inside JAR.");
return;
}
// Open output stream and copy data between source file in JAR and the temporary file
OutputStream os = null;
try {
os = new FileOutputStream(temp);
while ((readBytes = is.read(buffer)) != -1) {
os.write(buffer, 0, readBytes);
}
os.close();
is.close();
} catch(Exception e) {
e.printStackTrace();
}
// Finally, load the library
System.load(temp.getAbsolutePath());
}
public native boolean setMessageHook(long hwnd, long threadId);
public native boolean removeMessageHook();
}

View File

@@ -215,7 +215,7 @@ public class SynthuseDlg extends JFrame {
helpBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String about = "";
about += "Synthuse Version " + VERSION_STR + " create by Edward Jakubowski ejakubowski7@gmail.com\n\n";
about += "Synthuse Version " + VERSION_STR + " created by Edward Jakubowski ejakubowski7@gmail.com\n\n";
about += "Application information: \n";
about += " alwaysOnTop - " + config.isAlwaysOnTop() + "\n";

View File

@@ -235,7 +235,7 @@ public class XpathManager implements Runnable{
++matches;
}
}
lblStatus.setText(results + " matches");
lblStatus.setText(results + " match(es)");
if (cPos > 0 && matches == 0 && !alwaysFromTop) { //ask if user wants to search from top
int result = JOptionPane.showConfirmDialog(target.getTopLevelAncestor(), "No more matches found. Do you want to search from the top of the document?", "Find", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {