我在DLL里面有一个窗口,有一个 MEMO1 控件。。(50分)

  • 主题发起人 主题发起人 JasonLaw
  • 开始时间 开始时间
J

JasonLaw

Unregistered / Unconfirmed
GUEST, unregistred user!
我使用

Memo1.Lines.LoadFromFile('C:/a.txt');

但是不能将文本文件里面的数据读入到 memo1 中

我用 Memo1.Lines.LoadFromStream(MyStream); 流方式也不行。为什么呢?
 
我就可以
检查一下USER有没有StdCtrls,
 
Memo1.Lines.LoadFromFile('C:/a.txt'); 我试过了可以啊。
没有做过任何特殊设置的啊。
 
1.有出现什么错误提示吗?
2.确定 Memo1.Lines.LoadFromFile('C:/a.txt'); 这句被执行了吗?
 
to liyinwei

没有任务提示。

这被执行了的。。。。 不过。载入的是几个空格
 
可能是那个“文本文件”的问题,检查一下是否含有非法字符。
 
你的DLL在什么地方调用的?如果在IIS里面调用的话,检查一下权限.

应该可以调试的吧,怎么不调试看看呢...
 
不关文本文件时。

我试过,文本文件里面,就只简单的保存着字母。 ABC

都不能读取出来。

与IIS无关。。


我实际上是写一个通用的 SQL 数据库 动态创建的 DLL 文件。

以后写软件的时候,就直接调用这个DLL就行了。

所以这个 DLL 文件,我是用来读取 一个 *.sql 文件的。

后来发觉不行。

就在 DLL 窗口里面放了一个 MEMO 来试,,果然读不出。

干脆,就直接从文本文件里面读。也不行。。。。

哪怕那个 文本文件 里面只有简单的三个字母 ABC
 
应该不会有问题只要执行了 看你是不是设置文字颜色和背景颜色一样区分不出来
 
to delphfans,

无语中.............
 
那确实奇怪!
liyinwei@gmail.com
可以的话楼主发个 Dll 源代码和那个文本文件我看看。
 
library cntsql;

{ 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. }

{ 关于DLL内存管理的重要注释:}

uses
SysUtils,
Classes,
SQLCnt in 'SQLCnt.pas' {frmSQLCnt},
global in 'global.pas';

{$R *.res}

exports
info,
ShowConnectionSQLServer;

begin
end.


============================



unit SQLCnt;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IniFiles, DB, ADODB, ComCtrls ;

