Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Set hostname - can't write to registry (https://www.delphipraxis.net/167564-set-hostname-cant-write-registry.html)

WojTec 5. Apr 2012 15:44

Delphi-Version: 2010

Set hostname - can't write to registry
 
I'm trying change host name (like My Computer properties --> Computer Name).

I did this:

Delphi-Quellcode:
if OpenKey('SYSTEM\CurrentControlSet\services\Tcpip\Parameters', True) then
try
  WriteString('NV Hostname', FHostName);
  WriteString('Hostname', FHostName);
finally
  CloseKey;
end;
but raises exception that failed to set data for these values.

So, I tried this:

Delphi-Quellcode:
function SetComputerNameEx(NameType: TComputerNameFormat; lpComputerName: PWideChar): BOOL; stdcall;
  external 'kernel32.dll' name 'SetComputerNameExW';

SetComputerNameEx(ComputerNamePhysicalDnsHostname, 'NewName');
Returns false. I'm administrator.

DeddyH 5. Apr 2012 15:49

AW: Set hostname - can't write to registry
 
Zitat:

SetComputerNameEx function

...

Return value

If the function succeeds, the return value is a nonzero value.

If the function fails, the return value is zero. To get extended error information, call GetLastError.
What does GetLastError return?

Bernhard Geyer 5. Apr 2012 16:04

AW: Set hostname - can't write to registry
 
Zitat:

Zitat von WojTec (Beitrag 1160400)
Returns false. I'm administrator.

Did you start your application with administration rights (Run as Administrator)?

WojTec 5. Apr 2012 16:19

Re: Set hostname - can't write to registry
 
I have admin rights.

Last error looks to be success, but SetComputerName and SetComputerNameEx returns false.

himitsu 5. Apr 2012 16:38

AW: Re: Set hostname - can't write to registry
 
Zitat:

Zitat von WojTec (Beitrag 1160406)
I have admin rights.

You might, but also the program?

Zitat:

Last error looks to be success, but SetComputerName and SetComputerNameEx returns false.
And how did you tested it?

WojTec 5. Apr 2012 18:27

Re: Set hostname - can't write to registry
 
Delphi-Quellcode:
//  ShowMessage(BoolToStr(SetComputerName('TTX'), True));
//  ShowMessage(SysErrorMessage(GetLastError));

  // Set computer name and host name
  ShowMessage(BoolToStr(SetComputerNameEx(ComputerNamePhysicalDnsHostname, 'TTX-N'), True));

  // Set computer name
  ShowMessage(BoolToStr(SetComputerNameEx(ComputerNamePhysicalNetBIOS, 'TTX'), True));
I tried with Run as admin too.
On WXP is working, on W7 is not working.

himitsu 5. Apr 2012 21:05

AW: Re: Set hostname - can't write to registry
 
Zitat:

Delphi-Quellcode:
ShowMessage(BoolToStr(SetComputerName('TTX'), True));
ShowMessage(SysErrorMessage(GetLastError));

ShowMessage benutzt die WinAPI.
Und nun rate mal, vor welchem API-Aufruf das GetLastError den Status liefert?

Tipp: Das Anzeigen des Message-Form war erfolgreich.

WojTec 6. Apr 2012 16:22

Re: Set hostname - can't write to registry
 
I don't understand what you mean. What I should to do to make it working?

jaenicke 6. Apr 2012 16:37

AW: Set hostname - can't write to registry
 
You have to ensure, that no other API function is called after the one you want to check against errors. Otherwise the error state is overwritten before you check it...
But here you call ShowMessage for the result of SetComputerName and fetch the error after this. So you won't get an error unless ShowMessage fails...
What about this way?
Delphi-Quellcode:
if SetComputerName('TTX') then
  ShowMessage('Success')
else
  ShowMessage(SysErrorMessage(GetLastError));

WojTec 7. Apr 2012 14:45

Re: Set hostname - can't write to registry
 
Hm, now return True, but names are not changed after restart :(


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:54 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz