网文《在COM中使用数组参数》被广为转载,下面就是其中一个:http://blog.csdn.net/wcyoot/article/details/6562620 。
该文讲述在COM的接口中使用数组作为参数的三种方法。它们分别是:数组指针、SafeArray和ICollection。就其中的数组指针,该文中说“数组指针的操作非常简单,所以也是效率最高的传递方式。但是,这种方式不能够在VB中使用。数组指针可以传递一维数组,也可以传递多维数组。如果COM的客户端是VC++程序的话,这是最好的传递方式。”并给出例子说明:
/* 例1:计算数组中数字的和,采用辅助变量确定需要传递的数据的长度*/
HRESULT Sum (
[in, size_is(Count)] long * pNums,
long Count,
[out, retval] long * pResult);
// 也可以使用数组风格的定义
HRESULT Sum (
[in, size_is(Count)] long Numbers[],
long Count,
[out, retval] long * pResult);
从例子中不难看出,只需在IDL声明中正确的设置数组长度即可。
但请千万不要忘了文中还有这样一句话“数组指针作为跨套间的调用参数时,需要进行marshal。所以,应该编译和注册proxy/stub。”
也就是说,你还要编译和注册proxy/stub。怎么注册和编译proxy/stub呢?网友WM_USER给出的回答是:
if your server is in-process server,and your know clearly that your server and client run on same machine,you can merge the proxy/stub object with the server dll,otherwise,you should produce the proxy/stub dll and deployee it on the client machine and server machine.the dll is <your application name> PS.dll
特此提醒。