Autor Beitrag
Nano-Ware
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: Mo 10.10.11 20:31 
Hey,

ich nutze diese Funktion, die ich im Internet gefunden habe, um den Benutzernamen auszulesen :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function GetCurrUserName: string;
var
  Size              : DWORD;
begin
  Size := MAX_COMPUTERNAME_LENGTH + 1;
  SetLength(Result, Size);
  if GetUserName(PChar(Result), Size) then
    SetLength(Result, Size)
  else
    Result := '';
end;


Wenn ich jetzt diesen String ausgebe und mit "+" etwas hintendranhänge wird das unterschlagen:

ausblenden Delphi-Quelltext
1:
Codex.GetCurrUserName + ' ist cool!'					


Ergibt "Benutzername"
Sollte ergeben "Benutzername ist cool!"

Warum?


Moderiert von user profile iconNarses: Topic aus Internet / Netzwerk verschoben am Di 11.10.2011 um 09:33
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8549
Erhaltene Danke: 478

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mo 10.10.11 21:31 
Ich würde vermuten, dass da noch ein #0 hinten dranhängt. Funktioniert das?
ausblenden Delphi-Quelltext
1:
trim(Codex.GetCurrUserName) + ' ist cool!'					

_________________
We are, we were and will not be.
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: Mo 10.10.11 21:45 
Danke das hat das Problem gelöst (;
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19322
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 10.10.11 21:45 
Die Funktion ist auch falsch programmiert, siehe Doku:
msdn.microsoft.com/e...esktop/ms724432.aspx hat folgendes geschrieben:
On output, the variable receives the number of TCHARs copied to the buffer, including the terminating null character.


Korrekt wäre:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
function GetCurrUserName: string;
var
  Size: DWORD;
begin
  Size := MAX_COMPUTERNAME_LENGTH + 1;
  SetLength(Result, Size);
  if GetUserName(PChar(Result), Size) then
    SetLength(Result, Size - 1)
  else
    Result := '';
end;
Nano-Ware Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 7



BeitragVerfasst: Di 11.10.11 00:36 
Dann auch nocheinmal ein Danke an Sie Herr Jaenicke!