Solved

Dynamically calling methods in COM objects

Posted on 2001-07-23
2
358 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now