<span style="color:#000000"><code>// SpyDemo.cpp : 定义控制台应用程序的入口点。
//
<span style="color:#880000 !important"><em>#include "stdafx.h"</em></span>
<span style="color:#880000 !important"><em>#include <Windows.h></em></span>
<span style="color:#880000 !important"><em>#define MAX_TEXT_LEN 255</em></span>
BOOL CALLBACK EnumChildProcess(HWND hwnd, LPARAM lParam)
{
<span style="color:#000088 !important">if</span> (hwnd == NULL) {
<span style="color:#000088 !important">return</span> FALSE;
}
BOOL ret;
RECT rect;
ret = GetWindowRect(hwnd, &rect);
<span style="color:#000088 !important">if</span> (!ret) {
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetWindowRect hwnd=<span style="color:#4f4f4f !important">%p</span> -> fail(<span style="color:#4f4f4f !important">%ld</span>)\n"</span>, hwnd, GetLastError());
}
<span style="color:#000088 !important">else</span> {
<span style="color:#008800 !important">//printf</span>(<span style="color:#009900 !important">"GetWindowRect hwnd = <span style="color:#4f4f4f !important">%p</span> -> rect=(left=<span style="color:#4f4f4f !important">%ld</span>, top=<span style="color:#4f4f4f !important">%ld</span>, right=<span style="color:#4f4f4f !important">%ld</span>, bottom=<span style="color:#4f4f4f !important">%ld</span>)\n"</span>, hwnd, rect.left, rect.top, rect.right, rect.bottom);
ret = PtInRect(&rect, <span style="color:#4f4f4f !important">*(</span>POINT <span style="color:#4f4f4f !important">*)</span>lParam);
<span style="color:#000088 !important">if</span> (ret) {
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetWindowRect hwnd = <span style="color:#4f4f4f !important">%p</span> -> rect=(left=<span style="color:#4f4f4f !important">%ld</span>, top=<span style="color:#4f4f4f !important">%ld</span>, right=<span style="color:#4f4f4f !important">%ld</span>, bottom=<span style="color:#4f4f4f !important">%ld</span>)\n"</span>, hwnd, rect.left, rect.top, rect.right, rect.bottom);
<span style="color:#008800 !important">//printf</span>(<span style="color:#009900 !important">"PtInRect\n"</span>);
<span style="color:#008800 !important">/*
WINUSERAPI int WINAPI GetWindowText(
_In_ HWND hWnd,
_Out_writes_(nMaxCount) LPTSTR lpString, /</span><span style="color:#008800 !important">/可能是标题名或者file:/</span><span style="color:#008800 !important">//</span>打头的文件完整路径
_In<span style="color:#006666 !important">_</span> <span style="color:#000088 !important">int</span> nMaxCount
);
如果函数成功,返回值是拷贝的字符串的字符个数,不包括中断的空字符;如果窗口无标题栏或文本,或标题栏为空,或窗口或控制的句柄无效,则返回值为零。若想获得更多错误信息,请调用GetLastError函数。
<span style="color:#4f4f4f !important">*/</span>
TCHAR windowText[MAX_TEXT_LEN];
<span style="color:#000088 !important">int</span> lenRet = GetWindowText(hwnd, windowText, MAX_TEXT_LEN);
<span style="color:#000088 !important">if</span> (lenRet == <span style="color:#006666 !important">0</span> && GetLastError() != <span style="color:#006666 !important">0</span>) {
<span style="color:#008800 !important">//</span>GetLastError()〖<span style="color:#006666 !important">0</span>〗-操作成功完成
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetWindowText hwnd=<span style="color:#4f4f4f !important">%p</span> -> fail(<span style="color:#4f4f4f !important">%ld</span>)\n"</span>, hwnd, GetLastError());
}
<span style="color:#000088 !important">else</span> {
_tprintf(_T(<span style="color:#009900 !important">"GetWindowText hwnd=<span style="color:#4f4f4f !important">%p</span> -> windowText=<span style="color:#4f4f4f !important">%s</span>, lenRet=<span style="color:#4f4f4f !important">%d</span>\n"</span>), hwnd, windowText, lenRet);
}
/*
WINUSERAPI <span style="color:#000088 !important">int</span> WINAPI GetClassNameW(
_In<span style="color:#006666 !important">_</span> HWND hWnd,
_Out_writes_to<span style="color:#006666 !important">_</span>(nMaxCount, <span style="color:#000088 !important">return</span>) LPWSTR lpClassName,
_In<span style="color:#006666 !important">_</span> <span style="color:#000088 !important">int</span> nMaxCount
);
如果函数成功,返回值为拷贝到指定缓冲区的字符个数:如果函数失败,返回值为<span style="color:#006666 !important">0</span>。若想获得更多错误信息,请调用GetLastError函数。
<span style="color:#4f4f4f !important">*/</span>
TCHAR className[MAX_TEXT_LEN];
lenRet = GetClassName(hwnd, className, MAX_TEXT_LEN);
<span style="color:#000088 !important">if</span> (lenRet == <span style="color:#006666 !important">0</span>) {
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetClassName hwnd=<span style="color:#4f4f4f !important">%p</span> -> fail(<span style="color:#4f4f4f !important">%ld</span>)\n"</span>, hwnd, GetLastError());
}
<span style="color:#000088 !important">else</span> {
_tprintf(_T(<span style="color:#009900 !important">"GetClassName hwnd=<span style="color:#4f4f4f !important">%p</span> -> className=<span style="color:#4f4f4f !important">%s</span>, lenRet=<span style="color:#4f4f4f !important">%d</span>\n"</span>), hwnd, className, lenRet);
}
/*
找出某个窗口的创建者(线程或进程),返回创建者的标志符
哪个线程创建了这个窗口,返回的就是这个线程的id号 (进程只有一个线程的话,那么线程标志符与进程标志符就是指同一个标志符)
WINUSERAPI DWORD WINAPI GetWindowThreadProcessId(
_In<span style="color:#006666 !important">_</span> HWND hWnd,
_Out_opt<span style="color:#006666 !important">_</span> LPDWORD lpdwProcessId //进程号的存放地址(变量地址)
);
返回线程号
<span style="color:#4f4f4f !important">*/</span>
DWORD dwProcessId;
DWORD dwThreadId = GetWindowThreadProcessId(hwnd, &dwProcessId);
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetWindowThreadProcessId hwnd=<span style="color:#4f4f4f !important">%p</span> -> processId=<span style="color:#4f4f4f !important">%ld</span>, threadId=<span style="color:#4f4f4f !important">%ld</span>\n"</span>, hwnd, dwProcessId, dwThreadId);
<span style="color:#008800 !important">/*
WINUSERAPI UINT WINAPI GetWindowModuleFileName(
_In_ HWND hwnd,
_Out_writes_to_(cchFileNameMax, return) LPTSTR pszFileName, /</span><span style="color:#008800 !important">/模块完整路径
_In_ UINT cchFileNameMax
);
返回值是复制到缓冲区的字符总数。
*/</span>
TCHAR fileName[MAX_PATH];
lenRet = GetWindowModuleFileName(hwnd, fileName, MAX_PATH);
<span style="color:#000088 !important">if</span> (lenRet == <span style="color:#006666 !important">0</span>) {
<span style="color:#008800 !important">//</span>错误码〖<span style="color:#006666 !important">126</span>〗-找不到指定的模块。
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetWindowModuleFileName hwnd=<span style="color:#4f4f4f !important">%p</span> -> fail(<span style="color:#4f4f4f !important">%ld</span>)\n"</span>, hwnd, GetLastError());
} <span style="color:#000088 !important">else</span> {
_tprintf(_T(<span style="color:#009900 !important">"GetWindowModuleFileName hwnd=<span style="color:#4f4f4f !important">%p</span> -> fileName=<span style="color:#4f4f4f !important">%s</span>\n"</span>), hwnd, fileName);
}
/*
WINUSERAPI BOOL WINAPI GetWindowInfo(
_In<span style="color:#006666 !important">_</span> HWND hwnd,
_Inout<span style="color:#006666 !important">_</span> PWINDOWINFO pwi
);
typedef struct tagWINDOWINFO
{
DWORD cbSize;
RECT rcWindow;
RECT rcClient;
DWORD dwStyle;
DWORD dwExStyle;
DWORD dwWindowStatus;
UINT cxWindowBorders;
UINT cyWindowBorders;
ATOM atomWindowType;
WORD wCreatorVersion;
} WINDOWINFO, <span style="color:#4f4f4f !important">*PWINDOWINFO</span>, <span style="color:#4f4f4f !important">*LPWINDOWINFO</span>;
<span style="color:#4f4f4f !important">*/</span>
WINDOWINFO windowInfo;
windowInfo.cbSize = sizeof(WINDOWINFO);
ret = GetWindowInfo(hwnd, &windowInfo);
<span style="color:#000088 !important">if</span> (!ret) {
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetWindowInfo hwnd=<span style="color:#4f4f4f !important">%p</span> -> fail(<span style="color:#4f4f4f !important">%ld</span>)\n"</span>, hwnd, GetLastError());
}
<span style="color:#000088 !important">else</span> {
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetWindowInfo hwnd=<span style="color:#4f4f4f !important">%p</span> -> dwStyle=<span style="color:#4f4f4f !important">%ld</span>, dwExStyle=<span style="color:#4f4f4f !important">%ld</span>, dwWindowStatus=<span style="color:#4f4f4f !important">%ld</span>, cxWindowBorders=<span style="color:#4f4f4f !important">%d</span>, cyWindowBorders=<span style="color:#4f4f4f !important">%d</span>, wCreatorVersion=<span style="color:#4f4f4f !important">%d</span>\n"</span>, hwnd, windowInfo.dwStyle, windowInfo.dwExStyle, windowInfo.dwWindowStatus, windowInfo.cxWindowBorders, windowInfo.cyWindowBorders, windowInfo.wCreatorVersion);
}
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"\n"</span>);
}
}
<span style="color:#000088 !important">return</span> TRUE;
}
<span style="color:#000088 !important">int</span> main()
{
BOOL ret;
<span style="color:#000088 !important">while</span> (true) {
<span style="color:#008800 !important">/*
typedef struct tagPOINT
{
LONG x;
LONG y;
} POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT;
*/</span>
POINT point;
ret = GetCursorPos(&point);
<span style="color:#000088 !important">if</span> (!ret) {
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetCursorPos -> fail(<span style="color:#4f4f4f !important">%ld</span>)\n"</span>, GetLastError());
}
<span style="color:#000088 !important">else</span> {
<span style="color:#000088 !important">printf</span>(<span style="color:#009900 !important">"GetCursorPos -> (<span style="color:#4f4f4f !important">%ld</span>, <span style="color:#4f4f4f !important">%ld</span>)\n"</span>, point.<span style="color:#000088 !important">x</span>, point.<span style="color:#000088 !important">y</span>);
<span style="color:#008800 !important">//</span>获取桌面句柄
HWND desktopHwnd = GetDesktopWindow();
<span style="color:#008800 !important">/*
BOOL EnumChildWindows(
HWND hWndParent, /</span><span style="color:#008800 !important">/ handle to parent window /</span><span style="color:#008800 !important">/ 父窗口句柄
WNDENUMPROC lpEnumFunc, /</span><span style="color:#008800 !important">/ callback function /</span><span style="color:#008800 !important">/ 回调函数的地址
LPARAM lParam /</span><span style="color:#008800 !important">/ application-defined value /</span><span style="color:#008800 !important">/ 你自已定义的参数
);
直到调用到最个一个子窗口被枚举或回调函数返回一个false,否则将一直自动枚举下去。
*/</span>
ret = EnumChildWindows(desktopHwnd, EnumChildProcess, (LPARAM)&point);
}
/*
WINBASEAPI VOID WINAPI Sleep(
_In<span style="color:#006666 !important">_</span> DWORD dwMilliseconds
);
Sleep会将线程挂起,把CPU让给其它线程,单位是毫秒
<span style="color:#4f4f4f !important">*/</span>
Sleep(<span style="color:#006666 !important">20000</span>);
}
<span style="color:#000088 !important">system</span>(<span style="color:#009900 !important">"pause"</span>);
<span style="color:#000088 !important">return</span> <span style="color:#006666 !important">0</span>;
}</code></span>
在评论里有人说道,可以用“WindowFromPoint”来实现这个功能,于是就有了下列代码。
<span style="color:#000000"><code><span style="color:#008800 !important">//</span> SpyDemo.cpp : Defines the entry point <span style="color:#000088 !important">for</span> the <span style="color:#4f4f4f !important">console</span> application.
<span style="color:#008800 !important">//</span>
<span style="color:#880000 !important"><em>#include "stdafx.h"</em></span>
<span style="color:#880000 !important"><em>#include <Windows.h></em></span>
int main()
{
BOOL ret;
UINT count;
/*
typedef struct tagPOINT
{
LONG x;
LONG y;
} POINT, *PPOINT, NEAR *NPPOINT, FAR *LPPOINT;
*/
POINT point;
TCHAR pszFileName[MAX_PATH];
TCHAR lpClassName[MAX_PATH];
TCHAR windowText[MAX_PATH];
<span style="color:#000088 !important">while</span>(<span style="color:#006666 !important">true</span>) {
ret = GetCursorPos(&point);
<span style="color:#000088 !important">if</span> (!ret) {
printf(<span style="color:#009900 !important">"GetCursorPos -> fail(%ld)\n"</span>, GetLastError());
} <span style="color:#000088 !important">else</span> {
printf(<span style="color:#009900 !important">"GetCursorPos -> (%ld, %ld)\n"</span>, point.x, point.y);
HWND hwnd = WindowFromPoint(point);
<span style="color:#000088 !important">if</span> (hwnd == NULL || hwnd == INVALID_HANDLE_VALUE) {
printf<span style="color:#4f4f4f !important">(<span style="color:#009900 !important">"WindowFromPoint point=(%ld, %ld) -> hwnd=%p -> fail(%ld)\n"</span>, point.x, point.y, hwnd, GetLastError())</span>;
}
<span style="color:#009900 !important">else</span> {
<span style="color:#009900 !important">printf</span><span style="color:#4f4f4f !important">(<span style="color:#009900 !important">"WindowFromPoint -> hwnd=%p\n"</span>, hwnd)</span>;
/*
检索与指定的窗口句柄关联的模块的完整路径和文件名称
<span style="color:#009900 !important">WINUSERAPI</span> <span style="color:#009900 !important">UINT</span> <span style="color:#009900 !important">WINAPI</span> <span style="color:#009900 !important">GetWindowModuleFileNameW</span><span style="color:#4f4f4f !important">(
_In_ HWND hwnd,
_Out_writes_to_(cchFileNameMax, <span style="color:#000088 !important">return</span>) LPWSTR pszFileName,
_In_ UINT cchFileNameMax
)</span>;
返回值是复制到缓冲区的字符总数。
*/
<span style="color:#009900 !important">count</span> = <span style="color:#009900 !important">GetWindowModuleFileName</span><span style="color:#4f4f4f !important">(hwnd, pszFileName, MAX_PATH)</span>;
<span style="color:#009900 !important">_tprintf</span><span style="color:#4f4f4f !important">(_T(<span style="color:#009900 !important">"GetWindowModuleFileName hwnd=%p -> count=%d, fileName=%s\n"</span>), hwnd, count, pszFileName)</span>;
/*
<span style="color:#009900 !important">WINUSERAPI</span> <span style="color:#009900 !important">int</span> <span style="color:#009900 !important">WINAPI</span> <span style="color:#009900 !important">GetClassName</span><span style="color:#4f4f4f !important">(
_In_ HWND hWnd,
_Out_writes_to_(nMaxCount, <span style="color:#000088 !important">return</span>) LPTSTR lpClassName,
_In_ int nMaxCount
)</span>;
*/
<span style="color:#009900 !important">count</span> = <span style="color:#009900 !important">GetClassName</span><span style="color:#4f4f4f !important">(hwnd, lpClassName, MAX_PATH)</span>;
<span style="color:#009900 !important">_tprintf</span><span style="color:#4f4f4f !important">(_T(<span style="color:#009900 !important">"GetClassName hwnd=%p -> count=%d, lpClassName=%s\n"</span>), hwnd, count, lpClassName)</span>;
<span style="color:#009900 !important">count</span> = <span style="color:#009900 !important">GetWindowText</span><span style="color:#4f4f4f !important">(hwnd, windowText, MAX_PATH)</span>;
<span style="color:#009900 !important">_tprintf</span><span style="color:#4f4f4f !important">(_T(<span style="color:#009900 !important">"GetWindowText hwnd=%p -> count=%d, lpClassName=%s\n"</span>), hwnd, count, windowText)</span>;
}
//<span style="color:#009900 !important">Screen</span>(屏幕坐标)到<span style="color:#009900 !important">Client</span>(客户区坐标)的转换。
<span style="color:#009900 !important">ret</span> = <span style="color:#009900 !important">ScreenToClient</span><span style="color:#4f4f4f !important">(hwnd, &point)</span>;
<span style="color:#009900 !important">if</span> <span style="color:#4f4f4f !important">(!ret)</span> {
<span style="color:#009900 !important">printf</span><span style="color:#4f4f4f !important">(<span style="color:#009900 !important">"ScreenToClient hwnd=%p -> fail(%ld)\n"</span>, hwnd, GetLastError())</span>;
}
<span style="color:#009900 !important">else</span> {
<span style="color:#009900 !important">printf</span><span style="color:#4f4f4f !important">(<span style="color:#009900 !important">"ScreenToClient hwnd=%p -> point=(%ld, %ld)\n"</span>, hwnd, point.x, point.y)</span>;
//返回父窗口中包含了指定点的第一个子窗口的句柄
<span style="color:#009900 !important">HWND</span> <span style="color:#009900 !important">childHwnd</span> = <span style="color:#009900 !important">ChildWindowFromPoint</span><span style="color:#4f4f4f !important">(hwnd, point)</span>;
<span style="color:#009900 !important">printf</span><span style="color:#4f4f4f !important">(<span style="color:#009900 !important">"ChildWindowFromPoint hwnd=%p, point=(%ld, %ld) -> childHwnd=%p\n"</span>, hwnd, point.x, point.y, childHwnd)</span>;
<span style="color:#009900 !important">count</span> = <span style="color:#009900 !important">GetClassName</span><span style="color:#4f4f4f !important">(hwnd, lpClassName, MAX_PATH)</span>;
<span style="color:#009900 !important">_tprintf</span><span style="color:#4f4f4f !important">(_T(<span style="color:#009900 !important">"GetClassName hwnd=%p -> count=%d, lpClassName=%s\n"</span>), hwnd, count, lpClassName)</span>;
<span style="color:#009900 !important">count</span> = <span style="color:#009900 !important">GetWindowText</span><span style="color:#4f4f4f !important">(hwnd, windowText, MAX_PATH)</span>;
<span style="color:#009900 !important">_tprintf</span><span style="color:#4f4f4f !important">(_T(<span style="color:#009900 !important">"GetWindowText hwnd=%p -> count=%d, lpClassName=%s\n"</span>), hwnd, count, windowText)</span>;
}
}
<span style="color:#009900 !important">Sleep</span><span style="color:#4f4f4f !important">(<span style="color:#006666 !important">5000</span>)</span>;
}
<span style="color:#009900 !important">return</span> 0;
}</code></span>
但是有一个问题:
为什么 GetWindowModuleFileName 虽然句柄每次都不一样,但是获取到的模块文件路径每次都是一样的当前的路径。
希望有大神能帮我解答一下,感激不尽。
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- baomayou.com 版权所有 赣ICP备2024042794号-6
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务