Solved

Dynamically calling methods in COM objects

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
URL for downloading Google Chrome for Win XP 2 182
Base1 Encode/Decode 3 81
How to build JSON File in Delphi 6 3 45
firemonkey keyboard covers the controls 1 37
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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 …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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