[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Dynamically calling methods in COM objects

Posted on 2001-07-23
2
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
SChertkov earned 1200 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

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.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

650 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