kernel32: Display correct parameters in comm traces.
[wine] / dlls / kernel32 / computername.c
1 /*
2  * Win32 kernel functions
3  *
4  * Copyright 1995 Martin von Loewis and Cameron Heide
5  * Copyright 1999 Peter Ganten
6  * Copyright 2002 Martin Wilck
7  *
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.
12  *
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.
17  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <string.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <stdlib.h>
32 #include <errno.h>
33 #ifdef HAVE_NETDB_H
34 #include <netdb.h>
35 #endif
36
37 #include "ntstatus.h"
38 #define WIN32_NO_STATUS
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winerror.h"
42 #include "winnls.h"
43 #include "winternl.h"
44 #include "wine/unicode.h"
45 #include "wine/exception.h"
46 #include "excpt.h"
47 #include "wine/debug.h"
48
49 #include "kernel_private.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(computername);
52
53 /* Registry key and value names */
54 static const WCHAR ComputerW[] = {'M','a','c','h','i','n','e','\\',
55                                   'S','y','s','t','e','m','\\',
56                                   'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
57                                   'C','o','n','t','r','o','l','\\',
58                                   'C','o','m','p','u','t','e','r','N','a','m','e',0};
59 static const WCHAR ActiveComputerNameW[] =   {'A','c','t','i','v','e','C','o','m','p','u','t','e','r','N','a','m','e',0};
60 static const WCHAR ComputerNameW[] = {'C','o','m','p','u','t','e','r','N','a','m','e',0};
61
62 static const char default_ComputerName[] = "WINE";
63
64 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1')
65
66 /*********************************************************************** 
67  *                    dns_gethostbyname (INTERNAL)
68  *
69  *  From hostname(1):
70  *  "The FQDN is the name gethostbyname(2) returns for the host name returned by gethostname(2)."
71  *
72  *  Wine can use this technique only if the thread-safe gethostbyname_r is available.
73  */
74 #ifdef  HAVE_LINUX_GETHOSTBYNAME_R_6
75 static BOOL dns_gethostbyname ( char *name, int *size )
76 {
77     struct hostent* host = NULL;
78     char *extrabuf;
79     int ebufsize = 1024;
80     struct hostent hostentry;
81     int locerr = ENOBUFS, res = ENOMEM;
82
83     extrabuf = HeapAlloc( GetProcessHeap(), 0, ebufsize ) ;
84
85     while( extrabuf ) 
86     {
87         res = gethostbyname_r ( name, &hostentry, extrabuf, ebufsize, &host, &locerr );
88         if( res != ERANGE ) break;
89         ebufsize *= 2;
90         extrabuf = HeapReAlloc( GetProcessHeap(), 0, extrabuf, ebufsize ) ;
91     }
92     
93     if ( res )
94         WARN ("Error in gethostbyname_r %d (%d)\n", res, locerr);
95     else
96     {
97         int len = strlen ( host->h_name );
98         if ( len < *size )
99         {
100             strcpy ( name, host->h_name );
101             *size = len;
102         }
103         else
104         {
105             memcpy ( name, host->h_name, *size );
106             name[*size] = 0;
107             SetLastError ( ERROR_MORE_DATA );
108             res = 1;
109         }
110     }
111
112     HeapFree( GetProcessHeap(), 0, extrabuf );
113     return !res;
114 }
115 #else
116 #  define dns_gethostbyname(name,size) 0
117 #endif
118
119 /*********************************************************************** 
120  *                     dns_fqdn (INTERNAL)
121  */
122 static BOOL dns_fqdn ( char *name, int *size )
123 {
124     if ( gethostname ( name, *size + 1 ) ) 
125     {
126         switch( errno )
127         {
128         case ENAMETOOLONG:
129             SetLastError ( ERROR_MORE_DATA );
130         default:
131             SetLastError ( ERROR_INVALID_PARAMETER );
132         }
133         return FALSE;
134     }
135
136     if ( !dns_gethostbyname ( name, size ) )
137         *size = strlen ( name );
138
139     return TRUE;
140 }
141
142 /*********************************************************************** 
143  *                     dns_hostname (INTERNAL)
144  */
145 static BOOL dns_hostname ( char *name, int *size )
146 {
147     char *c;
148     if ( ! dns_fqdn ( name, size ) ) return FALSE;
149     c = strchr ( name, '.' );
150     if (c)
151     {
152         *c = 0;
153         *size = (c - name);
154     }
155     return TRUE;
156 }
157
158 /*********************************************************************** 
159  *                     dns_domainname (INTERNAL)
160  */
161 static BOOL dns_domainname ( char *name, int *size )
162 {
163     char *c;
164     if ( ! dns_fqdn ( name, size ) ) return FALSE;
165     c = strchr ( name, '.' );
166     if (c)
167     {
168         c += 1;
169         *size -= (c - name);
170         memmove ( name, c, *size + 1 );
171     }
172     return TRUE;
173 }
174
175 /*********************************************************************** 
176  *                      _init_attr    (INTERNAL)
177  */
178 inline static void _init_attr ( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *name )
179 {
180     attr->Length = sizeof (OBJECT_ATTRIBUTES);
181     attr->RootDirectory = 0;
182     attr->ObjectName = name;
183     attr->Attributes = 0;
184     attr->SecurityDescriptor = NULL;
185     attr->SecurityQualityOfService = NULL;
186 }
187
188 /***********************************************************************
189  *           get_use_dns_option
190  */
191 static BOOL get_use_dns_option(void)
192 {
193     static const WCHAR NetworkW[] = {'S','o','f','t','w','a','r','e','\\',
194                                      'W','i','n','e','\\','N','e','t','w','o','r','k',0};
195     static const WCHAR UseDNSW[] = {'U','s','e','D','n','s','C','o','m','p','u','t','e','r','N','a','m','e',0};
196
197     char tmp[80];
198     HANDLE root, hkey;
199     DWORD dummy;
200     OBJECT_ATTRIBUTES attr;
201     UNICODE_STRING nameW;
202     BOOL ret = TRUE;
203
204     _init_attr( &attr, &nameW );
205     RtlOpenCurrentUser( KEY_ALL_ACCESS, &root );
206     attr.RootDirectory = root;
207     RtlInitUnicodeString( &nameW, NetworkW );
208
209     /* @@ Wine registry key: HKCU\Software\Wine\Network */
210     if (!NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ))
211     {
212         RtlInitUnicodeString( &nameW, UseDNSW );
213         if (!NtQueryValueKey( hkey, &nameW, KeyValuePartialInformation, tmp, sizeof(tmp), &dummy ))
214         {
215             WCHAR *str = (WCHAR *)((KEY_VALUE_PARTIAL_INFORMATION *)tmp)->Data;
216             ret = IS_OPTION_TRUE( str[0] );
217         }
218         NtClose( hkey );
219     }
220     NtClose( root );
221     return ret;
222 }
223
224
225 /*********************************************************************** 
226  *                      COMPUTERNAME_Init    (INTERNAL)
227  */
228 void COMPUTERNAME_Init (void)
229 {
230     HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
231     OBJECT_ATTRIBUTES attr;
232     UNICODE_STRING nameW;
233     char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
234     DWORD len = sizeof( buf );
235     LPWSTR computer_name = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
236     NTSTATUS st = STATUS_INTERNAL_ERROR;
237
238     TRACE("(void)\n");
239     _init_attr ( &attr, &nameW );
240     
241     RtlInitUnicodeString( &nameW, ComputerW );
242     if ( ( st = NtCreateKey( &hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
243         goto out;
244     
245     attr.RootDirectory = hkey;
246     RtlInitUnicodeString( &nameW, ComputerNameW );
247     if ( (st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL ) ) != STATUS_SUCCESS )
248         goto out;
249     
250     st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len );
251
252     if ( st != STATUS_SUCCESS || get_use_dns_option() )
253     {
254         char hbuf[256];
255         int hlen = sizeof (hbuf);
256         char *dot;
257         TRACE( "retrieving Unix host name\n" );
258         if ( gethostname ( hbuf, hlen ) )
259         {
260             strcpy ( hbuf, default_ComputerName );
261             WARN( "gethostname() error: %d, using host name %s\n", errno, hbuf );
262         }
263         hbuf[MAX_COMPUTERNAME_LENGTH] = 0;
264         dot = strchr ( hbuf, '.' );
265         if ( dot ) *dot = 0;
266         hlen = strlen ( hbuf );
267         len = MultiByteToWideChar( CP_ACP, 0, hbuf, hlen + 1, computer_name, MAX_COMPUTERNAME_LENGTH + 1 )
268             * sizeof( WCHAR );
269         if ( NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len ) != STATUS_SUCCESS )
270             WARN ( "failed to set ComputerName\n" );
271     }
272     else
273     {
274         len = (len - offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
275         TRACE( "found in registry\n" );
276     }
277
278     NtClose( hsubkey );
279     TRACE(" ComputerName: %s (%u)\n", debugstr_w (computer_name), len);
280
281     RtlInitUnicodeString( &nameW, ActiveComputerNameW );
282     if ( ( st = NtCreateKey( &hsubkey, KEY_ALL_ACCESS, &attr, 0, NULL, REG_OPTION_VOLATILE, NULL ) )
283          != STATUS_SUCCESS )
284         goto out;
285     
286     RtlInitUnicodeString( &nameW, ComputerNameW );
287     st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, computer_name, len );
288
289 out:
290     NtClose( hsubkey );
291     NtClose( hkey );
292
293     if ( st == STATUS_SUCCESS )
294         TRACE( "success\n" );
295     else
296     {
297         WARN( "status trying to set ComputerName: %x\n", st );
298         SetLastError ( RtlNtStatusToDosError ( st ) );
299     }
300 }
301
302
303 /***********************************************************************
304  *              GetComputerNameW         (KERNEL32.@)
305  */
306 BOOL WINAPI GetComputerNameW(LPWSTR name,LPDWORD size)
307 {
308     UNICODE_STRING nameW;
309     OBJECT_ATTRIBUTES attr;
310     HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
311     char buf[offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ) + (MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
312     DWORD len = sizeof( buf );
313     LPWSTR theName = (LPWSTR) (buf + offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data ));
314     NTSTATUS st = STATUS_INVALID_PARAMETER;
315     
316     TRACE ("%p %p\n", name, size);
317
318     _init_attr ( &attr, &nameW );
319     RtlInitUnicodeString( &nameW, ComputerW );
320     if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
321         goto out;
322          
323     attr.RootDirectory = hkey;
324     RtlInitUnicodeString( &nameW, ActiveComputerNameW );
325     if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
326         goto out;
327     
328     RtlInitUnicodeString( &nameW, ComputerNameW );
329     if ( ( st = NtQueryValueKey( hsubkey, &nameW, KeyValuePartialInformation, buf, len, &len ) )
330          != STATUS_SUCCESS )
331         goto out;
332
333     len = (len -offsetof( KEY_VALUE_PARTIAL_INFORMATION, Data )) / sizeof (WCHAR) - 1;
334     TRACE ("ComputerName is %s (length %u)\n", debugstr_w ( theName ), len);
335
336     if ( *size < len + 1 )
337     {
338         *size = len + 1;
339         st = STATUS_MORE_ENTRIES;
340     }
341     else
342     {
343         memcpy ( name, theName, len * sizeof (WCHAR) );
344         name[len] = 0;
345         *size = len;
346         st = STATUS_SUCCESS;
347     }
348
349 out:
350     NtClose ( hsubkey );
351     NtClose ( hkey );
352
353     if ( st == STATUS_SUCCESS )
354         return TRUE;
355     else
356     {
357         SetLastError ( RtlNtStatusToDosError ( st ) );
358         WARN ( "Status %u reading computer name from registry\n", st );
359         return FALSE;
360     }
361 }
362
363 /***********************************************************************
364  *              GetComputerNameA         (KERNEL32.@)
365  */
366 BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
367 {
368     WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
369     DWORD sizeW = MAX_COMPUTERNAME_LENGTH;
370     unsigned int len;
371     BOOL ret;
372
373     if ( !GetComputerNameW (nameW, &sizeW) ) return FALSE;
374
375     len = WideCharToMultiByte ( CP_ACP, 0, nameW, -1, NULL, 0, NULL, 0 );
376     /* for compatibility with Win9x */
377     __TRY
378     {
379         if ( *size < len + 1 )
380         {
381             *size = len + 1;
382             SetLastError( ERROR_MORE_DATA );
383             ret = FALSE;
384         }
385         else 
386         {
387             WideCharToMultiByte ( CP_ACP, 0, nameW, -1, name, len, NULL, 0 );
388             name[len] = 0;
389             *size = len;
390             ret = TRUE;
391         }
392     }
393     __EXCEPT_PAGE_FAULT
394     {
395         SetLastError( ERROR_INVALID_PARAMETER );
396         ret = FALSE;
397     }
398     __ENDTRY
399
400     return ret;
401 }
402
403 /***********************************************************************
404  *              GetComputerNameExA         (KERNEL32.@)
405  */
406 BOOL WINAPI GetComputerNameExA(COMPUTER_NAME_FORMAT type, LPSTR name, LPDWORD size)
407 {
408     char buf[256];
409     int len = sizeof (buf), ret;
410     TRACE("%d, %p, %p\n", type, name, size);
411     switch( type )
412     {
413     case ComputerNameNetBIOS:
414     case ComputerNamePhysicalNetBIOS:
415         return GetComputerNameA (name, size);
416     case ComputerNameDnsHostname:
417     case ComputerNamePhysicalDnsHostname:
418         ret = dns_hostname (buf, &len);
419         break;
420     case ComputerNameDnsDomain:
421     case ComputerNamePhysicalDnsDomain:
422         ret = dns_domainname (buf, &len);
423         break;
424     case ComputerNameDnsFullyQualified:
425     case ComputerNamePhysicalDnsFullyQualified:
426         ret = dns_fqdn (buf, &len);
427         break;
428     default:
429         SetLastError (ERROR_INVALID_PARAMETER);
430         return FALSE;
431     }
432
433     if ( ret )
434     {
435         TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
436         if ( *size < len + 1 )
437         {
438             *size = len + 1;
439             SetLastError( ERROR_MORE_DATA );
440             ret = FALSE;
441         }
442         else
443         {
444             memcpy( name, buf, len );
445             name[len] = 0;
446             *size = len;
447             ret = TRUE;
448         }
449     }
450
451     return ret;
452 }
453
454
455 /***********************************************************************
456  *              GetComputerNameExW         (KERNEL32.@)
457  */
458 BOOL WINAPI GetComputerNameExW( COMPUTER_NAME_FORMAT type, LPWSTR name, LPDWORD size )
459 {
460     char buf[256];
461     int len = sizeof (buf), ret;
462
463     TRACE("%d, %p, %p\n", type, name, size);
464     switch( type )
465     {
466     case ComputerNameNetBIOS:
467     case ComputerNamePhysicalNetBIOS:
468         return GetComputerNameW (name, size);
469     case ComputerNameDnsHostname:
470     case ComputerNamePhysicalDnsHostname:
471         ret = dns_hostname (buf, &len);
472         break;
473     case ComputerNameDnsDomain:
474     case ComputerNamePhysicalDnsDomain:
475         ret = dns_domainname (buf, &len);
476         break;
477     case ComputerNameDnsFullyQualified:
478     case ComputerNamePhysicalDnsFullyQualified:
479         ret = dns_fqdn (buf, &len);
480         break;
481     default:
482         SetLastError (ERROR_INVALID_PARAMETER);
483         return FALSE;
484     }
485
486     if ( ret )
487     {
488         unsigned int lenW;
489
490         TRACE ("-> %s (%d)\n", debugstr_a (buf), len);
491
492         lenW = MultiByteToWideChar( CP_ACP, 0, buf, len, NULL, 0 );
493         if ( *size < lenW + 1 )
494         {
495             *size = lenW + 1;
496             SetLastError( ERROR_MORE_DATA );
497             ret = FALSE;
498         }
499         else
500         {
501             MultiByteToWideChar( CP_ACP, 0, buf, len, name, lenW );
502             name[lenW] = 0;
503             *size = lenW;
504             ret = TRUE;
505         }
506     }
507
508     return ret;
509 }
510
511 /******************************************************************************
512  * netbios_char (INTERNAL)
513  */
514 static WCHAR netbios_char ( WCHAR wc )
515 {
516     static const WCHAR special[] = {'!','@','#','$','%','^','&','\'',')','(','-','_','{','}','~'};
517     static const WCHAR deflt = '_';
518     unsigned int i;
519     
520     if ( isalnumW ( wc ) ) return wc;
521     for ( i = 0; i < sizeof (special) / sizeof (WCHAR); i++ )
522         if ( wc == special[i] ) return wc;
523     return deflt;
524 }
525
526 /******************************************************************************
527  * SetComputerNameW [KERNEL32.@]
528  *
529  * Set a new NetBIOS name for the local computer.
530  *
531  * PARAMS
532  *    lpComputerName [I] Address of new computer name
533  *
534  * RETURNS
535  *    Success: TRUE
536  *    Failure: FALSE
537  */
538 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
539 {
540     UNICODE_STRING nameW;
541     OBJECT_ATTRIBUTES attr;
542     HANDLE hkey = INVALID_HANDLE_VALUE, hsubkey = INVALID_HANDLE_VALUE;
543     int plen = strlenW ( lpComputerName );
544     int i;
545     NTSTATUS st = STATUS_INTERNAL_ERROR;
546
547     if (get_use_dns_option())
548     {
549         /* This check isn't necessary, but may help debugging problems. */
550         WARN( "Disabled by Wine Configuration.\n" );
551         WARN( "Set \"UseDnsComputerName\" = \"N\" in category [Network] to enable.\n" );
552         SetLastError ( ERROR_ACCESS_DENIED );
553         return FALSE;
554     }
555
556     TRACE( "%s\n", debugstr_w (lpComputerName) );
557
558     /* Check parameter */
559     if ( plen > MAX_COMPUTERNAME_LENGTH ) 
560         goto out;
561
562     /* This is NT behaviour. Win 95/98 would coerce characters. */
563     for ( i = 0; i < plen; i++ )
564     {
565         WCHAR wc = lpComputerName[i];
566         if ( wc != netbios_char( wc ) )
567             goto out;
568     }
569     
570     _init_attr ( &attr, &nameW );
571     
572     RtlInitUnicodeString (&nameW, ComputerW);
573     if ( ( st = NtOpenKey( &hkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
574         goto out;
575     attr.RootDirectory = hkey;
576     RtlInitUnicodeString( &nameW, ComputerNameW );
577     if ( ( st = NtOpenKey( &hsubkey, KEY_ALL_ACCESS, &attr ) ) != STATUS_SUCCESS )
578         goto out;
579     if ( ( st = NtSetValueKey( hsubkey, &nameW, 0, REG_SZ, lpComputerName, ( plen + 1) * sizeof(WCHAR) ) )
580          != STATUS_SUCCESS )
581         goto out;
582
583 out:
584     NtClose( hsubkey );
585     NtClose( hkey );
586     
587     if ( st == STATUS_SUCCESS )
588     {
589         TRACE( "ComputerName changed\n" );
590         return TRUE;
591     }
592
593     else
594     {
595         SetLastError ( RtlNtStatusToDosError ( st ) );
596         WARN ( "status %u\n", st );
597         return FALSE;
598     }
599 }
600
601 /******************************************************************************
602  * SetComputerNameA [KERNEL32.@]
603  *
604  * See SetComputerNameW.
605  */
606 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
607 {
608     BOOL ret;
609     DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
610     LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
611
612     MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
613     ret = SetComputerNameW( nameW );
614     HeapFree( GetProcessHeap(), 0, nameW );
615     return ret;
616 }
617
618 /******************************************************************************
619  * SetComputerNameExW [KERNEL32.@]
620  *
621  */
622 BOOL WINAPI SetComputerNameExW( COMPUTER_NAME_FORMAT type, LPCWSTR lpComputerName )
623 {
624     TRACE("%d, %s\n", type, debugstr_w (lpComputerName));
625     switch( type )
626     {
627     case ComputerNameNetBIOS:
628     case ComputerNamePhysicalNetBIOS:
629         return SetComputerNameW( lpComputerName );
630     default:
631         SetLastError( ERROR_ACCESS_DENIED );
632         return FALSE;
633     }
634 }
635
636 /******************************************************************************
637  * SetComputerNameExA [KERNEL32.@]
638  *
639  */
640 BOOL WINAPI SetComputerNameExA( COMPUTER_NAME_FORMAT type, LPCSTR lpComputerName )
641 {
642     TRACE( "%d, %s\n", type, debugstr_a (lpComputerName) );
643     switch( type )
644     {
645     case ComputerNameNetBIOS:
646     case ComputerNamePhysicalNetBIOS:
647         return SetComputerNameA( lpComputerName );
648     default:
649         SetLastError( ERROR_ACCESS_DENIED );
650         return FALSE;
651     }
652 }
653
654 /***********************************************************************
655  *              DnsHostnameToComputerNameA         (KERNEL32.@)
656  */
657 BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
658     LPSTR computername, LPDWORD size)
659 {
660     DWORD len;
661
662     FIXME("(%s, %p, %p): stub\n", debugstr_a(hostname), computername, size);
663
664     if (!hostname || !size) return FALSE;
665     len = lstrlenA(hostname);
666
667     if (len > MAX_COMPUTERNAME_LENGTH)
668         len = MAX_COMPUTERNAME_LENGTH;
669
670     if (*size < len)
671     {
672         *size = len;
673         return FALSE;
674     }
675     if (!computername) return FALSE;
676
677     memcpy( computername, hostname, len );
678     computername[len + 1] = 0;
679     return TRUE;
680 }
681
682 /***********************************************************************
683  *              DnsHostnameToComputerNameW         (KERNEL32.@)
684  */
685 BOOL WINAPI DnsHostnameToComputerNameW(LPCWSTR hostname,
686     LPWSTR computername, LPDWORD size)
687 {
688     DWORD len;
689
690     FIXME("(%s, %p, %p): stub\n", debugstr_w(hostname), computername, size);
691
692     if (!hostname || !size) return FALSE;
693     len = lstrlenW(hostname);
694
695     if (len > MAX_COMPUTERNAME_LENGTH)
696         len = MAX_COMPUTERNAME_LENGTH;
697
698     if (*size < len)
699     {
700         *size = len;
701         return FALSE;
702     }
703     if (!computername) return FALSE;
704
705     memcpy( computername, hostname, len * sizeof(WCHAR) );
706     computername[len + 1] = 0;
707     return TRUE;
708 }