Skip to content

Commit 91f139e

Browse files
authored
Deprecate BSTR String constructor and setValue (#1300)
BSTR allocation should be managed by COM, Automation, and Interop functions. See https://docs.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr
1 parent 58735db commit 91f139e

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Bug Fixes
3131
* [#1279](https://github.com/java-native-access/jna/issues/1279): Remove `DLLCallback` import from `CallbackReference` - [@dyorgio](https://github.com/dyorgio).
3232
* [#1278](https://github.com/java-native-access/jna/pull/1278): Improve compatibility of `c.s.j.p.WindowUtils#getProcessFilePath` and fix unittests for windows 32bit intel - [@matthiasblaesing](https://github.com/matthiasblaesing).
3333
* [#1284](https://github.com/java-native-access/jna/pull/1284): Fix illegal access exceptions, when retrieving options for private library interfaces with an instance field - [@fkistner](https://github.com/fkistner).
34+
* [#1300](https://github.com/java-native-access/jna/pull/1300): Deprecate `c.s.j.p.win32.WTypes.BSTR` String constructor and `setValue` method, as `BSTR` allocation should be managed by COM, Automation, and Interop functions - [@dbwiddis](https://github.com/dbwiddis).
35+
3436

3537
Breaking Changes
3638
----------------

contrib/platform/src/com/sun/jna/platform/win32/WTypes.java

+56-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
*/
2525
package com.sun.jna.platform.win32;
2626

27+
import java.io.UnsupportedEncodingException;
28+
2729
import com.sun.jna.Memory;
2830
import com.sun.jna.Native;
2931
import com.sun.jna.Pointer;
3032
import com.sun.jna.PointerType;
3133
import com.sun.jna.Structure;
3234
import com.sun.jna.platform.win32.WinDef.USHORT;
3335
import com.sun.jna.ptr.ByReference;
34-
import java.io.UnsupportedEncodingException;
3536

3637
/**
3738
* Constant defined in WTypes.h
@@ -103,15 +104,33 @@ public BSTR() {
103104
super(Pointer.NULL);
104105
}
105106

107+
/**
108+
* Instantiate a BSTR from a pointer. The user is responsible for allocating and
109+
* releasing memory for the {@link BSTR}, most commonly using
110+
* {@link OleAuto#SysAllocString(String)} and
111+
* {@link OleAuto#SysFreeString(BSTR)}
112+
*
113+
* @param pointer
114+
* A pointer to the string
115+
*/
106116
public BSTR(Pointer pointer) {
107117
super(pointer);
108118
}
109119

120+
/**
121+
* @deprecated Use {@link OleAuto#SysAllocString(String)} and
122+
* {@link OleAuto#SysFreeString(BSTR)}
123+
*/
124+
@Deprecated
110125
public BSTR(String value) {
111126
super();
112127
this.setValue(value);
113128
}
114129

130+
/**
131+
* @deprecated Users should not change the value of an allocated {@link BSTR}.
132+
*/
133+
@Deprecated
115134
public void setValue(String value) {
116135
if(value == null) {
117136
value = "";
@@ -154,21 +173,56 @@ public BSTRByReference() {
154173
super(Native.POINTER_SIZE);
155174
}
156175

176+
/**
177+
* Store a reference to the specified {@link BSTR}. This method does not
178+
* maintain a reference to the object passed as an argument. The user is
179+
* responsible for allocating and freeing the memory associated with this
180+
* {@link BSTR}.
181+
*
182+
* @param value
183+
* The BSTR to be referenced. Only the pointer is stored as a
184+
* reference.
185+
*/
157186
public BSTRByReference(BSTR value) {
158187
this();
159188
setValue(value);
160189
}
161190

191+
/**
192+
* Store a reference to the specified {@link BSTR}. This method does not
193+
* maintain a reference to the object passed as an argument. The user is
194+
* responsible for allocating and freeing the memory associated with this
195+
* {@link BSTR}.
196+
*
197+
* @param value
198+
* The BSTR to be referenced. Only the pointer is stored as a
199+
* reference.
200+
*/
162201
public void setValue(BSTR value) {
163202
this.getPointer().setPointer(0, value.getPointer());
164203
}
165204

205+
/**
206+
* Returns a copy of the {@link BSTR} referenced by this object. The memory
207+
* associated with the {@link BSTR} may be referenced by other objects/threads
208+
* which may also free the underlying native memory.
209+
*
210+
* @return A new {@link BSTR} object corresponding to the memory referenced by
211+
* this object.
212+
*/
166213
public BSTR getValue() {
167214
return new BSTR(getPointer().getPointer(0));
168215
}
169216

217+
/**
218+
* Returns the String represented by the referenced {@link BSTR}.
219+
*
220+
* @return the referenced String, if the reference is not {@code null},
221+
* {@code null} otherwise.
222+
*/
170223
public String getString() {
171-
return this.getValue().getValue();
224+
BSTR b = this.getValue();
225+
return b == null ? null : b.getValue();
172226
}
173227
}
174228

contrib/platform/test/com/sun/jna/platform/ByReferencePlatformToStringTest.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.junit.Test;
3030

31+
import com.sun.jna.Platform;
3132
import com.sun.jna.Pointer;
3233
import com.sun.jna.platform.unix.X11.AtomByReference;
3334
import com.sun.jna.platform.unix.X11.WindowByReference;
@@ -43,6 +44,7 @@
4344
import com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL;
4445
import com.sun.jna.platform.win32.OaIdl.VARIANT_BOOLByReference;
4546
import com.sun.jna.platform.win32.OaIdl._VARIANT_BOOLByReference;
47+
import com.sun.jna.platform.win32.OleAuto;
4648
import com.sun.jna.platform.win32.WTypes.BSTR;
4749
import com.sun.jna.platform.win32.WTypes.BSTRByReference;
4850
import com.sun.jna.platform.win32.WTypes.VARTYPE;
@@ -84,8 +86,12 @@ public void testPlatformToStrings() {
8486
BOOLByReference boolbr = new BOOLByReference(new BOOL(true));
8587
parseAndTest(boolbr.toString(), "BOOL", "true");
8688

87-
BSTRByReference bstrbr = new BSTRByReference(new BSTR("bstr"));
88-
parseAndTest(bstrbr.toString(), "BSTR", "bstr");
89+
if (Platform.isWindows()) {
90+
BSTR b = OleAuto.INSTANCE.SysAllocString("bstr");
91+
BSTRByReference bstrbr = new BSTRByReference(b);
92+
parseAndTest(bstrbr.toString(), "BSTR", "bstr");
93+
OleAuto.INSTANCE.SysFreeString(b);
94+
}
8995

9096
CHARByReference cbr = new CHARByReference(new CHAR(42));
9197
parseAndTest(cbr.toString(), "CHAR", "42");

0 commit comments

Comments
 (0)