https://blog.csdn.net/chy555chy/article/details/52900785
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
在评论里有人说道,可以用“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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
但是有一个问题:
为什么 GetWindowModuleFileName 虽然句柄每次都不一样,但是获取到的模块文件路径每次都是一样的当前的路径。
希望有大神能帮我解答一下,感激不尽。