在本机Cobol的safearray中使用Unicode(PIC N)

问题描述 投票:1回答:1

我需要创建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?

c# cobol comobject
1个回答
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时进行位置引用,因此我们只应传递实际的转换后的数据(而不在末尾填充空格)。

© www.soinside.com 2019 - 2024. All rights reserved.