如何 這個類TMyUDP 封裝 為 DLL(100分)

  • 主题发起人 主题发起人 hgptm
  • 开始时间 开始时间
H

hgptm

Unregistered / Unconfirmed
GUEST, unregistred user!
如題,
TmyUDP 單元見下:

unit UDPUnit;
interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, NMUDP,DateUtils;


type
TUDPRecDataEvent = procedure(Recstr:string) of object;

TMyUDP = Class
private
FRecstr: string;
//接收緩存

FonUDPRecData : TUDPRecDataEvent;

procedure FUDPRecData(Recstr:string);

procedure onReceiveData(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
protected
FLocalUDPPort: integer;
FRemoteUDPPort: integer;
FRemoteUDPIP: string;

NMUDP: TNMUDP;

Procedure SetLocalUDPPort(APort: integer);
Procedure SetRemoteUDPPort(APort: integer);
Procedure SetRemoteUDPIP(AIP: string);


public
constructor Create;
destructor Destroy;
override;

property LocalUDPPort: Integer read FLocalUDPPort write SetLocalUDPPort;
property RemoteUDPPort: integer Read FRemoteUDPPort write SetRemoteUDPPort;
property RemoteUDPIP : String Read FRemoteUDPIP write SetRemoteUDPIP;

Published

Property onUDPRecData: TUDPRecDataEvent Read FonUDPRecData write FonUDPRecData;
end;


implementation

constructor TMyUDP.Create;
begin

inherited Create;

FRecstr:='';

NMUDP := TNMUDP.Create(nil);
NMUDP.ReportLevel :=1;
NMUDP.OnDataReceived := onReceiveData;
end;


destructor TMyUDP.Destroy;
begin

if NMUDP <> nil then

begin

FreeAndnil(NMUDP);
end;

inherited;
end;


//設置本地端口
Procedure TMyUDP.SetLocalUDPPort(APort: integer);
begin

//if APort = FLocalUDPPort then
exit;
FLocalUDPPort := APort;
NMUDP.LocalPort := FLocalUDPPort;
end;


//設置遠程端口
Procedure TMyUDP.SetRemoteUDPPort(APort: integer);
begin

//if APort = FRemoteUDPPort then
exit;
FRemoteUDPPort := APort;
NMUDP.RemotePort := FRemoteUDPPort;
end;


//設置遠程IP
Procedure TMyUDP.SetRemoteUDPIP(AIP: string);
begin

//if Uppercase(trim(AIP)) = Uppercase(trim(FRemoteUDPIP)) then
exit;
FRemoteUDPIP := Uppercase(trim(AIP));
NMUDP.RemoteHost := FRemoteUDPIP;
end;




procedure TMyUDP.onReceiveData(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
var
SS: string;
MyStream: TMemoryStream;

begin

if NumberBytes=-1 then
exit;

MyStream := TMemoryStream.Create;

try
(Sender as TNMUDP).ReadStream(MyStream);
//非:NMUDP.ReadStream(MyStream);
ss :='';
SetLength(SS, NumberBytes);
MyStream.Read(SS[1], NumberBytes);

FRecstr := SS;

Finally
MyStream.Free;
end;



FUDPRecData(FRecstr);
//通知控件使用者:開始處理

end;



//通知 控件使用者
procedure TMyUDP.FUDPRecData(Recstr:string);
begin

if Assigned(onUDPRecData) then
begin

try
onUDPRecData(Recstr);
Except on E: Exceptiondo
begin

//showmessage(e.Message);
end;

end;

end;

end;






end.
 
按照正常的Dll封装提供出接口不就行了,
要那个功能,就提供那个接口函数。
你难道不会封装动态库??
 
Dear 草原骏马:
確實,我封裝 Dll 是個 新手, 請教了!

要求1. 能將以下函數,export :
Procedure SetLocalUDPPort(APort: integer);
Procedure SetRemoteUDPPort(APort: integer);
Procedure SetRemoteUDPIP(AIP: string);
2.接收數據的時候,能觸發事件.
最好,能給出例子!!

多謝了!!!!!
 
顶一个,祝你好运。
 
第一个问题好处理:
Procedure SetLocalUDPPort(APort: integer);
Procedure SetRemoteUDPPort(APort: integer);
Procedure SetRemoteUDPIP(AIP: string);
这三个函数必须是
public里的才可以

Procedure SetLocalUDPPort(APort: integer);
stdcall;
export;
Procedure SetRemoteUDPPort(APort: integer);stdcall;
export;
Procedure SetRemoteUDPIP(AIP: string);stdcall;
export;

要触发事件就麻烦了。因为要触发事件必须要提供类接口才可以,然后在类接口里做回调函数就可以实现,但是你现在只做了个dll,并没有发布类接口,这就麻烦了,不如你给用Activex做吧,这个一定可以实现你的要求,有函数也有触发事件
 
Dear 草原骏马:
太感謝你了!
那ActiveX 又該如何 做呢? 謝謝指點!!
 
Dear 草原骏马:
按照剛才的指點, 我完善了項目, 但是編譯通不過,是否還有玄機:
Dll單元:
library Project1;

uses
SysUtils,
Classes,
UDPUnit in 'UDPUnit.pas';

{$R *.res}
Exports

SetLocalUDPPort;
//這裡提示問題:Undeclared Identifier: SetLocalUDPPort


begin

end.



UDPUnit單元:

unit UDPUnit;
interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, NMUDP,DateUtils;


type
TUDPRecDataEvent = procedure(Recstr:string) of object;

TMyUDP = Class
private
FRecstr: string;
//接收緩存

FonUDPRecData : TUDPRecDataEvent;

procedure FUDPRecData(Recstr:string);

procedure onReceiveData(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
protected
FLocalUDPPort: integer;
FRemoteUDPPort: integer;
FRemoteUDPIP: string;

NMUDP: TNMUDP;

public
constructor Create;
destructor Destroy;
override;

Procedure SetLocalUDPPort(APort: integer);
stdcall;
export;
Procedure SetRemoteUDPPort(APort: integer);
Procedure SetRemoteUDPIP(AIP: string);

property LocalUDPPort: Integer read FLocalUDPPort write SetLocalUDPPort;
property RemoteUDPPort: integer Read FRemoteUDPPort write SetRemoteUDPPort;
property RemoteUDPIP : String Read FRemoteUDPIP write SetRemoteUDPIP;

Published

Property onUDPRecData: TUDPRecDataEvent Read FonUDPRecData write FonUDPRecData;
end;


implementation

constructor TMyUDP.Create;
begin

inherited Create;

FRecstr:='';

NMUDP := TNMUDP.Create(nil);
NMUDP.ReportLevel :=1;
NMUDP.OnDataReceived := onReceiveData;
end;


destructor TMyUDP.Destroy;
begin

if NMUDP <> nil then

begin

FreeAndnil(NMUDP);
end;

inherited;
end;


//設置本地端口
Procedure TMyUDP.SetLocalUDPPort(APort: integer);
stdcall;
export;
begin

//if APort = FLocalUDPPort then
exit;
FLocalUDPPort := APort;
NMUDP.LocalPort := FLocalUDPPort;
end;


//設置遠程端口
Procedure TMyUDP.SetRemoteUDPPort(APort: integer);
begin

//if APort = FRemoteUDPPort then
exit;
FRemoteUDPPort := APort;
NMUDP.RemotePort := FRemoteUDPPort;
end;


//設置遠程IP
Procedure TMyUDP.SetRemoteUDPIP(AIP: string);
begin

//if Uppercase(trim(AIP)) = Uppercase(trim(FRemoteUDPIP)) then
exit;
FRemoteUDPIP := Uppercase(trim(AIP));
NMUDP.RemoteHost := FRemoteUDPIP;
end;




procedure TMyUDP.onReceiveData(Sender: TComponent;
NumberBytes: Integer;
FromIP: String;
Port: Integer);
var
SS: string;
MyStream: TMemoryStream;

begin

if NumberBytes=-1 then
exit;

MyStream := TMemoryStream.Create;

try
(Sender as TNMUDP).ReadStream(MyStream);
//非:NMUDP.ReadStream(MyStream);
ss :='';
SetLength(SS, NumberBytes);
MyStream.Read(SS[1], NumberBytes);

FRecstr := SS;

Finally
MyStream.Free;
end;



FUDPRecData(FRecstr);
//通知控件使用者:開始處理

end;



//通知 控件使用者
procedure TMyUDP.FUDPRecData(Recstr:string);

begin

if Assigned(onUDPRecData) then
begin

try
onUDPRecData(Recstr);
Except on E: Exceptiondo
begin

//showmessage(e.Message);
end;

end;

end;

end;






end.
 

Similar threads

I
回复
0
查看
551
import
I
I
回复
0
查看
539
import
I
I
回复
0
查看
524
import
I
I
回复
0
查看
693
import
I
后退
顶部