I am trying to hide console input when a user has to enter a password in a command prompt window.
I had the bright idea of redirecting the output of the command prompt to the NUL file but this code does not work even though you can execute the function without error.
I am sure there used to be an easy way to do this!
Do forget to include windows in the uses clause.
Thanks for any help in advance Jack T
function GetPassWordInput:String;
var
C:Char;
S:String;
H,HNULL:Cardinal;
B:BOOL;
begin
HNULL := CreateFile('NUL',GENERIC_WRITE,FILE_SHARE_WRITE,nil, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM,0);
Assert(HNULL <> 0,'CREATING NUL FILE FAILED');
H:= GetStdHandle(STD_OUTPUT_HANDLE);
B:=SetStdHandle(STD_OUTPUT_HANDLE,HNULL);
Assert(B,'Set standhard handle failed to assign HNULL');
try
begin
repeat
Read(C);
if C=#8 then
begin
if Length(S)>1 then
begin
S:=LeftStr(S,Length(S)-1);
end;
end
else
if C<>#13 then
begin
S:=S+C;
end;
until C=#13;
end
finally
begin
SetStdHandle(STD_OUTPUT_HANDLE,H);
CloseHandle(HNULL);
end;
end;
Result := S;
end;