Skip to content

Commit

Permalink
Extended Msg hook text limit, msg hook prompts before switching betwe…
Browse files Browse the repository at this point in the history
…en 32 and 64 bits, optionally disable uiabridge's caches

Message Hook Viewer text limit is not set to 700,000 characters,
allowing it to show many more messages/lines
Message Hook Viewer will prompt user if the want to switch between 64
and 32 bits.
UiaBridge has an optional flag to disable cache.  This fixes an issue
where caching is either slower or causes issues when using multiple
threads in OASIS.
  • Loading branch information
ejakubowski committed Jun 4, 2014
1 parent d100d23 commit eaff0dd
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 53 deletions.
Binary file added native/MsgHook.suo
Binary file not shown.
17 changes: 14 additions & 3 deletions native/MsgHook/MsgHookWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HWND mainHwnd = NULL;
HMENU mainMenu = NULL;
#define TXTBOX_LIMIT 700000
HWND txtbox = NULL;
HWND targetHwnd = NULL;
DWORD targetPid = 0;
Expand Down Expand Up @@ -68,9 +69,9 @@ void AppendText(HWND txtHwnd, LPCTSTR newText)
if (isPaused)
return;
DWORD len = GetWindowTextLength(txtHwnd);
if (len > 25000)
if (len > (TXTBOX_LIMIT - 500))
{//need to truncate the beginning so the text doesn't go past it's limit
SendMessage(txtHwnd, EM_SETSEL, 0, 10000);
SendMessage(txtHwnd, EM_SETSEL, 0, 20000);
SendMessage(txtHwnd, EM_REPLACESEL, 0, (LPARAM)_T(""));
len = GetWindowTextLength(txtHwnd);
}
Expand Down Expand Up @@ -173,7 +174,10 @@ void StartMessageHook()
}
else
{
_stprintf_s(tmp, _T("Target PId (%ld) is a not matching bit process\r\n"), targetPid);
if (current64bit)
_stprintf_s(tmp, _T("Target PId (%ld) is a not matching 64 bit process.\r\n"), targetPid);
else
_stprintf_s(tmp, _T("Target PId (%ld) is a not matching 32 bit process.\r\n"), targetPid);
AppendText(txtbox, tmp);
TCHAR *dllname = dll32bitName;
TCHAR *exename = _T("SetMsgHook32.exe");
Expand All @@ -184,12 +188,17 @@ void StartMessageHook()
exename = _T("SetMsgHook64.exe");
setMsgHookRes = IDR_SETMH64;
}
_tcscat_s(tmp, 500, _T("Do you wish to open a new matching Message Hook Window?"));
int mbResult = MessageBox(mainHwnd, tmp, _T("Message Hook"), MB_ICONQUESTION | MB_YESNO);
if (mbResult == IDNO)
return ;
_stprintf_s(tmp, _T("%s %s 0 %d"), exename, dllname, targetPid);
RunResource(setMsgHookRes, tmp);
//EnableMenuItem(mainMenu, ID_FILE_STOPHOOK, MF_ENABLED);
//EnableMenuItem(mainMenu, ID_FILE_STARTHOOK, MF_DISABLED | MF_GRAYED);
_tcscat_s(tmp, 500, _T("\r\n"));
AppendText(txtbox, tmp);
PostQuitMessage(2);
return;
}
AppendText(txtbox, tmp);
Expand Down Expand Up @@ -431,6 +440,7 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
if (!hWnd) {
DWORD lastErr = GetLastError();
printf("Error Creating Window %d\n", lastErr);
_tprintf(_T("Window Class Name: %s, Instance: %ld\n"), szWindowClass, (long)hInstance);
return FALSE;
}
mainHwnd = hWnd;
Expand All @@ -441,6 +451,7 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
//WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL
txtbox = CreateWindow(TEXT("Edit"),TEXT(""), WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL | ES_AUTOVSCROLL | ES_READONLY,
txtboxSpacing, txtboxSpacing,rect.right-(txtboxSpacing*2), rect.bottom-(txtboxSpacing*2), hWnd, NULL, NULL, NULL);
SendMessage(txtbox, EM_SETLIMITTEXT, (WPARAM)TXTBOX_LIMIT, 0);

