Strings Delphi

Title: Using IStrings and TStringsAdapter
Question: A Simple, Straight forward way of using 'IStrings' interface to
pass Multiple Strings from a COM Object.
Answer:
The VCL Contains great amount of reusable code and Helper Objects implementing many standard COM interfaces. Particularly when in comes to the implementation of COM & related stuff, life has been made a lot easy.
One such demonstration of these beautiful helper objects is the Implementation of 'IStrings' interface via the 'TStringsAdapter' Class.
In this article we will take a sneak peak about how we can utilise this class effectively.
Assume this situation :
You are creating a loosely coupled, (COM based) business object.
you got to return a list of strings as result of a method call.
the options are many.
for ex. you may choose to return it as:
1. as a Variant Array.
2. as a huge string, seperated by delimeters
3. as an IStream
and more...
well, here is a much simpler option. most effective and faster to implement.
1. We create a TStringList object
2. fill it with strings
3. create a TStringsAdapter
4. pass the TStringList to the Adapter
5. retrieve an IStrings interface from the Adapter.
here is the code (COM Server) :
{-- Interface Declaration --}
IUsers = interface(IDispatch)
['{8195E30A-ADB7-459E-804A-32A97FE1A2DE}']
function Get_UserRights(const AUserID: WideString): IStrings; safecall;
end;
{-- Interface Implementation --}
TUsers = class(TAutoObject, IUsers)
protected
FUserRights : TStringList;
public
{Constructor, Destructor.... }
function Get_UserRights(const AUserID: WideString): IStrings; safecall;
end;
function TUsers.Get_UserRights(const AUserID: WideString): IStrings;
begin
{--
FUserRights : fill your TStringList with data here.
FUserRights.Clear;
FUserRights.Add('rt_view');
FUserRights.Add('rt_add');
FUserRights.Add('rt_update');
FUserRights.Add('rt_delete');
FUserRights.Add('rt_approve');
--}
Result := TStringsAdapter.Create( FUserRights ) as IStrings;
end;
and at the Client Side,
1. Declare a variable of type IStrings
2. Create your COM Object
3. Call Get_UserRights and assign the result to the IStrings variable
procedure DisplayUserRights;
var
oUsers : IUsers;
oUserRights : IStrings;
iCtr : integer;
begin
oUsers := COUsers.Create;
oUserRights := oUsers.Get_UserRights('');
{-- Access the individual strings as oUserRights.Item[0]... --}
{-- or you can copy it to a local TStringlist --}
oUserRights := nil;
oUsers := nil;
end;
{-- Dependencies, Deployment Considerations --}
You got to include 'StdVcl' unit in your uses clause and
stdvcl40.dll while deploying.
(stdvcl40.dll has to be registered.)
That's All Folks.
Hope it is useful.