COM互操作:可变数量的参数 [英] COM interop: Variable number of parameters

查看:74
本文介绍了COM互操作:可变数量的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个C#库,我需要从COBOL(非托管)代码中调用该库。因此,我正在出于这些目的使用COM互操作。

I am developing an C# libraries, that I need to call from COBOL (unmanaged) code. So I am using COM interop for these purposes.

我创建了一个 ModuleLanucher层来介导通信。
我已经通过以下命令注册了ModuleLanucher库:

I have created an layer "ModuleLanucher" that mediates the communication. I have registered this ModuleLanucher library by these commands:

regasm ModuleLanucher.dll /codebase
TLBEXP Modulelanucher.dll

如果我在函数中有具体数量的参数,一切都很好 foo。 (如下所述)

Everything works fine if I have concrete number of parameters in function foo. (mentioned below)

不幸的是,我的函数foo需要可变数量的参数。
这是我的代码的草图:

Unfortunately my function foo needs variable number of arguments. This is sketch of my code:

namespace NMSPC
{
   class ModuleLanucher
   {
      void foo(string moduleName, params object[] args)
      {
        //call some dlls with args as parameteres
      }
   }
}

从COBOL代码中,我正在调用COM函数foo:

From COBOL code I'm calling COM function foo:

VCCOMServer is class "$OLE$NMSPC.ModuleLanucher"

invoke VCCOMServer "new" returning anInstance
invoke anInstance "foo" using MODULENAME, PARAM01, PARAM02, PARAM03, PARAM04

当我尝试从此COBOL调用foo方法时代码,出现此错误:

When I try to call foo method from this COBOL code, I get this error:

异常65538未被类oleexceptionmanager捕获。
描述: OLE参数计数不匹配
(8002000E):参数数量无效

Exception 65538 not trapped by the class oleexceptionmanager. Description: "OLE Parameter count mismatch" (8002000E): Invalid number of parameters

是否有可能使COM对象函数具有变量有多少论点?

Is it even posiible to have COM object function with variable number of arguments?

谢谢您的任何建议

推荐答案

可变参数列表不是由COM支持。
我认为您方法的COM部分会转换为:

Variable argument lists are not supported by COM. I think the COM part of your method is converted into:

void foo(string moduleName, object[] args) //note the missing params...

要从COBOL调用此方法,您需要传递对象数组作为第二个参数。

To call this method from COBOL, you need to pass an array of objects as the 2nd argument.

如果您正手知道最大#parms,则可以选择使用最大#parms定义方法,其中

If you know the maximum #parms on forehand, an alternative can be that you define your method with this maximum #parms, where you specify them as optional to COM.

缺少参数作为 Type.Missing 传递。

这篇关于COM互操作:可变数量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