Skip to content

Commit

Permalink
Also deprecate BSTR string constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Jan 29, 2021
1 parent 190bdcb commit 9855882
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
18 changes: 18 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/WTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,33 @@ public BSTR() {
super(Pointer.NULL);
}

/**
* Instantiate a BSTR from a pointer. The user is responsible for allocating and
* releasing memory for the {@link BSTR}, most commonly using
* {@link OleAuto#SysAllocString(String)} and
* {@link OleAuto#SysFreeString(BSTR)}
*
* @param pointer
* A pointer to the string
*/
public BSTR(Pointer pointer) {
super(pointer);
}

/**
* @deprecated Use {@link OleAuto#SysAllocString(String)} and
* {@link OleAuto#SysFreeString(BSTR)}
*/
@Deprecated
public BSTR(String value) {
super();
this.setValue(value);
}

/**
* @deprecated Users should not change the value of an allocated {@link BSTR}.
*/
@Deprecated
public void setValue(String value) {
if(value == null) {
value = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@

import org.junit.Test;

import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.platform.unix.X11.AtomByReference;
import com.sun.jna.platform.unix.X11.WindowByReference;
import com.sun.jna.platform.win32.OleAuto;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTRByReference;
import com.sun.jna.platform.win32.HighLevelMonitorConfigurationAPI.MC_COLOR_TEMPERATURE;
Expand Down Expand Up @@ -84,9 +86,12 @@ public void testPlatformToStrings() {
BOOLByReference boolbr = new BOOLByReference(new BOOL(true));
parseAndTest(boolbr.toString(), "BOOL", "true");

BSTR b = new BSTR("bstr");
BSTRByReference bstrbr = new BSTRByReference(b.getPointer());
parseAndTest(bstrbr.toString(), "BSTR", "bstr");
if (Platform.isWindows()) {
BSTR b = OleAuto.INSTANCE.SysAllocString("bstr");
BSTRByReference bstrbr = new BSTRByReference(b.getPointer());
parseAndTest(bstrbr.toString(), "BSTR", "bstr");
OleAuto.INSTANCE.SysFreeString(b);
}

CHARByReference cbr = new CHARByReference(new CHAR(42));
parseAndTest(cbr.toString(), "CHAR", "42");
Expand Down

0 comments on commit 9855882

Please sign in to comment.