我写的读网卡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中调用提示内存读错!没有办法显示传回来的数!
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中调用提示内存读错!没有办法显示传回来的数!
解决方案 »
- MidasLib 单元怎么注册于系统?
- 控制台下使用SetTimer和KillTimer的一个问题
- 第一次用delphi写dll,能否在dll文件中改变对调用它的exe文件中窗体上的控件的属性
- 有谁用过简单且功能强大的报表工具?
- 一个过程和函数的问题,请各位大哥帮帮我
- 如何获得指定目录下的所以文件的文件名?
- 头上的毛又少了许多~~~~我用activex作的页面(包含数据库),怎么不能在客户端执行呀?
- 怎么动态改变窗体和控件的大小而使他们之间的相互之间的相对位置不变?
- 可变记录的用法?
- 在ListView中有没有哪个事件对滚动条操作进行处理?
- NetGetDCName和NetApiBufferFree的使用问题
- 求助!!
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;
strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);
ShowMessage(IntToStr(intIdx));如果里面加上一句ShowMessage()语句,他就不出错了???????
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;