Delphi获取当前计算名用户名代码
procedure TForm1.Button1Click(Sender: TObject);
var
pp:pChar;
ss:String;
ii:DWORD;
begin
GetMem(pp,255);
ii:=255;
GetUserName(pp,ii);
ss:=StrPas(pp);
ShowMessage(ss);
FreeMem(pp);
end;
一般用下面的
uses 中加入 ActiveX, ShlObj,
procedure TForm1.Button1Click(Sender: TObject);
var
Allocator: IMalloc;
SpecialDir: PItemIdList;
FBuf: array[0..MAX_PATH] of Char;
PerDir: string;
begin
if SHGetMalloc(Allocator) = NOERROR then
begin
SHGetSpecialFolderLocation(Form1.Handle, CSIDL_PERSONAL, SpecialDir);
SHGetPathFromIDList(SpecialDir, @FBuf[0]);
Allocator.Free(SpecialDir);
ShowMessage(string(FBuf));
end;
end;