How can I find out about undocumented .NET / COM l

2019-05-24 06:42发布

问题:

How can I find out the properties and methods of COM objects returned from some .NET functions, which do not appear to be documented?

In the particular example I'm looking at, I'm inserting a picture into Excel using a function like:

Set NewPic = ActiveSheet.Pictures.Insert(FileName)

(See the SO post on this here.)

However, the MSDN documentation for this function only says that Worksheet.Pictures returns an Object, and when I put a watch on the variable during debugging its type is System.__ComObject. Can I find out what other properties and functions might be available for that class (for example, I want to modify the alternative text for the picture)? How would the person who found out about the Insert function even have known about it?

The MSDN doc also tends to say of such functions that they are "not intended to be used directly from your code", but let's ignore that for now...

Thanks!


Edit: Well, I managed to answer my specific question at least. Instead of using Worksheet.Pictures.Insert, you can use Worksheet.Shapes.AddPicture to return a proper (documented) Excel.Shape class:

pic = range.Worksheet.Shapes.AddPicture(tmpFile, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, range.Left, range.Top, image.Width, image.Height)
pic.AlternativeText = "Help!"

Would still be interested in any resources for undocumented functions though.

回答1:

One way to find these things out is to use the OleView tool (which you can download from Microsoft here). This tool allows you to view COM Type Libraries. The Type Library (assuming the vendor of the COM component provided type library information) contains information about the interfaces and the methods and properties of the COM classes exposed by an application or library.

For example, on my machine, I can view the Type Library for C:\Program Files\Microsoft Office\Office12\EXCEL.EXE and see what COM objects are exposed by Excel, and their properties and methods. OleView displays information in IDL (Interface Description Language), which is more or less a C function prototype with extra attributes tacked on).

This is the IDL declaration I got using OleView for the __Worksheet.Pictures property:

    [id(0x00000303), hidden, helpcontext(0x00010303)]
    HRESULT Pictures(
                    [in, optional] VARIANT Index, 
                    [in, lcid] long lcid, 
                    [out, retval] IDispatch** RHS);

Note the hidden attribute on the declaration. This means it won't be displayed by most IDE's (and is a good hint to not rely on this method always existing - Microsoft could remove it in a later version of Excel).

Now what about the Pictures class? Here is the full IDL:

