不是使用接口技术,我以前的贴子:
再问的话,你别干这行了
unit TransComponent;
{本工具供Com/DCom间传递组件(从TComponent继承的所有对象)
调用 VariantToComponent 时会出现 "Class <TClassName> not Found "提示 多次 ,
在调用单元的initialization段加以下语句:
RegisterClass(<ClassName&gt
![Wink ;) ;)](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f609.png)
,如
RegisterClass(TClientDataSet);
RegisterClass(TStringField);
VariantToComponent 的参数 Component 必须声明为TComponent,然后使用强制类型转换使用它
如:TClientDataSet(Component)
}
interface
uses SysUtils,Classes;
procedure ComponentToVariant(const Component:TComponent;out vData:OleVariant);
procedure VariantToComponent(out Component:TComponent; vData:OleVariant);
implementation
procedure UnloadVariantArray(
var VData: OleVariant; PData: Pointer;
NumBytes: Integer);
var
PVData: PByteArray;
begin
{ Lock the variant array, copy the data to the array, and
unlock the variant array. }
PVData := VarArrayLock(VData);
try
{ Move the data in memory that PVData points to (the
variant array data), to the location in memory that
PData points to (the integer array). }
Move(PVData^, PData^, NumBytes);
finally
VarArrayUnlock(VData);
end; // try
end;
procedure LoadVariantArray(PData: Pointer;
NumBytes: Integer; var VData: OleVariant);
var
PVData: PByteArray;
begin
{ Create the variant array of bytes. Set the upper bound
to the size of the array, minus one, because the array
is zero-based. }
VData := VarArrayCreate([0, NumBytes - 1], varByte);
{ Lock the variant array for faster access. Then copy the
array to the variant array, and unlock the variant
array. }
PVData := VarArrayLock(Vdata);
try
{ Move the bytes at the location in memory that PData
points to into the location in memory that PVData
points to. PData points to the integer array and
PVData points to the variant array of bytes. }
Move(PData^, PVData^, NumBytes);
finally
VarArrayUnlock(VData);
end; // try
end ;
procedure ComponentToVariant(const Component:TComponent;out vData:OleVariant);
var
CompBuffer:TByteArray;
pCompBuffer
![Stick Out Tongue :p :p](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f61b.png)
ointer;
begin
with TMemoryStream.Create do
try
WriteComponent(Component);
Seek(0, soFromBeginning);
ReadBuffer(CompBuffer,Size);
LoadVariantArray(@CompBuffer,Size,vData);
finally
free;
end;
end;
procedure VariantToComponent(out Component:TComponent; vData:OleVariant);
var
CompBuffer:TByteArray;
pCompBuffer
![Stick Out Tongue :p :p](https://cdn.jsdelivr.net/joypixels/assets/8.0/png/unicode/64/1f61b.png)
ointer;
begin
with TMemoryStream.Create do
try
UnLoadVariantArray(vData,@CompBuffer,VarArrayHighBound(vData,1)+1);
WriteBuffer(CompBuffer,VarArrayHighBound(vData,1)+1);
Seek(0, soFromBeginning);
Component:=ReadComponent(nil);
//-the Component is AutoCreated
finally
free;
end;
end;
end.