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

@@ -12,7 +12,7 @@
#include "stdafx.h"
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
LRESULT CALLBACK CwpHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
COPYDATASTRUCT CDS;
HEVENT Event;
@@ -21,43 +21,84 @@ LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
CDS.cbData = sizeof(Event);
CDS.lpData = &Event;
if (nCode == HC_ACTION) {
//if (nCode == HC_ACTION)
{
//For WH_CALLWNDPROC hook a pointer to a CWPSTRUCT structure that contains details about the message.
CWPSTRUCT *cwps = (CWPSTRUCT *)lParam;
Event.hWnd = cwps->hwnd;
Event.lParam = cwps->lParam;
Event.wParam = cwps->wParam;
Event.nCode = cwps->message;
Event.dwHookType = WH_CALLWNDPROC;
memset((void *)&Event.wParamStr, '\0', sizeof(TCHAR) * 25);
memset((void *)&Event.lParamStr, '\0', sizeof(TCHAR) * 25);
//if (cwps->message == WM_SETTEXT && cwps->lParam != 0)
// _tcscpy_s(Event.lParamStr, 25, (const wchar_t*)Event.lParam);
Event.lParam = cwps->lParam;
Event.wParam = cwps->wParam;
Event.nCode = cwps->message;
Event.dwHookType = WH_CALLWNDPROC;
BOOL bRes = SendMessage(pData->g_hWnd, WM_COPYDATA, 0, (LPARAM)(VOID*)&CDS); // ask the controlling program if the hook should be passed
BOOL bRes = (BOOL)SendMessage(pData->g_hWnd, WM_COPYDATA, 0, (LPARAM)(VOID*)&CDS); // ask the controlling program if the hook should be passed
}
return CallNextHookEx(pData->g_hHook, nCode, wParam, lParam); // pass hook to next handler
return CallNextHookEx(pData->g_CwpHook, nCode, wParam, lParam); // pass hook to next handler
//return bRes; // Don't tell the other hooks about this message.
}
extern "C" __declspec(dllexport) BOOL SetHook(HWND callerHWnd, DWORD threadId)
LRESULT CALLBACK MsgHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
COPYDATASTRUCT CDS;
HEVENT Event;
CDS.dwData = 0;
CDS.cbData = sizeof(Event);
CDS.lpData = &Event;
//if (nCode == HC_ACTION)
{
//For WH_GETMESSAGE hook a pointer to a MSG structure that contains details about the message.
MSG *msg = (MSG *)lParam;
Event.hWnd = msg->hwnd;
Event.lParam = msg->lParam;
Event.wParam = msg->wParam;
Event.nCode = msg->message;
Event.dwHookType = WH_GETMESSAGE;
memset((void *)&Event.wParamStr, '\0', sizeof(TCHAR) * 25);
memset((void *)&Event.lParamStr, '\0', sizeof(TCHAR) * 25);
//if (msg->message == WM_SETTEXT && msg->lParam != 0)
// _tcscpy_s(Event.lParamStr, 25, (const wchar_t*)Event.lParam);
//if (msg->message == WM_COMMAND || msg->message == WM_MENUCOMMAND) //infinite loop?
BOOL bRes = (BOOL)SendMessage(pData->g_hWnd, WM_COPYDATA, 0, (LPARAM)(VOID*)&CDS); // ask the controlling program if the hook should be passed
}
return CallNextHookEx(pData->g_MsgHook, nCode, wParam, lParam); // pass hook to next handler
//return bRes; // Don't tell the other hooks about this message.
}
extern "C" __declspec(dllexport) BOOL SetMsgHook(HWND callerHWnd, DWORD threadId)
{
if(bStartingProcess) // if we're just starting the DLL for the first time,
{
pData->g_hWnd = callerHWnd; // remember the windows and hook handle for further instances
pData->g_hHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)HookProc, (HINSTANCE)pData->g_hInstance, threadId);
pData->g_CwpHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)CwpHookProc, (HINSTANCE)pData->g_hInstance, threadId);
//pData->g_MsgHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)MsgHookProc, (HINSTANCE)pData->g_hInstance, threadId);
return (pData->g_hHook != NULL);
return (pData->g_CwpHook != NULL); //pData->g_CwpHook != NULL &&
}
else
return false;
else
return false;
}
extern "C" __declspec(dllexport) BOOL RemoveHook()
{
if (pData == NULL)
return false;
if(pData->g_hHook) // if the hook is defined
if(pData->g_MsgHook) // if the hook is defined
{
bool ret = UnhookWindowsHookEx(pData->g_hHook);
UnhookWindowsHookEx(pData->g_MsgHook);
pData->g_MsgHook = NULL;
}
if(pData->g_CwpHook) // if the hook is defined
{
BOOL ret = UnhookWindowsHookEx(pData->g_CwpHook);
pData->g_hWnd = NULL; // reset data
pData->g_hHook = NULL;
pData->g_CwpHook = NULL;
return ret;
}
return false;

View File

@@ -70,9 +70,11 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(JAVA_HOME)\include;$(JAVA_HOME)\include\win32;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(JAVA_HOME)\include;$(JAVA_HOME)\include\win32;$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@@ -142,6 +144,7 @@ copy /Y "$(TargetPath)" "$(ProjectDir)bin\MsgHook$(PlatformArchitecture)$(Target
<None Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="org_synthuse_MsgHook.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
@@ -161,6 +164,7 @@ copy /Y "$(TargetPath)" "$(ProjectDir)bin\MsgHook$(PlatformArchitecture)$(Target
</PrecompiledHeader>
</ClCompile>
<ClCompile Include="MsgHook.cpp" />
<ClCompile Include="org_synthuse_MsgHook.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>

View File

@@ -24,6 +24,9 @@
<ClInclude Include="targetver.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="org_synthuse_MsgHook.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
@@ -35,5 +38,8 @@
<ClCompile Include="dllmain.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="org_synthuse_MsgHook.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

Binary file not shown.

Binary file not shown.

View File

@@ -19,9 +19,10 @@ BOOL APIENTRY DllMain( HMODULE hModule,
memset((void *)&szBaseName, '\0', sizeof(TCHAR) * _MAX_FNAME);
if (GetModuleBaseName(GetCurrentProcess(), (HMODULE)hModule, szTmp, sizeof(szTmp)))// compute MMF-filename from current module base name, uses Psapi
_wsplitpath(szTmp, NULL, NULL, szBaseName, NULL);
_wsplitpath_s(szTmp, NULL, NULL, szBaseName, _MAX_FNAME, NULL, NULL, NULL, NULL);
//_wsplitpath(szTmp, NULL, NULL, szBaseName, NULL);
wcscat(szBaseName, TEXT("MsgHookSharedMem")); // add specifier string
wcscat_s(szBaseName, TEXT("MsgHookSharedMem")); // add specifier string
hMappedFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(GLOBALDATA), szBaseName);
pData = (GLOBALDATA*)MapViewOfFile(hMappedFile, FILE_MAP_WRITE, 0, 0, 0);
@@ -29,9 +30,10 @@ BOOL APIENTRY DllMain( HMODULE hModule,
if(bStartingProcess) // if the MMF doesn't exist, we have the first instance
{
pData->g_hInstance = hModule; // so set the instance handle
pData->g_hWnd = NULL; // and initialize the other handles
pData->g_hHook = NULL;
pData->g_hInstance = hModule; // so set the instance handle
pData->g_hWnd = NULL; // and initialize the other handles
pData->g_CwpHook = NULL;
pData->g_MsgHook = NULL;
}
DisableThreadLibraryCalls((HMODULE)hModule);
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2014, Synthuse.org
* Released under the Apache Version 2.0 License.
*
* last modified by ejakubowski7@gmail.com
*/
#include "stdafx.h"
#include "org_synthuse_MsgHook.h"
/*
* Class: org_synthuse_MsgHook
* Method: setMessageHook
* Signature: (JJ)Z
*/
JNIEXPORT jboolean JNICALL Java_org_synthuse_MsgHook_setMessageHook(JNIEnv *env, jobject obj, jlong jhWnd, jlong jthreadId)
{
return SetMsgHook((HWND)jhWnd, (DWORD)jthreadId);
}
/*
* Class: org_synthuse_MsgHook
* Method: removeMessageHook
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_synthuse_MsgHook_removeMessageHook(JNIEnv *env, jobject obj)
{
return RemoveHook();
}

View File

@@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_synthuse_MsgHook */
#ifndef _Included_org_synthuse_MsgHook
#define _Included_org_synthuse_MsgHook
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_synthuse_MsgHook
* Method: setMessageHook
* Signature: (JJ)Z
*/
JNIEXPORT jboolean JNICALL Java_org_synthuse_MsgHook_setMessageHook
(JNIEnv *, jobject, jlong, jlong);
/*
* Class: org_synthuse_MsgHook
* Method: removeMessageHook
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_org_synthuse_MsgHook_removeMessageHook
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -10,6 +10,7 @@
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
#include <tchar.h>
#include <Psapi.h>
#include <stdlib.h>
@@ -19,15 +20,20 @@
typedef struct
{
HWND hWnd;
int nCode;
DWORD dwHookType;
WPARAM wParam;
LPARAM lParam;
TCHAR wParamStr[25];
TCHAR lParamStr[25];
}HEVENT;
typedef struct
{
HHOOK g_hHook;
HHOOK g_CwpHook;
HHOOK g_MsgHook;
//HHOOK g_hHook;
HWND g_hWnd;
HANDLE g_hInstance;
}GLOBALDATA;
@@ -35,6 +41,9 @@ typedef struct
#ifndef GLOBAL_VARS_H // header guards
#define GLOBAL_VARS_H
extern "C" __declspec(dllexport) BOOL SetMsgHook(HWND callerHWnd, DWORD threadId);
extern "C" __declspec(dllexport) BOOL RemoveHook();
//Global variables , remember not to initialize here
extern HANDLE hMappedFile;
extern GLOBALDATA* pData;