Jump to content
NamoRamana

NetWkstaTransportEnum API in NetAPI32.dll

Recommended Posts

I am running the following NetAPI32.dll > NetWkstaTransportEnum API (through JEDI JwaLM library) to get MAC address of a local machine... The API gives correct MAC addresses on somewhat older windows 10 machines.. It fails to return any MAC address on newer windows 10 machines (lEntriesRead comes out 0).. Tried both the scenarios through LAN and Wifi connectivity.

 

Any ideas?

 

function TForm1.GetNetAPIMacAddress: string;
const
  NERR_Success = 0;
var
  lStatus : NET_API_STATUS;
  lEntriesRead : DWORD;
  lEntriesTotal: DWORD;
  hRes: DWORD;
  lpBuffer : PWkstaTransportInfo0;
  lNBTransReq : PWkstaTransportInfo0;
  lServerName : WideString;
  i : DWORD;
  lMAC : string;
begin
  Memo1.Lines.Add('');
  Memo1.Lines.Add('');
  Memo1.Lines.Add('==========================');
  Memo1.Lines.Add('Call to NetApi32.DLL >>   lStatus := JwaLM.NetWkstaTransportEnum(LPWSTR(lServerName), 0, Pointer(lpBuffer),' +
    sLineBreak + ' MAX_PREFERRED_LENGTH, lEntriesRead, lEntriesTotal, @hRes)');
  Result := '';
  lServerName := '';
  hRes := 0;

  lStatus := JwaLM.NetWkstaTransportEnum(LPWSTR(lServerName), 0, Pointer(lpBuffer),
    MAX_PREFERRED_LENGTH, lEntriesRead, lEntriesTotal, @hRes);

  Memo1.Lines.Add('hRes: ' + IntToStr(hres));
  Memo1.Lines.Add('lEntriesRead: ' + IntToStr(lEntriesRead));
  Memo1.Lines.Add('lEntriesTotal: ' + IntToStr(lEntriesTotal));
  Memo1.Lines.Add('lStatus: ' + IntToStr(lStatus));

  try
    if (lStatus = NERR_Success) and (lEntriesRead > 0)  then
    begin
      lNBTransReq := lpBuffer;
      Result := '';
      for i := 0 to lEntriesRead - 1 do
      begin
        lMAC := lNBTransReq.wkti0_transport_address;
        // exclude disabled nics...
        if lMAC <> '000000000000' then
        begin
          Insert('-', lMAC, 11);
          Insert('-', lMAC, 9);
          Insert('-', lMAC, 7);
          Insert('-', lMAC, 5);
          Insert('-', lMAC, 3);
          Result := Result + lMAC+'  ';
          Memo1.Lines.Add('MAC Addess: ' + Result);
        end;
        Inc(lNBTransReq);
      end;
    end;
  finally
    NetApiBufferFree(lpBuffer);
  end;
  Memo1.Lines.Add('');
  Memo1.Lines.Add('');
end;

 

Share this post


Link to post

Yes, I tried that as well.. For all the interfaces, the addresses come out at 0s though...

 

I also tried the solution suggested by Remy here: https://stackoverflow.com/questions/23563932/getadaptersinfo-not-working-on-delphi-xe6. This works fine.

 

The code posted in my original post has been part of the production system. Looks like if I don't find any solution to fix that code (or any reasonable explanation as to why it works on few machines and why it isn't on others), then I might have to with alternate API suggested here. 

 

Thank you.

Share this post


Link to post

If NetWkstaTransportEnum belongs to APIs that use netbios, netbios itself may be disabled. Microsoft has begun to disable NETBIOS by default, still leaving the option to enable and use it. I read an article about this some time ago but I can't find it now.

Share this post


Link to post

I am not sure if NetWkstaTransportEnum belongs to APIs that use netbios.

"NetBIOS over tcpip" is already enabled in the case where that API is not retuning any addresses. 

Share this post


Link to post

This is funny, a few weeks ago I added advanced cross-platform support of network adapters in the mORMot 2 network layer.

It can retrieve all mac addresses of the network interfaces, and also much more information like each kind of adapter, its MTU, speed or gateway/broadcast IPs.

And it is cross-platform, so it works not only on Windows but also on POSIX (tested on MacOS and Linux).
https://github.com/synopse/mORMot2/blob/fe3fdf53e54dc770bda8d9858c8d6ff5ebf4ac4d/src/net/mormot.net.sock.pas#L517

  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×