diff --git a/CHANGES.md b/CHANGES.md index bea0090634..e8d144c923 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,9 @@ Release 4.2.1 Features -------- +* [#529](https://github.com/java-native-access/jna/pull/529): Added new files `com.sun.jna.platform.win32.Mpr`, `com.sun.jna.platform.win32.LmShare`, and `com.sun.jna.platform.win32.Winnetwk` [@amarcionek](https://github.com/amarcionek). +* [#529](https://github.com/java-native-access/jna/pull/529): Added `ACCESS_*` definitions to `com.sun.jna.platform.win32.LmAccess` [@amarcionek](https://github.com/amarcionek). +* [#529](https://github.com/java-native-access/jna/pull/529): Added `NetShareAdd` and `NetShareDel` to `com.sun.jna.platform.win32.Netapi32` [@amarcionek](https://github.com/amarcionek). * [#504](https://github.com/java-native-access/jna/pull/504): Add support for linux-sparcv9 [@alexvsimon](https://github.com/alexvsimon). * [#510](https://github.com/java-native-access/jna/pull/510): Added `GetCommState`, `GetCommTimeouts` `SetCommState` and `SetCommTimeouts` to `com.sun.jna.platform.win32.Kernel32`. Added `DCB` structure to `com.sun.jna.platform.win32.WinBase` - [@MBollig](https://github.com/MBollig). * [#512](https://github.com/java-native-access/jna/pull/512): Make loading debug flags mutable [@lwahonen](https://github.com/lwahonen). diff --git a/contrib/platform/src/com/sun/jna/platform/win32/LMAccess.java b/contrib/platform/src/com/sun/jna/platform/win32/LMAccess.java index ccd7259a31..38578ddea6 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/LMAccess.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/LMAccess.java @@ -422,5 +422,22 @@ protected List getFieldOrder() { int USER_PRIV_MASK = 3; int USER_PRIV_GUEST = 0; int USER_PRIV_USER = 1; - int USER_PRIV_ADMIN = 2; + int USER_PRIV_ADMIN = 2; + + // + // Bit values for the access permissions. ACCESS_ALL is a handy + // way to specify maximum permissions. These are used in + // acl_access field of access_list structures. + // + + int ACCESS_NONE = 0x00; + int ACCESS_READ = 0x01; + int ACCESS_WRITE = 0x02; + int ACCESS_CREATE = 0x04; + int ACCESS_EXEC = 0x08; + int ACCESS_DELETE = 0x10; + int ACCESS_ATRIB = 0x20; + int ACCESS_PERM = 0x40; + int ACCESS_ALL = ACCESS_READ | ACCESS_WRITE | ACCESS_CREATE | ACCESS_EXEC | ACCESS_DELETE | ACCESS_ATRIB | ACCESS_PERM; + int ACCESS_GROUP = 0x8000; } diff --git a/contrib/platform/src/com/sun/jna/platform/win32/LMShare.java b/contrib/platform/src/com/sun/jna/platform/win32/LMShare.java new file mode 100644 index 0000000000..53d1549c19 --- /dev/null +++ b/contrib/platform/src/com/sun/jna/platform/win32/LMShare.java @@ -0,0 +1,226 @@ +/* Copyright (c) 2015 Adam Marcionek, All Rights Reserved + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +package com.sun.jna.platform.win32; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Pointer; +import com.sun.jna.Structure; +import com.sun.jna.WString; +import com.sun.jna.win32.StdCallLibrary; + +/** + * Ported from LMShare.h. + * Windows SDK 7.1 + * @author amarcionek[at]seven10storage.com + */ +public interface LMShare extends StdCallLibrary { + + // + // Share types (shi1_type and shi2_type fields). + // + + /** + * Disk Drive. + */ + int STYPE_DISKTREE = 0; + + /** + * Print Queue. + */ + int STYPE_PRINTQ = 1; + + /** + * Communication device. + */ + int STYPE_DEVICE = 2; + + /** + * Interprocess communication (IPC). + */ + int STYPE_IPC = 3; + + /** + * A temporary share. + */ + int STYPE_TEMPORARY = 0x40000000; + + /** + * Special share reserved for interprocess communication (IPC$) or remote administration of the server (ADMIN$). + * Can also refer to administrative shares such as C$, D$, E$, and so forth. For more information, see the network share functions. + */ + int STYPE_SPECIAL = 0x80000000; + + /** + * Contains information about the shared resource, including name of the resource, type and permissions, number of connections, and other pertinent information. + */ + public static class SHARE_INFO_2 extends Structure { + public SHARE_INFO_2() { + super(); + } + + public SHARE_INFO_2(Pointer memory) { + super(memory); + read(); + } + + /** + * Pointer to a Unicode string specifying the name of a shared resource. Calls to the NetShareSetInfo function ignore this member. + */ + public WString shi2_netname; + + /** + * A combination of values that specify the type of share. Calls to the NetShareSetInfo function ignore this member. + * One of the following values may be specified. You can isolate these values by using the STYPE_MASK value. + * STYPE_DISKTREE, STYPE_PRINTQ, STYPE_DEVICE, STYPE_IPC, STYPE_TEMPORARY, STYPE_SPECIAL + */ + public int shi2_type; + + /** + * Pointer to a Unicode string specifying an optional comment about the shared resource. + */ + public WString shi2_remark; + + /** + * Specifies a DWORD value that indicates the shared resource's permissions for servers running with share-level security. + * This member is ignored on a server running user-level security. This member can be any of the following values. + * Calls to the NetShareSetInfo function ignore this member. Note that Windows does not support share-level security. + * For more information about controlling access to securable objects, see Access Control, Privileges, and Securable Objects. + * NOTE: Bit masks are defined in LmAccess.Java + */ + public int shi2_permissions; + + /** + * Specifies a DWORD value that indicates the maximum number of concurrent connections that the shared resource can accommodate. + * The number of connections is unlimited if the value specified in this member is -1. + */ + public int shi2_max_uses; + + /** + * Specifies a DWORD value that indicates the number of current connections to the resource. Calls to the NetShareSetInfo function ignore this member. + */ + public int shi2_current_uses; + + /** + * Pointer to a Unicode string that contains the local path for the shared resource. For disks, this member is the path being shared. + * For print queues, this member is the name of the print queue being shared. Calls to the NetShareSetInfo function ignore this member. + */ + public WString shi2_path; + + /** + * Pointer to a Unicode string that specifies the share's password (when the server is running with share-level security). If the server is + * running with user-level security, this member is ignored. Note that Windows does not support share-level security. + * This member can be no longer than SHPWLEN+1 bytes (including a terminating null character). Calls to the NetShareSetInfo function ignore this member. + */ + public WString shi2_passwd; + + protected List getFieldOrder() { + return Arrays.asList(new String[] { "shi2_netname", + "shi2_type", + "shi2_remark", + "shi2_permissions", + "shi2_max_uses", + "shi2_current_uses", + "shi2_path", + "shi2_passwd" }); + } + } + + /** + * Contains information about the shared resource, including name of the resource, type and permissions, number of connections, and other pertinent information. + */ + public static class SHARE_INFO_502 extends Structure { + public SHARE_INFO_502() { + super(); + } + + public SHARE_INFO_502(Pointer memory) { + super(memory); + read(); + } + + /** + * Pointer to a Unicode string specifying the name of a shared resource. Calls to the NetShareSetInfo function ignore this member. + */ + public WString shi502_netname; + + /** + * A combination of values that specify the type of share. Calls to the NetShareSetInfo function ignore this member. + * One of the following values may be specified. You can isolate these values by using the STYPE_MASK value. + * STYPE_DISKTREE, STYPE_PRINTQ, STYPE_DEVICE, STYPE_IPC, STYPE_TEMPORARY, STYPE_SPECIAL + */ + public int shi502_type; + + /** + * Pointer to a Unicode string specifying an optional comment about the shared resource. + */ + public WString shi502_remark; + + /** + * Specifies a DWORD value that indicates the shared resource's permissions for servers running with share-level security. + * This member is ignored on a server running user-level security. This member can be any of the following values. + * Calls to the NetShareSetInfo function ignore this member. Note that Windows does not support share-level security. + * For more information about controlling access to securable objects, see Access Control, Privileges, and Securable Objects. + * NOTE: Bit masks are defined in LmAccess.Java + */ + public int shi502_permissions; + + /** + * Specifies a DWORD value that indicates the maximum number of concurrent connections that the shared resource can accommodate. + * The number of connections is unlimited if the value specified in this member is -1. + */ + public int shi502_max_uses; + + /** + * Specifies a DWORD value that indicates the number of current connections to the resource. Calls to the NetShareSetInfo function ignore this member. + */ + public int shi502_current_uses; + + /** + * Pointer to a Unicode string that contains the local path for the shared resource. For disks, this member is the path being shared. + * For print queues, this member is the name of the print queue being shared. Calls to the NetShareSetInfo function ignore this member. + */ + public WString shi502_path; + + /** + * Pointer to a Unicode string that specifies the share's password (when the server is running with share-level security). If the server is + * running with user-level security, this member is ignored. Note that Windows does not support share-level security. + * This member can be no longer than SHPWLEN+1 bytes (including a terminating null character). Calls to the NetShareSetInfo function ignore this member. + */ + public WString shi502_passwd; + + /** + * Reserved; must be zero. Calls to the NetShareSetInfo function ignore this member. + */ + public int shi502_reserved; + + /** + * Specifies the SECURITY_DESCRIPTOR associated with this share. + */ + public Pointer shi502_security_descriptor; + + protected List getFieldOrder() { + return Arrays.asList(new String[] { "shi502_netname", + "shi502_type", + "shi502_remark", + "shi502_permissions", + "shi502_max_uses", + "shi502_current_uses", + "shi502_path", + "shi502_passwd", + "shi502_reserved", + "shi502_security_descriptor" }); + } + } +} diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Mpr.java b/contrib/platform/src/com/sun/jna/platform/win32/Mpr.java new file mode 100644 index 0000000000..d42717a307 --- /dev/null +++ b/contrib/platform/src/com/sun/jna/platform/win32/Mpr.java @@ -0,0 +1,321 @@ +/* Copyright (c) 2015 Adam Marcionek, All Rights Reserved + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +package com.sun.jna.platform.win32; + +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import com.sun.jna.WString; +import com.sun.jna.platform.win32.WinDef.HWND; +import com.sun.jna.platform.win32.WinNT.HANDLE; +import com.sun.jna.platform.win32.WinNT.HANDLEByReference; +import com.sun.jna.platform.win32.Winnetwk.*; +import com.sun.jna.ptr.IntByReference; +import com.sun.jna.ptr.PointerByReference; +import com.sun.jna.win32.StdCallLibrary; +import com.sun.jna.win32.W32APIOptions; + +/** + * Ported from Winnetwk.h. + * Microsoft Windows SDK 8.1 + * @author amarcionek[at]gmail.com + */ + +public interface Mpr extends StdCallLibrary { + + Mpr INSTANCE = (Mpr) Native.loadLibrary("Mpr", Mpr.class, W32APIOptions.UNICODE_OPTIONS); + + /** + * The WNetOpenEnum function starts an enumeration of network resources or existing connections. You can continue the enumeration + * by calling the WNetEnumResource function. + * @param dwScope + * Scope of the enumeration. This parameter can be one of the following values from NETRESOURCEScope: + * RESOURCE_CONNECTED, RESOURCE_CONTEXT, RESOURCE_GLOBALNET, RESOURCE_REMEMBERED + * @param dwType + * Resource types to be enumerated. This parameter can be a combination of the following values from NETRESOURCEType: + * RESOURCETYPE_ANY, RESOURCETYPE_DISK, RESOURCETYPE_PRINT + * @param dwUsage + * Resource usage type to be enumerated. This parameter can be a combination of the following values from NETRESOURCEUsage: + * 0, RESOURCEUSAGE_CONNECTABLE, RESOURCEUSAGE_CONTAINER, RESOURCEUSAGE_ATTACHED, RESOURCEUSAGE_ALL + * @param lpNETRESOURCE + * Pointer to a NETRESOURCE structure that specifies the container to enumerate. If the dwScope parameter is not RESOURCE_GLOBALNET, + * this parameter must be NULL. + * If this parameter is NULL, the root of the network is assumed. (The system organizes a network as a hierarchy; the root is the + * topmost container in the network.) If this parameter is not NULL, it must point to a NETRESOURCE structure. This structure can be filled + * in by the application or it can be returned by a call to the WNetEnumResource function. The NETRESOURCE structure must specify a container + * resource; that is, the RESOURCEUSAGE_CONTAINER value must be specified in the dwUsage parameter. + * To enumerate all network resources, an application can begin the enumeration by calling WNetOpenEnum with the lpNETRESOURCE parameter + * set to NULL, and then use the returned handle to call WNetEnumResource to enumerate resources. If one of the resources in the NETRESOURCE + * array returned by the WNetEnumResource function is a container resource, you can call WNetOpenEnum to open the resource for further enumeration. + * @param lphEnum + * Pointer to an enumeration handle that can be used in a subsequent call to WNetEnumResource. + * @return NO_ERROR if the function succeeds, otherwise a system error code. See MSDN documentation for common error values: + * https://msdn.microsoft.com/en-us/library/windows/desktop/aa385478(v=vs.85).aspx + */ + public int WNetOpenEnum(int dwScope, + int dwType, + int dwUsage, + NETRESOURCE.ByReference lpNETRESOURCE, + HANDLEByReference lphEnum); + + /** + * The WNetEnumResource function continues an enumeration of network resources that was started by a call to the WNetOpenEnum function. + * @param hEnum [in] + * Handle that identifies an enumeration instance. This handle must be returned by the WNetOpenEnum function. + * @param lpcCount [in, out] + * Pointer to a variable specifying the number of entries requested. If the number requested is -1, the function returns as + * many entries as possible. + * If the function succeeds, on return the variable pointed to by this parameter contains the number of entries actually read. + * @param lpBuffer [out] + * Pointer to the buffer that receives the enumeration results. The results are returned as an array of NETRESOURCE structures. + * Note that the buffer you allocate must be large enough to hold the structures, plus the strings to which their members point. + * For more information, see the Remarks section on MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/aa385449(v=vs.85).aspx + * The buffer is valid until the next call using the handle specified by the hEnum parameter. The order of NETRESOURCE + * structures in the array is not predictable. + * @param lpBufferSize [in, out] + * Pointer to a variable that specifies the size of the lpBuffer parameter, in bytes. If the buffer is too small to receive + * even one entry, this parameter receives the required size of the buffer. + * @return If the function succeeds, the return value is one of the following values: + * NO_ERROR - The enumeration succeeded, and the buffer contains the requested data. The calling application can continue + * to call WNetEnumResource to complete the enumeration. + * ERROR_NO_MORE_ITEMS - There are no more entries. The buffer contents are undefined. + * If the function fails, see MSDN documentation for common error values: + * https://msdn.microsoft.com/en-us/library/windows/desktop/aa385478(v=vs.85).aspx + */ + public int WNetEnumResource(HANDLE hEnum, + IntByReference lpcCount, + Pointer lpBuffer, + IntByReference lpBufferSize); + + + /** + * The WNetCloseEnum function ends a network resource enumeration started by a call to the WNetOpenEnum function. + * @param hEnum [in] + * Handle that identifies an enumeration instance. This handle must be returned by the WNetOpenEnum function. + * @return NO_ERROR if the function succeeds, otherwise a system error code. See MSDN documentation for common error values: + * https://msdn.microsoft.com/en-us/library/windows/desktop/aa385431(v=vs.85).aspx + */ + + int WNetCloseEnum(HANDLE hEnum); + /** + * The WNetGetUniversalName function takes a drive-based path for a network resource and returns an information structure that contains a + * more universal form of the name. + * @param lpLocalPath [in] + * A pointer to a constant null-terminated string that is a drive-based path for a network resource. For example, if drive H has + * been mapped to a network drive share, and the network resource of interest is a file named Sample.doc in the + * directory \Win32\Examples on that share, the drive-based path is H:\Win32\Examples\Sample.doc. + * @param dwInfoLevel [in] + * The type of structure that the function stores in the buffer pointed to by the lpBuffer parameter. This parameter can be + * one of the following values defined in the Winnetwk.java. + * UNIVERSAL_NAME_INFO_LEVEL - The function stores a UNIVERSAL_NAME_INFO structure in the buffer. + * REMOTE_NAME_INFO_LEVEL - The function stores a REMOTE_NAME_INFO structure in the buffer. + * The UNIVERSAL_NAME_INFO structure points to a Universal Naming Convention (UNC) name string. + * The REMOTE_NAME_INFO structure points to a UNC name string and two additional connection information strings. For more information, + * see the following Remarks section. + * @param lpBuffer [out] + * A pointer to a buffer that receives the structure specified by the dwInfoLevel parameter. + * @param lpBufferSize [in,out] + * A pointer to a variable that specifies the size, in bytes, of the buffer pointed to by the lpBuffer parameter. + * If the function succeeds, it sets the variable pointed to by lpBufferSize to the number of bytes stored in the buffer. If the function + * fails because the buffer is too small, this location receives the required buffer size, and the function returns ERROR_MORE_DATA. + * @return If the function succeeds, the return value is NO_ERROR, otherwise see MSDN for common error codes: + * https://msdn.microsoft.com/en-us/library/windows/desktop/aa385474(v=vs.85).aspx + */ + int WNetGetUniversalName(WString lpLocalPath, + int dwInfoLevel, + Pointer lpBuffer, + IntByReference lpBufferSize); + + + /** + * The WNetUseConnection function makes a connection to a network resource. + * The function can redirect a local device to a network resource. + * + * The WNetUseConnection function is similar to the WNetAddConnection3 function. + * The main difference is that WNetUseConnection can automatically select an + * unused local device to redirect to the network resource. + * + * @param hwndOwner [in] + * Handle to a window that the provider of network resources + * can use as an owner window for dialog boxes. Use this parameter + * if you set the CONNECT_INTERACTIVE value in the dwFlags parameter. + * @param lpNETRESOURCE [in] + * Pointer to a NETRESOURCE structure that specifies details of the + * proposed connection. The structure contains information about the + * network resource, the local device, and the network resource provider. + * + * You must specify the following members of the NETRESOURCE structure. The WNetUseConnection function ignores + * the other members of the NETRESOURCE structure. For more information, see the descriptions following for the + * dwFlags parameter. + * + * dwType + * Specifies the type of resource to connect to. It is most efficient to specify a resource type + * in this member, such as RESOURCETYPE_DISK or RESOURCETYPE_PRINT. However, if the lpLocalName + * member is NULL, or if it points to an empty string and CONNECT_REDIRECT is not set, + * dwType can be RESOURCETYPE_ANY. + * + * This method works only if the function does not automatically choose a + * device to redirect to the network resource. + * Although this member is required, its information may be + * ignored by the network service provider + * lpLocalName + * Pointer to a null-terminated string that specifies the name of a local device + * to be redirected, such as "F:" or "LPT1". The string is treated in a case-insensitive manner. + * If the string is empty, or if lpLocalName is NULL, a connection to the network occurs without redirection. + * If the CONNECT_REDIRECT value is set in the dwFlags parameter, or if the network requires a + * redirected local device, the function chooses a local device to redirect and returns + * the name of the device in the lpAccessName parameter. + * lpRemoveName + * Pointer to a null-terminated string that specifies the network resource to connect to. The string + * can be up to MAX_PATH characters in length, and it must follow the network provider's naming conventions. + * lpProvider + * Pointer to a null-terminated string that specifies the network provider to connect to. If lpProvider is NULL, + * or if it points to an empty string, the operating system attempts to determine the correct provider by parsing the + * string pointed to by the lpRemoteName member. + * If this member is not NULL, the operating system attempts to make a connection only to the named network provider. + * You should set this member only if you know the network provider you want to use. Otherwise, let the operating system + * determine which provider the network name maps to. + * @param lpPassword [in] + * Pointer to a constant null-terminated string that specifies a password to be used in making the network connection. + * If lpPassword is NULL, the function uses the current default password associated with the user specified by lpUserID. + * If lpPassword points to an empty string, the function does not use a password. + * If the connection fails because of an invalid password and the CONNECT_INTERACTIVE value is set in the dwFlags parameter, + * the function displays a dialog box asking the user to type the password. + * @param lpUserID [in] + * Pointer to a constant null-terminated string that specifies a user name for making the connection. + * If lpUserID is NULL, the function uses the default user name. (The user context for the process provides the default user name.) + * The lpUserID parameter is specified when users want to connect to a network resource for which they have been assigned a user + * name or account other than the default user name or account. + * The user-name string represents a security context. It may be specific to a network provider. + * For security context, see https://msdn.microsoft.com/en-us/library/windows/desktop/ms721625(v=vs.85).aspx + * @param dwFlags [in] + * Set of bit flags describing the connection. This parameter can be any combination of the values in ConnectFlag. + * @param lpAccessName [out] + * Pointer to a buffer that receives system requests on the connection. This parameter can be NULL. If this parameter is specified, + * and the lpLocalName member of the NETRESOURCE structure specifies a local device, this buffer receives the local device name. + * If lpLocalName does not specify a device and the network requires a local device redirection, or if the CONNECT_REDIRECT value is + * set, this buffer receives the name of the redirected local device. + * Otherwise, the name copied into the buffer is that of a remote resource. If specified, this buffer must be at least as large as + * the string pointed to by the lpRemoteName member. + * @param lpBufferSize [in, out] + * Pointer to a variable that specifies the size of the lpAccessName buffer, in characters. If the call fails because the buffer is + * not large enough, the function returns the required buffer size in this location. For more information, see the descriptions of the + * lpAccessName parameter and the ERROR_MORE_DATA error code in the Return Values section. + * @param lpResult [out] + * Pointer to a variable that receives additional information about the connection. This parameter can be the following value: + * + * ConnectFlag.CONNECT_LOCALDRIVE - If this flag is set, the connection was made using a local device redirection. If the lpAccessName + * parameter points to a buffer, the local device name is copied to the buffer. + * @return NO_ERROR if the function succeeds, otherwise a system error code. See MSDN documentation for common error values: + * https://msdn.microsoft.com/en-us/library/windows/desktop/aa385482(v=vs.85).aspx + */ + public int WNetUseConnection(HWND hwndOwner, + NETRESOURCE lpNETRESOURCE, + WString lpPassword, + WString lpUserID, + int dwFlags, + PointerByReference lpAccessName, + IntByReference lpBufferSize, + IntByReference lpResult); + + /** + * The WNetAddConnection3 function makes a connection to a network resource. The function can redirect a local device to the network resource. + * + * @param hwndOwner [in] + * Handle to a window that the provider of network resources + * can use as an owner window for dialog boxes. Use this parameter + * if you set the CONNECT_INTERACTIVE value in the dwFlags parameter. + * @param lpNETRESOURCE [in] + * Pointer to a NETRESOURCE structure that specifies details of the + * proposed connection. The structure contains information about the + * network resource, the local device, and the network resource provider. + * + * You must specify the following members of the NETRESOURCE structure. The WNetUseConnection function ignores + * the other members of the NETRESOURCE structure. For more information, see the descriptions following for the + * dwFlags parameter. + * + * dwType + * Specifies the type of resource to connect to. It is most efficient to specify a resource type + * in this member, such as RESOURCETYPE_DISK or RESOURCETYPE_PRINT. However, if the lpLocalName + * member is NULL, or if it points to an empty string and CONNECT_REDIRECT is not set, + * dwType can be RESOURCETYPE_ANY. + * + * This method works only if the function does not automatically choose a + * device to redirect to the network resource. + * Although this member is required, its information may be + * ignored by the network service provider + * lpLocalName + * Pointer to a null-terminated string that specifies the name of a local device + * to be redirected, such as "F:" or "LPT1". The string is treated in a case-insensitive manner. + * If the string is empty, or if lpLocalName is NULL, a connection to the network occurs without redirection. + * If the CONNECT_REDIRECT value is set in the dwFlags parameter, or if the network requires a + * redirected local device, the function chooses a local device to redirect and returns + * the name of the device in the lpAccessName parameter. + * lpRemoveName + * Pointer to a null-terminated string that specifies the network resource to connect to. The string + * can be up to MAX_PATH characters in length, and it must follow the network provider's naming conventions. + * lpProvider + * Pointer to a null-terminated string that specifies the network provider to connect to. If lpProvider is NULL, + * or if it points to an empty string, the operating system attempts to determine the correct provider by parsing the + * string pointed to by the lpRemoteName member. + * If this member is not NULL, the operating system attempts to make a connection only to the named network provider. + * You should set this member only if you know the network provider you want to use. Otherwise, let the operating system + * determine which provider the network name maps to. + * @param lpPassword [in] + * Pointer to a constant null-terminated string that specifies a password to be used in making the network connection. + * If lpPassword is NULL, the function uses the current default password associated with the user specified by lpUserID. + * If lpPassword points to an empty string, the function does not use a password. + * If the connection fails because of an invalid password and the CONNECT_INTERACTIVE value is set in the dwFlags parameter, + * the function displays a dialog box asking the user to type the password. + * @param lpUserID [in] + * Pointer to a constant null-terminated string that specifies a user name for making the connection. + * If lpUserID is NULL, the function uses the default user name. (The user context for the process provides the default user name.) + * The lpUserID parameter is specified when users want to connect to a network resource for which they have been assigned a user + * name or account other than the default user name or account. + * The user-name string represents a security context. It may be specific to a network provider. + * For security context, see https://msdn.microsoft.com/en-us/library/windows/desktop/ms721625(v=vs.85).aspx + * @param dwFlags [in] + * Set of bit flags describing the connection. This parameter can be any combination of the values in ConnectFlag. + */ + public int WNetAddConnection3(HWND hwndOwner, + NETRESOURCE lpNETRESOURCE, + WString lpPassword, + WString lpUserID, + int dwFlags); + + /** + * The WNetCancelConnection2 function cancels an existing network connection. You can also call the function to remove remembered network connections + * that are not currently connected. + * + * @param lpName [in] + * Pointer to a constant null-terminated string that specifies the name of either the redirected local device or the remote network resource to disconnect from. + * If this parameter specifies a redirected local device, the function cancels only the specified device redirection. If the parameter specifies a remote network + * resource, all connections without devices are canceled. + * @param dwFlags [in] + * Connection type. The following values are defined. + * 0 - The system does not update information about the connection. If the connection was marked as persistent in the registry, the system continues to + * restore the connection at the next logon. If the connection was not marked as persistent, the function ignores the setting of the CONNECT_UPDATE_PROFILE flag. + * CONNECT_UPDATE_PROFILE - The system updates the user profile with the information that the connection is no longer a persistent one. The system will not + * restore this connection during subsequent logon operations. (Disconnecting resources using remote names has no effect on persistent connections.) + * @param fForce [in] + * Specifies whether the disconnection should occur if there are open files or jobs on the connection. If this parameter is FALSE, the function fails if there + * are open files or jobs. + * @return NO_ERROR if the function succeeds, otherwise a system error code. See MSDN documentation for common error values: + * https://msdn.microsoft.com/en-us/library/windows/desktop/aa385482(v=vs.85).aspx + */ + public int WNetCancelConnection2(WString lpName, + int dwFlags, + boolean fForce); + +} diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32.java b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32.java index 2bd667ba60..746816f604 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32.java @@ -15,6 +15,7 @@ import com.sun.jna.Native; import com.sun.jna.Pointer; import com.sun.jna.Structure; +import com.sun.jna.WString; import com.sun.jna.platform.win32.DsGetDC.PDOMAIN_CONTROLLER_INFO; import com.sun.jna.platform.win32.Guid.GUID; import com.sun.jna.platform.win32.NTSecApi.PLSA_FOREST_TRUST_INFORMATION; @@ -426,6 +427,40 @@ public int DsEnumerateDomainTrusts(String serverName, int Flags, * @return * If the function succeeds, the return value is NERR_Success. */ - public int NetUserGetInfo( String servername, String username, int level, PointerByReference bufptr ); + public int NetUserGetInfo(String servername, String username, int level, PointerByReference bufptr ); + /** + * Shares a server resource. + * @param servername [in] + * Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. + * If this parameter is NULL, the local computer is used. + * @param level [in] + * Specifies the information level of the data. This parameter can be one of the following values: + * 2 - Specifies information about the shared resource, including the name of the resource, type and permissions, and number of connections. + * The buf parameter points to a SHARE_INFO_2 structure. + * 502 - Specifies information about the shared resource, including the name of the resource, type and permissions, number of connections, + * and other pertinent information. The buf parameter points to a SHARE_INFO_502 structure. + * 503 - Specifies information about the shared resource, including the name of the resource, type and permissions, number of connections, + * and other pertinent information. The buf parameter points to a SHARE_INFO_503 structure. + * @param buf [in] + * Pointer to the buffer that specifies the data. The format of this data depends on the value of the level parameter. + * For more information, see Network Management Function Buffers (https://msdn.microsoft.com/en-us/library/windows/desktop/aa370676(v=vs.85).aspx) + * @param parm_err [out] + * Pointer to a value that receives the index of the first member of the share information structure that causes the ERROR_INVALID_PARAMETER error. + * If this parameter is NULL, the index is not returned on error. For more information, see the NetShareSetInfo function. + * @return If the function succeeds, the return value is NERR_Success. If the function fails, the return value can be an error code as seen on MSDN. + */ + public int NetShareAdd(WString servername, int level, Pointer buf, IntByReference parm_err); + + /** + * Deletes a share name from a server's list of shared resources, disconnecting all connections to the shared resource. + * @param servername [in] + * Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. If this parameter is NULL, the local computer is used. + * @param netname [in] + * Pointer to a string that specifies the name of the share to delete. + * @param reserved + * Reserved, must be zero. + * @return If the function succeeds, the return value is LMErr.NERR_Success. If the function fails, the return value can be an error code as seen on MSDN. + */ + public int NetShareDel(WString servername, WString netname, int reserved); } diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Winnetwk.java b/contrib/platform/src/com/sun/jna/platform/win32/Winnetwk.java new file mode 100644 index 0000000000..25899ab0f3 --- /dev/null +++ b/contrib/platform/src/com/sun/jna/platform/win32/Winnetwk.java @@ -0,0 +1,412 @@ +/* Copyright (c) 2015 Adam Marcionek, All Rights Reserved + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +package com.sun.jna.platform.win32; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Pointer; +import com.sun.jna.Structure; +import com.sun.jna.WString; +import com.sun.jna.win32.StdCallLibrary; + +/** + * Ported from AccCtrl.h. + * Microsoft Windows SDK 7.1 + * @author amarcionek[at]gmail.com + */ + +public abstract class Winnetwk implements StdCallLibrary { + + /** + * The scope of the enumeration. This member can be one of the following values defined in the Winnetwk.h header file. + * NOTE: This are for the dwScope member of NetResource. + * NOTE: Certain functions allow different values of the scope parameter. Consult MSDN for more info. + */ + public class RESOURCESCOPE { + + /** + * Enumerate currently connected resources. The dwUsage member cannot be specified. + */ + public static final int RESOURCE_CONNECTED = 1; + + /** + * Enumerate all resources on the network. The dwUsage member is specified. + */ + public static final int RESOURCE_GLOBALNET = 2; + + /** + * Enumerate remembered (persistent) connections. The dwUsage member cannot be specified. + */ + public static final int RESOURCE_REMEMBERED = 3; + + /** + * NOTE: Definition for this is not defined in Windows Kits nor on MSDN + */ + public static final int RESOURCE_RECENT = 4; + + /** + * Enumerate only resources in the network context of the caller. Specify this value for a Network + * Neighborhood view. The function ignores the dwUsage parameter. + */ + public static final int RESOURCE_CONTEXT = 5; + } + + /** + * The type of resource. This member can be one of the following values defined in the Winnetwk.h header file. + * NOTE: This are for the dwType member of NetResource + */ + public class RESOURCETYPE { + + /** + * All resources + */ + public static final int RESOURCETYPE_ANY = 0; + + /** + * Disk resources + */ + public static final int RESOURCETYPE_DISK = 1; + + /** + * Print resources + */ + public static final int RESOURCETYPE_PRINT = 2; + + /** + * NOTE: Definition for this is not defined in Windows Kits nor on MSDN + */ + public static final int RESOURCETYPE_RESERVED = 8; + + /** + * The WNetEnumResource function can also return the value RESOURCETYPE_UNKNOWN if a resource is neither a disk nor a print resource. + */ + public static final int RESOURCETYPE_UNKNOWN = 0xFFFFFFFF; + } + + /** + * The type of resource. This member can be one of the following values defined in the Winnetwk.h header file. + * NOTE: This are for the dwScope member of NetResource + */ + public class RESOURCEDISPLAYTYPE { + + /** + * The method used to display the object does not matter. + */ + public static final int RESOURCEDISPLAYTYPE_GENERIC = 0; + + /** + * The object should be displayed as a domain. + */ + public static final int RESOURCEDISPLAYTYPE_DOMAIN = 1; + + /** + * The object should be displayed as a server. + */ + public static final int RESOURCEDISPLAYTYPE_SERVER = 2; + + /** + * The object should be displayed as a share. + */ + public static final int RESOURCEDISPLAYTYPE_SHARE = 3; + + /** + * The object should be displayed as a file. + */ + public static final int RESOURCEDISPLAYTYPE_FILE = 4; + + // TODO: Add the others + } + + /** + * A set of bit flags describing how the resource can be used. + * Note that this member can be specified only if the dwScope member is equal to RESOURCE_GLOBALNET. + * This member can be one of the following values defined in the Winnetwk.h header file. + * NOTE: This are for the dwUsage member of NetResource + */ + public class RESOURCEUSAGE { + /** + * The resource is a connectable resource; the name pointed to by the lpRemoteName member can be passed to the WNetAddConnection function to make a network connection. + */ + public static final int RESOURCEUSAGE_CONNECTABLE = 0x00000001; + + /** + * The resource is a container resource; the name pointed to by the lpRemoteName member can be passed to the WNetOpenEnum function to enumerate the resources in the container. + */ + public static final int RESOURCEUSAGE_CONTAINER = 0x00000002; + + /** + * The resource is not a local device. + */ + public static final int RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004; + + /** + * The resource is a sibling. This value is not used by Windows. + */ + public static final int RESOURCEUSAGE_SIBLING = 0x00000008; + + /** + * The resource must be attached. This value specifies that a function to enumerate resource this should fail if the caller is not authenticated, + * even if the network permits enumeration without authentication. + */ + public static final int RESOURCEUSAGE_ATTACHED = 0x00000010; + + /** + * Setting this value is equivalent to setting RESOURCEUSAGE_CONNECTABLE, RESOURCEUSAGE_CONTAINER, and RESOURCEUSAGE_ATTACHED. + */ + public static final int RESOURCEUSAGE_ALL = RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED; + } + + /** + * A set of bit flags describing how the resource can be used. + * Note that this member can be specified only if the dwScope member is equal to RESOURCE_GLOBALNET. + * This member can be one of the following values defined in the Winnetwk.h header file. + * NOTE: This are for the dwUsage member of NetResource + */ + public class ConnectFlag { + + /** + * This flag instructs the operating system to store the network resource connection. If this bit flag is set, + * the operating system automatically attempts to restore the connection when the user logs on. The system + * remembers only successful connections that redirect local devices. It does not remember connections that are + * unsuccessful or deviceless connections. (A deviceless connection occurs when lpLocalName is NULL or when it + * points to an empty string.) If this bit flag is clear, the operating system does not automatically restore + * the connection at logon. + */ + public static final int CONNECT_UPDATE_PROFILE = 0x00000001; + + /** + * If this flag is set, the operating system may interact with the user for authentication purposes. + */ + public static final int CONNECT_INTERACTIVE = 0x00000008; + + /** + * This flag instructs the system not to use any default settings for user names or passwords without + * offering the user the opportunity to supply an alternative. This flag is ignored unless + * CONNECT_INTERACTIVE is also set. + */ + public static final int CONNECT_PROMPT = 0x00000010; + + /** + * This flag forces the redirection of a local device when making the connection. If the lpLocalName + * member of NETRESOURCE specifies a local device to redirect, this flag has no effect, because the + * operating system still attempts to redirect the specified device. When the operating system automatically + * chooses a local device, the dwType member must not be equal to RESOURCETYPE_ANY. + * If this flag is not set, a local device is automatically chosen for redirection only if the network + * requires a local device to be redirected. + * Windows XP: When the system automatically assigns network drive letters, letters are assigned beginning + * with Z:, then Y:, and ending with C:. This reduces collision between per-logon drive letters (such as + * network drive letters) and global drive letters (such as disk drives). Note that previous releases + * assigned drive letters beginning with C: and ending with Z:. + */ + public static final int CONNECT_REDIRECT = 0x00000080; + + /** + * If this flag is set, the connection was made using a local device redirection. If the lpAccessName parameter + * points to a buffer, the local device name is copied to the buffer. + */ + public static final int CONNECT_LOCALDRIVE = 0x00000100; + + /** + * If this flag is set, the operating system prompts the user for authentication using the command line instead + * of a graphical user interface (GUI). This flag is ignored unless CONNECT_INTERACTIVE is also set. + * Windows 2000/NT and Windows Me/98/95: This value is not supported. + */ + public static final int CONNECT_COMMANDLINE = 0x00000800; + + /** + * If this flag is set, and the operating system prompts for a credential, the credential should be saved by the + * credential manager. If the credential manager is disabled for the caller's logon session, or if the network + * provider does not support saving credentials, this flag is ignored. This flag is also ignored unless you + * set the CONNECT_COMMANDLINE flag. + * Windows 2000/NT and Windows Me/98/95: This value is not supported. + */ + public static final int CONNECT_CMD_SAVECRED = 0x00001000; + } + + /** + * The NETRESOURCE structure contains information about a network resource. + */ + public static class NETRESOURCE extends Structure { + + public static class ByReference extends NETRESOURCE implements Structure.ByReference { + + public ByReference() { + + } + + public ByReference(Pointer memory) { + super(memory); + } + } + + public NETRESOURCE() { + + } + + public NETRESOURCE(Pointer address) { + super(address); + read(); + } + + @Override + protected List getFieldOrder() { + return Arrays.asList("dwScope", "dwType", "dwDisplayType", "dwUsage", "lpLocalName", "lpRemoteName", "lpComment", "lpProvider"); + } + + /** + * The scope of the enumeration. This member can be one of the values defined in class NetResourceSope. + */ + public int dwScope; + + /** + * The type of resource. This member can be one of first 3 values defined in the NetResourceType. + */ + public int dwType; + + /** + * The display options for the network object in a network browsing user interface. This member can be one of the values defined in the NetResourceDisplayType. + */ + public int dwDisplayType; + + /** + * A set of bit flags describing how the resource can be used. + */ + public int dwUsage; + + /** + * If the dwScope member is equal to RESOURCE_CONNECTED or RESOURCE_REMEMBERED, + * this member is a pointer to a null-terminated character string that specifies + * the name of a local device. This member is NULL if the connection does not use + * a device. + */ + public WString lpLocalName; + + /** + * If the entry is a network resource, this member is a pointer to a null-terminated + * character string that specifies the remote network name. + * + * If the entry is a current or persistent connection, lpRemoteName member points to + * the network name associated with the name pointed to by the lpLocalName member. + * + * The string can be MAX_PATH characters in length, and it must follow the network + * provider's naming conventions + */ + public WString lpRemoteName; + + /** + * A pointer to a NULL-terminated string that contains a comment supplied by the network provider. + */ + public WString lpComment; + + /** + * A pointer to a NULL-terminated string that contains the name of the provider that owns the resource. + * This member can be NULL if the provider name is unknown. To retrieve the provider name, you can + * call the WNetGetProviderName function. + */ + public WString lpProvider; + } + + // + // Universal Naming. + // + public static int UNIVERSAL_NAME_INFO_LEVEL = 0x00000001; + public static int REMOTE_NAME_INFO_LEVEL = 0x00000002; + + /** + * The UNIVERSAL_NAME_INFO structure contains a pointer to a Universal Naming Convention (UNC) + * name string for a network resource. + */ + public static class UNIVERSAL_NAME_INFO extends Structure { + + public static class ByReference extends REMOTE_NAME_INFO implements Structure.ByReference { + + public ByReference() { + + } + + public ByReference(Pointer memory) { + super(memory); + } + } + + public UNIVERSAL_NAME_INFO() { + + } + + public UNIVERSAL_NAME_INFO(Pointer address) { + super(address); + read(); + } + + /** + * Pointer to the null-terminated UNC name string that identifies a network resource. + */ + public WString lpUniversalName; + + @Override + protected List getFieldOrder() { + return Arrays.asList("lpUniversalName"); + } + } + + /** + * The REMOTE_NAME_INFO structure contains path and name information for a network resource. + * The structure contains a member that points to a Universal Naming Convention (UNC) name string for + * the resource, and two members that point to additional network connection information strings. + */ + public static class REMOTE_NAME_INFO extends Structure { + + public static class ByReference extends REMOTE_NAME_INFO implements Structure.ByReference { + + public ByReference() { + + } + + public ByReference(Pointer memory) { + super(memory); + } + } + + public REMOTE_NAME_INFO() { + + } + + public REMOTE_NAME_INFO(Pointer address) { + super(address); + read(); + } + + /** + * Pointer to the null-terminated UNC name string that identifies a network resource. + */ + public WString lpUniversalName; + + /** + * Pointer to a null-terminated string that is the name of a network connection. + */ + public WString lpConnectionName; + + /** + * Pointer to a null-terminated name string. + */ + public WString lpRemainingPath; + + @Override + protected List getFieldOrder() { + return Arrays.asList("lpUniversalName", "lpConnectionName", "lpRemainingPath"); + } + } +} diff --git a/contrib/platform/test/com/sun/jna/platform/win32/MprTest.java b/contrib/platform/test/com/sun/jna/platform/win32/MprTest.java new file mode 100644 index 0000000000..a1aca145da --- /dev/null +++ b/contrib/platform/test/com/sun/jna/platform/win32/MprTest.java @@ -0,0 +1,309 @@ +/* Copyright (c) 2015 Adam Marcionek, All Rights Reserved + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +package com.sun.jna.platform.win32; + +import java.io.File; + +import com.sun.jna.Memory; +import com.sun.jna.WString; +import com.sun.jna.platform.win32.LMShare.SHARE_INFO_2; +import com.sun.jna.platform.win32.WinNT.HANDLEByReference; +import com.sun.jna.platform.win32.Winnetwk.ConnectFlag; +import com.sun.jna.platform.win32.Winnetwk.NETRESOURCE; +import com.sun.jna.platform.win32.Winnetwk.REMOTE_NAME_INFO; +import com.sun.jna.platform.win32.Winnetwk.RESOURCESCOPE; +import com.sun.jna.platform.win32.Winnetwk.RESOURCETYPE; +import com.sun.jna.platform.win32.Winnetwk.RESOURCEUSAGE; +import com.sun.jna.platform.win32.Winnetwk.UNIVERSAL_NAME_INFO; +import com.sun.jna.ptr.IntByReference; + +import junit.framework.TestCase; + +/** + * @author amarcionek[at]gmail[dot]com + */ +public class MprTest extends TestCase { + + public static void main(String[] args) throws Exception { + junit.textui.TestRunner.run(MprTest.class); + } + + public void testWNetUseConnection() throws Exception { + // First create a share on the local machine + File fileShareFolder = createTempFolder(); + WString share = createLocalShare(fileShareFolder); + + NETRESOURCE resource = new NETRESOURCE(); + + resource.dwDisplayType = 0; + resource.dwScope = 0; + resource.dwType = RESOURCETYPE.RESOURCETYPE_DISK; + resource.lpRemoteName = new WString("\\\\" + getLocalComputerName() + "\\" + share); + + try { + // Cancel any existing connections of the same name + Mpr.INSTANCE.WNetCancelConnection2(resource.lpRemoteName, 0, true); + // Establish a new one + assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetUseConnection(null, resource, null, null, 0, null, null, null)); + } + finally { + // Clean up resources + Mpr.INSTANCE.WNetCancelConnection2(resource.lpRemoteName, 0, true); + Netapi32.INSTANCE.NetShareDel(null, share, 0); + fileShareFolder.delete(); + } + } + + public void testWNetAddConnection3() throws Exception { + // First create a share on the local machine + File fileShareFolder = createTempFolder(); + WString share = createLocalShare(fileShareFolder); + + NETRESOURCE resource = new NETRESOURCE(); + + resource.dwDisplayType = 0; + resource.dwScope = 0; + resource.dwType = RESOURCETYPE.RESOURCETYPE_DISK; + resource.lpRemoteName = new WString("\\\\" + getLocalComputerName() + "\\" + share); + + try { + // Cancel any existing connections of the same name + Mpr.INSTANCE.WNetCancelConnection2(resource.lpRemoteName, 0, true); + // Establish a new one + assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetAddConnection3(null, resource, null, null, 0)); + } + finally { + // Clean up resources + Mpr.INSTANCE.WNetCancelConnection2(resource.lpRemoteName, 0, true); + Netapi32.INSTANCE.NetShareDel(null, share, 0); + fileShareFolder.delete(); + } + } + + public void testWNetOpenCloseConnection() throws Exception { + HANDLEByReference lphEnum = new HANDLEByReference(); + assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetOpenEnum(RESOURCESCOPE.RESOURCE_CONNECTED, RESOURCETYPE.RESOURCETYPE_DISK, RESOURCEUSAGE.RESOURCEUSAGE_ALL, null, lphEnum)); + assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetCloseEnum(lphEnum.getValue())); + } + + public void testWNetEnumConnection() throws Exception { + int bufferSize = 16 * 1024; // MSDN recommends this as a reasonable size + HANDLEByReference lphEnum = new HANDLEByReference(); + + // Create a local share and connect to it. This ensures the enum will find at least one entry. + File fileShareFolder = createTempFolder(); + WString share = createLocalShare(fileShareFolder); + // Connect to local share + connectToLocalShare(share, null); + + try { + + // Open an enumeration + assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetOpenEnum(RESOURCESCOPE.RESOURCE_CONNECTED, RESOURCETYPE.RESOURCETYPE_DISK, RESOURCEUSAGE.RESOURCEUSAGE_ALL, null, lphEnum)); + + int winError = WinError.ERROR_SUCCESS; + + while (true) { + + Memory memory = new Memory(bufferSize); + + IntByReference lpBufferSize = new IntByReference(bufferSize); + IntByReference lpcCount = new IntByReference(1); + + // Get next value + winError = Mpr.INSTANCE.WNetEnumResource(lphEnum.getValue(), lpcCount, memory, lpBufferSize); + + // Reached end of enumeration + if (winError == WinError.ERROR_NO_MORE_ITEMS) + break; + + // Unlikely, but means our buffer size isn't large enough. + if (winError == WinError.ERROR_MORE_DATA) { + bufferSize = bufferSize * 2; + continue; + } + + // If we get here, it means it has to be a success or our programming logic was wrong. + assertEquals(winError, WinError.ERROR_SUCCESS); + + // Asked for one, should only get one. + assertEquals(1, lpcCount.getValue()); + + // Create a NETRESOURCE based on the memory + NETRESOURCE resource = new NETRESOURCE(memory); + + // Assert things we know for sure. + assertNotNull(resource.lpRemoteName); + } + + // Expect ERROR_NO_MORE_ITEMS here. + assertEquals(winError, WinError.ERROR_NO_MORE_ITEMS); + } + finally { + // Clean up resources + Mpr.INSTANCE.WNetCloseEnum(lphEnum.getValue()); + disconnectFromLocalShare(new WString("\\\\" + getLocalComputerName() + "\\" + share)); + deleteLocalShare(share); + fileShareFolder.delete(); + } + } + + public void testWNetGetUniversalName() throws Exception { + int bufferSize = 1024; // MSDN recommends this as a reasonable size + Memory memory = new Memory(bufferSize); + IntByReference lpBufferSize = new IntByReference(bufferSize); + File file = null; + WString share = null; + String driveLetter = new String("x:"); + File fileShareFolder = createTempFolder(); + + try { + // Create a local share and connect to it. + share = createLocalShare(fileShareFolder); + // Connect to share using a drive letter. + connectToLocalShare(share, new WString(driveLetter)); + + // Create a path on local device redirected to the share. + String filePath = new String(driveLetter + "\\testfile.txt"); + file = new File(filePath); + file.createNewFile(); + + // Test WNetGetUniversalName using UNIVERSAL_NAME_INFO_LEVEL + assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetGetUniversalName(new WString(filePath), Winnetwk.UNIVERSAL_NAME_INFO_LEVEL, memory, lpBufferSize)); + + UNIVERSAL_NAME_INFO uinfo = new UNIVERSAL_NAME_INFO(memory); + assertNotNull(uinfo.lpUniversalName); + + // Test WNetGetUniversalName using REMOTE_NAME_INFO_LEVEL + assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetGetUniversalName(new WString(filePath), Winnetwk.REMOTE_NAME_INFO_LEVEL, memory, lpBufferSize)); + + REMOTE_NAME_INFO rinfo = new REMOTE_NAME_INFO(memory); + assertNotNull(rinfo.lpUniversalName); + assertNotNull(rinfo.lpConnectionName); + assertNotNull(rinfo.lpRemainingPath); + } + finally { + // Clean up resources + if (file != null) + file.delete(); + if (share != null) { + disconnectFromLocalShare(new WString(driveLetter)); + deleteLocalShare(share); + fileShareFolder.delete(); + } + } + } + + private static File createTempFolder() throws Exception { + String folderPath = System.getProperty("java.io.tmpdir") + File.separatorChar + System.nanoTime(); + File file = new File(folderPath); + file.mkdir(); + return file; + } + + /** + * Get local NETBIOS machine name + * @return String with machine name + * @throws Exception + */ + private String getLocalComputerName() throws Exception { + IntByReference lpnSize = new IntByReference(0); + // Get size of char array + Kernel32.INSTANCE.GetComputerName(null, lpnSize); + assertEquals(WinError.ERROR_BUFFER_OVERFLOW, Kernel32.INSTANCE.GetLastError()); + // Allocate character array + char buffer[] = new char[WinBase.MAX_COMPUTERNAME_LENGTH + 1]; + lpnSize.setValue(buffer.length); + assertTrue(Kernel32.INSTANCE.GetComputerName(buffer, lpnSize)); + // Return string with computer name + String computerName = new String(buffer); + computerName = computerName.trim(); + return computerName; + } + + /** + * Create a share on the local machine. + * Uses a temporary directory and shares it out with ACCESS_ALL + * + * @param shareFolder the full path local folder to share + * @return WString with the share name, essentially the top level folder name. + * @throws Exception the exception + */ + private WString createLocalShare(File shareFolder) throws Exception { + + SHARE_INFO_2 shi = new SHARE_INFO_2(); + shi.shi2_netname = new WString(shareFolder.getName()); + shi.shi2_type = LMShare.STYPE_DISKTREE; + shi.shi2_remark = new WString(""); + shi.shi2_permissions = LMAccess.ACCESS_ALL; + shi.shi2_max_uses = -1; + shi.shi2_current_uses = 0; + shi.shi2_path = new WString(shareFolder.getAbsolutePath()); + shi.shi2_passwd = new WString(""); + + // Write from struct to native memory. + shi.write(); + + IntByReference parm_err = new IntByReference(0); + assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetShareAdd(null, // Use local computer + 2, + shi.getPointer(), + parm_err)); + + return new WString(shareFolder.getName()); + } + + /** + * Delete a local share + * @param share + */ + private void deleteLocalShare(WString share) { + Netapi32.INSTANCE.NetShareDel(null, share, 0); + } + + /** + * Connect to a local share on the local machine. Assumes the share is already present + * + * @param share name of share on local computer. + * @param lpLocalName name of local device to redirect to. E.g. F:. If null, makes a + * connection without redirecting. + * @throws Exception the exception + */ + private void connectToLocalShare(WString share, WString lpLocalName) throws Exception { + NETRESOURCE resource = new NETRESOURCE(); + + resource.dwDisplayType = 0; + resource.dwScope = 0; + resource.dwType = RESOURCETYPE.RESOURCETYPE_DISK; + resource.lpLocalName = lpLocalName; + resource.lpRemoteName = new WString("\\\\" + getLocalComputerName() + "\\" + share); + + // Establish connection + assertEquals(WinError.ERROR_SUCCESS, Mpr.INSTANCE.WNetAddConnection3(null, resource, null, null, 0)); + } + + /** + * Disconnect from a share. + * + * @param lpName [in] Pointer to a constant null-terminated string that specifies the name of either + * the redirected local device or the remote network resource to disconnect from. If this parameter + * specifies a redirected local device, the function cancels only the specified device redirection. + * If the parameter specifies a remote network resource, all connections without devices are canceled. + */ + private void disconnectFromLocalShare(WString lpName) { + // Remove connection + Mpr.INSTANCE.WNetCancelConnection2(lpName, ConnectFlag.CONNECT_UPDATE_PROFILE, true); + } +} \ No newline at end of file diff --git a/contrib/platform/test/com/sun/jna/platform/win32/Netapi32Test.java b/contrib/platform/test/com/sun/jna/platform/win32/Netapi32Test.java index ca6c47b43a..297454e1e3 100644 --- a/contrib/platform/test/com/sun/jna/platform/win32/Netapi32Test.java +++ b/contrib/platform/test/com/sun/jna/platform/win32/Netapi32Test.java @@ -12,7 +12,7 @@ */ package com.sun.jna.platform.win32; -import junit.framework.TestCase; +import java.io.File; import com.sun.jna.WString; import com.sun.jna.platform.win32.DsGetDC.DS_DOMAIN_TRUSTS; @@ -21,6 +21,8 @@ import com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0; import com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0; import com.sun.jna.platform.win32.LMAccess.USER_INFO_1; +import com.sun.jna.platform.win32.LMShare.SHARE_INFO_2; +import com.sun.jna.platform.win32.LMShare.SHARE_INFO_502; import com.sun.jna.platform.win32.NTSecApi.LSA_FOREST_TRUST_RECORD; import com.sun.jna.platform.win32.NTSecApi.PLSA_FOREST_TRUST_INFORMATION; import com.sun.jna.platform.win32.NTSecApi.PLSA_FOREST_TRUST_RECORD; @@ -29,6 +31,8 @@ import com.sun.jna.ptr.IntByReference; import com.sun.jna.ptr.PointerByReference; +import junit.framework.TestCase; + /** * @author dblock[at]dblock[dot]org */ @@ -257,4 +261,106 @@ public void testDsEnumerateDomainTrusts() { assertEquals(W32Errors.ERROR_SUCCESS, Netapi32.INSTANCE.NetApiBufferFree(domainTrustRefs.getPointer())); } + public void testNetShareAddShareInfo2() throws Exception { + + File fileShareFolder = createTempFolder(); + + SHARE_INFO_2 shi = new SHARE_INFO_2(); + shi.shi2_netname = new WString(fileShareFolder.getName()); + shi.shi2_type = LMShare.STYPE_DISKTREE; + shi.shi2_remark = new WString(""); + shi.shi2_permissions = LMAccess.ACCESS_ALL; + shi.shi2_max_uses = -1; + shi.shi2_current_uses = 0; + shi.shi2_path = new WString(fileShareFolder.getAbsolutePath()); + shi.shi2_passwd = new WString(""); + + // Write from struct to native memory. + shi.write(); + + IntByReference parm_err = new IntByReference(0); + int winError = Netapi32.INSTANCE.NetShareAdd(null, // Use local computer + 2, + shi.getPointer(), + parm_err); + + if (winError == W32Errors.ERROR_INVALID_PARAMETER) + { + // fail with offset. + throw new Exception("testNetShareAddShareInfo2 failed with invalid parameter on structure offset: " + parm_err.getValue()); + } + + assertEquals(LMErr.NERR_Success, winError); + + Netapi32.INSTANCE.NetShareDel(null, shi.shi2_netname, 0); + } + + public void testNetShareAddShareInfo502() throws Exception { + + File fileShareFolder = createTempFolder(); + + SHARE_INFO_502 shi = new SHARE_INFO_502(); + shi.shi502_netname = new WString(fileShareFolder.getName()); + shi.shi502_type = LMShare.STYPE_DISKTREE; + shi.shi502_remark = new WString(""); + shi.shi502_permissions = LMAccess.ACCESS_ALL; + shi.shi502_max_uses = -1; + shi.shi502_current_uses = 0; + shi.shi502_path = new WString(fileShareFolder.getAbsolutePath()); + shi.shi502_passwd = null; + shi.shi502_reserved = 0; + shi.shi502_security_descriptor = null; + + // Write from struct to native memory. + shi.write(); + + IntByReference parm_err = new IntByReference(0); + int winError = Netapi32.INSTANCE.NetShareAdd(null, // Use local computer + 502, + shi.getPointer(), + parm_err); + + if (winError == W32Errors.ERROR_INVALID_PARAMETER) + { + // fail with offset. + throw new Exception("testNetShareAddShareInfo502 failed with invalid parameter on structure offset: " + parm_err.getValue()); + } + + assertEquals(LMErr.NERR_Success, winError); + + Netapi32.INSTANCE.NetShareDel(null, shi.shi502_netname, 0); + } + + public void testNetShareDel() throws Exception { + + File fileShareFolder = createTempFolder(); + + SHARE_INFO_2 shi = new SHARE_INFO_2(); + shi.shi2_netname = new WString(fileShareFolder.getName()); + shi.shi2_type = LMShare.STYPE_DISKTREE; + shi.shi2_remark = new WString(""); + shi.shi2_permissions = LMAccess.ACCESS_ALL; + shi.shi2_max_uses = -1; + shi.shi2_current_uses = 0; + shi.shi2_path = new WString(fileShareFolder.getAbsolutePath()); + shi.shi2_passwd = new WString(""); + + // Write from struct to native memory. + shi.write(); + + IntByReference parm_err = new IntByReference(0); + assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetShareAdd(null, // Use local computer + 2, + shi.getPointer(), + parm_err)); + + assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetShareDel(null, shi.shi2_netname, 0)); + } + + private File createTempFolder() throws Exception{ + String folderPath = System.getProperty("java.io.tmpdir") + File.separatorChar + System.nanoTime(); + File file = new File(folderPath); + file.mkdir(); + return file; + } }