用dll 封装窗体后 iUDPServer 收不到数据?高手进 ( 积分: 100 )

  • 主题发起人 主题发起人 鸢都剑客
  • 开始时间 开始时间

鸢都剑客

Unregistered / Unconfirmed
GUEST, unregistred user!
我把窗本放到DLL中扣,iUDPServer 服务端口可以打开,但是收不到数据,
把窗体生成EXE就可以正常收到数据?
是什么会事啊?
===============DLL文件================================
library FTDI;

{ 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
SysUtils,
Classes,
Forms,
Windows,
Messages,
Ftdiform in 'Ftdiform.pas' {F_FTDI},
KFMSFun in 'KFMSFun.pas';

{$R *.res}
var
DLLApp: TApplication;
DLLScr: TScreen;


function FTDIOpenForm(App: TApplication; Scr: TScreen):TForm;
//var
// ptr:PLongInt;
begin
Application := App;
Screen := Scr;
Application.CreateForm(TF_FTDI, F_FTDI);
result:=F_FTDI;
end;

procedure ExitDLL(Reason: Integer);
begin
if Reason = DLL_PROCESS_DETACH then
begin
Application := DLLApp;
Screen := DLLScr;
end;
end;


exports
FTDIOpenForm;

begin
DLLApp := Application;
DLLScr := Screen;
DLLProc := @ExitDLL;

end.

================Dll Form==================
unit Ftdiform;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer,
IdSocketHandle, KFMSFun;

type
TF_FTDI = class(TForm)
mmo1: TMemo;
btn1: TButton;
idpsrvr1: TIdUDPServer;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure idpsrvr1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
private
{ Private declarations }
public
{ Public declarations }
end;

var
F_FTDI: TF_FTDI;

implementation

{$R *.dfm}

procedure TF_FTDI.FormCreate(Sender: TObject);
begin
idpsrvr1.Active:=False;
idpsrvr1.DefaultPort:=C_CFTDIPort;
idpsrvr1.Active:=True;
MessageBox(Handle, '你好啊', 'TEST', MB_OK);

end;

procedure TF_FTDI.FormClose(Sender: TObject; var Action: TCloseAction);
begin
idpsrvr1.Free;
end;

procedure TF_FTDI.idpsrvr1UDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
KHand:TKFMsgHead;
KFilelist:TFileList;
v_MsgType:Integer;
begin
AData.Read(KHand,SizeOf(KHand));
v_MsgType:=StrToInt(KHand.MsgType);
if v_MsgType=C_SListSign then
begin
AData.Read(KFilelist,SizeOf(KFilelist));
mmo1.Lines.Add(KFilelist.KNum+'---'+KFileList.KFileName);

end;
end;

end.
==================主调用程序=========================
unit Unit1;

interface

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

type
InvokeDLLForm = function(App: TApplication; Scr: TScreen): TForm;
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
F_FTDIForm: TForm;
implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
DLLHandle: THandle;
DLLSub: InvokeDLLForm;

begin
DLLHandle := LoadLibrary('FTDI.dll');
if DLLHandle <> 0 then
begin
@DLLSub := GetProcAddress(DLLHandle, 'FTDIOpenForm');
if Assigned(DLLSub) then
begin
F_FTDIForm := DLLSub(Application, Screen);
F_FTDIForm.Show;
end;
end;
end;

end.
======================公用定义文件==================
unit KFMSFun;
interface
//------------常量定义----------------

const
C_CPort = 8010;
C_SPort = 7010;
C_CFTDIPort = 7011;
C_InitsysSign = 0; //初始化系统
C_LoginSign = 1; //登陆标志
C_Logoutsign = 2; //退出标志
C_SlistSign = 3; //文件列表
C_UpFileSign = 4; //更新文件
C_PCON = 'on';
C_PCOff = 'off';
C_PCdis = 'dis';

//-----------数据头定义---------------
type TKFMsgHead = record
MsgType:string[2];
DataSize:LongWord;
end;
//----------数据体定义----------------
//登陆数据
type TLoginData = packed record
KName:array [0..20] of Char;
KIp:array [0..20] of Char;
Kunit:array [0..10] of Char;
KResult:array[0..1] of Char;
end;
//退出数据
type TLogoutData = packed record
KName:array [0..20] of Char;
KResult:array [0..1] of Char;
end;
//传输文件列表
type TFileList = packed record
KNum:array[0..3] of char;
KFileName:array[0..20] of Char;
KResult:array [0..1] of Char;
end;
//文件更新数据
type TUpFile = packed record
KFile : array[0..20] of Char;
KResult : array[0..20] of Char;
end;
//----------整体数据包--------------
//---------登陆数据包-----------
type TLoginDataPackage = packed record
Head:TKFMsgHead;
body:TLoginData;
end;
//---------下线数据包-----------
type TLogoutDataPackage = packed record
Head:TKFMsgHead;
Body:TLogoutData;
end;
//-------文件列表数据包文件-----------
type TFileListPackage = packed record
Head:TKFMsgHead;
Body:TFIlelist;
end;
//--------更新分发数据包-----------
type TUpFilePackage = packed record
Head:TKFMsgHead;
Body:TUpFile;
end;
 
IdUDPServer.ThreadEvent := True;
 
谢谢成功了.
能说一下这个
IdUDPServer.ThreadEvent := True;
是做什么的吗,
为什么在exe里,不用加这个也可以.
谢谢.!!
 
Indicates the method used for UDP read events.

property ThreadedEvent: boolean;

Description

ThreadedEvent is a Boolean property that indicates if the UDP server is using a seperate thread to perform reads for the UDP connection, or if reads must be synchronised using a UDP method.

When ThreadedEvent is True, the TIdUDPListenerThread.UDPRead method is used to handle read operations. The default value for ThreadedEvent is False.
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
831
import
I
后退
顶部