将键值对数组发送到COM中的API的最佳方法

时间:2022-06-09 07:37:49

I want to create an interface with API that can accept array of key-value pairs in COM. What is the best way to do that? I can think of sending SAFEARRAY of BSTR .Each BSTR can be considered as key-value pair . Logic of packing /unpacking key-values need to be added.

我想创建一个API接口,可以接受COM中的键值对数组。最好的方法是什么?我可以考虑发送BSTR的SAFEARRAY。每个BSTR都可以被认为是键值对。需要添加打包/解包键值的逻辑。

4 个解决方案

#1


You can introduce an interface for a key-value pair and another one for the collection of such pairs.

您可以为键值对引入接口,并为此类对的集合引入另一个接口。

#2


I wouldn't use BSTR for non-string uses -- it may save you a nanosecond but will eventually give problems if and when you need to extend or port your code. I'd just use a SAFEARRAY of variants with the keys at index 0, 2, 4... and the corresponding values at 1, 3, 5, ...

我不会将BSTR用于非字符串使用 - 它可能会为您节省一个纳秒,但如果您需要扩展或移植代码,最终会出现问题。我只使用SAFEARRAY的变体,其索引为0,2,4 ......并且相应的值为1,3,5 ......

#3


You need to define "best". COM is -- despite its designers' best wishes -- a very heterogeneous platform. The nature of the the client (aka what language?) that you expect will interact with the object should heavily influence your choice.

你需要定义“最好”。 COM是 - 尽管有设计师的最佳愿望 - 一个非常不同的平台。您期望与客户交互的客户(也称为什么语言?)的性质会严重影响您的选择。

Just two quick examples:

只有两个简单的例子:

  • Being called from Classing ASP?
    Your only tools are VARIANTS. Ok -- you're using ATL: Your only tools are either BSTRs or SAFEARRAYs of VARIANTS.

    被分类ASP调用?您唯一的工具是VARIANTS。好的 - 您正在使用ATL:您唯一的工具是BSTR或VARFAN的SAFEARRAY。

  • Being called from C++?
    You can use a custom data array (read up on [size_is]). Or even fixed-sized arrays if possible. It's TONS easier on both sides.

    从C ++调用?您可以使用自定义数据数组(在[size_is]上阅读)。或者甚至是固定大小的数组。双方都很容易。

There are other considerations:

还有其他考虑因素:

Are you writing the client? Then you want to make things easier on both sides.

你在写客户吗?然后你想让双方都更容易。

Are you writing only the object? Then you probably want to make things easiest on the client, even if it's hardest on the server (why? Because you want the other party to be happy working with you, especially in a consulting or commercial environment -- don't look lazy!).

你只写这个对象吗?然后你可能想在客户端做最简单的事情,即使它在服务器上最难(为什么?因为你希望对方很高兴与你合作,特别是在咨询或商业环境中 - 不要看起来很懒惰! )。

Also, is this a one-off object? Or will it be used in a number of circumstances in the future? In the former case, you might find that it really doesn't matter. In the latter, you will want to think it thoroughly.

此外,这是一个一次性的对象吗?或者将来会在很多情况下使用它?在前一种情况下,您可能会发现它并不重要。在后者中,你会想要彻底地思考它。

If you post more details on your circumstances, we can probably give more specific guidance.

如果您发布有关您情况的更多详细信息,我们可能会提供更具体的指导。

#4


Consider IPersistPropertyBag for the standard way to do this.

考虑使用IPersistPropertyBag来执行此操作的标准方法。

Consider an OLE Property Set object.

考虑一个OLE属性集对象。

Consider accepting Scripting.Dictionary object or ADODB.Record if you are creating something quick and dirty.

如果要创建快速和脏的东西,请考虑接受Scripting.Dictionary对象或ADODB.Record。

#1


You can introduce an interface for a key-value pair and another one for the collection of such pairs.

您可以为键值对引入接口,并为此类对的集合引入另一个接口。

#2


I wouldn't use BSTR for non-string uses -- it may save you a nanosecond but will eventually give problems if and when you need to extend or port your code. I'd just use a SAFEARRAY of variants with the keys at index 0, 2, 4... and the corresponding values at 1, 3, 5, ...

我不会将BSTR用于非字符串使用 - 它可能会为您节省一个纳秒,但如果您需要扩展或移植代码,最终会出现问题。我只使用SAFEARRAY的变体,其索引为0,2,4 ......并且相应的值为1,3,5 ......

#3


You need to define "best". COM is -- despite its designers' best wishes -- a very heterogeneous platform. The nature of the the client (aka what language?) that you expect will interact with the object should heavily influence your choice.

你需要定义“最好”。 COM是 - 尽管有设计师的最佳愿望 - 一个非常不同的平台。您期望与客户交互的客户(也称为什么语言?)的性质会严重影响您的选择。

Just two quick examples:

只有两个简单的例子:

  • Being called from Classing ASP?
    Your only tools are VARIANTS. Ok -- you're using ATL: Your only tools are either BSTRs or SAFEARRAYs of VARIANTS.

    被分类ASP调用?您唯一的工具是VARIANTS。好的 - 您正在使用ATL:您唯一的工具是BSTR或VARFAN的SAFEARRAY。

  • Being called from C++?
    You can use a custom data array (read up on [size_is]). Or even fixed-sized arrays if possible. It's TONS easier on both sides.

    从C ++调用?您可以使用自定义数据数组(在[size_is]上阅读)。或者甚至是固定大小的数组。双方都很容易。

There are other considerations:

还有其他考虑因素:

Are you writing the client? Then you want to make things easier on both sides.

你在写客户吗?然后你想让双方都更容易。

Are you writing only the object? Then you probably want to make things easiest on the client, even if it's hardest on the server (why? Because you want the other party to be happy working with you, especially in a consulting or commercial environment -- don't look lazy!).

你只写这个对象吗?然后你可能想在客户端做最简单的事情,即使它在服务器上最难(为什么?因为你希望对方很高兴与你合作,特别是在咨询或商业环境中 - 不要看起来很懒惰! )。

Also, is this a one-off object? Or will it be used in a number of circumstances in the future? In the former case, you might find that it really doesn't matter. In the latter, you will want to think it thoroughly.

此外,这是一个一次性的对象吗?或者将来会在很多情况下使用它?在前一种情况下,您可能会发现它并不重要。在后者中,你会想要彻底地思考它。

If you post more details on your circumstances, we can probably give more specific guidance.

如果您发布有关您情况的更多详细信息,我们可能会提供更具体的指导。

#4


Consider IPersistPropertyBag for the standard way to do this.

考虑使用IPersistPropertyBag来执行此操作的标准方法。

Consider an OLE Property Set object.

考虑一个OLE属性集对象。

Consider accepting Scripting.Dictionary object or ADODB.Record if you are creating something quick and dirty.

如果要创建快速和脏的东西,请考虑接受Scripting.Dictionary对象或ADODB.Record。