1 /* Copyright (c) 2015 Adam Marcionek, All Rights Reserved
2  *
3  * The contents of this file is dual-licensed under 2
4  * alternative Open Source/Free licenses: LGPL 2.1 or later and
5  * Apache License 2.0. (starting with JNA version 4.0.0).
6  *
7  * You can freely decide which license you want to apply to
8  * the project.
9  *
10  * You may obtain a copy of the LGPL License at:
11  *
12  * http://www.gnu.org/licenses/licenses.html
13  *
14  * A copy is also included in the downloadable source code package
15  * containing JNA, in file "LGPL2.1".
16  *
17  * You may obtain a copy of the Apache License at:
18  *
19  * http://www.apache.org/licenses/
20  *
21  * A copy is also included in the downloadable source code package
22  * containing JNA, in file "AL2.0".
23  */
24 package com.sun.jna.platform.win32;
25 
26 import com.sun.jna.Native;
27 import com.sun.jna.Pointer;
28 import com.sun.jna.platform.win32.WinDef.HWND;
29 import com.sun.jna.platform.win32.WinNT.HANDLE;
30 import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
31 import com.sun.jna.platform.win32.Winnetwk.*;
32 import com.sun.jna.ptr.IntByReference;
33 import com.sun.jna.win32.StdCallLibrary;
34 import com.sun.jna.win32.W32APIOptions;
35 
36 /**
37  * Ported from Winnetwk.h. Microsoft Windows SDK 8.1
38  *
39  * @author amarcionek[at]gmail.com
40  */
41 
42 public interface Mpr extends StdCallLibrary {
43 
44     Mpr INSTANCE = Native.load("Mpr", Mpr.class, W32APIOptions.DEFAULT_OPTIONS);
45 
46     /**
47      * The WNetOpenEnum function starts an enumeration of network resources or
48      * existing connections. You can continue the enumeration by calling the
49      * WNetEnumResource function.
50      *
51      * @param dwScope
52      *            Scope of the enumeration. This parameter can be one of the
53      *            following values from NETRESOURCEScope: RESOURCE_CONNECTED,
54      *            RESOURCE_CONTEXT, RESOURCE_GLOBALNET, RESOURCE_REMEMBERED
55      * @param dwType
56      *            Resource types to be enumerated. This parameter can be a
57      *            combination of the following values from NETRESOURCEType:
58      *            RESOURCETYPE_ANY, RESOURCETYPE_DISK, RESOURCETYPE_PRINT
59      * @param dwUsage
60      *            Resource usage type to be enumerated. This parameter can be a
61      *            combination of the following values from NETRESOURCEUsage: 0,
62      *            RESOURCEUSAGE_CONNECTABLE, RESOURCEUSAGE_CONTAINER,
63      *            RESOURCEUSAGE_ATTACHED, RESOURCEUSAGE_ALL
64      * @param lpNETRESOURCE
65      *            Pointer to a NETRESOURCE structure that specifies the
66      *            container to enumerate. If the dwScope parameter is not
67      *            RESOURCE_GLOBALNET, this parameter must be NULL. If this
68      *            parameter is NULL, the root of the network is assumed. (The
69      *            system organizes a network as a hierarchy; the root is the
70      *            topmost container in the network.) If this parameter is not
71      *            NULL, it must point to a NETRESOURCE structure. This structure
72      *            can be filled in by the application or it can be returned by a
73      *            call to the WNetEnumResource function. The NETRESOURCE
74      *            structure must specify a container resource; that is, the
75      *            RESOURCEUSAGE_CONTAINER value must be specified in the dwUsage
76      *            parameter. To enumerate all network resources, an application
77      *            can begin the enumeration by calling WNetOpenEnum with the
78      *            lpNETRESOURCE parameter set to NULL, and then use the returned
79      *            handle to call WNetEnumResource to enumerate resources. If one
80      *            of the resources in the NETRESOURCE array returned by the
81      *            WNetEnumResource function is a container resource, you can
82      *            call WNetOpenEnum to open the resource for further
83      *            enumeration.
84      * @param lphEnum
85      *            Pointer to an enumeration handle that can be used in a
86      *            subsequent call to WNetEnumResource.
87      * @return <code>NO_ERROR</code> if the function succeeds, otherwise a
88      *         system error code. See MSDN documentation for common error
89      *         values:
90      *         https://msdn.microsoft.com/en-us/library/windows/desktop/aa385478
91      *         (v=vs.85).aspx
92      */
WNetOpenEnum(int dwScope, int dwType, int dwUsage, NETRESOURCE.ByReference lpNETRESOURCE, HANDLEByReference lphEnum)93     public int WNetOpenEnum(int dwScope, int dwType, int dwUsage, NETRESOURCE.ByReference lpNETRESOURCE, HANDLEByReference lphEnum);
94 
95     /**
96      * The WNetEnumResource function continues an enumeration of network
97      * resources that was started by a call to the WNetOpenEnum function.
98      *
99      * @param hEnum
100      *            [in] Handle that identifies an enumeration instance. This
101      *            handle must be returned by the WNetOpenEnum function.
102      * @param lpcCount
103      *            [in, out] Pointer to a variable specifying the number of
104      *            entries requested. If the number requested is -1, the function
105      *            returns as many entries as possible. If the function succeeds,
106      *            on return the variable pointed to by this parameter contains
107      *            the number of entries actually read.
108      * @param lpBuffer
109      *            [out] Pointer to the buffer that receives the enumeration
110      *            results. The results are returned as an array of NETRESOURCE
111      *            structures. Note that the buffer you allocate must be large
112      *            enough to hold the structures, plus the strings to which their
113      *            members point. For more information, see the Remarks section
114      *            on MSDN:
115      *            https://msdn.microsoft.com/en-us/library/windows/desktop/
116      *            aa385449(v=vs.85).aspx The buffer is valid until the next call
117      *            using the handle specified by the hEnum parameter. The order
118      *            of NETRESOURCE structures in the array is not predictable.
119      * @param lpBufferSize
120      *            [in, out] Pointer to a variable that specifies the size of the
121      *            lpBuffer parameter, in bytes. If the buffer is too small to
122      *            receive even one entry, this parameter receives the required
123      *            size of the buffer.
124      * @return If the function succeeds, the return value is one of the
125      *         following values: NO_ERROR - The enumeration succeeded, and the
126      *         buffer contains the requested data. The calling application can
127      *         continue to call WNetEnumResource to complete the enumeration.
128      *         ERROR_NO_MORE_ITEMS - There are no more entries. The buffer
129      *         contents are undefined. If the function fails, see MSDN
130      *         documentation for common error values:
131      *         https://msdn.microsoft.com/en-us/library/windows/desktop/aa385478
132      *         (v=vs.85).aspx
133      */
WNetEnumResource(HANDLE hEnum, IntByReference lpcCount, Pointer lpBuffer, IntByReference lpBufferSize)134     public int WNetEnumResource(HANDLE hEnum, IntByReference lpcCount, Pointer lpBuffer, IntByReference lpBufferSize);
135 
136     /**
137      * The WNetCloseEnum function ends a network resource enumeration started by
138      * a call to the WNetOpenEnum function.
139      *
140      * @param hEnum
141      *            [in] Handle that identifies an enumeration instance. This
142      *            handle must be returned by the WNetOpenEnum function.
143      * @return <code>NO_ERROR</code> if the function succeeds, otherwise a
144      *         system error code. See MSDN documentation for common error
145      *         values:
146      *         https://msdn.microsoft.com/en-us/library/windows/desktop/aa385431
147      *         (v=vs.85).aspx
148      */
WNetCloseEnum(HANDLE hEnum)149     int WNetCloseEnum(HANDLE hEnum);
150 
151     /**
152      * The WNetGetUniversalName function takes a drive-based path for a network
153      * resource and returns an information structure that contains a more
154      * universal form of the name.
155      *
156      * @param lpLocalPath
157      *            [in] A pointer to a constant null-terminated string that is a
158      *            drive-based path for a network resource. For example, if drive
159      *            H has been mapped to a network drive share, and the network
160      *            resource of interest is a file named Sample.doc in the
161      *            directory \Win32\Examples on that share, the drive-based path
162      *            is H:\Win32\Examples\Sample.doc.
163      * @param dwInfoLevel
164      *            [in] The type of structure that the function stores in the
165      *            buffer pointed to by the lpBuffer parameter. This parameter
166      *            can be one of the following values defined in the
167      *            Winnetwk.java. UNIVERSAL_NAME_INFO_LEVEL - The function stores
168      *            a UNIVERSAL_NAME_INFO structure in the buffer.
169      *            REMOTE_NAME_INFO_LEVEL - The function stores a
170      *            REMOTE_NAME_INFO structure in the buffer. The
171      *            UNIVERSAL_NAME_INFO structure points to a Universal Naming
172      *            Convention (UNC) name string. The REMOTE_NAME_INFO structure
173      *            points to a UNC name string and two additional connection
174      *            information strings. For more information, see the following
175      *            Remarks section.
176      * @param lpBuffer
177      *            [out] A pointer to a buffer that receives the structure
178      *            specified by the dwInfoLevel parameter.
179      * @param lpBufferSize
180      *            [in,out] A pointer to a variable that specifies the size, in
181      *            bytes, of the buffer pointed to by the lpBuffer parameter. If
182      *            the function succeeds, it sets the variable pointed to by
183      *            lpBufferSize to the number of bytes stored in the buffer. If
184      *            the function fails because the buffer is too small, this
185      *            location receives the required buffer size, and the function
186      *            returns ERROR_MORE_DATA.
187      * @return If the function succeeds, the return value is NO_ERROR, otherwise
188      *         see MSDN for common error codes:
189      *         https://msdn.microsoft.com/en-us/library/windows/desktop/aa385474
190      *         (v=vs.85).aspx
191      */
WNetGetUniversalName(String lpLocalPath, int dwInfoLevel, Pointer lpBuffer, IntByReference lpBufferSize)192     int WNetGetUniversalName(String lpLocalPath, int dwInfoLevel, Pointer lpBuffer, IntByReference lpBufferSize);
193 
194     /**
195      * The WNetUseConnection function makes a connection to a network resource.
196      * The function can redirect a local device to a network resource.
197      *
198      * The WNetUseConnection function is similar to the WNetAddConnection3
199      * function. The main difference is that WNetUseConnection can automatically
200      * select an unused local device to redirect to the network resource.
201      *
202      * @param hwndOwner
203      *            [in] Handle to a window that the provider of network resources
204      *            can use as an owner window for dialog boxes. Use this
205      *            parameter if you set the CONNECT_INTERACTIVE value in the
206      *            dwFlags parameter.
207      * @param lpNETRESOURCE
208      *            [in] Pointer to a NETRESOURCE structure that specifies details
209      *            of the proposed connection. The structure contains information
210      *            about the network resource, the local device, and the network
211      *            resource provider.
212      *
213      *            You must specify the following members of the NETRESOURCE
214      *            structure. The WNetUseConnection function ignores the other
215      *            members of the NETRESOURCE structure. For more information,
216      *            see the descriptions following for the dwFlags parameter.
217      *
218      *            dwType Specifies the type of resource to connect to. It is
219      *            most efficient to specify a resource type in this member, such
220      *            as RESOURCETYPE_DISK or RESOURCETYPE_PRINT. However, if the
221      *            lpLocalName member is NULL, or if it points to an empty string
222      *            and CONNECT_REDIRECT is not set, dwType can be
223      *            RESOURCETYPE_ANY.
224      *
225      *            This method works only if the function does not automatically
226      *            choose a device to redirect to the network resource. Although
227      *            this member is required, its information may be ignored by the
228      *            network service provider lpLocalName Pointer to a
229      *            null-terminated string that specifies the name of a local
230      *            device to be redirected, such as "F:" or "LPT1". The string is
231      *            treated in a case-insensitive manner. If the string is empty,
232      *            or if lpLocalName is NULL, a connection to the network occurs
233      *            without redirection. If the CONNECT_REDIRECT value is set in
234      *            the dwFlags parameter, or if the network requires a redirected
235      *            local device, the function chooses a local device to redirect
236      *            and returns the name of the device in the lpAccessName
237      *            parameter. lpRemoveName Pointer to a null-terminated string
238      *            that specifies the network resource to connect to. The string
239      *            can be up to MAX_PATH characters in length, and it must follow
240      *            the network provider's naming conventions. lpProvider Pointer
241      *            to a null-terminated string that specifies the network
242      *            provider to connect to. If lpProvider is NULL, or if it points
243      *            to an empty string, the operating system attempts to determine
244      *            the correct provider by parsing the string pointed to by the
245      *            lpRemoteName member. If this member is not NULL, the operating
246      *            system attempts to make a connection only to the named network
247      *            provider. You should set this member only if you know the
248      *            network provider you want to use. Otherwise, let the operating
249      *            system determine which provider the network name maps to.
250      * @param lpPassword
251      *            [in] Pointer to a constant null-terminated string that
252      *            specifies a password to be used in making the network
253      *            connection. If lpPassword is NULL, the function uses the
254      *            current default password associated with the user specified by
255      *            lpUserID. If lpPassword points to an empty string, the
256      *            function does not use a password. If the connection fails
257      *            because of an invalid password and the CONNECT_INTERACTIVE
258      *            value is set in the dwFlags parameter, the function displays a
259      *            dialog box asking the user to type the password.
260      * @param lpUserID
261      *            [in] Pointer to a constant null-terminated string that
262      *            specifies a user name for making the connection. If lpUserID
263      *            is NULL, the function uses the default user name. (The user
264      *            context for the process provides the default user name.) The
265      *            lpUserID parameter is specified when users want to connect to
266      *            a network resource for which they have been assigned a user
267      *            name or account other than the default user name or account.
268      *            The user-name string represents a security context. It may be
269      *            specific to a network provider. For security context, see
270      *            https://msdn.microsoft.com/en-us/library/windows/desktop/
271      *            ms721625(v=vs.85).aspx
272      * @param dwFlags
273      *            [in] Set of bit flags describing the connection. This
274      *            parameter can be any combination of the values in ConnectFlag.
275      * @param lpAccessName
276      *            [out] Pointer to a buffer that receives system requests on the
277      *            connection. This parameter can be NULL. If this parameter is
278      *            specified, and the lpLocalName member of the NETRESOURCE
279      *            structure specifies a local device, this buffer receives the
280      *            local device name. If lpLocalName does not specify a device
281      *            and the network requires a local device redirection, or if the
282      *            CONNECT_REDIRECT value is set, this buffer receives the name
283      *            of the redirected local device. Otherwise, the name copied
284      *            into the buffer is that of a remote resource. If specified,
285      *            this buffer must be at least as large as the string pointed to
286      *            by the lpRemoteName member.
287      * @param lpBufferSize
288      *            [in, out] Pointer to a variable that specifies the size of the
289      *            lpAccessName buffer, in characters. If the call fails because
290      *            the buffer is not large enough, the function returns the
291      *            required buffer size in this location. For more information,
292      *            see the descriptions of the lpAccessName parameter and the
293      *            ERROR_MORE_DATA error code in the Return Values section.
294      * @param lpResult
295      *            [out] Pointer to a variable that receives additional
296      *            information about the connection. This parameter can be the
297      *            following value:
298      *
299      *            ConnectFlag.CONNECT_LOCALDRIVE - If this flag is set, the
300      *            connection was made using a local device redirection. If the
301      *            lpAccessName parameter points to a buffer, the local device
302      *            name is copied to the buffer.
303      * @return <code>NO_ERROR</code> if the function succeeds, otherwise a
304      *         system error code. See MSDN documentation for common error
305      *         values:
306      *         https://msdn.microsoft.com/en-us/library/windows/desktop/aa385482
307      *         (v=vs.85).aspx
308      */
WNetUseConnection(HWND hwndOwner, NETRESOURCE lpNETRESOURCE, String lpPassword, String lpUserID, int dwFlags, Pointer lpAccessName, IntByReference lpBufferSize, IntByReference lpResult)309     public int WNetUseConnection(HWND hwndOwner, NETRESOURCE lpNETRESOURCE, String lpPassword, String lpUserID, int dwFlags,
310             Pointer lpAccessName, IntByReference lpBufferSize, IntByReference lpResult);
311 
312     /**
313      * The WNetAddConnection3 function makes a connection to a network resource.
314      * The function can redirect a local device to the network resource.
315      *
316      * @param hwndOwner
317      *            [in] Handle to a window that the provider of network resources
318      *            can use as an owner window for dialog boxes. Use this
319      *            parameter if you set the CONNECT_INTERACTIVE value in the
320      *            dwFlags parameter.
321      * @param lpNETRESOURCE
322      *            [in] Pointer to a NETRESOURCE structure that specifies details
323      *            of the proposed connection. The structure contains information
324      *            about the network resource, the local device, and the network
325      *            resource provider.
326      *
327      *            You must specify the following members of the NETRESOURCE
328      *            structure. The WNetUseConnection function ignores the other
329      *            members of the NETRESOURCE structure. For more information,
330      *            see the descriptions following for the dwFlags parameter.
331      *
332      *            dwType Specifies the type of resource to connect to. It is
333      *            most efficient to specify a resource type in this member, such
334      *            as RESOURCETYPE_DISK or RESOURCETYPE_PRINT. However, if the
335      *            lpLocalName member is NULL, or if it points to an empty string
336      *            and CONNECT_REDIRECT is not set, dwType can be
337      *            RESOURCETYPE_ANY.
338      *
339      *            This method works only if the function does not automatically
340      *            choose a device to redirect to the network resource. Although
341      *            this member is required, its information may be ignored by the
342      *            network service provider lpLocalName Pointer to a
343      *            null-terminated string that specifies the name of a local
344      *            device to be redirected, such as "F:" or "LPT1". The string is
345      *            treated in a case-insensitive manner. If the string is empty,
346      *            or if lpLocalName is NULL, a connection to the network occurs
347      *            without redirection. If the CONNECT_REDIRECT value is set in
348      *            the dwFlags parameter, or if the network requires a redirected
349      *            local device, the function chooses a local device to redirect
350      *            and returns the name of the device in the lpAccessName
351      *            parameter. lpRemoveName Pointer to a null-terminated string
352      *            that specifies the network resource to connect to. The string
353      *            can be up to MAX_PATH characters in length, and it must follow
354      *            the network provider's naming conventions. lpProvider Pointer
355      *            to a null-terminated string that specifies the network
356      *            provider to connect to. If lpProvider is NULL, or if it points
357      *            to an empty string, the operating system attempts to determine
358      *            the correct provider by parsing the string pointed to by the
359      *            lpRemoteName member. If this member is not NULL, the operating
360      *            system attempts to make a connection only to the named network
361      *            provider. You should set this member only if you know the
362      *            network provider you want to use. Otherwise, let the operating
363      *            system determine which provider the network name maps to.
364      * @param lpPassword
365      *            [in] Pointer to a constant null-terminated string that
366      *            specifies a password to be used in making the network
367      *            connection. If lpPassword is NULL, the function uses the
368      *            current default password associated with the user specified by
369      *            lpUserID. If lpPassword points to an empty string, the
370      *            function does not use a password. If the connection fails
371      *            because of an invalid password and the CONNECT_INTERACTIVE
372      *            value is set in the dwFlags parameter, the function displays a
373      *            dialog box asking the user to type the password.
374      * @param lpUserID
375      *            [in] Pointer to a constant null-terminated string that
376      *            specifies a user name for making the connection. If lpUserID
377      *            is NULL, the function uses the default user name. (The user
378      *            context for the process provides the default user name.) The
379      *            lpUserID parameter is specified when users want to connect to
380      *            a network resource for which they have been assigned a user
381      *            name or account other than the default user name or account.
382      *            The user-name string represents a security context. It may be
383      *            specific to a network provider. For security context, see
384      *            https://msdn.microsoft.com/en-us/library/windows/desktop/
385      *            ms721625(v=vs.85).aspx
386      * @param dwFlags
387      *            [in] Set of bit flags describing the connection. This
388      *            parameter can be any combination of the values in ConnectFlag.
389      */
WNetAddConnection3(HWND hwndOwner, NETRESOURCE lpNETRESOURCE, String lpPassword, String lpUserID, int dwFlags)390     public int WNetAddConnection3(HWND hwndOwner, NETRESOURCE lpNETRESOURCE, String lpPassword, String lpUserID, int dwFlags);
391 
392     /**
393      * The WNetCancelConnection2 function cancels an existing network
394      * connection. You can also call the function to remove remembered network
395      * connections that are not currently connected.
396      *
397      * @param lpName
398      *            [in] Pointer to a constant null-terminated string that
399      *            specifies the name of either the redirected local device or
400      *            the remote network resource to disconnect from. If this
401      *            parameter specifies a redirected local device, the function
402      *            cancels only the specified device redirection. If the
403      *            parameter specifies a remote network resource, all connections
404      *            without devices are canceled.
405      * @param dwFlags
406      *            [in] Connection type. The following values are defined. 0 -
407      *            The system does not update information about the connection.
408      *            If the connection was marked as persistent in the registry,
409      *            the system continues to restore the connection at the next
410      *            logon. If the connection was not marked as persistent, the
411      *            function ignores the setting of the CONNECT_UPDATE_PROFILE
412      *            flag. CONNECT_UPDATE_PROFILE - The system updates the user
413      *            profile with the information that the connection is no longer
414      *            a persistent one. The system will not restore this connection
415      *            during subsequent logon operations. (Disconnecting resources
416      *            using remote names has no effect on persistent connections.)
417      * @param fForce
418      *            [in] Specifies whether the disconnection should occur if there
419      *            are open files or jobs on the connection. If this parameter is
420      *            FALSE, the function fails if there are open files or jobs.
421      * @return <code>NO_ERROR</code> if the function succeeds, otherwise a
422      *         system error code. See MSDN documentation for common error
423      *         values:
424      *         https://msdn.microsoft.com/en-us/library/windows/desktop/aa385482
425      *         (v=vs.85).aspx
426      */
WNetCancelConnection2(String lpName, int dwFlags, boolean fForce)427     public int WNetCancelConnection2(String lpName, int dwFlags, boolean fForce);
428 }
429