gpt4 book ai didi

delphi - Delphi/C++Builder COM 的可选参数

转载 作者:行者123 更新时间:2023-12-02 03:50:46 25 4
gpt4 key购买 nike

我正在尝试调用 COM 对象的方法。该方法(在 OLEView 中)如下所示:-

    VARIANT_BOOL GetValues(
[out] rsValues** values,
[in, optional] rsCategory* category,
[in, optional] rsProcess* process);

最后两个参数是可选的,我想从调用中“省略”它们(仅传递 NULL 不起作用)。

我的理解是,Delphi/C++Builder 类型库导入程序生成的所有 COM 包装器都会将每个传递的参数转换为变体。但要传递“省略”参数,我想我必须构造一个像这样的特殊变体。

  varOpt.vt = VT_ERROR;
varOpt.sCode = DISP_E_PARAMNOTFOUND;

这似乎意味着我无法使用任何自动生成的组件包装器。

如何(使用 C++Builder Delphi - 我应该能够将 Delphi 转换为 C++)调用该方法并传递“省略”变体?

更新

以下是我按照 David Heffernan 的回答中的建议尝试调用该方法的方法。

procedure TForm38.Wibble(obj : IrsObject);
var vals : IrsValues;
begin
ps.GetValues(vals, emptyparam, emptyparam);

但这给出了这个错误:

[DCC Error] E2010 Incompatible types: 'IrsCategory' and 'OleVariant'
[DCC Error] E2010 Incompatible types: 'IrsProcess' and 'OleVariant'
<小时/>

由于我使用的是非“Disp”对象,因此我相信我正在使用早期绑定(bind)。

我试图避免发布 45000 行的 foo_TLB.pas 文件,但这里(我希望)是相关部分。

IrsCategory = interface;
IrsCategoryDisp = dispinterface;
IrsProcess = interface;
IrsProcessDisp = dispinterface;

// *********************************************************************//
// Interface: IrsObject
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {861817F7-EA49-49D1-89FC-395BCA57E342}
// *********************************************************************//
IrsObject = interface(IDispatch)
['{861817F7-EA49-49D1-89FC-395BCA57E342}']
...
function GetValues(out values : IrsValue;
const Category: IrsCategory;
const Process: IrsProcess):): WordBool; safecall;
...
end;


// *********************************************************************//
// Interface: IrsObjectDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {861817F7-EA49-49D1-89FC-395BCA57E342}
// *********************************************************************//

IrsObjectDisp = dispinterface ... // basically the same, repeated

最佳答案

海森堡选项 1

您正在使用自动化接口(interface),可能带有后期绑定(bind)的 COM,并且参数都是变体。

通行证EmptyParam到这些参数。

function EmptyParam: OleVariant;

Contains an OleVariant that represents an unused optional parameter on a dual interface.

EmptyParam can be used for Variants that represent optional parameters whose value is unassigned. This is useful when the code that marshals method calls requires a fixed number of parameters, even if some of them are optional.

When marshaling interface calls that include optional parameters, COM requires a value for those parameters even when they are not used. EmptyParam returns an OleVariant you can pass as that value to indicate that the parameter is not used.

海森堡选项 2

您正在使用早期绑定(bind)的 COM,参数是接口(interface)。

nil 传递给这些参数以省略它们。如果参数是 COM 接口(interface)而不是变体,那么这是您唯一可能的选择。 COM 接口(interface)变量必须是有效接口(interface),或者nil

关于delphi - Delphi/C++Builder COM 的可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24491125/

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