AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Set hostname - can't write to registry

Ein Thema von WojTec · begonnen am 5. Apr 2012 · letzter Beitrag vom 7. Apr 2012
Antwort Antwort
Seite 1 von 2  1 2      
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

Set hostname - can't write to registry

  Alt 5. Apr 2012, 15:44
Delphi-Version: 2010
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.dllname 'SetComputerNameExW';

SetComputerNameEx(ComputerNamePhysicalDnsHostname, 'NewName');
Returns false. I'm administrator.
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.540 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: Set hostname - can't write to registry

  Alt 5. Apr 2012, 15:49
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?
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Benutzerbild von Bernhard Geyer
Bernhard Geyer

Registriert seit: 13. Aug 2002
17.171 Beiträge
 
Delphi 10.4 Sydney
 
#3

AW: Set hostname - can't write to registry

  Alt 5. Apr 2012, 16:04
Returns false. I'm administrator.
Did you start your application with administration rights (Run as Administrator)?
Windows Vista - Eine neue Erfahrung in Fehlern.
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#4

Re: Set hostname - can't write to registry

  Alt 5. Apr 2012, 16:19
I have admin rights.

Last error looks to be success, but SetComputerName and SetComputerNameEx returns false.
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu
Online

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.133 Beiträge
 
Delphi 12 Athens
 
#5

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

  Alt 5. Apr 2012, 16:38
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?
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#6

Re: Set hostname - can't write to registry

  Alt 5. Apr 2012, 18:27
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.
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu
Online

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.133 Beiträge
 
Delphi 12 Athens
 
#7

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

  Alt 5. Apr 2012, 21:05
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.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu ( 5. Apr 2012 um 21:12 Uhr)
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#8

Re: Set hostname - can't write to registry

  Alt 6. Apr 2012, 16:22
I don't understand what you mean. What I should to do to make it working?
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.343 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: Set hostname - can't write to registry

  Alt 6. Apr 2012, 16:37
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));
Sebastian Jänicke
Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#10

Re: Set hostname - can't write to registry

  Alt 7. Apr 2012, 14:45
Hm, now return True, but names are not changed after restart
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:52 Uhr.
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