Solved

Dynamically calling methods in COM objects

Posted on 2001-07-23
2
361 Views
Last Modified: 2010-04-06
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
Comment
Question by:mhoughton
2 Comments
 
LVL 2

Accepted Solution

by:
SChertkov earned 300 total points
ID: 6311446
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
 

Author Comment

by:mhoughton
ID: 6311981
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ddeman not working in activex 3 103
Delphi XE10, MySQL Query 4 143
Send message from delphi to whatsapp, is it possible? 3 1,422
RESTRequest Parameter 4 30
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question