As you know, approaches to creating external components of 1C involve the use of two technologies - Native API and COM. The example with the Native API is well described in the
article .
But the problem is that in the case of applying the Native API technology, there arises the rather nontrivial task of exchanging arrays of information between the external component and 1C: Enterprise. As it was correctly noted in the comments to the article, this task has to be solved either by multiple procedure calls, or by serializing the contents of the array.
But if you use COM-technology, then everything is greatly simplified. The fact is that in 1C there is such a little-known, but in this case, an indispensable data type, like COMSafeArray.
')
Extract from syntax assistant:COMSafeArray (COMSafeArray)
Description:
Object wrapper over a multidimensional SAFEARRAY array from COM. Allows you to create and use SAFEARRAY to exchange data between COM objects.
To transfer an array as a parameter of a COM object method, you need to build a COMSafeArray of the desired dimension with the desired element type and specify the constructed COMSafeArray as the value of the input parameter. Other 1C: Enterprise objects can be used as values ββof input parameters of the Array type only if you have comprehensive information on the types of parameters in the library of the type of a COM object.
The result of a COM object method or the value of the output parameter of type Array is always represented by a COMSafeArray object.
In addition to providing the type itself, COMSafeArray 1C accompanies it with an exhaustive set of methods that allow you to convert standard 1C arrays into values ββof this type and vice versa.
The trick is that if you pass this object as a parameter to the function of an external component, then in the most external component built using COM technology, this array will be obtained as a pointer to an array of type SAFEARRAY.
Similarly, if you return a pointer to such an array from the function of an external component, in 1C the result will be interpreted as an object of type COMSafeArray.
Moreover, in the function of the external component, you can change the input array itself, the pointer to which is obtained as a parameter, and return only S_OK from the function. In 1C, then you can continue working with the transferred array and it will contain changes formed by the external component. Those. in conjunction between 1C and the COM component, you can use the usual transfer of parameters by reference.
Let us show by example.
In 1C, everything can be placed in a simple button event:
()
Those. created and filled the usual array, created on its basis an object of type COMSafeArray and fed its functions to the external component.
The function somehow converts the input COMSafeArray and returns something also in the form COMSafeArray.
Next, we unload both received COMSafeArray objects into regular 1C arrays and with the viewer (Alt + F9) we see the result.
On the side of the external component, everything looks a little more complicated. In the corresponding switch-case block, located in the
CallAsFunc function
, we will not only create the resulting array, but also change the input array itself.
Actually, everything is described in the comments:
case arrayFunc: {
Those. we got the original array and somehow changed (in this case, incremented) it.
After that, we created a new array of the same dimension and filled it with certain values ββ(in this case, the input, multiplied by 2).
Thus, if before processing the function of the external component, we had an array:
Array Parameters: {10.1, 20.2, 30.3}
Then after practicing this function, the arrays obtained in 1C will take the following form:
Array Parameters: {11.1, 21.2, 31.3}
Array of Results: {22.2, 42.4, 62.6}
Those. and the parameter-array and the resulting array were processed by the function of the external component and obtained in 1C
in a modified form.
The complete design of the external component (with extended demo functions) is
here .