gpt4 book ai didi

.net - 如何找到未记录的 .NET/COM 库函数?

转载 作者:行者123 更新时间:2023-12-02 07:56:28 25 4
gpt4 key购买 nike

如何找出从某些 .NET 函数返回的 COM 对象的属性和方法,这些函数似乎没有记录在案?

在我正在查看的特定示例中,我使用如下函数将图片插入到 Excel 中:

Set NewPic = ActiveSheet.Pictures.Insert(FileName)

(请参阅此 here 上的 SO 帖子。)

然而,MSDN documentation因为这个函数只说 Worksheet.Pictures 返回一个对象,当我在调试过程中监视变量时,它的类型是 System.__ComObject。我能否找出该类可能可用的其他属性和函数(例如,我想修改图片的替代文本)?发现 Insert 函数的人怎么会知道它呢?

MSDN 文档也倾向于说这些函数“不打算直接从您的代码中使用”,但我们暂时忽略它...

谢谢!


编辑:好吧,至少我设法回答了我的具体问题。除了使用 Worksheet.Pictures.Insert,您还可以使用 Worksheet.Shapes.AddPicture 返回适当的(已记录的)Excel.Shape 类:

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!"

不过,仍然会对未记录函数的任何资源感兴趣。

最佳答案

找出这些问题的一种方法是使用 OleView 工具(您可以从 Microsoft here 下载)。此工具允许您查看 COM 类型库。类型库(假设 COM 组件的供应商提供了类型库信息)包含有关接口(interface)以及应用程序或库公开的 COM 类的方法和属性的信息。

例如,在我的机器上,我可以查看 C:\Program Files\Microsoft Office\Office12\EXCEL.EXE 的类型库,并查看 Excel 公开了哪些 COM 对象,以及它们属性和方法。 OleView 以 IDL(接口(interface)描述语言)显示信息,它或多或少是附加了额外属性的 C 函数原型(prototype)。

这是我为 __Worksheet.Pictures 属性使用 OleView 得到的 IDL 声明:

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

注意声明中的hidden 属性。这意味着它不会被大多数 IDE 显示(这是一个很好的提示,不要依赖这个一直存在的方法——微软可能会在更高版本的 Excel 中删除它)。

现在 Pictures 类呢?这是完整的 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);

据此,您可以推测 Pictures 接口(interface)有一个 CutDelete 方法,以及一个 Item 属性等。但是,请注意该接口(interface)也被标记为 hidden,这(再次)很好地表明您不应该真正使用它(是的,我知道您忽略了该警告,但我支持 Mitch Wheat 的评论是,使用隐藏类通常不是一个好主意,因为它们通常供应用程序内部使用,如有更改,恕不另行通知。)

关于.net - 如何找到未记录的 .NET/COM 库函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/688485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com