• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

Dynamically calling methods in COM objects

How do you call methods in a COM object by name?

I need to be able to dynamically list all the properties of a COM object to the user, then allow them to choose one and set it's value.

The property 'Prop' appears to be implemented within COM using a pair of methods, both called 'Prop', one with input paramater and one with a return.

Here's the code I wrote to list the available methods:

begin
  List := TStringList.Create;

  LoadTypeLib(PWideChar(LibName), TypeLib);

  New(ParamNames);
  for i := 0 to TypeLib.GetTypeInfoCount-1 do
  begin
    TypeLib.GetTypeInfo(i, TypeInfo);
    OleCheck(TypeInfo.GetTypeAttr(TypeAttr));
    if TypeAttr.typekind in [tkind_Interface,
                             tkind_Dispatch] then
    begin
      for j := 0 to TypeAttr.cFuncs-1 do
      begin
        TypeInfo.GetFuncDesc(j, FuncDesc);
        TypeInfo.GetDocumentation(FuncDesc.MemID, @AName,
                                  nil, nil, nil);

        TypeInfo.GetNames(FuncDesc.MemID, ParamNames,
                          SizeOf(TBStrList), cNames);

{ guesses that this is a property if it has one param with
  the same name as the method - might be dangerous but
  will do }
        if (FuncDesc.cParams = 1) and
           (AName = ParamNames[0]) then
          List.Add(AName);

        TypeInfo.ReleaseFuncDesc(FuncDesc);
      end;
      Break;
    end;
    TypeInfo.ReleaseTypeAttr(TypeAttr);
  end;
  Dispose(ParamNames);
end;

How can I then use the name of a property to call the associated function to set it?
0
mhoughton
Asked:
mhoughton
1 Solution
 
SChertkovCommented:
Following code get object IDispatch and list it
properties as pair name/value in string grid.

procedure TForm1.ListObjectProps(dispItem: IDispatch);
var
  ctinfo: Integer;
  TypeInfo: ITypeInfo;
  typeattr: PTypeAttr;
  Index, S: Integer;
  fdesc: PFuncDesc;
  Names: array [0..0] of PWideChar;
  cNames: Integer;

  DispParams: TDispParams;
  DispID: TDispID;
  excepinfo: Texcepinfo;
  Status: HResult;
  Result: OleVariant;

begin
  S := 1;
  OleCheck(dispItem.GetTypeInfoCount(ctinfo));
  if ctinfo = 0 then
    raise Exception.Create('No type info ');
  OleCheck(dispItem.GetTypeInfo(0, GetSystemDefaultLCID, TypeInfo));
  OleCheck(TypeInfo.GetTypeAttr(typeattr));
  for Index := 0 to typeattr.cFuncs -1 do
    begin
      OleCheck(TypeInfo.GetFuncDesc(Index, fdesc));
      if fdesc.invkind = INVOKE_PROPERTYGET  then
        begin
          OleCheck(TypeInfo.GetNames(fdesc.memid, @Names, 1, cNames));
          if S = StringGrid1.RowCount then
            StringGrid1.RowCount := StringGrid1.RowCount + 1;
          StringGrid1.Cells[1, S] := Names[0];
          if dispItem.GetIDsOfNames(GUID_NULL, @Names[0], 1,
               GetSystemDefaultLCID, @DispID) = S_OK then
            begin
              FillChar(DispParams, sizeof(DispParams), 0);
              Status :=
                dispItem.Invoke(DispID, GUID_NULL, GetSystemDefaultLCID,
                   INVOKE_PROPERTYGET, DispParams, @Result, @excepinfo, nil);
              if Status <> S_OK then
                DispatchInvokeError(Status, excepinfo)
              else
                StringGrid1.Cells[2, S] := Result;
            end;
          SysFreeString(Names[0]);
          Inc(S);
        end;
      TypeInfo.ReleaseFuncDesc(fdesc);
    end;
  TypeInfo.ReleaseTypeAttr(typeattr);
end;

Following code set property value:
procedure TForm1.SetObjectProp(dispItem: IDispatch; Name: WideString; Value: OleVariant);
var
  DispParams: TDispParams;
  DispID, PropPutDispID: TDispID;
  excepinfo: Texcepinfo;
  Status: HResult;
begin
  if dispItem.GetIDsOfNames(GUID_NULL, @Name, 1,
    GetSystemDefaultLCID, @DispID) = S_OK then
    begin
      FillChar(DispParams, sizeof(DispParams), 0);
      PropPutDispID := DISPID_PROPERTYPUT;
      DispParams.rgvarg := @Value;
      DispParams.rgdispidNamedArgs := @PropPutDispID;
      DispParams.cArgs := 1;
      DispParams.cNamedArgs := 1;
      Status :=
        dispItem.Invoke(DispID, GUID_NULL, GetSystemDefaultLCID,
           INVOKE_PROPERTYPUT, DispParams, nil, @excepinfo, nil);
      if Status <> S_OK then
        DispatchInvokeError(Status, excepinfo)
    end;
end;
0
 
mhoughtonAuthor Commented:
I am very impressed.  Thanks a lot!

Apart from a bit of error trapping (some of the properties are interfaces themselves, so copying the value into the string grid causes an exception), this is exactly what I needed.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now