Don't open device if already open.
[wine] / dlls / advapi32 / advapi.c
1 /*
2  * Win32 advapi functions
3  *
4  * Copyright 1995 Sven Verdoolaege
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32 #include "winreg.h"
33 #include "winternl.h"
34 #include "winerror.h"
35
36 #include "wine/library.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
40
41 /******************************************************************************
42  * GetUserNameA [ADVAPI32.@]
43  *
44  * Get the current user name.
45  *
46  * PARAMS
47  *  lpszName [O]   Destination for the user name.
48  *  lpSize   [I/O] Size of lpszName.
49  *
50  * RETURNS
51  *  Success: The length of the user name, including terminating NUL.
52  *  Failure: ERROR_MORE_DATA if *lpSize is too small.
53  */
54 BOOL WINAPI
55 GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
56 {
57     WCHAR *buffer;
58     BOOL ret;
59     DWORD sizeW = *lpSize * 2;
60
61     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeW * sizeof(WCHAR) )))
62     {
63         SetLastError( ERROR_NOT_ENOUGH_MEMORY );
64         return FALSE;
65     }
66     ret = GetUserNameW( buffer, &sizeW );
67     if (ret)
68     {
69         if (!(*lpSize = WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpszName, *lpSize, NULL, NULL )))
70         {
71             *lpSize = WideCharToMultiByte( CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL );
72             SetLastError( ERROR_MORE_DATA );
73             ret = FALSE;
74         }
75     }
76     else *lpSize = sizeW * 2;
77     HeapFree( GetProcessHeap(), 0, buffer );
78     return ret;
79 }
80
81 /******************************************************************************
82  * GetUserNameW [ADVAPI32.@]
83  *
84  * See GetUserNameA.
85  */
86 BOOL WINAPI
87 GetUserNameW( LPWSTR lpszName, LPDWORD lpSize )
88 {
89     const char *name = wine_get_user_name();
90     DWORD len = MultiByteToWideChar( CP_UNIXCP, 0, name, -1, NULL, 0 );
91
92     if (len > *lpSize)
93     {
94         SetLastError(ERROR_MORE_DATA);
95         *lpSize = len;
96         return FALSE;
97     }
98
99     *lpSize = len;
100     MultiByteToWideChar( CP_UNIXCP, 0, name, -1, lpszName, len );
101     return TRUE;
102 }
103
104 /******************************************************************************
105  * GetCurrentHwProfileA [ADVAPI32.@]
106  *
107  * Get the current hardware profile.
108  *
109  * PARAMS
110  *  pInfo [O] Destination for hardware profile information.
111  *
112  * RETURNS
113  *  Success: TRUE. pInfo is updated with the hardware profile details.
114  *  Failure: FALSE.
115  */
116 BOOL WINAPI GetCurrentHwProfileA(LPHW_PROFILE_INFOA pInfo)
117 {
118         FIXME("(%p) semi-stub\n", pInfo);
119         pInfo->dwDockInfo = DOCKINFO_DOCKED;
120         strcpy(pInfo->szHwProfileGuid,"{12340001-1234-1234-1234-1233456789012}");
121         strcpy(pInfo->szHwProfileName,"Wine Profile");
122         return 1;
123 }
124
125 /******************************************************************************
126  * GetCurrentHwProfileW [ADVAPI32.@]
127  *
128  * See GetCurrentHwProfileA.
129  */
130 BOOL WINAPI GetCurrentHwProfileW(LPHW_PROFILE_INFOW pInfo)
131 {
132        FIXME("(%p)\n", pInfo);
133        return FALSE;
134 }
135
136
137 /**************************************************************************
138  *      IsTextUnicode (ADVAPI32.@)
139  *
140  * Attempt to guess whether a text buffer is Unicode.
141  *
142  * PARAMS
143  *  buf   [I] Text buffer to test
144  *  len   [I] Length of buf
145  *  flags [O] Destination for test results
146  */
147 BOOL WINAPI IsTextUnicode( LPCVOID buf, INT len, LPINT flags )
148 {
149     return RtlIsTextUnicode( buf, len, flags );
150 }
151
152
153 /******************************************************************************
154  * AbortSystemShutdownA [ADVAPI32.@]
155  *
156  * Stop a system shutdown if one is in progress.
157  *
158  * PARAMS
159  *  lpMachineName [I] Name of machine to not shutdown.
160  *
161  * RETURNS
162  *  Success: TRUE.
163  *  Failure: FALSE.
164  *
165  * NOTES
166  *  The Wine implementation of this function is a harmless stub.
167  */
168 BOOL WINAPI AbortSystemShutdownA( LPSTR lpMachineName )
169 {
170     TRACE("stub %s (harmless)\n", lpMachineName);
171     return TRUE;
172 }
173
174 /******************************************************************************
175  * AbortSystemShutdownW [ADVAPI32.@]
176  *
177  * See AbortSystemShutdownA.
178  */
179 BOOL WINAPI AbortSystemShutdownW( LPWSTR lpMachineName )
180 {
181     TRACE("stub %s (harmless)\n", debugstr_w(lpMachineName));
182     return TRUE;
183 }
184
185 /******************************************************************************
186  * InitiateSystemShutdownExA [ADVAPI32.@]
187  *
188  * Initiate a shutdown or optionally restart the computer.
189  *
190  * PARAMS
191  *  lpMachineName    [I] Network name of machine to shutdown.
192  *  lpMessage        [I] Message displayed in shutdown dialog box.
193  *  dwTimeout        [I] Number of seconds dialog is displayed before shutdown.
194  *  bForceAppsClosed [I] If TRUE, apps close without saving, else dialog is
195  *                       displayed requesting user to close apps.
196  *  bRebootAfterShutdown [I] If TRUE, system reboots after restart, else the
197  *                           system flushes all caches to disk and clears
198  *                           the screen
199  *  dwReason [I] Reason for shutting down.  Must be a system shutdown reason
200  *               code.
201  *
202  *  RETURNS
203  *   Success: TRUE
204  *   Failure: FALSE
205  *
206  *  NOTES
207  *   if lpMachineName is NULL, the local computer is shutdown.
208  */
209 BOOL WINAPI InitiateSystemShutdownExA( LPSTR lpMachineName, LPSTR lpMessage,
210          DWORD dwTimeout, BOOL bForceAppsClosed, BOOL bRebootAfterShutdown,
211          DWORD dwReason)
212 {
213      FIXME("%s %s %ld %d %d %ld\n", debugstr_a(lpMachineName),
214             debugstr_a(lpMessage), dwTimeout, bForceAppsClosed,
215             bRebootAfterShutdown, dwReason);
216      return TRUE;
217
218
219 /******************************************************************************
220  * InitiateSystemShutdownExW [ADVAPI32.@]
221  *
222  * see InitiateSystemShutdownExA
223  */
224 BOOL WINAPI InitiateSystemShutdownExW( LPWSTR lpMachineName, LPWSTR lpMessage,
225          DWORD dwTimeout, BOOL bForceAppsClosed, BOOL bRebootAfterShutdown,
226          DWORD dwReason)
227 {
228      FIXME("%s %s %ld %d %d %ld\n", debugstr_w(lpMachineName),
229             debugstr_w(lpMessage), dwTimeout, bForceAppsClosed,
230             bRebootAfterShutdown, dwReason);
231      return TRUE;
232
233
234 BOOL WINAPI InitiateSystemShutdownA( LPSTR lpMachineName, LPSTR lpMessage, DWORD dwTimeout,
235                                      BOOL bForceAppsClosed, BOOL bRebootAfterShutdown )
236 {
237     return InitiateSystemShutdownExA( lpMachineName, lpMessage, dwTimeout,
238                                       bForceAppsClosed, bRebootAfterShutdown,
239                                       SHTDN_REASON_MAJOR_LEGACY_API );
240 }
241
242 BOOL WINAPI InitiateSystemShutdownW( LPWSTR lpMachineName, LPWSTR lpMessage, DWORD dwTimeout,
243                                      BOOL bForceAppsClosed, BOOL bRebootAfterShutdown )
244 {
245     return InitiateSystemShutdownExW( lpMachineName, lpMessage, dwTimeout,
246                                       bForceAppsClosed, bRebootAfterShutdown,
247                                       SHTDN_REASON_MAJOR_LEGACY_API );
248 }
249
250 BOOL WINAPI LogonUserA( LPCSTR lpszUsername, LPCSTR lpszDomain, LPCSTR lpszPassword,
251                         DWORD dwLogonType, DWORD dwLogonProvider, PHANDLE phToken )
252 {
253     FIXME("%s %s %p 0x%08lx 0x%08lx %p - stub\n", debugstr_a(lpszUsername),
254           debugstr_a(lpszDomain), lpszPassword, dwLogonType, dwLogonProvider, phToken);
255
256     return TRUE;
257 }
258
259 BOOL WINAPI LogonUserW( LPCWSTR lpszUsername, LPCWSTR lpszDomain, LPCWSTR lpszPassword,
260                         DWORD dwLogonType, DWORD dwLogonProvider, PHANDLE phToken )
261 {
262     FIXME("%s %s %p 0x%08lx 0x%08lx %p - stub\n", debugstr_w(lpszUsername),
263           debugstr_w(lpszDomain), lpszPassword, dwLogonType, dwLogonProvider, phToken);
264
265     return TRUE;
266 }
267
268 DWORD WINAPI CommandLineFromMsiDescriptor(WCHAR *Descriptor, WCHAR *CommandLine,
269  DWORD *CommandLineLength)
270 {
271     FIXME("stub (%s)\n", debugstr_w(Descriptor));
272     return ERROR_CALL_NOT_IMPLEMENTED;
273 }