-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
WTypes.java
332 lines (282 loc) · 9.56 KB
/
WTypes.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Copyright (c) 2010 Timothy Wall, All Rights Reserved
* Copyright 2010 Digital Rapids Corp.
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna.platform.win32;
import java.io.UnsupportedEncodingException;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.PointerType;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef.USHORT;
import com.sun.jna.ptr.ByReference;
/**
* Constant defined in WTypes.h
*
* @author scott.palmer
* @author Tobias Wolf, wolf.tobias@gmx.net
*/
public interface WTypes {
public static int CLSCTX_INPROC_SERVER = 0x1;
public static int CLSCTX_INPROC_HANDLER = 0x2;
public static int CLSCTX_LOCAL_SERVER = 0x4;
public static int CLSCTX_INPROC_SERVER16 = 0x8;
public static int CLSCTX_REMOTE_SERVER = 0x10;
public static int CLSCTX_INPROC_HANDLER16 = 0x20;
public static int CLSCTX_RESERVED1 = 0x40;
public static int CLSCTX_RESERVED2 = 0x80;
public static int CLSCTX_RESERVED3 = 0x100;
public static int CLSCTX_RESERVED4 = 0x200;
public static int CLSCTX_NO_CODE_DOWNLOAD = 0x400;
public static int CLSCTX_RESERVED5 = 0x800;
public static int CLSCTX_NO_CUSTOM_MARSHAL = 0x1000;
public static int CLSCTX_ENABLE_CODE_DOWNLOAD = 0x2000;
public static int CLSCTX_NO_FAILURE_LOG = 0x4000;
public static int CLSCTX_DISABLE_AAA = 0x8000;
public static int CLSCTX_ENABLE_AAA = 0x10000;
public static int CLSCTX_FROM_DEFAULT_CONTEXT = 0x20000;
public static int CLSCTX_ACTIVATE_32_BIT_SERVER = 0x40000;
public static int CLSCTX_ACTIVATE_64_BIT_SERVER = 0x80000;
public static int CLSCTX_ENABLE_CLOAKING = 0x100000;
public static int CLSCTX_APPCONTAINER = 0x400000;
public static int CLSCTX_ACTIVATE_AAA_AS_IU = 0x800000;
public static int CLSCTX_PS_DLL = 0x80000000;
public static int CLSCTX_SERVER = CLSCTX_INPROC_SERVER
| CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER;
public static int CLSCTX_ALL = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER
| CLSCTX_LOCAL_SERVER;
/**
* BSTR wrapper.
*
* <p>From MSDN:</p>
*
* <blockquote>A BSTR (Basic string or binary string) is a string data type
* that is used by COM, Automation, and Interop functions. Use the BSTR data
* type in all interfaces that will be accessed from script.</blockquote>
*
* <p>The memory structure:</p>
*
* <dl>
* <dt>Length prefix</dt>
* <dd>Length of the data array holding the string data and does not include
* the final two NULL characters.</dd>
* <dt>Data string</dt>
* <dd>UTF-16LE encoded bytes for the string.</dd>
* <dt>Terminator</dt>
* <dd>Two null characters</dd>
* </dl>
*
* <p>The "value" of the BSTR is the pointer to the start of the Data String,
* the length prefix is the four bytes before that.</p>
*
* <p>The MSDN states, that a BSTR derived from a Nullpointer is treated
* as a string containing zero characters.</p>
*/
public static class BSTR extends PointerType {
public BSTR() {
super(Pointer.NULL);
}
public BSTR(Pointer pointer) {
super(pointer);
}
public BSTR(String value) {
super();
this.setValue(value);
}
public void setValue(String value) {
if(value == null) {
value = "";
}
try {
byte[] encodedValue = value.getBytes("UTF-16LE");
// 4 bytes for the length prefix, length for the encoded data,
// 2 bytes for the two NULL terminators
Memory mem = new Memory(4 + encodedValue.length + 2);
mem.clear();
mem.setInt(0, encodedValue.length);
mem.write(4, encodedValue, 0, encodedValue.length);
this.setPointer(mem.share(4));
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("UTF-16LE charset is not supported", ex);
}
}
public String getValue() {
try {
Pointer pointer = this.getPointer();
if(pointer == null) {
return "";
}
int stringLength = pointer.getInt(-4);
return new String(pointer.getByteArray(0, stringLength), "UTF-16LE");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("UTF-16LE charset is not supported", ex);
}
}
@Override
public String toString() {
return this.getValue();
}
}
public class BSTRByReference extends ByReference {
public BSTRByReference() {
super(Native.POINTER_SIZE);
}
public BSTRByReference(BSTR value) {
this();
setValue(value);
}
public void setValue(BSTR value) {
this.getPointer().setPointer(0, value.getPointer());
}
public BSTR getValue() {
return new BSTR(getPointer().getPointer(0));
}
public String getString() {
return this.getValue().getValue();
}
@Override
public String toString() {
return super.toString(getValue());
}
}
public static class LPSTR extends PointerType {
public static class ByReference extends LPSTR implements
Structure.ByReference {
}
public LPSTR() {
super(Pointer.NULL);
}
public LPSTR(Pointer pointer) {
super(pointer);
}
public LPSTR(String value) {
this(new Memory(value.length() + 1L));
this.setValue(value);
}
public void setValue(String value) {
this.getPointer().setString(0, value);
}
public String getValue() {
Pointer pointer = this.getPointer();
String str = null;
if (pointer != null)
str = pointer.getString(0);
return str;
}
@Override
public String toString() {
return this.getValue();
}
}
public static class LPWSTR extends PointerType {
public static class ByReference extends LPWSTR implements
Structure.ByReference {
}
public LPWSTR() {
super(Pointer.NULL);
}
public LPWSTR(Pointer pointer) {
super(pointer);
}
public LPWSTR(String value) {
this(new Memory((value.length() + 1L) * Native.WCHAR_SIZE));
this.setValue(value);
}
public void setValue(String value) {
this.getPointer().setWideString(0, value);
}
public String getValue() {
Pointer pointer = this.getPointer();
String str = null;
if (pointer != null)
str = pointer.getWideString(0);
return str;
}
@Override
public String toString() {
return this.getValue();
}
}
public static class LPOLESTR extends PointerType {
public static class ByReference extends LPOLESTR implements
Structure.ByReference {
}
public LPOLESTR() {
super(Pointer.NULL);
}
public LPOLESTR(Pointer pointer) {
super(pointer);
}
public LPOLESTR(String value) {
super(new Memory((value.length() + 1L) * Native.WCHAR_SIZE));
this.setValue(value);
}
public void setValue(String value) {
this.getPointer().setWideString(0, value);
}
public String getValue() {
Pointer pointer = this.getPointer();
String str = null;
if (pointer != null)
str = pointer.getWideString(0);
return str;
}
@Override
public String toString() {
return this.getValue();
}
}
public static class VARTYPE extends USHORT {
private static final long serialVersionUID = 1L;
public VARTYPE() {
this(0);
}
public VARTYPE(int value) {
super(value);
}
}
public static class VARTYPEByReference extends ByReference {
public VARTYPEByReference() {
super(VARTYPE.SIZE);
}
public VARTYPEByReference(VARTYPE type) {
super(VARTYPE.SIZE);
setValue(type);
}
public VARTYPEByReference(short type) {
super(VARTYPE.SIZE);
getPointer().setShort(0, type);
}
public void setValue(VARTYPE value) {
getPointer().setShort(0, value.shortValue());
}
public VARTYPE getValue() {
return new VARTYPE(getPointer().getShort(0));
}
@Override
public String toString() {
return super.toString(getValue());
}
}
}