Board index » delphi » duplicating VB's TypeName() in Delphi

duplicating VB's TypeName() in Delphi

in VB and VBScript there is a function called TypeName() that allows you to
get the name of a COM object.

is there a way to get the classname or typename of an IDispatch like you can
get the classname of a delphi object
( object.ClassName )?

I have come across code to list the properties and methods of an IDispatch
but cannot find a way to get the classname
of the object itself.

thanks,
Robert Ehtesham
r...@Amherst.com

 

Re:duplicating VB's TypeName() in Delphi


Hi Robert

Robert Ehteshamzadeh schrieb:

Quote
> is there a way to get the classname or typename of an IDispatch
> like you can get the classname of a delphi object

function TypeNameOfDispatch(I: IDispatch): string;
var
  ProvideClassInfo: IProvideClassInfo;
  TypeInfo: ITypeInfo;
  ClassName: TBStr;
begin
  // Check our reference is pointing to smth at all
  if not Assigned(I) then
  begin
    Result:= 'Nothing';
    Exit;
  end;

  try
    if Supports(I, IProvideClassInfo, ProvideClassInfo) then
    begin
      // We could get reference to coclass
      // Now get TypeInfo about coclass
      OleCheck(ProvideClassInfo.GetClassInfo(TypeInfo));
    end
    else
    begin
      // We couldn't get reference to coclass
      // so get info about interface itself
      OleCheck(I.GetTypeInfo(0, 0, TypeInfo));
    end;

    // Get Name of coclass resp. interface
    try
      OleCheck(TypeInfo.GetDocumentation(MEMBERID_NIL, @ClassName,
        nil, nil, nil));
         Result:= OleStrToString(ClassName);
    finally
      SysFreeString(ClassName);
    end;
  except
    Result:= 'Unknown';
  end;
end;

Traubensaft gibt Traubenkraft

Christian "NineBerry" Schwarz

--
Mit Geduld und Spucke
deflorierte der Elefant die Mucke

Re:duplicating VB's TypeName() in Delphi


Thank you so much! That worked beautifully.

"Christian NineBerry Schwarz" <Christian.Schw...@nineberry.de> wrote in
message news:Xns90E47BFE35580NINEBERRY@NineBerry.de...

Quote
> Hi Robert

> Robert Ehteshamzadeh schrieb:

> > is there a way to get the classname or typename of an IDispatch
> > like you can get the classname of a delphi object

> function TypeNameOfDispatch(I: IDispatch): string;
> var
>   ProvideClassInfo: IProvideClassInfo;
>   TypeInfo: ITypeInfo;
>   ClassName: TBStr;
> begin
>   // Check our reference is pointing to smth at all
>   if not Assigned(I) then
>   begin
>     Result:= 'Nothing';
>     Exit;
>   end;

>   try
>     if Supports(I, IProvideClassInfo, ProvideClassInfo) then
>     begin
>       // We could get reference to coclass
>       // Now get TypeInfo about coclass
>       OleCheck(ProvideClassInfo.GetClassInfo(TypeInfo));
>     end
>     else
>     begin
>       // We couldn't get reference to coclass
>       // so get info about interface itself
>       OleCheck(I.GetTypeInfo(0, 0, TypeInfo));
>     end;

>     // Get Name of coclass resp. interface
>     try
>       OleCheck(TypeInfo.GetDocumentation(MEMBERID_NIL, @ClassName,
>         nil, nil, nil));
>     Result:= OleStrToString(ClassName);
>     finally
>       SysFreeString(ClassName);
>     end;
>   except
>     Result:= 'Unknown';
>   end;
> end;

> Traubensaft gibt Traubenkraft

> Christian "NineBerry" Schwarz

> --
> Mit Geduld und Spucke
> deflorierte der Elefant die Mucke

Other Threads