HFONT hFont = CreateFont(14, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
Expand Down
Binary file modified native/MsgHook/bin/MsgHook32.dll
Binary file not shown.
Binary file modified native/MsgHook/bin/MsgHook64.dll
Binary file not shown.
4 changes: 4 additions & 0 deletions native/SetMsgHook/MsgHook.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ void MsgHook_CreateMsgHookWindow(LPTSTR args)
if (msgHookDll != NULL)
{
CreateMsgHookWindow = (CREATEMSGHOOKWINDOW)GetProcAddress(msgHookDll, "CreateMsgHookWindow");
SetGlobalDLLInstance = (SETGLOBALDLLINSTANCE)GetProcAddress(msgHookDll, "SetGlobalDLLInstance");
if (CreateMsgHookWindow)
{
SetGlobalDLLInstance(msgHookDll);
CreateMsgHookWindow(args);
}
}
if (msgHookDll != NULL)
FreeLibrary(msgHookDll);
}

BOOL MsgHook_SetMsgHook(HWND hw, int threadId)
Expand Down
1 change: 1 addition & 0 deletions native/SetMsgHook/SetMsgHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ int _tmain(int argc, _TCHAR* argv[])
TCHAR tmp[100];
_stprintf_s(tmp, _T("%ld"), (long)procId);
MsgHook_CreateMsgHookWindow(tmp);
//_getch();
return 0;
}

