【发布时间】:2009-07-20 23:34:54
【问题描述】:
代码需要兼容D2007和D2009。
我的回答:感谢所有回答的人,我同意了:
function ComputerName : String;
var
buffer: array[0..255] of char;
size: dword;
begin
size := 256;
if GetComputerName(buffer, size) then
Result := buffer
else
Result := ''
end;
【问题讨论】:
-
我通常不使用单独的缓冲区来使其更短:SetLength(Result,256); SetLength(Result,GetComputerName(PChar(Result),255));
-
Stijn,您肯定不会使用该代码。
GetComputerName返回Bool并且不接受数字文字作为其第二个参数。 -
Alister,我建议您使用命名常量
Max_ComputerName_Length代替幻数作为缓冲区大小。
标签: delphi computer-name