Link to home
Start Free TrialLog in
Avatar of yeow
yeow

asked on

Transfer component through DCOM?

Hi, All
I want transfer the server's directory structure to the client, how to do that?
Avatar of xsoft
xsoft

Hi yeow,

I don't know what you mean by the server's directory structure but if the question is, how to transfer a component through DCOM, the answer would be:

function Component2Var(Component:TComponent):OleVariant;
var P:Pointer;MStream:TMemoryStream;Size:integer;
begin
  MStream:=TMemoryStream.Create;
  try
    MStream.WriteComponent(Component);
    Result:=VarArrayCreate([0,MStream.Size-1],varByte);
    P:=VarArrayLock(Result);
    try
      Move(MStream.Memory^,P^,MStream.Size);
    finally
      VarArrayUnlock(Result);
    end;
  finally
    MStream.Free;
  end;
end;

HTH,

Thomas
Avatar of yeow

ASKER

Hi, Xsoft
yes. But how to translate it from variant to component?
Hi yeow,

to translate the resulting variant to component you could use:

function Var2Component(OleVar:OleVariant):TComponent;
var P:Pointer;MStream:TMemoryStream;
begin
  MStream:=TMemoryStream.Create;
  try
    P:=VarArrayLock(OleVar);
    try
      MStream.WriteBuffer(P^,VarArrayHighBound(OleVar,1)+1);
    finally
      VarArrayUnlock(OleVar);
    end;
    MStream.Seek(0,soFromBeginning);
    Result:=MStream.ReadComponent(nil);
  finally
    MStream.Free;
  end;
end;

HTH,

Thomas

Avatar of yeow

ASKER

Hi Xsoft,
It works. But When I assign it to a component, such as TButton, I got a error: "Class TButton Not Found" ?! My Code is:
  MyButton := MStream.ReadComponent(nil);
What's wrong in it?  
ASKER CERTIFIED SOLUTION
Avatar of xsoft
xsoft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yeow

ASKER

u r really expert! THANK U! THANK U!! THANK U!!!
Nice to hear that everything is fine.

Thanks for your enthusiasm,

Thomas