我在可执行程序中实现通过TIdTelnet登录telnet并发送/执行各种指令,获取相应的执行结果。但是按照同样的思路和方法移植到DLL中时就不灵。执行指令时无任何反映,TIdtelnet控件的OnConnect,OnConnected事件都触发了,就是不触发OnDataAvailable事件。library telnetDLL;{ 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,
UModle in 'UModle.pas' {telnetMode: TDataModule};{$R *.res}
exports
ExeCMDFromFile;
begin
end.unit UModle;interfaceuses
Windows,Forms,Messages,SysUtils,Controls,StdCtrls, ExtCtrls, Classes,
IdBaseComponent,IdComponent, IdTCPConnection,IdTCPClient, IdTelnet,
IniFiles,WinSock;type
TtelnetMode = class(TDataModule)
Telnet: TIdTelnet;
procedure DataModuleCreate(Sender: TObject);
private
{ Private declarations }
procedure SendCMD(cmd:String);
public
{ Public declarations }
procedure ExecuteCMDFromFile;
end;
var
telnetMode: TtelnetMode;
CharInterval,CmdInterval:Integer;
procedure ExeCMDFromFile(AppHandle:LongWord);stdcall;
implementation{$R *.dfm}procedure ExeCMDFromFile(AppHandle:LongWord);stdcall;
var
telnetMode: TtelnetMode;
begin
Application.Handle:=AppHandle;
telnetMode:=TtelnetMode.Create(Application);
If telnetMode.Telnet.Connected then
begin
telnetMode.Telnet.Disconnect;
end;
telnetMode.Telnet.Connect;
Application.ProcessMessages;
telnetMode.ExecuteCMDFromFile;
end;{ TtelnetMode }procedure TtelnetMode.ExecuteCMDFromFile;
var
F:TextFile;
buf:String;
begin
AssignFile(F,ExtractFilePath(Application.ExeName)+'Command.txt');
try
Reset(F);
While Not Eof(F) do
begin
buf:='';
Readln(F,buf);
If Length(buf)>0 then
begin
SendCmd(buf);
Sleep(CmdInterval);
end;
end;
finally
CloseFile(F);
end;
end;procedure TtelnetMode.SendCMD(cmd: String);
var
I:Integer;
buf:string;
begin
If Telnet.Connected then
begin
buf:=cmd;
For I:=1 to Length(buf) do
begin
Telnet.SendCh(buf[I]);
Sleep(CharInterval);
end;
Telnet.SendCh(#13);
Application.ProcessMessages;
end;
end;procedure TtelnetMode.DataModuleCreate(Sender: TObject);
var
F:TIniFile;
begin
F:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'Param.ini');
try
CharInterval:=F.ReadInteger('SysParam','CharInterval',100);
CmdInterval:=F.ReadInteger('SysParam','CommandInterval',1000);
finally
FreeAndNil(F);
end;
end;end.
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,
UModle in 'UModle.pas' {telnetMode: TDataModule};{$R *.res}
exports
ExeCMDFromFile;
begin
end.unit UModle;interfaceuses
Windows,Forms,Messages,SysUtils,Controls,StdCtrls, ExtCtrls, Classes,
IdBaseComponent,IdComponent, IdTCPConnection,IdTCPClient, IdTelnet,
IniFiles,WinSock;type
TtelnetMode = class(TDataModule)
Telnet: TIdTelnet;
procedure DataModuleCreate(Sender: TObject);
private
{ Private declarations }
procedure SendCMD(cmd:String);
public
{ Public declarations }
procedure ExecuteCMDFromFile;
end;
var
telnetMode: TtelnetMode;
CharInterval,CmdInterval:Integer;
procedure ExeCMDFromFile(AppHandle:LongWord);stdcall;
implementation{$R *.dfm}procedure ExeCMDFromFile(AppHandle:LongWord);stdcall;
var
telnetMode: TtelnetMode;
begin
Application.Handle:=AppHandle;
telnetMode:=TtelnetMode.Create(Application);
If telnetMode.Telnet.Connected then
begin
telnetMode.Telnet.Disconnect;
end;
telnetMode.Telnet.Connect;
Application.ProcessMessages;
telnetMode.ExecuteCMDFromFile;
end;{ TtelnetMode }procedure TtelnetMode.ExecuteCMDFromFile;
var
F:TextFile;
buf:String;
begin
AssignFile(F,ExtractFilePath(Application.ExeName)+'Command.txt');
try
Reset(F);
While Not Eof(F) do
begin
buf:='';
Readln(F,buf);
If Length(buf)>0 then
begin
SendCmd(buf);
Sleep(CmdInterval);
end;
end;
finally
CloseFile(F);
end;
end;procedure TtelnetMode.SendCMD(cmd: String);
var
I:Integer;
buf:string;
begin
If Telnet.Connected then
begin
buf:=cmd;
For I:=1 to Length(buf) do
begin
Telnet.SendCh(buf[I]);
Sleep(CharInterval);
end;
Telnet.SendCh(#13);
Application.ProcessMessages;
end;
end;procedure TtelnetMode.DataModuleCreate(Sender: TObject);
var
F:TIniFile;
begin
F:=TIniFile.Create(ExtractFilePath(Application.ExeName)+'Param.ini');
try
CharInterval:=F.ReadInteger('SysParam','CharInterval',100);
CmdInterval:=F.ReadInteger('SysParam','CommandInterval',1000);
finally
FreeAndNil(F);
end;
end;end.
http://community.csdn.net/Expert/topic/3997/3997815.xml?temp=.9533197
期待高人指点中...