我写的读网卡MAC地址的DLL:library Project1;{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }  uses
   Windows , SysUtils , NB30;function NBGetAdapterAddress: string;
var
  NCB: TNCB;                                                // Netbios control block //NetBios控制块
  ADAPTER: TADAPTERSTATUS;                                  // Netbios adapter status//取网卡状态
  LANAENUM: TLANAENUM;                                      // Netbios lana
  intIdx: Integer;                                          // Temporary work value//临时变量
  cRC: Char;                                                // Netbios return code//NetBios返回值
  strTemp: string;                                          // Temporary string//临时变量
begin
  Result := '';  try
    ZeroMemory(@NCB, SizeOf(NCB));                          // Zero control blocl    NCB.ncb_command := Chr(NCBENUM);                        // Issue enum command
    cRC := NetBios(@NCB);    NCB.ncb_buffer := @LANAENUM;                            // Reissue enum command
    NCB.ncb_length := SizeOf(LANAENUM);
    cRC := NetBios(@NCB);
    if Ord(cRC) <> 0 then
      exit;    ZeroMemory(@NCB, SizeOf(NCB));                          // Reset adapter
    NCB.ncb_command := Chr(NCBRESET);
    NCB.ncb_lana_num := LANAENUM.lana[0];
    cRC := NetBios(@NCB);
    if Ord(cRC) <> 0 then
      exit;
    ZeroMemory(@NCB, SizeOf(NCB));                          // Get adapter address
    NCB.ncb_command := Chr(NCBASTAT);
    NCB.ncb_lana_num := LANAENUM.lana[0];
    StrPCopy(NCB.ncb_callname, '*');
    NCB.ncb_buffer := @ADAPTER;
    NCB.ncb_length := SizeOf(ADAPTER);
    cRC := NetBios(@NCB);    strTemp := '';                                          // Convert it to string
    for intIdx := 0 to 5 do
      strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);
    Result := strTemp;
  finally
  end;
end;exports NBGetAdapterAddress;
{$R *.res}begin
end.调用程序:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, NB30;type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  function NBGetAdapterAddress: string;
  external 'Project1.dll';
implementation{$R *.dfm}procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(NBGetAdapterAddress);
end;end.调用后,网卡的MAC地址是读出来了,可是出错,显示"Invalid pointer operation"
在VC VB中调用提示内存读错!没有办法显示传回来的数!

解决方案 »

  1.   

    function NBGetAdapterAddress: string 后面加 stdcal
      

  2.   

    function NBGetAdapterAddress  : PChar ; export; {$ifdef WIN32} stdcall ; {$endif}
      

  3.   

    用PCHAR传
    function NBGetAdapterAddress  : PChar ; export;  stdcall ;
    for intIdx := 0 to 5 do
          strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);
        Result := strnew(pchar(strTemp));
    主程序使用
    implementation
    function NBGetAdapterAddress: pchar;stdcall
      external 'Project1.dll';procedure TForm1.Button2Click(Sender: TObject);
    var
     pc:pchar;
    begin
     pc:=NBGetAdapterAddress;
     showmessage(strpas(pc));
     strdispose(pc);
    end;
      

  4.   

    for intIdx := 0 to 5 do
          strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);
          
          ShowMessage(IntToStr(intIdx));如果里面加上一句ShowMessage()语句,他就不出错了???????
      

  5.   

    我解决了,在DLL中不要返回String这个类型!
    DLL中:function NBGetAdapterAddress : Pchar;for intIdx := 0 to 5 do
      strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);      Result := Pchar(strTemp);主程序中调用:procedure TForm1.Button1Click(Sender: TObject);
    begin
     Edit1.Text:=string(NBGetAdapterAddress);
    end;