From 60531d90106ce2564674189d0a1c67739cc4b3d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=A4sing?= Date: Tue, 26 Jul 2016 23:05:48 +0200 Subject: [PATCH] Correct typemapper used for structures defined Winnetwk definitions follow the typemapper set by the "w32.ascii" property, following the convention demonstrated/used in W32APIOptions.DEFAULT. The definitions in LMShare are defined as pure UNICODE. Closes: #668 --- CHANGES.md | 1 + .../src/com/sun/jna/platform/win32/LMShare.java | 9 +++++---- .../src/com/sun/jna/platform/win32/Winnetwk.java | 13 +++++++------ .../test/com/sun/jna/platform/win32/MprTest.java | 15 +++++++++++---- src/com/sun/jna/win32/W32APITypeMapper.java | 5 ++++- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1e01d5b4aa..017950e875 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -76,6 +76,7 @@ Bug Fixes * [#674](https://github.com/java-native-access/jna/pull/674): Update references to Apache License as requested by issue #673 [@bhamail](https://github.com/bhamail) * [#636](https://github.com/java-native-access/jna/issues/636): Staticly link visual c++ runtime when building with MSVC - [@matthiasblaesing](https://github.com/matthiasblaesing). * [#624](https://github.com/java-native-access/jna/issues/624): WinDef.DWORD getLow() & getHigh() using incorrect bit mask - [@matthiasblaesing](https://github.com/matthiasblaesing). +* [#668](https://github.com/java-native-access/jna/issues/668): Correct typemapper used for structures defined in `com.sun.jna.platform.win32.LMShare` and `com.sun.jna.platform.win32.Winnetwk` - [@matthiasblaesing](https://github.com/matthiasblaesing). Release 4.2.1 ============= diff --git a/contrib/platform/src/com/sun/jna/platform/win32/LMShare.java b/contrib/platform/src/com/sun/jna/platform/win32/LMShare.java index 650b3f61c6..72b03a3e9b 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/LMShare.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/LMShare.java @@ -16,6 +16,7 @@ import com.sun.jna.Pointer; import com.sun.jna.Structure; +import com.sun.jna.win32.W32APITypeMapper; /** * Ported from LMShare.h. @@ -118,11 +119,11 @@ public static class SHARE_INFO_2 extends Structure { public String shi2_passwd; public SHARE_INFO_2() { - super(); + super(W32APITypeMapper.UNICODE); } public SHARE_INFO_2(Pointer memory) { - super(memory); + super(memory, Structure.ALIGN_DEFAULT, W32APITypeMapper.UNICODE); read(); } @@ -202,11 +203,11 @@ public static class SHARE_INFO_502 extends Structure { public Pointer shi502_security_descriptor; public SHARE_INFO_502() { - super(); + super(W32APITypeMapper.UNICODE); } public SHARE_INFO_502(Pointer memory) { - super(memory); + super(memory, Structure.ALIGN_DEFAULT, W32APITypeMapper.UNICODE); read(); } diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Winnetwk.java b/contrib/platform/src/com/sun/jna/platform/win32/Winnetwk.java index 5a37ca6ede..5efce2e0e3 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Winnetwk.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Winnetwk.java @@ -18,6 +18,7 @@ import com.sun.jna.Pointer; import com.sun.jna.Structure; +import com.sun.jna.win32.W32APITypeMapper; /** * Ported from AccCtrl.h. Microsoft Windows SDK 7.1 @@ -346,11 +347,11 @@ public ByReference(Pointer memory) { public String lpProvider; public NETRESOURCE() { - super(); + super(W32APITypeMapper.DEFAULT); } public NETRESOURCE(Pointer address) { - super(address); + super(address, Structure.ALIGN_DEFAULT, W32APITypeMapper.DEFAULT); read(); } @@ -391,11 +392,11 @@ public ByReference(Pointer memory) { public String lpUniversalName; public UNIVERSAL_NAME_INFO() { - super(); + super(W32APITypeMapper.DEFAULT); } public UNIVERSAL_NAME_INFO(Pointer address) { - super(address); + super(address, Structure.ALIGN_DEFAULT, W32APITypeMapper.DEFAULT); read(); } @@ -444,11 +445,11 @@ public ByReference(Pointer memory) { public String lpRemainingPath; public REMOTE_NAME_INFO() { - super(); + super(W32APITypeMapper.DEFAULT); } public REMOTE_NAME_INFO(Pointer address) { - super(address); + super(address, Structure.ALIGN_DEFAULT, W32APITypeMapper.DEFAULT); read(); } diff --git a/contrib/platform/test/com/sun/jna/platform/win32/MprTest.java b/contrib/platform/test/com/sun/jna/platform/win32/MprTest.java index c174136911..869eaf096c 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/MprTest.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/MprTest.java @@ -39,6 +39,12 @@ public static void main(String[] args) throws Exception { junit.textui.TestRunner.run(MprTest.class); } + public void testCreateLocalShare() throws Exception { + File fileShareFolder = createTempFolder(); + String share = createLocalShare(fileShareFolder); + Netapi32.INSTANCE.NetShareDel(null, share, 0); + } + public void testWNetUseConnection() throws Exception { // First create a share on the local machine File fileShareFolder = createTempFolder(); @@ -262,10 +268,11 @@ private String createLocalShare(File shareFolder) throws Exception { shi.write(); IntByReference parm_err = new IntByReference(0); - assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetShareAdd(null, // Use - // local - // computer - 2, shi.getPointer(), parm_err)); + int errorCode = Netapi32.INSTANCE.NetShareAdd(null, // Use local computer + 2, shi.getPointer(), parm_err); + assertEquals( + String.format("Failed to create share - errorCode: %d (Param: %d)", errorCode, parm_err.getValue()), + LMErr.NERR_Success, errorCode); return shareFolder.getName(); } diff --git a/src/com/sun/jna/win32/W32APITypeMapper.java b/src/com/sun/jna/win32/W32APITypeMapper.java index 98d193c18a..63b3e0dc2e 100644 --- a/src/com/sun/jna/win32/W32APITypeMapper.java +++ b/src/com/sun/jna/win32/W32APITypeMapper.java @@ -29,9 +29,12 @@ * @author twall */ public class W32APITypeMapper extends DefaultTypeMapper { - + /** Standard TypeMapper to use the unicode version of a w32 API. */ public static final TypeMapper UNICODE = new W32APITypeMapper(true); + /** Standard TypeMapper to use the ASCII/MBCS version of a w32 API. */ public static final TypeMapper ASCII = new W32APITypeMapper(false); + /** Default TypeMapper to use - depends on the value of {@code w32.ascii} system property */ + public static final TypeMapper DEFAULT = Boolean.getBoolean("w32.ascii") ? ASCII : UNICODE; protected W32APITypeMapper(boolean unicode) { if (unicode) {