1
2
3
4 package org.sirius.server.win32;
5
6 import javax.jws.WebService;
7
8 import org.sirius.server.win32.classes.Common;
9 import org.sirius.server.win32.core.User32Ext;
10
11 import com.sun.jna.Pointer;
12 import com.sun.jna.platform.win32.WinDef.HWND;
13 import com.sun.jna.platform.win32.WinUser;
14 import com.sun.jna.ptr.IntByReference;
15
16
17
18
19
20 @WebService
21 public class Win32Utils extends Common {
22
23
24
25
26
27
28 public class WNDENUMPROC implements WinUser.WNDENUMPROC {
29
30
31
32
33 private final int maxLength = 128;
34
35
36
37
38 private int currIndex;
39
40
41
42 private final Win32Locator locator;
43
44
45
46
47
48 public WNDENUMPROC(final Win32Locator locatorVal) {
49 this.locator = locatorVal;
50 currIndex = 0;
51 }
52
53
54
55
56
57
58
59 @Override
60 public final boolean callback(final HWND arg0, final Pointer arg1) {
61 User32Ext user32 = User32Ext.INSTANCE;
62 int length = user32.GetWindowTextLength(arg0) + 1;
63 char[] buf = new char[length];
64
65 user32.GetWindowText(arg0, buf, length);
66 String text = String.valueOf(buf).trim();
67
68 if (!text.matches(locator.getCaption())) {
69 return true;
70 }
71
72 buf = null;
73 buf = new char[maxLength];
74
75 user32.GetClassName(arg0, buf, maxLength);
76 String clazz = String.valueOf(buf).trim();
77
78 if (!clazz.matches(locator.getWinClass())) {
79 return true;
80 }
81
82 if (currIndex < locator.getIndex()) {
83 currIndex++;
84 } else {
85 locator.setHwnd(arg0);
86 }
87
88 if (locator.getHwnd() == 0) {
89 return true;
90 } else if (!user32.IsWindow(longToHwnd(locator.getHwnd()))) {
91 locator.setHwnd(0L);
92 return true;
93 }
94
95 buf = null;
96 locator.setCaption(text);
97 locator.setWinClass(clazz);
98
99 return false;
100 }
101
102
103
104
105 public final Win32Locator getLocator() {
106 return locator;
107 }
108 }
109
110
111
112
113 public Win32Utils() {
114 }
115
116
117
118
119
120
121
122 public final long searchSameThreadWindow(final long baseHwnd,
123 final Win32Locator locator) {
124 User32Ext user32 = User32Ext.INSTANCE;
125
126 HWND hWnd = new HWND();
127 hWnd.setPointer(Pointer.createConstant(baseHwnd));
128
129 IntByReference lpdwProcessId = new IntByReference();
130 int threadID = user32.GetWindowThreadProcessId(hWnd, lpdwProcessId);
131
132 Pointer pt = Pointer.NULL;
133 locator.setHwnd(0L);
134
135 WNDENUMPROC enumProc = new WNDENUMPROC(locator);
136 user32.EnumThreadWindows(threadID, enumProc, pt);
137
138 return enumProc.getLocator().getHwnd();
139 }
140
141
142
143
144
145
146 public final long searchWindow(final Win32Locator locator) {
147 User32Ext user32 = User32Ext.INSTANCE;
148
149 locator.setHwnd(0L);
150 WNDENUMPROC enumProc = new WNDENUMPROC(locator);
151 Pointer pt = Pointer.NULL;
152
153 if (locator.getParent() == 0L) {
154 user32.EnumWindows(enumProc, pt);
155 } else {
156 HWND hWnd = new HWND();
157 hWnd.setPointer(Pointer.createConstant(locator.getParent()));
158 user32.EnumChildWindows(hWnd, enumProc, pt);
159 }
160 return enumProc.getLocator().getHwnd();
161 }
162 }