[
  uuid(000208A7-0000-0000-C000-000000000046),
  helpcontext(0x00020067),
  hidden
]
dispinterface Pictures {
    properties:
    methods:
        [id(0x60000000), restricted]
        void QueryInterface(
                        [in] GUID* riid, 
                        [out] void** ppvObj);
        [id(0x60000001), restricted]
        unsigned long AddRef();
        [id(0x60000002), restricted]
        unsigned long Release();
        [id(0x60010000), restricted]
        void GetTypeInfoCount([out] unsigned int* pctinfo);
        [id(0x60010001), restricted]
        void GetTypeInfo(
                        [in] unsigned int itinfo, 
                        [in] unsigned long lcid, 
                        [out] void** pptinfo);
        [id(0x60010002), restricted]
        void GetIDsOfNames(
                        [in] GUID* riid, 
                        [in] char** rgszNames, 
                        [in] unsigned int cNames, 
                        [in] unsigned long lcid, 
                        [out] long* rgdispid);
        [id(0x60010003), restricted]
        void Invoke(
                        [in] long dispidMember, 
                        [in] GUID* riid, 
                        [in] unsigned long lcid, 
                        [in] unsigned short wFlags, 
                        [in] DISPPARAMS* pdispparams, 
                        [out] VARIANT* pvarResult, 
                        [out] EXCEPINFO* pexcepinfo, 
                        [out] unsigned int* puArgErr);
        [id(0x00000094), propget, helpcontext(0x00010094)]
        Application* Application();
        [id(0x00000095), propget, helpcontext(0x00010095)]
        XlCreator Creator();
        [id(0x00000096), propget, helpcontext(0x00010096)]
        IDispatch* Parent();
        [id(0x00010003), restricted, hidden]
        void _Dummy3();
        [id(0x0000025a), helpcontext(0x0001025a)]
        VARIANT BringToFront();
        [id(0x00000227), helpcontext(0x00010227)]
        VARIANT Copy();
        [id(0x000000d5), helpcontext(0x000100d5)]
        VARIANT CopyPicture(
                        [in, optional, defaultvalue(2)] XlPictureAppearance Appearance, 
                        [in, optional, defaultvalue(-4147)] XlCopyPictureFormat Format);
        [id(0x00000235), helpcontext(0x00010235)]
        VARIANT Cut();
        [id(0x00000075), helpcontext(0x00010075)]
        VARIANT Delete();
        [id(0x0000040f), helpcontext(0x0001040f)]
        IDispatch* Duplicate();
        [id(0x00000258), propget, helpcontext(0x00010258)]
        VARIANT_BOOL Enabled();
        [id(0x00000258), propput, helpcontext(0x00010258)]
        void Enabled([in] VARIANT_BOOL rhs);
        [id(0x0000007b), propget, helpcontext(0x0001007b)]
        double Height();
        [id(0x0000007b), propput, helpcontext(0x0001007b)]
        void Height([in] double rhs);
        [id(0x0001000c), restricted, hidden]
        void _Dummy12();
        [id(0x0000007f), propget, helpcontext(0x0001007f)]
        double Left();
        [id(0x0000007f), propput, helpcontext(0x0001007f)]
        void Left([in] double rhs);
        [id(0x0000010d), propget, helpcontext(0x0001010d)]
        VARIANT_BOOL Locked();
        [id(0x0000010d), propput, helpcontext(0x0001010d)]
        void Locked([in] VARIANT_BOOL rhs);
        [id(0x0001000f), restricted, hidden]
        void _Dummy15();
        [id(0x00000254), propget, hidden, helpcontext(0x00010254)]
        BSTR OnAction();
        [id(0x00000254), propput, hidden, helpcontext(0x00010254)]
        void OnAction([in] BSTR rhs);
        [id(0x00000269), propget, helpcontext(0x00010269)]
        VARIANT Placement();
        [id(0x00000269), propput, helpcontext(0x00010269)]
        void Placement([in] VARIANT rhs);
        [id(0x0000026a), propget, helpcontext(0x0001026a)]
        VARIANT_BOOL PrintObject();
        [id(0x0000026a), propput, helpcontext(0x0001026a)]
        void PrintObject([in] VARIANT_BOOL rhs);
        [id(0x000000eb), helpcontext(0x000100eb)]
        VARIANT Select([in, optional] VARIANT Replace);
        [id(0x0000025d), helpcontext(0x0001025d)]
        VARIANT SendToBack();
        [id(0x0000007e), propget, helpcontext(0x0001007e)]
        double Top();
        [id(0x0000007e), propput, helpcontext(0x0001007e)]
        void Top([in] double rhs);
        [id(0x00010016), restricted, hidden]
        void _Dummy22();
        [id(0x0000022e), propget, helpcontext(0x0001022e)]
        VARIANT_BOOL Visible();
        [id(0x0000022e), propput, helpcontext(0x0001022e)]
        void Visible([in] VARIANT_BOOL rhs);
        [id(0x0000007a), propget, helpcontext(0x0001007a)]
        double Width();
        [id(0x0000007a), propput, helpcontext(0x0001007a)]
        void Width([in] double rhs);
        [id(0x0000026e), propget, helpcontext(0x0001026e)]
        long ZOrder();
        [id(0x000005f8), propget, helpcontext(0x000105f8)]
        ShapeRange* ShapeRange();
        [id(0x00000080), propget, helpcontext(0x00010080)]
        Border* Border();
        [id(0x00000081), propget, helpcontext(0x00010081)]
        Interior* Interior();
        [id(0x00000067), propget, helpcontext(0x00010067)]
        VARIANT_BOOL Shadow();
        [id(0x00000067), propput, helpcontext(0x00010067)]
        void Shadow([in] VARIANT_BOOL rhs);
        [id(0x00000105), propget, helpcontext(0x00010105)]
        BSTR Formula();
        [id(0x00000105), propput, helpcontext(0x00010105)]
        void Formula([in] BSTR rhs);
        [id(0x000000b5), helpcontext(0x000100b5)]
        Picture* Add(
                        [in] double Left, 
                        [in] double Top, 
                        [in] double Width, 
                        [in] double Height);
        [id(0x00000076), propget, helpcontext(0x00010076)]
        long Count();
        [id(0x0000002e), helpcontext(0x0001002e)]
        GroupObject* Group();
        [id(0x000000fc), helpcontext(0x000100fc)]
        Picture* Insert(
                        [in] BSTR Filename, 
                        [in, optional] VARIANT Converter);
        [id(0x000000aa), helpcontext(0x000100aa)]
        IDispatch* Item([in] VARIANT Index);
        [id(0xfffffffc), helpcontext(0x0000fffc)]
        IUnknown* _NewEnum();
        [id(0x000000d3), helpcontext(0x000100d3)]
        Picture* Paste([in, optional] VARIANT Link);

From this, you can surmise that the Pictures interface has a Cut and Delete method, as well as an Item property, among others. However, note that this interface is also marked hidden, which (again) is a good indication that you shouldn't really be using it (yes, I know you were ignoring that warning, but I second Mitch Wheat's comment that it's generally a bad idea to use hidden classes, because they are usually meant for the application's own internal use and are subject to change without notice.)



标签: .net com vsto