S
shopman
Unregistered / Unconfirmed
GUEST, unregistred user!
代码要产生 “意外错误”情况如下
这段代码的作用是将客户端用户调整的树形结构保存在一个结构里放在TList内,之后
将 TList -> TStream -> OLEVarian 传送到服务器,我这样做的目的是客户无论对树形
结构做任何调整只调用一次AppServer就可以将数据库更新。当然服务器要完成
OLEVarian -> TStreazm -> TList的转换进而处理每一个结构。但是我在代码里看不到任
何错误同样的代码我在单进程环境下又无任何问题。但一放在三层中就要出现“意外错误”
从咋天下午一直搞到今天早上5点实在是不知错在哪里。。。请高手指点。
实在不行我就只有将结构中的数据存成字符串再在服务器中解析出来,但我不知道为什么
用我的自定义结构就不行呢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//结构如下
type
TTreeItem = record
Command :String;
Layer :Integer;
PID :String;
KID :String;
DisPlayName :String;
FS :String;
end;
type
PTreeItem = ^TTreeItem;
//客户端
/////////////////////////////////////////////////////////////////
//TList -> TStream
procedure TForm1.SaveListToStream(List: TList;
Stream: TStream);
var
i :Integer;
begin
Stream.Position := 0;
for i := 0 to List.Count - 1do
Stream.WriteBuffer(PTreeItem(List.Items)^, SizeOf(TTreeItem));
end;
//TStream -> OLEVariant
procedure TForm1.StreamToOleVariant(ova: OleVariant;
Stream: TStream);
var
P: Pointer;
begin
ova := VarArrayCreate([0, Stream.Size - 1], varByte);
P := VarArrayLock(ova);
try
Stream.Position := 0;
Stream.Read(P^, Stream.Size);
finally
VarArrayUnlock(ova);
end;
end;
//调用处
procedure TForm1.Button9Click(Sender: TObject);
var
ito :OLEVariant;
it :TStream;
begin
it := TMemoryStream.Create;
//SaveTreeItem 是全局性TList类变量,其中就保存着所有用户修改过的数形单元
SaveListToStream(SaveTreeItem, it);
StreamToOleVariant(ito, it);
//更新服务器树形单元数据库
SocketConnection1.AppServer.Modify_DBTree(ito);
it.Free;
end;
//服务器
////////////////////////////////////////////////////////////////
//OLEVariant -> TStream
procedure TGrade.OLEVariantToStream(ova: OLEVariant;
Stream: TStream);
var
P: Pointer;
begin
P := VarArrayLock(ova);
try
Stream.Write(P^, VarArrayHighBound(ova,1) + 1);
finally
VarArrayUnlock(ova);
end;
end;
//TStream -> TList
procedure TGrade.ReadStreamToList(List: TList;
Stream: TStream);
var
i, n :Integer;
P TreeItem;
begin
n := Stream.Size div SizeOf(ttreeitem);
Stream.Position := 0;
for i := 0 to n - 1do
begin
New(P);
Stream.ReadBuffer(P^, SizeOf(TTreeItem));
List.Add(P);
end;
end;
//调用处
procedure TGrade.Modify_DBTree(ito: OleVariant);
var
i :Integer;
it :TStream;
mytreeitem TreeItem;
TreeUnit :TList;
begin
it := TMemoryStream.Create;
TreeUnit := TList.Create;
OLEVariantToStream(ito, it);
ReadStreamToList(TreeUnit, it);
for i := 0 to TreeUnit.Count - 1do
begin
mytreeitem := PTreeItem(TreeUnit.Items);
//跟踪代码到这就出现“意外错误”只要一访问mytreeitem.Command马上出错。。。
if mytreeitem.Command = 'Inster' then
begin
//..............
end;
if mytreeitem.Command = 'Delete' then
begin
//..........................
end;
if mytreeitem.Command = 'Modify' then
begin
//.................
end;
end;
end;
这段代码的作用是将客户端用户调整的树形结构保存在一个结构里放在TList内,之后
将 TList -> TStream -> OLEVarian 传送到服务器,我这样做的目的是客户无论对树形
结构做任何调整只调用一次AppServer就可以将数据库更新。当然服务器要完成
OLEVarian -> TStreazm -> TList的转换进而处理每一个结构。但是我在代码里看不到任
何错误同样的代码我在单进程环境下又无任何问题。但一放在三层中就要出现“意外错误”
从咋天下午一直搞到今天早上5点实在是不知错在哪里。。。请高手指点。
实在不行我就只有将结构中的数据存成字符串再在服务器中解析出来,但我不知道为什么
用我的自定义结构就不行呢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//结构如下
type
TTreeItem = record
Command :String;
Layer :Integer;
PID :String;
KID :String;
DisPlayName :String;
FS :String;
end;
type
PTreeItem = ^TTreeItem;
//客户端
/////////////////////////////////////////////////////////////////
//TList -> TStream
procedure TForm1.SaveListToStream(List: TList;
Stream: TStream);
var
i :Integer;
begin
Stream.Position := 0;
for i := 0 to List.Count - 1do
Stream.WriteBuffer(PTreeItem(List.Items)^, SizeOf(TTreeItem));
end;
//TStream -> OLEVariant
procedure TForm1.StreamToOleVariant(ova: OleVariant;
Stream: TStream);
var
P: Pointer;
begin
ova := VarArrayCreate([0, Stream.Size - 1], varByte);
P := VarArrayLock(ova);
try
Stream.Position := 0;
Stream.Read(P^, Stream.Size);
finally
VarArrayUnlock(ova);
end;
end;
//调用处
procedure TForm1.Button9Click(Sender: TObject);
var
ito :OLEVariant;
it :TStream;
begin
it := TMemoryStream.Create;
//SaveTreeItem 是全局性TList类变量,其中就保存着所有用户修改过的数形单元
SaveListToStream(SaveTreeItem, it);
StreamToOleVariant(ito, it);
//更新服务器树形单元数据库
SocketConnection1.AppServer.Modify_DBTree(ito);
it.Free;
end;
//服务器
////////////////////////////////////////////////////////////////
//OLEVariant -> TStream
procedure TGrade.OLEVariantToStream(ova: OLEVariant;
Stream: TStream);
var
P: Pointer;
begin
P := VarArrayLock(ova);
try
Stream.Write(P^, VarArrayHighBound(ova,1) + 1);
finally
VarArrayUnlock(ova);
end;
end;
//TStream -> TList
procedure TGrade.ReadStreamToList(List: TList;
Stream: TStream);
var
i, n :Integer;
P TreeItem;
begin
n := Stream.Size div SizeOf(ttreeitem);
Stream.Position := 0;
for i := 0 to n - 1do
begin
New(P);
Stream.ReadBuffer(P^, SizeOf(TTreeItem));
List.Add(P);
end;
end;
//调用处
procedure TGrade.Modify_DBTree(ito: OleVariant);
var
i :Integer;
it :TStream;
mytreeitem TreeItem;
TreeUnit :TList;
begin
it := TMemoryStream.Create;
TreeUnit := TList.Create;
OLEVariantToStream(ito, it);
ReadStreamToList(TreeUnit, it);
for i := 0 to TreeUnit.Count - 1do
begin
mytreeitem := PTreeItem(TreeUnit.Items);
//跟踪代码到这就出现“意外错误”只要一访问mytreeitem.Command马上出错。。。
if mytreeitem.Command = 'Inster' then
begin
//..............
end;
if mytreeitem.Command = 'Delete' then
begin
//..........................
end;
if mytreeitem.Command = 'Modify' then
begin
//.................
end;
end;
end;