0

I would like to track various input interactions (Pen, Touch and Mouse) of users with a Microsoft Whiteboard application. Is there a way without hooking the application and using WM_POINTER Messages? (Because for some reason i can not make this work) Programming language is not really important for me.

DLL Code:

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
#pragma data_seg("Shared")
#pragma data_seg()
#pragma comment(linker,"/section:Shared,rws")

#include <windows.h>
#include <stdio.h>

HHOOK global;

extern "C" __declspec(dllexport) LRESULT WINAPI procedure(int nCode, WPARAM wParam, LPARAM lParam) {
    if (nCode == HC_ACTION) {
        //lets extract the data
        CWPSTRUCT* data = (CWPSTRUCT*)lParam;
        if (data->message == WM_CLOSE) {
            //lets get the name of the program closed
            char name[260];
            GetWindowModuleFileNameA(data->hwnd, name, 260);
            //extract only the exe from the path
            char* extract = (char*)((DWORD)name + lstrlenA(name) - 1);
            while (*extract != '\\')
                extract--;
            extract++;
            MessageBoxA(0, "A program has been closed", extract, 0);
        }
    }
    return CallNextHookEx(global, nCode, wParam, lParam);
}

Python Code(Which should receive data like when and where it was touched from dll):

import ctypes
import os
from ctypes import *
from ctypes.wintypes import *

user32 = WinDLL('user32', use_last_error=True)
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

user32.SetWindowsHookExA.errcheck = errcheck_bool
user32.SetWindowsHookExA.restype = HHOOK
user32.SetWindowsHookExA.argtypes = (c_int,     # _In_ idHook
                                     HOOKPROC,  # _In_ lpfn
                                     HINSTANCE, # _In_ hMod
                                     DWORD)     # _In_ dwThreadId

user32.CallNextHookEx.restype = LRESULT
user32.CallNextHookEx.argtypes = (HHOOK,  # _In_opt_ hhk
                                  c_int,  # _In_     nCode
                                  WPARAM, # _In_     wParam
                                  LPARAM) # _In_     lParam

user32.GetMessageW.argtypes = (LPMSG, # _Out_    lpMsg
                               HWND,  # _In_opt_ hWnd
                               UINT,  # _In_     wMsgFilterMin
                               UINT)  # _In_     wMsgFilterMax

user32.TranslateMessage.argtypes = (LPMSG,)
user32.DispatchMessageW.argtypes = (LPMSG,)

GetModuleHandle = ctypes.windll.kernel32.GetModuleHandleA
GetModuleHandle.restype = POINTER(c_void_p)

#LoadLibrary = ctypes.windll.kernel32.LoadLibraryA
#LoadLibrary.argtypes = [c_wchar_p]
#LoadLibrary.restype = c_int

GetProcAddress = ctypes.windll.kernel32.GetProcAddress
GetProcAddress.restype = HOOKPROC

def pointer_msg_loop():
            dll_name = 'Dll.dll'
            dll_abspath = os.path.abspath(os.path.join(os.path.dirname(__file__), '.', dll_name))
            print(dll_abspath)
            lib = kernel32.LoadLibraryA(dll_abspath.encode(encoding='ascii'))
            handle = GetModuleHandle(dll_abspath.encode(encoding='ascii'))
            print(lib)
            print(handle)
            procedure = GetProcAddress(handle, "procedure")
            print(procedure)
            if (procedure):
                print('correct value procedure')
                tHook = user32.SetWindowsHookExW(WH_CALLWNDPROC, procedure, lib, 0)
                print(ctypes.WinError(GetLastError()))
                print(tHook)
                print(ctypes.WinError(GetLastError()))
                msg = MSG()
                while True:
                    bRet = user32.GetMessageW(byref(msg), None, 0, 0)
                    if not bRet:
                        break
                    if bRet == -1:
                        raise WinError(get_last_error())
                    user32.TranslateMessage(byref(msg))
                    user32.DispatchMessageW(byref(msg))

1: Added a working example, which i want to transform to listen to my Microsoft Whiteboard window. 2: Added a dll solution from another example (Does not work)

  • What's the reason you are preemptively rejecting the solution? – IInspectable Feb 13 '20 at 16:01
  • I tried injecting a dll and its not really what i want. I actually have a working example with a local hook now, but i struggle passing the target window to the SetWindowLong function. My goal is to hook into the Microsoft Whiteboard application to catch the WM_POINTER family. – Oliver Braun Feb 13 '20 at 18:56
  • What you have shown is not a hook. It's an attempt at [subclassing controls](https://learn.microsoft.com/en-us/windows/win32/controls/subclassing-overview), using a technique that has gone out of fashion decades ago. The term [hook](https://learn.microsoft.com/en-us/windows/win32/winmsg/hooks) in Windows means something different. – IInspectable Feb 13 '20 at 23:39
  • So what would be the correct approach in this situation? I dont want to inject a dll though – Oliver Braun Feb 14 '20 at 07:43
  • Again, why do you reject the solution? If you don't want to inject a DLL, you aren't going to be monitoring input messages. It's really that simple. – IInspectable Feb 14 '20 at 07:59
  • Because i have to write the callback in the dll and i need to get data like position and time from these messages into my python code so i can continue working with it. – Oliver Braun Feb 14 '20 at 08:07
  • If you need to communicate between processes, use one of the [Interprocess communication](https://learn.microsoft.com/en-us/windows/win32/ipc/interprocess-communications) facilities. Since we already established that you *must* inject a DLL into the target process, that's not really an *"if"*. – IInspectable Feb 14 '20 at 08:38
  • I changed my example to a Dll solution. Would you mind looking over it and telling me whats wrong with it, also what would be the best to use for me in terms of communication between dll and python app – Oliver Braun Feb 14 '20 at 09:06

0 Answers0