我需要创建safearray(将其传递到Cobol之外的COM对象中),并且该safearray中的每个项目都必须为PIC N字符串。
我正在使用以下代码:
05 w-hostArray object reference.
05 w-listA.
10 w-item pic n(500) occurs 8.
......
move VT-BSTR to w-vartype
move 1 to w-dimension
move xx-z to cElements of w-saBound(1)
move 0 to llBound of w-saBound(1)
invoke OLESafeArray "new" using by value w-vartype
w-dimension
by reference w-saBound(1)
returning w-hostArray
end-invoke
perform varying w-Index from 0 by 1 until w-Index >= xx-z
invoke w-hostArray "putString"
using by reference w-Index
by value 100
by reference w-item(w-Index + 1)
returning w-hresult
end-invoke
end-perform
其中w-item是PIC N(500)。但是它不起作用,在C#的另一端,我收到字符串变量完全乱码。我认为“ putString”仅接受PIC X。
那么如何使用Unicode字符串创建safearray?
答案 0 :(得分:0)
真的需要成为PIC N吗?如果要使用Unicode,请尝试以下操作:
05 w-hostArray object reference.
05 w-listA.
10 w-item pic n(500) occurs 8.
05 w-unicode-data pic x(1000).
05 w-unicode-len pic 9(4) comp.
05 WS-EBCDIC-CCSID PIC 9(4) VALUE 1140.
05 WS-UTF8-CCSID PIC 9(4) VALUE 1208.
......
move VT-BSTR to w-vartype
move 1 to w-dimension
move xx-z to cElements of w-saBound(1)
move 0 to llBound of w-saBound(1)
invoke OLESafeArray "new" using by value w-vartype
w-dimension
by reference w-saBound(1)
returning w-hostArray
end-invoke
perform varying w-Index from 0 by 1 until w-Index >= xx-z
MOVE 1 TO w-unicode-len
MOVE SPACES TO w-Unicode-data
STRING
FUNCTION DISPLAY-OF (
FUNCTION NATIONAL-OF (
w-item(w-Index + 1)
WS-EBCDIC-CCSID
)
WS-UTF8-CCSID
)
DELIMITED BY SIZE
INTO w-unicode-data
WITH POINTER w-unicode-len
END-STRING
invoke w-hostArray "putString"
using by reference w-Index
by value 100
by reference w-unicode-data(1:w-unicode-len - 1)
returning w-hresult
end-invoke
end-perform
此STRING会将数据从EBCDIC转换为应可读的UTF-8。 w-unicode-data的大小是w-item的两倍,其原因在于考虑了双字节字符的可能性。
由于我们在调用putString时进行了位置引用,因此我们只应传递实际的转换数据(而不在末尾填充空格)。