type
TfrmSQLCnt = class(TForm)
Label1: TLabel;
EdtServerAddress: TEdit;
GroupBox1: TGroupBox;
RBWinNT: TRadioButton;
RBSQLServer: TRadioButton;
EdtUserName: TEdit;
Label2: TLabel;
EdtPassword: TEdit;
Label3: TLabel;
Label4: TLabel;
CmbDBName: TComboBox;
btnTestConnect: TButton;
BtnSaveSetup: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
btnCreateDB: TButton;
BtnCancel: TButton;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
ADOCommand1: TADOCommand;
procedure BtnSaveSetupClick(Sender: TObject);
procedure RBWinNTClick(Sender: TObject);
procedure RBSQLServerClick(Sender: TObject);
procedure btnTestConnectClick(Sender: TObject);
procedure CmbDBNameDropDown(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure BtnCancelClick(Sender: TObject);
procedure btnCreateDBClick(Sender: TObject);
private
{ Private declarations }
procedure ReadSetup(const fn,SectionName:string);
procedure SaveSetup(const fn,SectionName:string);
procedure EnabledEdit(const bUsed:Boolean);
public
{ Public declarations }
end;

//function ShowConnectionSQLServer( IniSetupFile,SectionName:PChar;Phandle: Thandle):Boolean;stdcall;
function ShowConnectionSQLServer(Phandle: Thandle):Boolean;stdcall;
function info(IniSetupFile,SectionName:PChar):Boolean;stdcall;

var
frmSQLCnt: TfrmSQLCnt;

implementation

uses global;

{$R *.dfm}

//function ShowConnectionSQLServer(IniSetupFile,SectionName:PChar;Phandle: Thandle):Boolean;stdcall;
//begin
// sIniSetupFile:=IniSetupFile;
// sSectionName :=SectionName;
// Application.Handle :=Phandle;
// Result :=False;
// with TfrmSQLCnt.Create(Application) do begin
// try
// MsgBox(sIniSetupFile);
// finally
// Free;
// end;
//
// end;
//end;

function info(IniSetupFile,SectionName:PChar):Boolean;stdcall;
begin
sIniSetupFile:=IniSetupFile;
sSectionName :=SectionName;
Result :=True;
end;

function ShowConnectionSQLServer(Phandle: Thandle):Boolean;stdcall;
begin
Application.Handle :=Phandle;
Result :=False;
with TfrmSQLCnt.Create(Application) do begin
try
ShowModal;
finally
Free;
end;

end;
end;

procedure TfrmSQLCnt.ReadSetup(const fn,SectionName:string);
var
MyIni:TIniFile;
begin
MyIni:=TIniFile.Create(fn);
try
EdtServerAddress.Text :=MyIni.ReadString(SectionName,'SERVERIP','127.0.0.1');
RBWinNT.Checked :=MyIni.ReadBool(SectionName,'NTLogin',true);
EnabledEdit( not RBWinNT.Checked);
RBSQLServer.Checked :=MyIni.ReadBool(SectionName,'SQLLogin',False);
EnabledEdit( RBSQLServer.Checked);
EdtUserName.Text :=MyIni.ReadString(SectionName,'UserName','sa');
EdtPassword.Text :=Decc(MyIni.ReadString(SectionName,'Password',''));
CmbDBName.Text :=MyIni.ReadString(SectionName,'DataBaseName','');
finally
MyIni.Free;
end;
end;

procedure TfrmSQLCnt.SaveSetup(const fn,SectionName:string);
var
MyIni:TIniFile;
begin
MyIni:=TIniFile.Create(fn);
try
MyIni.WriteString(SectionName,'SERVERIP',EdtServerAddress.Text);
MyIni.WriteBool(SectionName,'NTLogin',RBWinNT.Checked);
MyIni.WriteBool(SectionName,'SQLLogin',RBSQLServer.Checked);
MyIni.WriteString(SectionName,'UserName',EdtUserName.Text);
MyIni.WriteString(SectionName,'Password',Encc(EdtPassword.Text));
MyIni.WriteString(SectionName,'DataBaseName',CmbDBName.Text);
finally
MyIni.Free;
end;
end;

procedure TfrmSQLCnt.BtnSaveSetupClick(Sender: TObject);
begin
SaveSetup(sIniSetupFile,sSectionName);
end;

procedure TfrmSQLCnt.RBWinNTClick(Sender: TObject);
begin
EnabledEdit(False);
end;

procedure TfrmSQLCnt.RBSQLServerClick(Sender: TObject);
begin
EnabledEdit(true);
end;

procedure TfrmSQLCnt.EnabledEdit(const bUsed: Boolean);
begin
if bUsed then begin
Label2.Enabled :=True; EdtUserName.Enabled :=True;
Label3.Enabled :=True; EdtPassword.Enabled :=True;
end else begin
Label2.Enabled :=False; EdtUserName.Enabled :=False;
Label3.Enabled :=False; EdtPassword.Enabled :=False;
end;

end;

procedure TfrmSQLCnt.btnTestConnectClick(Sender: TObject);
begin
with ADOConnection1 do begin
Close;
if RBWinNT.Checked then
ConnectionString :=format(sConnectSQLServerString2,[Trim(EdtServerAddress.Text),
Trim(CmbDBName.Text)])
else
ConnectionString :=format(sConnectSQLServerString1,[Trim(EdtPassword.Text),
Trim(EdtUserName.Text),Trim(EdtServerAddress.Text),Trim(CmbDBName.Text)]);
// MsgBox(ConnectionString);
try
Open;
Application.MessageBox('连接成功!', '成功', MB_OK + MB_ICONINFORMATION
+ MB_TOPMOST);

except
Application.MessageBox('数据服务器连接成功!', '错误', MB_OK +
MB_ICONSTOP + MB_TOPMOST);

end;
end;
end;

procedure TfrmSQLCnt.CmbDBNameDropDown(Sender: TObject);
begin
with ADOConnection1 do begin
Close;
if RBWinNT.Checked then
ConnectionString :=format(sConnectSQLServerString2,[Trim(EdtServerAddress.Text),'master'])
else
ConnectionString :=format(sConnectSQLServerString1,[Trim(EdtPassword.Text),
Trim(EdtUserName.Text),Trim(EdtServerAddress.Text),'master']);
// MsgBox(ConnectionString);
try
Open;
ADOQuery1.Close;
ADOQuery1.SQL.Text :='select [name],dbid from sysdatabases '+
'where dbid >6 '+
'order by dbid DESC';
ADOQuery1.Open;
ADOQuery1.First;
while not ADOQuery1.Eof do begin
CmbDBName.Items.Add(ADOQuery1.FieldValues['name']);
ADOQuery1.Next;
end;
ADOQuery1.Close;
except
MsgBox('数据库连接失败!',1);
end;
end;
end;

procedure TfrmSQLCnt.FormShow(Sender: TObject);
begin
ReadSetup(sIniSetupFile,sSectionName);
end;

procedure TfrmSQLCnt.BtnCancelClick(Sender: TObject);
begin
close;
end;

procedure TfrmSQLCnt.btnCreateDBClick(Sender: TObject);
var
MyStream:TFileStream;
begin
MyStream :=TFileStream.Create(ExtractFilePath(ParamStr(0))+'test.txt',1);


Memo1.Clear;

////////////////////////////////////////////////////////
// 就是这里。无论用流方式还是用普通方式,均不能载入 aaa.txt 里面的文本。
// 哪怕这个 aaa.txt 只是简单的 ABC 三个字母,也不行
////////////////////////////////////////////////////////
Memo1.Lines.LoadFromStream(MyStream);
Memo1.Lines.SaveToFile(ExtractFilePath(ParamStr(0))+'aaa.txt')



// Memo1.Lines.Add(ExtractFilePath(ParamStr(0))+'test.txt');
// Memo1.Lines.LoadFromFile(ExtractFilePath(ParamStr(0))+'test.txt');
{ try
ADOCommand1.CommandText :='use [数据库] ' +
'CREATE TABLE [tttttttttttttt] ( '+
' [fID] [int] IDENTITY (1, 1) NOT NULL ) ';
ADOCommand1.Execute;
except
Application.MessageBox('失败2!', '错误', MB_OK + MB_ICONSTOP +
MB_TOPMOST);

end;
end; }
// if not ADOConnection1.Connected then ADOConnection1.Open;
end;
end.
 
你用INI文件读试试看看能不能读出来..,
 
楼主就不能发个源代码过来吗?不测试一下很难发现问题的。
 
to liyinwei 我不是发了吗?

你没注意看吧。
 
楼主,你是不是犯了低级错误了啊。
下面这句有问题:
MyStream :=TFileStream.Create(ExtractFilePath(ParamStr(0))+'test.txt',1);

注意TFileStream.Create的第二个参数,
fmOpenRead = $0000;
fmOpenWrite = $0001;
fmOpenReadWrite = $0002;
所以读取文件内容至少也得是 0 或 2 吧。
 
to xifengge

呵,就算是 换成其它两个参数,

读出来的内容。也只是

 
你最好把你的DLL单元完整贴出来,或者发给我一份看看。
 
library cntsql;

{ 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. }

{ 关于DLL内存管理的重要注释:}

uses
SysUtils,
Classes,
SQLCnt in 'SQLCnt.pas' {frmSQLCnt},
global in 'global.pas';

{$R *.res}

exports
info,
ShowConnectionSQLServer;

begin
end.


=====================================



unit SQLCnt;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IniFiles, DB, ADODB, ComCtrls ;

type
TfrmSQLCnt = class(TForm)
Label1: TLabel;
EdtServerAddress: TEdit;
GroupBox1: TGroupBox;
RBWinNT: TRadioButton;
RBSQLServer: TRadioButton;
EdtUserName: TEdit;
Label2: TLabel;
EdtPassword: TEdit;
Label3: TLabel;
Label4: TLabel;
CmbDBName: TComboBox;
btnTestConnect: TButton;
BtnSaveSetup: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
btnCreateDB: TButton;
BtnCancel: TButton;
Memo1: TMemo;
OpenDialog1: TOpenDialog;
ADOCommand1: TADOCommand;
procedure BtnSaveSetupClick(Sender: TObject);
procedure RBWinNTClick(Sender: TObject);
procedure RBSQLServerClick(Sender: TObject);
procedure btnTestConnectClick(Sender: TObject);
procedure CmbDBNameDropDown(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure BtnCancelClick(Sender: TObject);
procedure btnCreateDBClick(Sender: TObject);
private
{ Private declarations }
procedure ReadSetup(const fn,SectionName:string);
procedure SaveSetup(const fn,SectionName:string);
procedure EnabledEdit(const bUsed:Boolean);
public
{ Public declarations }
end;

//function ShowConnectionSQLServer( IniSetupFile,SectionName:PChar;Phandle: Thandle):Boolean;stdcall;
function ShowConnectionSQLServer(Phandle: Thandle):Boolean;stdcall;
function info(IniSetupFile,SectionName:PChar):Boolean;stdcall;

var
frmSQLCnt: TfrmSQLCnt;

implementation

uses global;

{$R *.dfm}

//function ShowConnectionSQLServer(IniSetupFile,SectionName:PChar;Phandle: Thandle):Boolean;stdcall;
//begin
// sIniSetupFile:=IniSetupFile;
// sSectionName :=SectionName;
// Application.Handle :=Phandle;
// Result :=False;
// with TfrmSQLCnt.Create(Application) do begin
// try
// MsgBox(sIniSetupFile);
// finally
// Free;
// end;
//
// end;
//end;

function info(IniSetupFile,SectionName:PChar):Boolean;stdcall;
begin
sIniSetupFile:=IniSetupFile;
sSectionName :=SectionName;
Result :=True;
end;

function ShowConnectionSQLServer(Phandle: Thandle):Boolean;stdcall;
begin
Application.Handle :=Phandle;
Result :=False;
with TfrmSQLCnt.Create(Application) do begin
try
ShowModal;
finally
Free;
end;

end;
end;

procedure TfrmSQLCnt.ReadSetup(const fn,SectionName:string);
var
MyIni:TIniFile;
begin
MyIni:=TIniFile.Create(fn);
try
EdtServerAddress.Text :=MyIni.ReadString(SectionName,'SERVERIP','127.0.0.1');
RBWinNT.Checked :=MyIni.ReadBool(SectionName,'NTLogin',true);
EnabledEdit( not RBWinNT.Checked);
RBSQLServer.Checked :=MyIni.ReadBool(SectionName,'SQLLogin',False);
EnabledEdit( RBSQLServer.Checked);
EdtUserName.Text :=MyIni.ReadString(SectionName,'UserName','sa');
EdtPassword.Text :=Decc(MyIni.ReadString(SectionName,'Password',''));
CmbDBName.Text :=MyIni.ReadString(SectionName,'DataBaseName','');
finally
MyIni.Free;
end;
end;

procedure TfrmSQLCnt.SaveSetup(const fn,SectionName:string);
var
MyIni:TIniFile;
begin
MyIni:=TIniFile.Create(fn);
try
MyIni.WriteString(SectionName,'SERVERIP',EdtServerAddress.Text);
MyIni.WriteBool(SectionName,'NTLogin',RBWinNT.Checked);
MyIni.WriteBool(SectionName,'SQLLogin',RBSQLServer.Checked);
MyIni.WriteString(SectionName,'UserName',EdtUserName.Text);
MyIni.WriteString(SectionName,'Password',Encc(EdtPassword.Text));
MyIni.WriteString(SectionName,'DataBaseName',CmbDBName.Text);
finally
MyIni.Free;
end;
end;

procedure TfrmSQLCnt.BtnSaveSetupClick(Sender: TObject);
begin
SaveSetup(sIniSetupFile,sSectionName);
end;

procedure TfrmSQLCnt.RBWinNTClick(Sender: TObject);
begin
EnabledEdit(False);
end;

procedure TfrmSQLCnt.RBSQLServerClick(Sender: TObject);
begin
EnabledEdit(true);
end;

procedure TfrmSQLCnt.EnabledEdit(const bUsed: Boolean);
begin
if bUsed then begin
Label2.Enabled :=True; EdtUserName.Enabled :=True;
Label3.Enabled :=True; EdtPassword.Enabled :=True;
end else begin
Label2.Enabled :=False; EdtUserName.Enabled :=False;
Label3.Enabled :=False; EdtPassword.Enabled :=False;
end;

end;

procedure TfrmSQLCnt.btnTestConnectClick(Sender: TObject);
begin
with ADOConnection1 do begin
Close;
if RBWinNT.Checked then
ConnectionString :=format(sConnectSQLServerString2,[Trim(EdtServerAddress.Text),
Trim(CmbDBName.Text)])
else
ConnectionString :=format(sConnectSQLServerString1,[Trim(EdtPassword.Text),
Trim(EdtUserName.Text),Trim(EdtServerAddress.Text),Trim(CmbDBName.Text)]);
// MsgBox(ConnectionString);
try
Open;
Application.MessageBox('连接成功!', '成功', MB_OK + MB_ICONINFORMATION
+ MB_TOPMOST);

except
Application.MessageBox('数据服务器连接成功!', '错误', MB_OK +
MB_ICONSTOP + MB_TOPMOST);

end;
end;
end;

procedure TfrmSQLCnt.CmbDBNameDropDown(Sender: TObject);
begin
with ADOConnection1 do begin
Close;
if RBWinNT.Checked then
ConnectionString :=format(sConnectSQLServerString2,[Trim(EdtServerAddress.Text),'master'])
else
ConnectionString :=format(sConnectSQLServerString1,[Trim(EdtPassword.Text),
Trim(EdtUserName.Text),Trim(EdtServerAddress.Text),'master']);
// MsgBox(ConnectionString);
try
Open;
ADOQuery1.Close;
ADOQuery1.SQL.Text :='select [name],dbid from sysdatabases '+
'where dbid >6 '+
'order by dbid DESC';
ADOQuery1.Open;
ADOQuery1.First;
while not ADOQuery1.Eof do begin
CmbDBName.Items.Add(ADOQuery1.FieldValues['name']);
ADOQuery1.Next;
end;
ADOQuery1.Close;
except
MsgBox('数据库连接失败!',1);
end;
end;
end;

procedure TfrmSQLCnt.FormShow(Sender: TObject);
begin
ReadSetup(sIniSetupFile,sSectionName);
end;

procedure TfrmSQLCnt.BtnCancelClick(Sender: TObject);
begin
close;
end;

procedure TfrmSQLCnt.btnCreateDBClick(Sender: TObject);
var
MyStream:TFileStream;
begin
MyStream :=TFileStream.Create(ExtractFilePath(ParamStr(0))+'test.txt',0);


Memo1.Clear;
{ with ADOConnection1 do begin
Close;
if RBWinNT.Checked then
ConnectionString :=format(sConnectSQLServerString2,[Trim(EdtServerAddress.Text),'master'])
else
ConnectionString :=format(sConnectSQLServerString1,[Trim(EdtPassword.Text),
Trim(EdtUserName.Text),Trim(EdtServerAddress.Text),'master']);
// MsgBox(ConnectionString);
try
Open;
with ADOQuery1 do begin
Close;
SQL.Text :='select filename from sysdatabases where dbid =1 ';
Open;
memo1.Lines.Add('数据库路径:'+ExtractFilePath(fieldbyName('filename').AsString ));
Close;
SQL.Text :='IF EXISTS (SELECT * FROM sysdatabases WHERE name = ''数据库'') '+
'BEGIN '+
' DROP database 数据库 '+
'END; '+
'CREATE database 数据库 ';
ExecSQL;

end;

except
Application.MessageBox('失败1!', '错误', MB_OK + MB_ICONSTOP +
MB_TOPMOST);

end; }
Memo1.Lines.LoadFromStream(MyStream);

// Memo1.Lines.SaveToFile(ExtractFilePath(ParamStr(0))+'aaa.txt')

// Memo1.Lines.Add(ExtractFilePath(ParamStr(0))+'test.txt');
// Memo1.Lines.LoadFromFile(ExtractFilePath(ParamStr(0))+'test.txt');
{ try
ADOCommand1.CommandText :='use [数据库] ' +
'CREATE TABLE [tttttttttttttt] ( '+
' [fID] [int] IDENTITY (1, 1) NOT NULL ) ';
ADOCommand1.Execute;
except
Application.MessageBox('失败2!', '错误', MB_OK + MB_ICONSTOP +
MB_TOPMOST);

end;
end; }
// if not ADOConnection1.Connected then ADOConnection1.Open;
end;



end.


==========================
 
晕,没看出来。
 
后退
顶部