是否可以全局挂接窗口的创建,以便我可以控制窗口在屏幕上的放置位置? [英] Is it possible to hook the creation of windows globally so I can control where the windows are placed on the screen?

查看:47
本文介绍了是否可以全局挂接窗口的创建,以便我可以控制窗口在屏幕上的放置位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将钩子插入正在运行的进程中,以捕获它们创建,销毁时的最大值/最小值.但是我还没有想出一种方法来赶上新流程的创建,因此我无法将自己的想法注入其中.有谁知道实现此目标的最佳方法?

I can inject a hook into running processes to catch when they create, destroy, max/min. But I haven't come up with a way to catch the creation of a new process so that I can inject my hook into that one. Does anyone know the best way to accomplish this?

推荐答案

SetWindowsHookEx 是您最简单的解决方案.

SetWindowsHookEx is your easiest solution.

如果您不介意安装防病毒软件,则还可以将DLL注入每个进程,然后将它们钩住CreateProcess(将DLL注入其他进程)和CreateWindowEx(出于您的目的).

If you don't mind upsetting the anti-virus software, you can also inject a DLL into each process that will then hook CreateProcess (to inject the DLL into further processes) and CreateWindowEx (for your purposes).

我只是完整地阅读了您的问题.是的,您只需要钩上CreateProcessW并将钩子注入到将来的进程中即可.

I just read your question completely. Yes, you'll want to just hook CreateProcessW and inject your hook into future processes.

编辑#2:昨天我实际上正在做类似这样的事情,所以有一些代码可以满足您的需求.

EDIT #2: I was actually working on something like this yesterday, so some code which does what you want.

#include <windows.h>

// call GetModuleFileNameto get the full path of the module before installing the hook
static LPWSTR lpszDllName;

HMODULE LoadModuleEx(__in HANDLE hProcess, __in_z LPCTSTR lpcszDll)
{
  DWORD   cdwSize;
  LPVOID  lpvAllocation;
  HANDLE  hThread;
  HMODULE hRet;

  cdwSize = lstrlen(lpcszDll) + 1;
  cdwSize *= sizeof(TCHAR);

  lpvAllocation = VirtualAllocEx(hProcess, NULL, cdwSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  if (lpvAllocation != NULL)
  {
    if (WriteProcessMemory(hProcess, lpvAllocation, lpcszDll, cdwSize, NULL))
    {
      hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibrary, lpvAllocation, 0, NULL);
      if (hThread != NULL)
      {
        GetExitCodeThread(hThread, (LPDWORD)&hRet);
        CloseHandle(hThread);
      }
    }
    VirtualFreeEx(hProcess, lpvAllocation, cdwSize, MEM_DECOMMIT);
  }
  return hRet;
}

// hook future process creation - install this hook on top of CreateProcessW
// I'd suggest using Microsoft Detours [http://research.microsoft.com/en-us/projects/detours/]
BOOL WINAPI CreateProcessWHook(__in_opt LPCWSTR lpApplicationName, __inout_opt LPWSTR lpCommandLine, __in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes, __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, __in BOOL bInheritHandles, __in DWORD dwCreationFlags, __in_opt LPVOID lpEnvironment, __in_opt LPCWSTR lpCurrentDirectory, __in LPSTARTUPINFO lpStartupInfo, __out LPPROCESS_INFORMATION lpProcessInformation)
{
  // create the process suspended
  if (dwCreationFlags & CREATE_SUSPENDED != CREATE_SUSPENDED)
    dwCreationFlags |= CREATE_SUSPENDED;

  // call original CreateProcessW
  BOOL bRet = _CreateProcessW(lpApplicationName, lpCommandLine, lpProcessAttributes, lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
  if (bRet)
  {
    // inject DLL
    LoadModuleEx(lpProcessInformation->hProcess, lpszDllName);

    // resume thread
    ResumeThread(lpProcessInformation->hThread);
  }

  return bRet;
}

这篇关于是否可以全局挂接窗口的创建,以便我可以控制窗口在屏幕上的放置位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