2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
5 * Copyright 1999 Peter Ganten
6 * Copyright 2002 Martin Wilck
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
43 #include "wine/unicode.h"
44 #include "wine/exception.h"
46 #include "wine/debug.h"
48 #include "kernel_private.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(computername
);
52 /* Registry key and value names */
53 static const WCHAR ComputerW
[] = {'M','a','c','h','i','n','e','\\',
54 'S','y','s','t','e','m','\\',
55 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
56 'C','o','n','t','r','o','l','\\',
57 'C','o','m','p','u','t','e','r','N','a','m','e',0};
58 static const WCHAR ActiveComputerNameW
[] = {'A','c','t','i','v','e','C','o','m','p','u','t','e','r','N','a','m','e',0};
59 static const WCHAR ComputerNameW
[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
61 static const char default_ComputerName
[] = "WINE";
63 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
65 /* filter for page-fault exceptions */
66 static WINE_EXCEPTION_FILTER(page_fault
)
68 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
69 return EXCEPTION_EXECUTE_HANDLER
;
70 return EXCEPTION_CONTINUE_SEARCH
;
73 /***********************************************************************
74 * dns_gethostbyname (INTERNAL)
77 * "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
79 * Wine can use this technique only if the thread-safe gethostbyname_r is available.
81 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
82 static BOOL
dns_gethostbyname ( char *name
, int *size
)
84 struct hostent
* host
= NULL
;
87 struct hostent hostentry
;
88 int locerr
= ENOBUFS
, res
= ENOMEM
;
90 extrabuf
= HeapAlloc( GetProcessHeap(), 0, ebufsize
) ;
94 res
= gethostbyname_r ( name
, &hostentry
, extrabuf
, ebufsize
, &host
, &locerr
);
95 if( res
!= ERANGE
) break;
97 extrabuf
= HeapReAlloc( GetProcessHeap(), 0, extrabuf
, ebufsize
) ;
101 WARN ("Error in gethostbyname_r %d (%d)\n", res
, locerr
);
104 int len
= strlen ( host
->h_name
);
107 strcpy ( name
, host
->h_name
);
112 memcpy ( name
, host
->h_name
, *size
);
114 SetLastError ( ERROR_MORE_DATA
);
119 HeapFree( GetProcessHeap(), 0, extrabuf
);
123 # define dns_gethostbyname(name,size) 0
126 /***********************************************************************
127 * dns_fqdn (INTERNAL)
129 static BOOL
dns_fqdn ( char *name
, int *size
)
131 if ( gethostname ( name
, *size
+ 1 ) )
136 SetLastError ( ERROR_MORE_DATA
);
138 SetLastError ( ERROR_INVALID_PARAMETER
);
143 if ( !dns_gethostbyname ( name
, size
) )
144 *size
= strlen ( name
);
149 /***********************************************************************
150 * dns_hostname (INTERNAL)
152 static BOOL
dns_hostname ( char *name
, int *size
)
155 if ( ! dns_fqdn ( name
, size
) ) return FALSE
;
156 c
= strchr ( name
, '.' );
165 /***********************************************************************
166 * dns_domainname (INTERNAL)
168 static BOOL
dns_domainname ( char *name
, int *size
)
171 if ( ! dns_fqdn ( name
, size
) ) return FALSE
;
172 c
= strchr ( name
, '.' );
177 memmove ( name
, c
, *size
+ 1 );
182 /***********************************************************************
183 * _init_attr (INTERNAL)
185 inline static void _init_attr ( OBJECT_ATTRIBUTES
*attr
, UNICODE_STRING
*name
)
187 attr
->Length
= sizeof (OBJECT_ATTRIBUTES
);
188 attr
->RootDirectory
= 0;
189 attr
->ObjectName
= name
;
190 attr
->Attributes
= 0;
191 attr
->SecurityDescriptor
= NULL
;
192 attr
->SecurityQualityOfService
= NULL
;
195 /***********************************************************************
198 static BOOL
get_use_dns_option(void)
200 static const WCHAR NetworkW
[] = {'S','o','f','t','w','a','r','e','\\',
201 'W','i','n','e','\\','N','e','t','w','o','r','k',0};
202 static const WCHAR UseDNSW
[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
207 OBJECT_ATTRIBUTES attr
;
208 UNICODE_STRING nameW
;
211 _init_attr( &attr
, &nameW
);
212 RtlOpenCurrentUser( KEY_ALL_ACCESS
, &root
);
213 attr
.RootDirectory
= root
;
214 RtlInitUnicodeString( &nameW
, NetworkW
);
216 /* @@ Wine registry key: HKCU\Software\Wine\Network */
217 if (!NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
))
219 RtlInitUnicodeString( &nameW
, UseDNSW
);
220 if (!NtQueryValueKey( hkey
, &nameW
, KeyValuePartialInformation
, tmp
, sizeof(tmp
), &dummy
))
222 WCHAR
*str
= (WCHAR
*)((KEY_VALUE_PARTIAL_INFORMATION
*)tmp
)->Data
;
223 ret
= IS_OPTION_TRUE( str
[0] );
232 /***********************************************************************
233 * COMPUTERNAME_Init (INTERNAL)
235 void COMPUTERNAME_Init (void)
237 HANDLE hkey
= INVALID_HANDLE_VALUE
, hsubkey
= INVALID_HANDLE_VALUE
;
238 OBJECT_ATTRIBUTES attr
;
239 UNICODE_STRING nameW
;
240 char buf
[offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
) + (MAX_COMPUTERNAME_LENGTH
+ 1) * sizeof( WCHAR
)];
241 DWORD len
= sizeof( buf
);
242 LPWSTR computer_name
= (LPWSTR
) (buf
+ offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
));
243 NTSTATUS st
= STATUS_INTERNAL_ERROR
;
246 _init_attr ( &attr
, &nameW
);
248 RtlInitUnicodeString( &nameW
, ComputerW
);
249 if ( ( st
= NtCreateKey( &hkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
) ) != STATUS_SUCCESS
)
252 attr
.RootDirectory
= hkey
;
253 RtlInitUnicodeString( &nameW
, ComputerNameW
);
254 if ( (st
= NtCreateKey( &hsubkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, 0, NULL
) ) != STATUS_SUCCESS
)
257 st
= NtQueryValueKey( hsubkey
, &nameW
, KeyValuePartialInformation
, buf
, len
, &len
);
259 if ( st
!= STATUS_SUCCESS
|| get_use_dns_option() )
262 int hlen
= sizeof (hbuf
);
264 TRACE( "retrieving Unix host name\n" );
265 if ( gethostname ( hbuf
, hlen
) )
267 strcpy ( hbuf
, default_ComputerName
);
268 WARN( "gethostname() error: %d, using host name %s\n", errno
, hbuf
);
270 hbuf
[MAX_COMPUTERNAME_LENGTH
] = 0;
271 dot
= strchr ( hbuf
, '.' );
273 hlen
= strlen ( hbuf
);
274 len
= MultiByteToWideChar( CP_ACP
, 0, hbuf
, hlen
+ 1, computer_name
, MAX_COMPUTERNAME_LENGTH
+ 1 )
276 if ( NtSetValueKey( hsubkey
, &nameW
, 0, REG_SZ
, computer_name
, len
) != STATUS_SUCCESS
)
277 WARN ( "failed to set ComputerName\n" );
279 else if ( st
== STATUS_SUCCESS
)
281 len
= (len
- offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
));
282 TRACE( "found in registry\n" );
287 TRACE(" ComputerName: %s (%lu)\n", debugstr_w ( computer_name
), len
/ sizeof(WCHAR
));
289 RtlInitUnicodeString( &nameW
, ActiveComputerNameW
);
290 if ( ( st
= NtCreateKey( &hsubkey
, KEY_ALL_ACCESS
, &attr
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
) )
294 RtlInitUnicodeString( &nameW
, ComputerNameW
);
295 st
= NtSetValueKey( hsubkey
, &nameW
, 0, REG_SZ
, computer_name
, len
);
301 if ( st
== STATUS_SUCCESS
)
302 TRACE( "success\n" );
305 WARN( "status trying to set ComputerName: %lx\n", st
);
306 SetLastError ( RtlNtStatusToDosError ( st
) );
311 /***********************************************************************
312 * GetComputerNameW (KERNEL32.@)
314 BOOL WINAPI
GetComputerNameW(LPWSTR name
,LPDWORD size
)
316 UNICODE_STRING nameW
;
317 OBJECT_ATTRIBUTES attr
;
318 HANDLE hkey
= INVALID_HANDLE_VALUE
, hsubkey
= INVALID_HANDLE_VALUE
;
319 char buf
[offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
) + (MAX_COMPUTERNAME_LENGTH
+ 1) * sizeof( WCHAR
)];
320 DWORD len
= sizeof( buf
);
321 LPWSTR theName
= (LPWSTR
) (buf
+ offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
));
322 NTSTATUS st
= STATUS_INVALID_PARAMETER
;
324 TRACE ("%p %p\n", name
, size
);
326 _init_attr ( &attr
, &nameW
);
327 RtlInitUnicodeString( &nameW
, ComputerW
);
328 if ( ( st
= NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
) ) != STATUS_SUCCESS
)
331 attr
.RootDirectory
= hkey
;
332 RtlInitUnicodeString( &nameW
, ActiveComputerNameW
);
333 if ( ( st
= NtOpenKey( &hsubkey
, KEY_ALL_ACCESS
, &attr
) ) != STATUS_SUCCESS
)
336 RtlInitUnicodeString( &nameW
, ComputerNameW
);
337 if ( ( st
= NtQueryValueKey( hsubkey
, &nameW
, KeyValuePartialInformation
, buf
, len
, &len
) )
341 len
= (len
-offsetof( KEY_VALUE_PARTIAL_INFORMATION
, Data
)) / sizeof (WCHAR
) - 1;
342 TRACE ("ComputerName is %s (length %lu)\n", debugstr_w ( theName
), len
);
348 memcpy ( name
, theName
, *size
* sizeof (WCHAR
) );
351 st
= STATUS_MORE_ENTRIES
;
355 memcpy ( name
, theName
, len
* sizeof (WCHAR
) );
363 st
= STATUS_INVALID_PARAMETER
;
371 if ( st
== STATUS_SUCCESS
)
375 SetLastError ( RtlNtStatusToDosError ( st
) );
376 WARN ( "Status %lu reading computer name from registry\n", st
);
381 /***********************************************************************
382 * GetComputerNameA (KERNEL32.@)
384 BOOL WINAPI
GetComputerNameA(LPSTR name
, LPDWORD size
)
386 WCHAR nameW
[ MAX_COMPUTERNAME_LENGTH
+ 1 ];
387 DWORD sizeW
= MAX_COMPUTERNAME_LENGTH
;
391 if ( !GetComputerNameW (nameW
, &sizeW
) ) return FALSE
;
393 len
= WideCharToMultiByte ( CP_ACP
, 0, nameW
, sizeW
, NULL
, 0, NULL
, 0 );
398 WideCharToMultiByte ( CP_ACP
, 0, nameW
, sizeW
, name
, *size
, NULL
, 0 );
401 SetLastError( ERROR_MORE_DATA
);
406 WideCharToMultiByte ( CP_ACP
, 0, nameW
, sizeW
, name
, len
, NULL
, 0 );
414 SetLastError( ERROR_INVALID_PARAMETER
);
422 /***********************************************************************
423 * GetComputerNameExA (KERNEL32.@)
425 BOOL WINAPI
GetComputerNameExA(COMPUTER_NAME_FORMAT type
, LPSTR name
, LPDWORD size
)
428 int len
= sizeof (buf
), ret
;
429 TRACE("%d, %p, %p\n", type
, name
, size
);
432 case ComputerNameNetBIOS
:
433 case ComputerNamePhysicalNetBIOS
:
434 return GetComputerNameA (name
, size
);
435 case ComputerNameDnsHostname
:
436 case ComputerNamePhysicalDnsHostname
:
437 ret
= dns_hostname (buf
, &len
);
439 case ComputerNameDnsDomain
:
440 case ComputerNamePhysicalDnsDomain
:
441 ret
= dns_domainname (buf
, &len
);
443 case ComputerNameDnsFullyQualified
:
444 case ComputerNamePhysicalDnsFullyQualified
:
445 ret
= dns_fqdn (buf
, &len
);
448 SetLastError (ERROR_INVALID_PARAMETER
);
454 TRACE ("-> %s (%d)\n", debugstr_a (buf
), len
);
459 memcpy( name
, buf
, *size
);
462 SetLastError( ERROR_MORE_DATA
);
467 memcpy( name
, buf
, len
);
475 SetLastError( ERROR_INVALID_PARAMETER
);
485 /***********************************************************************
486 * GetComputerNameExW (KERNEL32.@)
488 BOOL WINAPI
GetComputerNameExW( COMPUTER_NAME_FORMAT type
, LPWSTR name
, LPDWORD size
)
491 int len
= sizeof (buf
), ret
;
493 TRACE("%d, %p, %p\n", type
, name
, size
);
496 case ComputerNameNetBIOS
:
497 case ComputerNamePhysicalNetBIOS
:
498 return GetComputerNameW (name
, size
);
499 case ComputerNameDnsHostname
:
500 case ComputerNamePhysicalDnsHostname
:
501 ret
= dns_hostname (buf
, &len
);
503 case ComputerNameDnsDomain
:
504 case ComputerNamePhysicalDnsDomain
:
505 ret
= dns_domainname (buf
, &len
);
507 case ComputerNameDnsFullyQualified
:
508 case ComputerNamePhysicalDnsFullyQualified
:
509 ret
= dns_fqdn (buf
, &len
);
512 SetLastError (ERROR_INVALID_PARAMETER
);
518 TRACE ("-> %s (%d)\n", debugstr_a (buf
), len
);
521 unsigned int lenW
= MultiByteToWideChar( CP_ACP
, 0, buf
, len
, NULL
, 0 );
524 MultiByteToWideChar( CP_ACP
, 0, buf
, len
, name
, *size
);
527 SetLastError( ERROR_MORE_DATA
);
532 MultiByteToWideChar( CP_ACP
, 0, buf
, len
, name
, lenW
);
540 SetLastError( ERROR_INVALID_PARAMETER
);
549 /******************************************************************************
550 * netbios_char (INTERNAL)
552 static WCHAR
netbios_char ( WCHAR wc
)
554 static const WCHAR special
[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
555 static const WCHAR deflt
= '_';
558 if ( isalnumW ( wc
) ) return wc
;
559 for ( i
= 0; i
< sizeof (special
) / sizeof (WCHAR
); i
++ )
560 if ( wc
== special
[i
] ) return wc
;
564 /******************************************************************************
565 * SetComputerNameW [KERNEL32.@]
568 * lpComputerName [I] Address of new computer name
572 BOOL WINAPI
SetComputerNameW( LPCWSTR lpComputerName
)
574 UNICODE_STRING nameW
;
575 OBJECT_ATTRIBUTES attr
;
576 HANDLE hkey
= INVALID_HANDLE_VALUE
, hsubkey
= INVALID_HANDLE_VALUE
;
577 int plen
= strlenW ( lpComputerName
);
579 NTSTATUS st
= STATUS_INTERNAL_ERROR
;
581 if (get_use_dns_option())
583 /* This check isn't necessary, but may help debugging problems. */
584 WARN( "Disabled by Wine Configuration.\n" );
585 WARN( "Set \"UseDnsComputerName\" = \"N\" in category [Network] to enable.\n" );
586 SetLastError ( ERROR_ACCESS_DENIED
);
590 TRACE( "%s\n", debugstr_w (lpComputerName
) );
592 /* Check parameter */
593 if ( plen
> MAX_COMPUTERNAME_LENGTH
)
596 /* This is NT behaviour. Win 95/98 would coerce characters. */
597 for ( i
= 0; i
< plen
; i
++ )
599 WCHAR wc
= lpComputerName
[i
];
600 if ( wc
!= netbios_char( wc
) )
604 _init_attr ( &attr
, &nameW
);
606 RtlInitUnicodeString (&nameW
, ComputerW
);
607 if ( ( st
= NtOpenKey( &hkey
, KEY_ALL_ACCESS
, &attr
) ) != STATUS_SUCCESS
)
609 attr
.RootDirectory
= hkey
;
610 RtlInitUnicodeString( &nameW
, ComputerNameW
);
611 if ( ( st
= NtOpenKey( &hsubkey
, KEY_ALL_ACCESS
, &attr
) ) != STATUS_SUCCESS
)
613 if ( ( st
= NtSetValueKey( hsubkey
, &nameW
, 0, REG_SZ
, lpComputerName
, ( plen
+ 1) * sizeof(WCHAR
) ) )
621 if ( st
== STATUS_SUCCESS
)
623 TRACE( "ComputerName changed\n" );
629 SetLastError ( RtlNtStatusToDosError ( st
) );
630 WARN ( "status %lu\n", st
);
635 /******************************************************************************
636 * SetComputerNameA [KERNEL32.@]
638 BOOL WINAPI
SetComputerNameA( LPCSTR lpComputerName
)
641 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpComputerName
, -1, NULL
, 0 );
642 LPWSTR nameW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
644 MultiByteToWideChar( CP_ACP
, 0, lpComputerName
, -1, nameW
, len
);
645 ret
= SetComputerNameW( nameW
);
646 HeapFree( GetProcessHeap(), 0, nameW
);
650 /******************************************************************************
651 * SetComputerNameExW [KERNEL32.@]
654 BOOL WINAPI
SetComputerNameExW( COMPUTER_NAME_FORMAT type
, LPCWSTR lpComputerName
)
656 TRACE("%d, %s\n", type
, debugstr_w (lpComputerName
));
659 case ComputerNameNetBIOS
:
660 case ComputerNamePhysicalNetBIOS
:
661 return SetComputerNameW( lpComputerName
);
663 SetLastError( ERROR_ACCESS_DENIED
);
668 /******************************************************************************
669 * SetComputerNameExA [KERNEL32.@]
672 BOOL WINAPI
SetComputerNameExA( COMPUTER_NAME_FORMAT type
, LPCSTR lpComputerName
)
674 TRACE( "%d, %s\n", type
, debugstr_a (lpComputerName
) );
677 case ComputerNameNetBIOS
:
678 case ComputerNamePhysicalNetBIOS
:
679 return SetComputerNameA( lpComputerName
);
681 SetLastError( ERROR_ACCESS_DENIED
);
686 /***********************************************************************
687 * DnsHostnameToComputerNameA (KERNEL32.@)
689 BOOL WINAPI
DnsHostnameToComputerNameA(LPCSTR hostname
,
690 LPSTR computername
, LPDWORD size
)
694 FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname
), computername
, size
);
696 if (!hostname
|| !size
) return FALSE
;
697 len
= lstrlenA(hostname
);
699 if (len
> MAX_COMPUTERNAME_LENGTH
)
700 len
= MAX_COMPUTERNAME_LENGTH
;
707 if (!computername
) return FALSE
;
709 memcpy( computername
, hostname
, len
);
710 computername
[len
+ 1] = 0;
714 /***********************************************************************
715 * DnsHostnameToComputerNameW (KERNEL32.@)
717 BOOL WINAPI
DnsHostnameToComputerNameW(LPCWSTR hostname
,
718 LPWSTR computername
, LPDWORD size
)
722 FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname
), computername
, size
);
724 if (!hostname
|| !size
) return FALSE
;
725 len
= lstrlenW(hostname
);
727 if (len
> MAX_COMPUTERNAME_LENGTH
)
728 len
= MAX_COMPUTERNAME_LENGTH
;
735 if (!computername
) return FALSE
;
737 memcpy( computername
, hostname
, len
* sizeof(WCHAR
) );
738 computername
[len
+ 1] = 0;