2012년 10월 13일 토요일

ipconfig /all 명령으로 맥어드레스 가져오기

function RunDosCommand : string; var hReadPipe : THandle; hWritePipe : THandle; SI : TStartUpInfo; PI : TProcessInformation; SA : TSecurityAttributes; SD : TSecurityDescriptor; BytesRead : DWORD; Dest : array[0..8192] of Ansichar; CmdLine : array[0..1024] of char; TmpList : TStringList; S, Param : string; Avail, ExitCode, wrResult : DWORD; begin { Dos Application } InitializeSecurityDescriptor(@SD, SECURITY_DESCRIPTOR_REVISION); SetSecurityDescriptorDacl(@SD, True, nil, False); SA.nLength := SizeOf(SA); SA.lpSecurityDescriptor := @SD; SA.bInheritHandle := True; CreatePipe(hReadPipe, hWritePipe, @SA, 4096); try Screen.Cursor := crHourglass; FillChar(SI, SizeOf(SI), 0); SI.cb := SizeOf(TStartUpInfo); SI.wShowWindow := SW_HIDE; SI.dwFlags := STARTF_USESHOWWINDOW; SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES; SI.hStdOutput := hWritePipe; SI.hStdError := hWritePipe; StrPCopy(CmdLine, 'ipconfig /all'); if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then begin ExitCode := 0; while ExitCode = 0 do begin wrResult := WaitForSingleObject(PI.hProcess, 500); if PeekNamedPipe(hReadPipe, nil, 0, nil, @Avail, nil) then begin if Avail > 0 then begin FillChar(Dest, SizeOf(Dest), 0); ReadFile(hReadPipe, Dest, Avail, BytesRead, nil); Result := StrPas(Dest); Application.ProcessMessages; end; end; if wrResult <> WAIT_TIMEOUT then ExitCode := 1; end; GetExitCodeProcess(PI.hProcess, ExitCode); CloseHandle(PI.hProcess); CloseHandle(PI.hThread); end; finally CloseHandle(hReadPipe); CloseHandle(hWritePipe); Screen.Cursor := crDefault; end; end; function GetMacAddress : string; var i : Integer; sList, tList : TStringList; begin sList := TStringList.Create; tList := TStringList.Create; ExtractStrings([#13],[], PChar(RunDosCommand), sList); for i := 0 to sList.Count - 1 do begin if (Pos('물리적 주소', sList[i]) > 0) or (Pos('Physical Address', sList[i]) > 0) then begin tList.Clear; ExtractStrings([':'], [], PChar(sList[i]), tList); // ':' 로 나눈 뒤에 것이 MAC Address if Length(Trim(tList[1])) = 17 then begin Result := Trim(tList[1]); Exit; end else Result := 'Could not found'; end; end; end; 위의 두 펑션을 추가한뒤 다음과 같이 사용한다. 버튼의 커멘드에서

댓글 없음: