Recent

Author Topic: Code from Delphi does not work in Lazarus  (Read 6333 times)

gcrazydv

  • Newbie
  • Posts: 6
Code from Delphi does not work in Lazarus
« on: April 03, 2017, 11:53:39 am »
Hello!
Have a problem.
There is a code by definition mac addresses.
And so swears at every line where Char is used;
And everything works perfectly in Delphi 7.0. Help me guys plz

At compilation in lazarus I receive an error
Unit1.pas (40,36) Error: Incompatible types: got "Char" expected "Byte"
On this line,
NCB.ncb_command := Char(NCBRESET); Used AnsiChar, Nothing has changed, the same mistake
NCB.ncb_lana_num := AnsiChar(Lana);

And unit1.pas(42,39) Error: Incompatible types: got "Char" expected "QWord"
on line
if Netbios(@NCB) <> Char(NRC_GOODRET) then

And unit1.pas(51,21) Error: Incompatible types: got "Char" expected "NCB.Array[0..15] Of Byte"
On
NCB.ncb_callname := '*';


Code: Pascal  [Select][+][-]
  1. uses
  2. windows,nb30;
  3.  
  4. function GetAdapterInfo(Lana: Char): string;
  5. var
  6. Adapter: TAdapterStatus;
  7. NCB: TNCB;
  8. begin
  9. FillChar(NCB, SizeOf(NCB), 0);
  10. NCB.ncb_command := Char(NCBRESET);
  11. NCB.ncb_lana_num := AnsiChar(Lana);
  12. if Netbios(@NCB) <> Char(NRC_GOODRET) then
  13. begin
  14. Result := 'Address not known';
  15. Exit;
  16. end;
  17.  
  18. FillChar(NCB, SizeOf(NCB), 0);
  19. NCB.ncb_command := Char(NCBASTAT);
  20. NCB.ncb_lana_num := AnsiChar(Lana);
  21. NCB.ncb_callname := '*';
  22.  
  23. FillChar(Adapter, SizeOf(Adapter), 0);
  24. NCB.ncb_buffer := @Adapter;
  25. NCB.ncb_length := SizeOf(Adapter);
  26. if Netbios(@NCB) <> Char(NRC_GOODRET) then
  27. begin
  28. Result := 'Address not known';
  29. Exit;
  30. end;
  31. Result :=
  32. IntToHex(Byte(Adapter.adapter_addre ss[0]), 2) + '-' +
  33. IntToHex(Byte(Adapter.adapter_addre ss[1]), 2) + '-' +
  34. IntToHex(Byte(Adapter.adapter_addre ss[2]), 2) + '-' +
  35. IntToHex(Byte(Adapter.adapter_addre ss[3]), 2) + '-' +
  36. IntToHex(Byte(Adapter.adapter_addre ss[4]), 2) + '-' +
  37. IntToHex(Byte(Adapter.adapter_addre ss[5]), 2);
  38. end;
  39.  
  40. function GetMACAddress: string;
  41. var
  42. AdapterList: TLanaEnum;
  43. NCB: TNCB;
  44. begin
  45. FillChar(NCB, SizeOf(NCB), 0);
  46. NCB.ncb_command := Char(NCBENUM);
  47. NCB.ncb_buffer := @AdapterList;
  48. NCB.ncb_length := SizeOf(AdapterList);
  49. Netbios(@NCB);
  50. if Byte(AdapterList.length) > 0 then
  51. Result := GetAdapterInfo(Char(AdapterList.lan a[0]))
  52. else
  53. Result := 'Address not known';
  54. end;

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Code from Delphi does not work in Lazarus
« Reply #1 on: April 03, 2017, 12:04:53 pm »
Wouldn't it be better to rewrite it for Lazarus/FPC.

See this topic. There is code to do what you want in FPC/Lazarus.
http://forum.lazarus.freepascal.org/index.php/topic,10465.0.html


(Begin with downloading the example from this post because the other code might be corrupted because the code-tags are not used in the post itself.)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Code from Delphi does not work in Lazarus
« Reply #2 on: April 03, 2017, 12:16:19 pm »
Besides what rvk wrote, the definition of UCHAR is clear.

No idea why you would want to solve that with using normal characters, and in case delphi7 did so, then it is plain wrong.

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Code from Delphi does not work in Lazarus
« Reply #3 on: April 03, 2017, 12:19:10 pm »
The declarations in D7 are wrong: char <> byte.
Code: Pascal  [Select][+][-]
  1.  
  2. function GetAdapterInfo(Lana: Char): string;
  3. var
  4. Adapter: TAdapterStatus;
  5. NCB: TNCB;
  6. begin
  7. FillChar(NCB, SizeOf(NCB), 0);
  8. NCB.ncb_command := byte(NCBRESET);
  9. NCB.ncb_lana_num := byte(Lana);
  10. if Netbios(@NCB) <> byte(NRC_GOODRET) then
  11. begin
  12. Result := 'Address not known';
  13. Exit;
  14. end;
  15.  
  16. FillChar(NCB, SizeOf(NCB), 0);
  17. NCB.ncb_command := byte(NCBASTAT);
  18. NCB.ncb_lana_num := byte(Lana);
  19. NCB.ncb_callname[0] := byte('*');
  20.  
  21. FillChar(Adapter, SizeOf(Adapter), 0);
  22. NCB.ncb_buffer := @Adapter;
  23. NCB.ncb_length := SizeOf(Adapter);
  24. if Netbios(@NCB) <> byte(NRC_GOODRET) then
  25. begin
  26. Result := 'Address not known';
  27. Exit;
  28. end;
  29. Result :=
  30. IntToHex(byte(Adapter.adapter_address[0]), 2) + '-' +
  31. IntToHex(byte(Adapter.adapter_address[1]), 2) + '-' +
  32. IntToHex(byte(Adapter.adapter_address[2]), 2) + '-' +
  33. IntToHex(byte(Adapter.adapter_address[3]), 2) + '-' +
  34. IntToHex(byte(Adapter.adapter_address[4]), 2) + '-' +
  35. IntToHex(Byte(Adapter.adapter_address[5]), 2);
  36. end;
  37.  
  38. function GetMACAddress: string;
  39. var
  40. AdapterList: TLanaEnum;
  41. NCB: TNCB;
  42. begin
  43. FillChar(NCB, SizeOf(NCB), 0);
  44. NCB.ncb_command := byte(NCBENUM);
  45. NCB.ncb_buffer := @AdapterList;
  46. NCB.ncb_length := SizeOf(AdapterList);
  47. Netbios(@NCB);
  48. if Byte(AdapterList.length) > 0 then
  49. Result := GetAdapterInfo(char(AdapterList.lana[0]))
  50. else
  51. Result := 'Address not known';
  52. end;
  53.  
Specialize a type, not a var.

gcrazydv

  • Newbie
  • Posts: 6
Re: Code from Delphi does not work in Lazarus
« Reply #4 on: April 03, 2017, 12:30:37 pm »
The declarations in D7 are wrong: char <> byte.
Code: Pascal  [Select][+][-]
  1.  
  2. function GetAdapterInfo(Lana: Char): string;
  3. var
  4. Adapter: TAdapterStatus;
  5. NCB: TNCB;
  6. begin
  7. FillChar(NCB, SizeOf(NCB), 0);
  8. NCB.ncb_command := byte(NCBRESET);
  9. NCB.ncb_lana_num := byte(Lana);
  10. if Netbios(@NCB) <> byte(NRC_GOODRET) then
  11. begin
  12. Result := 'Address not known';
  13. Exit;
  14. end;
  15.  
  16. FillChar(NCB, SizeOf(NCB), 0);
  17. NCB.ncb_command := byte(NCBASTAT);
  18. NCB.ncb_lana_num := byte(Lana);
  19. NCB.ncb_callname[0] := byte('*');
  20.  
  21. FillChar(Adapter, SizeOf(Adapter), 0);
  22. NCB.ncb_buffer := @Adapter;
  23. NCB.ncb_length := SizeOf(Adapter);
  24. if Netbios(@NCB) <> byte(NRC_GOODRET) then
  25. begin
  26. Result := 'Address not known';
  27. Exit;
  28. end;
  29. Result :=
  30. IntToHex(byte(Adapter.adapter_address[0]), 2) + '-' +
  31. IntToHex(byte(Adapter.adapter_address[1]), 2) + '-' +
  32. IntToHex(byte(Adapter.adapter_address[2]), 2) + '-' +
  33. IntToHex(byte(Adapter.adapter_address[3]), 2) + '-' +
  34. IntToHex(byte(Adapter.adapter_address[4]), 2) + '-' +
  35. IntToHex(Byte(Adapter.adapter_address[5]), 2);
  36. end;
  37.  
  38. function GetMACAddress: string;
  39. var
  40. AdapterList: TLanaEnum;
  41. NCB: TNCB;
  42. begin
  43. FillChar(NCB, SizeOf(NCB), 0);
  44. NCB.ncb_command := byte(NCBENUM);
  45. NCB.ncb_buffer := @AdapterList;
  46. NCB.ncb_length := SizeOf(AdapterList);
  47. Netbios(@NCB);
  48. if Byte(AdapterList.length) > 0 then
  49. Result := GetAdapterInfo(char(AdapterList.lana[0]))
  50. else
  51. Result := 'Address not known';
  52. end;
  53.  

I did it that way, it compiles.
The problem is that it does not show the mac address.
Why is that?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Code from Delphi does not work in Lazarus
« Reply #5 on: April 03, 2017, 12:32:17 pm »
Just leave the casting to byte away. Only required at one location. the rest matches the structures.

oh, and:
function GetAdapterInfo(Lana: UCHAR): string;

gcrazydv

  • Newbie
  • Posts: 6
Re: Code from Delphi does not work in Lazarus
« Reply #6 on: April 03, 2017, 12:38:11 pm »
Just leave the casting to byte away. Only required at one location. the rest matches the structures.

oh, and:
function GetAdapterInfo(Lana: UCHAR): string;
Nothing changed.
I already went through all the options, the outcome is one.
Does not work.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Code from Delphi does not work in Lazarus
« Reply #7 on: April 03, 2017, 12:43:05 pm »
Nothing changed.
I already went through all the options, the outcome is one.
Does not work.
it works for me tm, so then the question becomes: what is the target and do you have enough rights ? antivirus ?

edit: and for good measures:
edit2:and a slightly modified version that is able to show info for all adapters it was able to find
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$MODE OBJFPC}
  4.  
  5. uses
  6.   windows, nb30, sysutils, strutils;
  7.  
  8. function GetAdapterInfo(Lana: UCHAR): string;
  9. var
  10.   Adapter: TAdapterStatus;
  11.   NCB: TNCB;
  12. begin
  13.   NCB := default(TNCB);                 //  FillChar(NCB, SizeOf(NCB), 0);
  14.   NCB.ncb_command  := NCBRESET;
  15.   NCB.ncb_lana_num := Lana;
  16.   if Netbios(@NCB) <> NRC_GOODRET then
  17.   begin
  18.     Result := 'Address not known';
  19.     Exit;
  20.   end;
  21.  
  22.   NCB := default(TNCB);                 //  FillChar(NCB, SizeOf(NCB), 0);
  23.   NCB.ncb_command  := NCBASTAT;
  24.   NCB.ncb_lana_num := Lana;
  25.   NCB.ncb_callname[0] := Byte('*');     //  NCB.ncb_callname := Asc('*');
  26.  
  27.   Adapter := default(TAdapterStatus);   //  FillChar(Adapter, SizeOf(Adapter), 0);
  28.   NCB.ncb_buffer := @Adapter;
  29.   NCB.ncb_length := SizeOf(Adapter);
  30.   if Netbios(@NCB) <> NRC_GOODRET then
  31.   begin
  32.     Result := 'Address not known';
  33.     Exit;
  34.   end;
  35.   Result :=
  36.     IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' +
  37.     IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' +
  38.     IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' +
  39.     IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' +
  40.     IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' +
  41.     IntToHex(Byte(Adapter.adapter_address[5]), 2);
  42. end;
  43.  
  44. function GetMACAddress: string;
  45. var
  46.   AdapterList: TLanaEnum;
  47.   NCB: TNCB;
  48.   AdapterIndex: UCHAR;
  49.   Info: string;
  50. begin
  51.   NCB := default(TNCB);         //  FillChar(NCB, SizeOf(NCB), 0);
  52.   NCB.ncb_command := NCBENUM;
  53.   NCB.ncb_buffer  := @AdapterList;
  54.   NCB.ncb_length  := SizeOf(AdapterList);
  55.  
  56.   if Netbios(@NCB) = NRC_GOODRET then
  57.   begin
  58.     if AdapterList.length > 0 then    //   if Byte(AdapterList.length) > 0
  59.     begin
  60.       Result := '';
  61.       for AdapterIndex := 0 to AdapterList.length-1 do
  62.       begin
  63.         WriteStr
  64.         (
  65.           Info,
  66.           'Adapter[', AdapterIndex, '].MacAddress = ',
  67.           GetAdapterInfo(AdapterList.lana[AdapterIndex]),
  68.           IfThen(AdapterIndex < AdapterList.length-1, ', ', '')
  69.         );
  70.         Result := Result + Info;
  71.       end;
  72.     end
  73.     else Result := 'No adapters found';
  74.   end
  75.   else
  76.     Result := 'Unable to obtain adapterlist';
  77. end;
  78.  
  79. var
  80.   S: String;
  81. begin
  82.   S := GetMacAddress;
  83.   WriteLn('MacAddressess are : ', S);
  84. end.
  85.  
« Last Edit: April 03, 2017, 01:29:26 pm by molly »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Code from Delphi does not work in Lazarus
« Reply #8 on: April 03, 2017, 02:00:58 pm »
And since it does not seem for TS to ever return and answer the questions, a note of warning for those (still) wanting to use such code:

Netbios is deprecated:
Quote
[Netbios is not supported on Windows Vista, Windows Server 2008, and subsequent versions of the operating system]

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Code from Delphi does not work in Lazarus
« Reply #9 on: April 03, 2017, 02:04:43 pm »
Extra note...
The solution mentioned in the following topic (including the example.zip) still works perfectly (at least on Windows 10 64 bit). It does not use the old Netbios.

http://forum.lazarus.freepascal.org/index.php/topic,10465.msg160794.html#msg160794

gcrazydv

  • Newbie
  • Posts: 6
Re: Code from Delphi does not work in Lazarus
« Reply #10 on: April 03, 2017, 03:16:45 pm »
Nothing changed.
I already went through all the options, the outcome is one.
Does not work.
it works for me tm, so then the question becomes: what is the target and do you have enough rights ? antivirus ?

edit: and for good measures:
edit2:and a slightly modified version that is able to show info for all adapters it was able to find
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$MODE OBJFPC}
  4.  
  5. uses
  6.   windows, nb30, sysutils, strutils;
  7.  
  8. function GetAdapterInfo(Lana: UCHAR): string;
  9. var
  10.   Adapter: TAdapterStatus;
  11.   NCB: TNCB;
  12. begin
  13.   NCB := default(TNCB);                 //  FillChar(NCB, SizeOf(NCB), 0);
  14.   NCB.ncb_command  := NCBRESET;
  15.   NCB.ncb_lana_num := Lana;
  16.   if Netbios(@NCB) <> NRC_GOODRET then
  17.   begin
  18.     Result := 'Address not known';
  19.     Exit;
  20.   end;
  21.  
  22.   NCB := default(TNCB);                 //  FillChar(NCB, SizeOf(NCB), 0);
  23.   NCB.ncb_command  := NCBASTAT;
  24.   NCB.ncb_lana_num := Lana;
  25.   NCB.ncb_callname[0] := Byte('*');     //  NCB.ncb_callname := Asc('*');
  26.  
  27.   Adapter := default(TAdapterStatus);   //  FillChar(Adapter, SizeOf(Adapter), 0);
  28.   NCB.ncb_buffer := @Adapter;
  29.   NCB.ncb_length := SizeOf(Adapter);
  30.   if Netbios(@NCB) <> NRC_GOODRET then
  31.   begin
  32.     Result := 'Address not known';
  33.     Exit;
  34.   end;
  35.   Result :=
  36.     IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' +
  37.     IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' +
  38.     IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' +
  39.     IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' +
  40.     IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' +
  41.     IntToHex(Byte(Adapter.adapter_address[5]), 2);
  42. end;
  43.  
  44. function GetMACAddress: string;
  45. var
  46.   AdapterList: TLanaEnum;
  47.   NCB: TNCB;
  48.   AdapterIndex: UCHAR;
  49.   Info: string;
  50. begin
  51.   NCB := default(TNCB);         //  FillChar(NCB, SizeOf(NCB), 0);
  52.   NCB.ncb_command := NCBENUM;
  53.   NCB.ncb_buffer  := @AdapterList;
  54.   NCB.ncb_length  := SizeOf(AdapterList);
  55.  
  56.   if Netbios(@NCB) = NRC_GOODRET then
  57.   begin
  58.     if AdapterList.length > 0 then    //   if Byte(AdapterList.length) > 0
  59.     begin
  60.       Result := '';
  61.       for AdapterIndex := 0 to AdapterList.length-1 do
  62.       begin
  63.         WriteStr
  64.         (
  65.           Info,
  66.           'Adapter[', AdapterIndex, '].MacAddress = ',
  67.           GetAdapterInfo(AdapterList.lana[AdapterIndex]),
  68.           IfThen(AdapterIndex < AdapterList.length-1, ', ', '')
  69.         );
  70.         Result := Result + Info;
  71.       end;
  72.     end
  73.     else Result := 'No adapters found';
  74.   end
  75.   else
  76.     Result := 'Unable to obtain adapterlist';
  77. end;
  78.  
  79. var
  80.   S: String;
  81. begin
  82.   S := GetMacAddress;
  83.   WriteLn('MacAddressess are : ', S);
  84. end.
  85.  


Windows 10 , not use Antivirus

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   windows, nb30, strutils;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.     { private declarations }
  20.   public
  21.     { public declarations }
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  function GetAdapterInfo(Lana: UCHAR): string;
  31. var
  32.   Adapter: TAdapterStatus;
  33.   NCB: TNCB;
  34. begin
  35.   NCB := default(TNCB);                 //  FillChar(NCB, SizeOf(NCB), 0);
  36.   NCB.ncb_command  := NCBRESET;
  37.   NCB.ncb_lana_num := Lana;
  38.   if Netbios(@NCB) <> NRC_GOODRET then
  39.   begin
  40.     Result := 'Address not known';
  41.     Exit;
  42.   end;
  43.  
  44.   NCB := default(TNCB);                 //  FillChar(NCB, SizeOf(NCB), 0);
  45.   NCB.ncb_command  := NCBASTAT;
  46.   NCB.ncb_lana_num := Lana;
  47.   NCB.ncb_callname[0] := Byte('*');     //  NCB.ncb_callname := Asc('*');
  48.  
  49.   Adapter := default(TAdapterStatus);   //  FillChar(Adapter, SizeOf(Adapter), 0);
  50.   NCB.ncb_buffer := @Adapter;
  51.   NCB.ncb_length := SizeOf(Adapter);
  52.   if Netbios(@NCB) <> NRC_GOODRET then
  53.   begin
  54.     Result := 'Address not known';
  55.     Exit;
  56.   end;
  57.   Result :=
  58.     IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' +
  59.     IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' +
  60.     IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' +
  61.     IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' +
  62.     IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' +
  63.     IntToHex(Byte(Adapter.adapter_address[5]), 2);
  64. end;
  65.  
  66. function GetMACAddress: string;
  67. var
  68.   AdapterList: TLanaEnum;
  69.   NCB: TNCB;
  70.   AdapterIndex: UCHAR;
  71.   Info: string;
  72. begin
  73.   NCB := default(TNCB);         //  FillChar(NCB, SizeOf(NCB), 0);
  74.   NCB.ncb_command := NCBENUM;
  75.   NCB.ncb_buffer  := @AdapterList;
  76.   NCB.ncb_length  := SizeOf(AdapterList);
  77.  
  78.   if Netbios(@NCB) = NRC_GOODRET then
  79.   begin
  80.     if AdapterList.length > 0 then    //   if Byte(AdapterList.length) > 0
  81.     begin
  82.       Result := '';
  83.       for AdapterIndex := 0 to AdapterList.length-1 do
  84.       begin
  85.         WriteStr
  86.         (
  87.           Info,
  88.           'Adapter[', AdapterIndex, '].MacAddress = ',
  89.           GetAdapterInfo(AdapterList.lana[AdapterIndex]),
  90.           IfThen(AdapterIndex < AdapterList.length-1, ', ', '')
  91.         );
  92.         Result := Result + Info;
  93.       end;
  94.     end
  95.     else Result := 'No adapters found';
  96.   end
  97.   else
  98.     Result := 'Unable to obtain adapterlist';
  99. end;
  100.  
  101. procedure TForm1.Button1Click(Sender: TObject);
  102. begin
  103.  ShowMessage(GetMACAddress);
  104. end;
  105.  
  106. End.
  107.  
  108.  
click button and see Unable to obtain adapterlist.
What i do bad?(

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Code from Delphi does not work in Lazarus
« Reply #11 on: April 03, 2017, 03:22:33 pm »
What i do bad?(
What exact part of:
Quote
[Netbios is not supported on Windows Vista, Windows Server 2008, and subsequent versions of the operating system]
...did you not understood ?

Or, to put it into other words: you are using the wrong Operating System for that code to work.

gcrazydv

  • Newbie
  • Posts: 6
Re: Code from Delphi does not work in Lazarus
« Reply #12 on: April 03, 2017, 03:24:42 pm »
What i do bad?(
What exact part of:
Quote
[Netbios is not supported on Windows Vista, Windows Server 2008, and subsequent versions of the operating system]
...did you not understood ?

Or, to put it into other words: you are using the wrong Operating System for that code to work.
ty man, i go sleep...

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Code from Delphi does not work in Lazarus
« Reply #13 on: April 03, 2017, 03:28:24 pm »
@gcrazydv:
just use the code that was linked to by rvk and you won't have nightmare's :D

gcrazydv

  • Newbie
  • Posts: 6
Re: Code from Delphi does not work in Lazarus
« Reply #14 on: April 03, 2017, 03:32:50 pm »
@gcrazydv:
just use the code that was linked to by rvk and you won't have nightmare's :D
code from rvk good work.
ty all ;)

 

TinyPortal © 2005-2018