Forum > Windows CE

Reading MAC address of ethernet (wifi) from WinCE device

(1/3) > >>

ertank:
Hi,

I have found several threads in forum.lazarus about subject. Unfortunately, none of them worked for me.

Is there any one which already could get this piece of information?

Thanks.

Jurassic Pork:
hello,
you can try using tprocess and  tregexpr  with the command ipconfig /all.

something like that ( description and mac address of each network adapter)  :


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// uses process,regexpr;procedure TForm1.ReadAdapterInfos();      const READ_BYTES = 2048;      var      i : integer;      Re1,Re2: TRegExpr;      aProcess: TProcess; //TProcess is crossplatform is best way      MemStream: TMemoryStream;      NumBytes: LongInt;      BytesRead: LongInt;      Lines: TStringList;    begin     // A temp Memorystream is used to buffer the output     MemStream := TMemoryStream.Create;     Lines :=TStringList.Create;     BytesRead := 0;      try       re1 := TRegExpr.Create;       // regex for description of network adapter       re1.Expression := '.*Description.*:(.+)';       re2 :=  TRegExpr.Create;       // regex for mac address       re2.Expression := '.*Adresse physique.*:(.+)';       aProcess := TProcess.Create(nil);       aProcess.Executable := 'ipconfig.exe';       aProcess.Parameters.Add('/all');       aprocess.ShowWindow := swoHIDE;       AProcess.Options := AProcess.Options + [poUsePipes,poStderrToOutPut];       Memo1.Lines.Clear;       aProcess.Execute;       while aProcess.Running do       begin         // make sure we have room         MemStream.SetSize(BytesRead + READ_BYTES);         // try reading it         NumBytes := aProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);         if NumBytes > 0 // All read() calls will block, except the final one.            then Inc(BytesRead, NumBytes)         else            BREAK // Program has finished execution.       end;       MemStream.SetSize(BytesRead);       Lines.LoadFromStream(MemStream);       for i := 0 to Lines.Count-1 do       begin       if re1.Exec(Lines[i]) then  Memo1.lines.Add(Trim(Re1.Match[1]));       if re2.Exec(Lines[i]) then  Memo1.lines.Add(Trim(Re2.Match[1]));       end;       finally       re1.Free;       re2.Free;       aProcess.Free;       Lines.Free;       MemStream.Free;       end;    end;        
1 - The expressions for the regexp are trivial -> can be optimized
2 - The expression of re2 for mac address must be changed depending of the country. In the example it is for french Ipconfig.

Friendly J.P

ertank:
Hi,

Thanks for the sample code. I tried to use it. However, my WinCE device insists not to run ipconfig.exe

First, I get "Cannot execute empty command-line" error message. Then I also assign "ipconfig.exe" to aProcess.CommandLine (which supposed to be depreciated).
Second, I started to get "Failed to execute ipconfig.exe : 87" which I have no idea what it means.

I tested and confirm that my device has ipconfig.exe and it executes as expected from command shell.

Are you able to support on above problems?

Thanks.

Jurassic Pork:
 hello,
try to call the command-line interpreter before the command : 

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- AProcess.Executable := 'cmd.exe'; // or with the whole path    AProcess.Parameters.Add('/c');    AProcess.Parameters.Add('ipconfig.exe');    AProcess.Parameters.Add('/all'); 
Friendly, J.P

ertank:
Hi,

I didn't mention earlier. I am using Lazaus 1.6.0. Cross compiler for ARM is installed.

Still receiving error saying "Cannot execute empty command-line" again. I suppose I need to put something in aProcess.CommandLine parameter. This parameter is depreciated as it is indicated though. Current code is as follows:
I have some type casting to prevent compiler warnings.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// uses process,regexpr;function TDM.ReadAdapterInfos:String;const READ_BYTES = 2048;vari : integer;Re1,Re2: TRegExpr;aProcess: TProcess; //TProcess is crossplatform is best wayMemStream: TMemoryStream;NumBytes: LongInt;BytesRead: LongInt;Lines: TStringList;begin  Result := EmptyStr;  // A temp Memorystream is used to buffer the output  MemStream := TMemoryStream.Create;  Lines :=TStringList.Create;  BytesRead := 0;  try    re1 := TRegExpr.Create;    // regex for description of network adapter    re1.Expression := '.*Description.*:(.+)';    re2 :=  TRegExpr.Create;    // regex for mac address    re2.Expression := '.*Adresse physique.*:(.+)';    aProcess := TProcess.Create(nil);    AProcess.Executable := 'cmd.exe'; // or with the whole path    AProcess.Parameters.Add('/c');    AProcess.Parameters.Add('ipconfig.exe');    AProcess.Parameters.Add('/all');    AProcess.ShowWindow := swoHIDE;    AProcess.Options := AProcess.Options + [poUsePipes,poStderrToOutPut];    aProcess.Execute;    while aProcess.Running do begin      // make sure we have room      MemStream.SetSize(BytesRead + READ_BYTES);      // try reading it      NumBytes := aProcess.Output.Read((MemStream.Memory + BytesRead)^, READ_BYTES);      if NumBytes > 0 // All read() calls will block, except the final one.        then Inc(BytesRead, NumBytes)      else        BREAK // Program has finished execution.    end;    MemStream.SetSize(BytesRead);    Lines.LoadFromStream(MemStream);    for i := 0 to Lines.Count-1 do begin      if re1.Exec(WideString(Lines[i])) then  Result := Result + #13 + Trim(string(Re1.Match[1]));      if re2.Exec(WideString(Lines[i])) then  Result := Result + #13 + Trim(string(Re2.Match[1]));    end;  finally    re1.Free;    re2.Free;    aProcess.Free;    Lines.Free;    MemStream.Free;  end;end; 

Navigation

[0] Message Index

[#] Next page

Go to full version