Przechwycenie wyjścia konsoli jest proste:
function GetMACFromCommandLine():string;
var mac:string;
function ExecuteConsole(Executable:PChar; CommandLine:PChar; Directory:PChar):boolean;
const bufSize = 256;
var readPipe :THandle;
writePipe :THandle;
security :SECURITY_ATTRIBUTES;
info :STARTUPINFO;
process :PROCESS_INFORMATION;
buf :array[0..bufSize-1] of char;
bytesRead :DWord;
text :string;
line :string;
newLinePos :integer;
res:integer;
const ADRES = 'Adres fizyczny. . . . . . . . . . :';
begin
result:=FALSE;
security.nLength:=sizeof(security);
security.lpSecurityDescriptor:=nil;
security.bInheritHandle:=TRUE;
if CreatePipe(readPipe, writePipe, @security, 0) then
begin
ZeroMemory(@info, sizeof(info));
with info do
begin
cb := sizeof( info );
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := 0;
hStdOutput := writePipe;
hStdError := writePipe;
end;
if CreateProcess(Executable, CommandLine, nil, nil, TRUE, 0, nil, Directory, info, process) then
begin
CloseHandle( writePipe );
text:='';
while ReadFile( readPipe, buf, bufSize, bytesRead, nil) do
begin
text:=text+buf;
repeat
newLinePos:=Pos(#13, text);
if (newLinePos=0) then break;
line:=copy(text,1, newLinePos-1);
delete(text, 1, newLinePos);
//'line' zawiera tu już pełną linię tekstu z wyjścia uruchomionego programu
res:=Pos(ADRES, Line);
if res>0 then
mac:=Copy(line, res+Length(ADRES)+1, 17);
until Application.Terminated;
ZeroMemory(@buf, bufSize);
end;
result:=TRUE;
end;
CloseHandle( readPipe );
end;
end;
begin
result:='';
if ExecuteConsole('c:\windows\system32\ipconfig.exe', ' /all', nil) then
result:=mac;
end;
Przykład użycia:
Caption:=GetMACFromCommandLine();
Kilka uwag: po pierwsze pobierze to MAC ostatniej znalezionej karty sieciowej (modem Neo jest widziany przecież jako normalna karta sieciowa). Po drugie - wcale taka zabawna metoda pobrania to nie jest, bo jest zależna choćby od wersji językowej systemu; Do pobrania MACa 'normalnie' wystarczy użyć zwykłe funkcje systemowe. One na pewno działają, tylko może nie potrafisz ich użyć.