用Indy 控件写一个Telnet的dll 出现的问题?急急 !!!
小弟由于要通过Windows客户端发批命令去一台Linux主机上执行相关命令,因此想用Indy控件写一个通过telnet方式去执行命令的dll来供其他开发工具调用。
由于我第一次写DLL,出现问题很大。
调用出现主要错误提示如下:
---------------------------
Debugger Exception Notification
---------------------------
Project Project2.exe raised exception class EAccessViolation with message 'Access violation at address 00245DD9 in module 'Project1.dll'. Read of address 00000000'. Process stopped. Use Step or Run to continue.
现把源代码发布如下:(敬请高手指正)
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
SysUtils,
Classes,
IdTCPConnection, IdTCPClient, IdTelnet;
//{$R *.res}
Function Telnet(host,user,pwd,command:pchar;handle:Thandle) :integer;stdcall;
var
i : integer;
lhost,luser,lpwd,lcommand:string;
IdTelnetDemo: TIdTelnet;
begin
lhost:=string(host);
luser:=string(user) ;
lpwd:=string(pwd);
lcommand:=string(command) ;
try
//CoInitialize(nil);
IDTelnetDemo.Create(nil);
IDTelnetDemo.Host :=lhost;
IDTelnetDemo.port :=23;
IdTelnetDemo.Connect;
if IdTelnetDemo.Connected then
begin
for i := 1 to length(luser) do
begin
IdTelnetDemo.SendCh(luser[i]);
IdTelnetDemo.SendCh(#13);
end;
for i:= 1 to length(lpwd) do
begin
IdTelnetDemo.SendCh(lpwd[i]);
IdTelnetDemo.SendCh(#13);
end;
for i:= 1 to length(lcommand) do
begin
IdTelnetDemo.SendCh(lcommand[i]);
IdTelnetDemo.SendCh(#13);
end;
result:=1;
IdTelnetDemo.Disconnect;
end
else
result:=2;
//except
//result:=3;
finally
begin
IdTelnetDemo.Destroy;
end;
end;
end;
exports
Telnet;
end.
Delphi调用程序部分:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons;
type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
edtID: TEdit;
edtUser: TEdit;
edtHost: TEdit;
GroupBox2: TGroupBox;
Memo1: TMemo;
BitBtnOK: TBitBtn;
BitBtnCancel: TBitBtn;
procedure BitBtnCancelClick(Sender: TObject);
procedure BitBtnOKClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Function Telnet(host:string;user:string;id:string;command:string) :integer;stdcall;
var
Form1: TForm1;
implementation
{$R *.dfm}
Function Telnet;external 'Project1.dll' name 'Telnet';
procedure TForm1.BitBtnCancelClick(Sender: TObject);
begin
close;
end;
procedure TForm1.BitBtnOKClick(Sender: TObject);
var
host,user,id,command :string;
i:integer;
begin
host:='192.168.5.2';
user:='bonny';
id:='happysky';
command:='mkdir telnet';
i:=Telnet(pchar('192.168.5.2'),pchar('bonny'),pchar('happysky'),pchar('mkdir telnet'));
edtuser.text:=inttostr(i);
end;
end.
这个调用程序是一个Demo,最终结果是想传一个批处理命令给Linux主机去执行。用户只要在客户机上点击一下相关选项就行了。
现在问题只是在传递参数过程中出现问题,不知这种方式的可执行程度如何,且程序当中还有未知的问题敬请高手指教。
谢谢!!!