Expand Down
Binary file modified native/SetMsgHook/bin/SetMsgHook32.exe
Binary file not shown.
Binary file modified native/SetMsgHook/bin/SetMsgHook64.exe
Binary file not shown.
Binary file modified native/uiabridge/bin/uiabridge32.dll
Binary file not shown.
Binary file modified native/uiabridge/bin/uiabridge64.dll
Binary file not shown.
10 changes: 10 additions & 0 deletions native/uiabridge/org_synthuse_UiaBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ JNIEXPORT void JNICALL Java_org_synthuse_UiaBridge_shutdown(JNIEnv *env, jobject
{
}

/*
* Class: org_synthuse_UiaBridge
* Method: useCachedRequests
* Signature: (Z)V
*/
JNIEXPORT void JNICALL Java_org_synthuse_UiaBridge_useCachedRequests(JNIEnv *env, jobject obj, jboolean jcacheRequestsFlg)
{
Global::AUTO_BRIDGE->useCachedRequests((bool)(jcacheRequestsFlg == JNI_TRUE));
}

/*
* Class: org_synthuse_UiaBridge
* Method: addEnumFilter
Expand Down
8 changes: 8 additions & 0 deletions native/uiabridge/org_synthuse_UiaBridge.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 27 additions & 4 deletions native/uiabridge/uiabridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ AutomationBridge::AutomationBridge()
{
enumFilters = gcnew Dictionary<System::String ^, System::String ^>();
cacheRequest = nullptr;
useCache = true;
initializeCache("");
}

AutomationBridge::AutomationBridge(System::String ^cachedProperties)
{
enumFilters = gcnew Dictionary<System::String ^, System::String ^>();
cacheRequest = nullptr;
useCache = true;
initializeCache(cachedProperties);
}

Expand All @@ -36,6 +38,11 @@ AutomationBridge::~AutomationBridge()
//Console::WriteLine("disposing of AutomationBridge");
}

void AutomationBridge::useCachedRequests(System::Boolean cacheRequestsFlg)
{
useCache = cacheRequestsFlg;
}

void AutomationBridge::initializeCache(System::String ^cachedProperties)
{
cacheRequest = gcnew CacheRequest();
Expand Down Expand Up @@ -158,7 +165,10 @@ Boolean AutomationBridge::isElementFiltered(System::Windows::Automation::Automat
}
else //all other property types that are strings
{
valStr = element->GetCachedPropertyValue(ap)->ToString();
if (useCache)
valStr = element->GetCachedPropertyValue(ap)->ToString();
else
valStr = element->GetCurrentPropertyValue(ap)->ToString();
//valStr = element->GetCurrentPropertyValue(ap)->ToString();
}
//System::Console::WriteLine("test property vals: {0} , {1}", valStr, enumFilters[key]);
Expand Down Expand Up @@ -285,7 +295,12 @@ array<System::String ^> ^ AutomationBridge::enumWindowInfo(AutomationElement ^pa
//System::Console::WriteLine("get info: {0}", getWindowInfo(element, properties));
//AutomationElement ^currentElement = tw->GetFirstChild(element, cacheRequest);
AutomationElement ^currentElement = nullptr;
currentElement = tw->GetFirstChild(parentElement, cacheRequest);

if (useCache)
currentElement = tw->GetFirstChild(parentElement, cacheRequest);
else
currentElement = tw->GetFirstChild(parentElement);

if (currentElement == nullptr)
{
//System::Console::WriteLine("no children {0}", element->CachedChildren->Count);
Expand Down Expand Up @@ -319,7 +334,10 @@ array<System::String ^> ^ AutomationBridge::enumWindowInfo(AutomationElement ^pa
{
output(ex, "");
}
currentElement = tw->GetNextSibling(currentElement, cacheRequest);
if (useCache)
currentElement = tw->GetNextSibling(currentElement, cacheRequest);
else
currentElement = tw->GetNextSibling(currentElement);
}
return winInfoList->ToArray();
}
Expand Down Expand Up @@ -387,7 +405,12 @@ System::String ^ AutomationBridge::getWindowInfo(AutomationElement ^element, Sys
else
currentVal = element->GetCurrentPropertyValue(ap); //cached pattern was having issues;
}
currentVal = element->GetCachedPropertyValue(ap);

if (useCache)
currentVal = element->GetCachedPropertyValue(ap);
else
currentVal = element->GetCurrentPropertyValue(ap);

if (currentVal == nullptr)
continue;
if (ap->ProgrammaticName->Equals(L"AutomationElementIdentifiers.RuntimeIdProperty"))
Expand Down
3 changes: 3 additions & 0 deletions native/uiabridge/uiabridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace uiabridge {
AutomationBridge(void);
AutomationBridge(System::String ^cachedProperties);
~AutomationBridge();
void useCachedRequests(System::Boolean cacheRequestsFlg);
int addEnumFilter(System::String ^propertyName, System::String ^propertyValue);
void clearEnumFilters();
Boolean isElementFiltered(System::Windows::Automation::AutomationElement ^element);
Expand All @@ -38,12 +39,14 @@ namespace uiabridge {
static System::String ^PARENT_MODIFIER = L"Parent";//find all children of this matching parent filter
static System::String ^FIRST_MODIFIER = L"First"; //find first element matching this filter then stop
static System::String ^DEFAULT_CACHED_PROPS = L"RuntimeIdProperty,ParentRuntimeIdProperty,NativeWindowHandleProperty,ProcessIdProperty,FrameworkIdProperty,LocalizedControlTypeProperty,ControlTypeProperty,ClassNameProperty,NameProperty,BoundingRectangleProperty,ValueProperty";

private:
void initializeCache(System::String ^cachedProperties);
void output(Exception ^ex, System::String ^message);
Dictionary<System::String ^, System::String ^> ^enumFilters;
void AutomationBridge::processFilterModifier(Boolean filtered, Boolean modifierChanged, List<System::String ^> ^filterModifierList);
CacheRequest ^cacheRequest;
System::Boolean useCache;
array<AutomationProperty^> ^cachedRootProperties;
};
}
Binary file modified native/uiabtest/Release/uiabridge.dll
Binary file not shown.
Binary file modified native/uiabtest/Release/uiabtest.exe
Binary file not shown.
5 changes: 5 additions & 0 deletions native/uiabtest/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
REM set path=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin;%path%

%WinDir%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /p:configuration=release /p:platform=win32 %*

pause
11 changes: 9 additions & 2 deletions native/uiabtest/uiabtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ int main(array<System::String ^> ^args)
{
Console::WriteLine(L"UI Automation Bridge Test");
//System::String ^propList = L"RuntimeIdProperty,ProcessIdProperty,FrameworkIdProperty,LocalizedControlTypeProperty,ClassNameProperty,NameProperty";
System::String ^propList = L"RuntimeIdProperty,ParentRuntimeIdProperty,NativeWindowHandleProperty,ProcessIdProperty,FrameworkIdProperty,LocalizedControlTypeProperty,ControlTypeProperty,ClassNameProperty,NameProperty,BoundingRectangleProperty";
System::String ^propList = L"RuntimeIdProperty,ParentRuntimeIdProperty,NativeWindowHandleProperty,ProcessIdProperty,FrameworkIdProperty,LocalizedControlTypeProperty,ControlTypeProperty,ClassNameProperty,NameProperty,ValueProperty,BoundingRectangleProperty";
AutomationBridge ^ab = gcnew AutomationBridge(propList);
//System::String ^propList = L"RuntimeIdProperty,BoundingRectangleProperty";
Console::WriteLine(propList);
Console::WriteLine(L"\nCached Requests Enabled");
ab->useCachedRequests(true);//disable cache
runBasicTests(ab, propList);
runBasicTests(ab, propList);
runBasicTests(ab, propList);

Console::WriteLine(L"Cached Requests Disabled");
ab->useCachedRequests(false);//disable cache
runBasicTests(ab, propList);
runBasicTests(ab, propList);
runBasicTests(ab, propList);
Expand Down Expand Up @@ -102,6 +109,6 @@ int main(array<System::String ^> ^args)
//System::Double seconds = System::Math::Round(System::DateTime::Now.Subtract(start).TotalSeconds, 4);
//Console::WriteLine(L"Total Elements: {0} in {1} seconds", winInfo->Length, seconds);
Console::WriteLine(L"press any key to exit");
getch();//wait for user input
_getch();//wait for user input
return 0;
}
35 changes: 27 additions & 8 deletions src/org/synthuse/KeyboardHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public void keyPressed(TargetKeyPress target) {
public static HHOOK hHook = null;
public static LowLevelKeyboardProc lpfn;
public static volatile boolean quit = false;

private static Thread khThread = null;


public interface User32Ex extends W32APIOptions {
Expand Down Expand Up @@ -199,6 +201,14 @@ public void unhook() {
//stops Keyboard hook and causes the unhook command to be called
public static void stopKeyboardHook() {
quit = true;
if (khThread != null)
{
try {
khThread.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}

// search target keyboard event list for a match and return it otherwise return null if no match
Expand Down Expand Up @@ -228,14 +238,27 @@ private TargetKeyPress findTargetKeyPressById(int idNumber)
}
return target;
}

// clear all target keys to watch for
public static void clearKeyEvent() {
KeyboardHook.targetList.clear();
}


// add more target keys to watch for
public static void addKeyEvent(int targetKeyCode, boolean withShift, boolean withCtrl, boolean withAlt) {
KeyboardHook.targetList.add(new TargetKeyPress(KeyboardHook.targetList.size() + 1 , targetKeyCode, withShift, withCtrl, withAlt));
}

// add more target keys to watch for
public static void addKeyEvent(int targetKeyCode) {
KeyboardHook.targetList.add(new TargetKeyPress(targetKeyCode));
}


private void registerAllHotKeys() // must register hot keys in the same thread that is watching for hotkey messages
{
//System.out.println("registering hotkeys");
for (TargetKeyPress tkp : KeyboardHook.targetList) {
//BOOL WINAPI RegisterHotKey(HWND hWnd, int id, UINT fsModifiers, UINT vk);
int modifiers = User32.MOD_NOREPEAT;
Expand All @@ -256,19 +279,14 @@ private void registerAllHotKeys() // must register hot keys in the same thread t

private void unregisterAllHotKeys() // must register hot keys in the same thread that is watching for hotkey messages
{
//System.out.println("unregistering hotkeys");
for (TargetKeyPress tkp : KeyboardHook.targetList) {
if (!User32.INSTANCE.UnregisterHotKey(Pointer.NULL, tkp.idNumber))
{
System.out.println("Couldn't unregister hotkey " + tkp.targetKeyCode);
}
}
}


// add more target keys to watch for
public static void addKeyEvent(int targetKeyCode) {
KeyboardHook.targetList.add(new TargetKeyPress(targetKeyCode));
}

@Override
public void run() {
Expand All @@ -286,8 +304,9 @@ public KeyboardHook(KeyboardEvents events) {
}

public static void StartKeyboardHookThreaded(KeyboardEvents events) {
Thread t = new Thread(new KeyboardHook(events));
t.start();
quit = false;
khThread = new Thread(new KeyboardHook(events));
khThread.start();
}

/*
Expand Down
Loading

0 comments on commit eaff0dd

Please sign in to comment.