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"
44 #include "wine/unicode.h"
45 #include "wine/exception.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(computername);
51 /* Registry key and value names */
52 static const WCHAR ComputerW[] = {'M','a','c','h','i','n','e','\\',
53 'S','y','s','t','e','m','\\',
54 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
55 'C','o','n','t','r','o','l','\\',
56 'C','o','m','p','u','t','e','r','N','a','m','e',0};
57 static const WCHAR ActiveComputerNameW[] = {'A','c','t','i','v','e','C','o','m','p','u','t','e','r','N','a','m','e',0};
58 static const WCHAR ComputerNameW[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
60 static const char default_ComputerName[] = "WINE";
62 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
64 /* filter for page-fault exceptions */
65 static WINE_EXCEPTION_FILTER(page_fault)
67 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
68 return EXCEPTION_EXECUTE_HANDLER;
69 return EXCEPTION_CONTINUE_SEARCH;
72 /***********************************************************************
73 * dns_gethostbyname (INTERNAL)
76 * "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
78 * Wine can use this technique only if the thread-safe gethostbyname_r is available.
80 #ifdef HAVE_LINUX_GETHOSTBYNAME_R_6
81 static BOOL dns_gethostbyname ( char *name, int *size )
83 struct hostent* host = NULL;
86 struct hostent hostentry;
87 int locerr = ENOBUFS, res = ENOMEM;
89 extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
93 res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
94 if( res != ERANGE ) break;
96 extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
100 WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
103 int len = strlen ( host->h_name );
106 strcpy ( name, host->h_name );
111 memcpy ( name, host->h_name, *size );
113 SetLastError ( ERROR_MORE_DATA );
118 HeapFree( GetProcessHeap(), 0, extrabuf );
122 # define dns_gethostbyname(name,size) 0
125 /***********************************************************************
126 * dns_fqdn (INTERNAL)
128 static BOOL dns_fqdn ( char *name, int *size )
130 if ( gethostname ( name, *size + 1 ) )
135 SetLastError ( ERROR_MORE_DATA );
137 SetLastError ( ERROR_INVALID_PARAMETER );
142 if ( !dns_gethostbyname ( name, size ) )
143 *size = strlen ( name );
148 /***********************************************************************
149 * dns_hostname (INTERNAL)
151 static BOOL dns_hostname ( char *name, int *size )
154 if ( ! dns_fqdn ( name, size ) ) return FALSE;
155 c = strchr ( name, '.' );
164 /***********************************************************************
165 * dns_domainname (INTERNAL)
167 static BOOL dns_domainname ( char *name, int *size )
170 if ( ! dns_fqdn ( name, size ) ) return FALSE;
171 c = strchr ( name, '.' );
176 memmove ( name, c, *size + 1 );
181 /***********************************************************************
182 * _init_attr (INTERNAL)
184 inline static void _init_attr ( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *name )
186 attr->Length = sizeof (OBJECT_ATTRIBUTES);
187 attr->RootDirectory = 0;
188 attr->ObjectName = name;
189 attr->Attributes = 0;
190 attr->SecurityDescriptor = NULL;
191 attr->SecurityQualityOfService = NULL;
194 /***********************************************************************
197 static BOOL get_use_dns_option(void)
199 static const WCHAR NetworkW[] = {'M','a','c','h','i','n','e','\\',
200 'S','o','f','t','w','a','r','e','\\',
201 'W','i','n','e','\\','W','i','n','e','\\',
202 'C','o','n','f','i','g','\\','N','e','t','w','o','r','k',0};
203 static const WCHAR UseDNSW[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
208 OBJECT_ATTRIBUTES attr;
209 UNICODE_STRING nameW;
212 _init_attr( &attr, &nameW );
213 RtlInitUnicodeString( &nameW, NetworkW );
215 /* @@ Wine registry key: HKLM\Software\Wine\Wine\Config\Network */
216 if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
218 RtlInitUnicodeString( &nameW, UseDNSW );
219 if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
221 WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
222 ret = IS_OPTION_TRUE( str[0] );
230 /***********************************************************************
231 * COMPUTERNAME_Init (INTERNAL)
233 void COMPUTERNAME_Init (void)
235 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
236 OBJECT_ATTRIBUTES attr;
237 UNICODE_STRING nameW;
238 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
239 DWORD len = sizeof( buf );
240 LPWSTR computer_name = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
241 NTSTATUS st = STATUS_INTERNAL_ERROR;
244 _init_attr ( &attr, &nameW );
246 RtlInitUnicodeString( &nameW, ComputerW );
247 if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
250 attr.RootDirectory = hkey;
251 RtlInitUnicodeString( &nameW, ComputerNameW );
252 if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
255 st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
257 if ( st != STATUS_SUCCESS || get_use_dns_option() )
260 int hlen = sizeof (hbuf);
262 TRACE( "retrieving Unix host name\n" );
263 if ( gethostname ( hbuf, hlen ) )
265 strcpy ( hbuf, default_ComputerName );
266 WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
268 hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
269 dot = strchr ( hbuf, '.' );
271 hlen = strlen ( hbuf );
272 len = MultiByteToWideChar( CP_ACP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
274 if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
275 WARN ( "failed to set ComputerName\n" );
277 else if ( st == STATUS_SUCCESS)
279 len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
280 TRACE( "found in registry\n" );
285 TRACE(" ComputerName: %s (%lu)\n", debugstr_w ( computer_name ), len / sizeof(WCHAR));
287 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
288 if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
292 RtlInitUnicodeString( &nameW, ComputerNameW );
293 st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
299 if ( st == STATUS_SUCCESS )
300 TRACE( "success\n" );
303 WARN( "status trying to set ComputerName: %lx\n", st );
304 SetLastError ( RtlNtStatusToDosError ( st ) );
309 /***********************************************************************
310 * GetComputerNameW (KERNEL32.@)
312 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
314 UNICODE_STRING nameW;
315 OBJECT_ATTRIBUTES attr;
316 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
317 char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
318 DWORD len = sizeof( buf );
319 LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
320 NTSTATUS st = STATUS_INVALID_PARAMETER;
322 TRACE ("%p %p\n", name, size);
324 _init_attr ( &attr, &nameW );
325 RtlInitUnicodeString( &nameW, ComputerW );
326 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
329 attr.RootDirectory = hkey;
330 RtlInitUnicodeString( &nameW, ActiveComputerNameW );
331 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
334 RtlInitUnicodeString( &nameW, ComputerNameW );
335 if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
339 len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
340 TRACE ("ComputerName is %s (length %lu)\n", debugstr_w ( theName ), len);
346 memcpy ( name, theName, *size * sizeof (WCHAR) );
349 st = STATUS_MORE_ENTRIES;
353 memcpy ( name, theName, len * sizeof (WCHAR) );
361 st = STATUS_INVALID_PARAMETER;
369 if ( st == STATUS_SUCCESS )
373 SetLastError ( RtlNtStatusToDosError ( st ) );
374 WARN ( "Status %lu reading computer name from registry\n", st );
379 /***********************************************************************
380 * GetComputerNameA (KERNEL32.@)
382 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
384 WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
385 DWORD sizeW = MAX_COMPUTERNAME_LENGTH;
389 if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
391 len = WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, NULL, 0, NULL, 0 );
396 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, *size, NULL, 0 );
399 SetLastError( ERROR_MORE_DATA );
404 WideCharToMultiByte ( CP_ACP, 0, nameW, sizeW, name, len, NULL, 0 );
412 SetLastError( ERROR_INVALID_PARAMETER );
420 /***********************************************************************
421 * GetComputerNameExA (KERNEL32.@)
423 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
426 int len = sizeof (buf), ret;
427 TRACE("%d, %p, %p\n", type, name, size);
430 case ComputerNameNetBIOS:
431 case ComputerNamePhysicalNetBIOS:
432 return GetComputerNameA (name, size);
433 case ComputerNameDnsHostname:
434 case ComputerNamePhysicalDnsHostname:
435 ret = dns_hostname (buf, &len);
437 case ComputerNameDnsDomain:
438 case ComputerNamePhysicalDnsDomain:
439 ret = dns_domainname (buf, &len);
441 case ComputerNameDnsFullyQualified:
442 case ComputerNamePhysicalDnsFullyQualified:
443 ret = dns_fqdn (buf, &len);
446 SetLastError (ERROR_INVALID_PARAMETER);
452 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
457 memcpy( name, buf, *size );
460 SetLastError( ERROR_MORE_DATA );
465 memcpy( name, buf, len );
473 SetLastError( ERROR_INVALID_PARAMETER );
483 /***********************************************************************
484 * GetComputerNameExW (KERNEL32.@)
486 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
489 int len = sizeof (buf), ret;
491 TRACE("%d, %p, %p\n", type, name, size);
494 case ComputerNameNetBIOS:
495 case ComputerNamePhysicalNetBIOS:
496 return GetComputerNameW (name, size);
497 case ComputerNameDnsHostname:
498 case ComputerNamePhysicalDnsHostname:
499 ret = dns_hostname (buf, &len);
501 case ComputerNameDnsDomain:
502 case ComputerNamePhysicalDnsDomain:
503 ret = dns_domainname (buf, &len);
505 case ComputerNameDnsFullyQualified:
506 case ComputerNamePhysicalDnsFullyQualified:
507 ret = dns_fqdn (buf, &len);
510 SetLastError (ERROR_INVALID_PARAMETER);
516 TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
519 unsigned int lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
522 MultiByteToWideChar( CP_ACP, 0, buf, len, name, *size );
525 SetLastError( ERROR_MORE_DATA );
530 MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
538 SetLastError( ERROR_INVALID_PARAMETER );
547 /******************************************************************************
548 * netbios_char (INTERNAL)
550 static WCHAR netbios_char ( WCHAR wc )
552 static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
553 static const WCHAR deflt = '_';
556 if ( isalnumW ( wc ) ) return wc;
557 for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
558 if ( wc == special[i] ) return wc;
562 /******************************************************************************
563 * SetComputerNameW [KERNEL32.@]
566 * lpComputerName [I] Address of new computer name
570 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
572 UNICODE_STRING nameW;
573 OBJECT_ATTRIBUTES attr;
574 HKEY hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
575 int plen = strlenW ( lpComputerName );
577 NTSTATUS st = STATUS_INTERNAL_ERROR;
579 if (get_use_dns_option())
581 /* This check isn't necessary, but may help debugging problems. */
582 WARN( "Disabled by Wine Configuration.\n" );
583 WARN( "Set \"UseDnsComputerName\" = \"N\" in category [Network] to enable.\n" );
584 SetLastError ( ERROR_ACCESS_DENIED );
588 TRACE( "%s\n", debugstr_w (lpComputerName) );
590 /* Check parameter */
591 if ( plen > MAX_COMPUTERNAME_LENGTH )
594 /* This is NT behaviour. Win 95/98 would coerce characters. */
595 for ( i = 0; i < plen; i++ )
597 WCHAR wc = lpComputerName[i];
598 if ( wc != netbios_char( wc ) )
602 _init_attr ( &attr, &nameW );
604 RtlInitUnicodeString (&nameW, ComputerW);
605 if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
607 attr.RootDirectory = hkey;
608 RtlInitUnicodeString( &nameW, ComputerNameW );
609 if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
611 if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
619 if ( st == STATUS_SUCCESS )
621 TRACE( "ComputerName changed\n" );
627 SetLastError ( RtlNtStatusToDosError ( st ) );
628 WARN ( "status %lu\n", st );
633 /******************************************************************************
634 * SetComputerNameA [KERNEL32.@]
636 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
639 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
640 LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
642 MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
643 ret = SetComputerNameW( nameW );
644 HeapFree( GetProcessHeap(), 0, nameW );
648 /******************************************************************************
649 * SetComputerNameExW [KERNEL32.@]
652 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
654 TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
657 case ComputerNameNetBIOS:
658 case ComputerNamePhysicalNetBIOS:
659 return SetComputerNameW( lpComputerName );
661 SetLastError( ERROR_ACCESS_DENIED );
666 /******************************************************************************
667 * SetComputerNameExA [KERNEL32.@]
670 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
672 TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
675 case ComputerNameNetBIOS:
676 case ComputerNamePhysicalNetBIOS:
677 return SetComputerNameA( lpComputerName );
679 SetLastError( ERROR_ACCESS_DENIED );
684 /***********************************************************************
685 * DnsHostnameToComputerNameA (KERNEL32.@)
687 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
688 LPSTR computername, LPDWORD size)
692 FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname), computername, size);
694 if (!hostname || !size) return FALSE;
695 len = lstrlenA(hostname);
697 if (len > MAX_COMPUTERNAME_LENGTH)
698 len = MAX_COMPUTERNAME_LENGTH;
705 if (!computername) return FALSE;
707 memcpy( computername, hostname, len );
708 computername[len + 1] = 0;
712 /***********************************************************************
713 * DnsHostnameToComputerNameW (KERNEL32.@)
715 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
716 LPWSTR computername, LPDWORD size)
720 FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname), computername, size);
722 if (!hostname || !size) return FALSE;
723 len = lstrlenW(hostname);
725 if (len > MAX_COMPUTERNAME_LENGTH)
726 len = MAX_COMPUTERNAME_LENGTH;
733 if (!computername) return FALSE;
735 memcpy( computername, hostname, len * sizeof(WCHAR) );
736 computername[len + 1] = 0;