1

I need to create safearray (to pass it into COM object outside Cobol), and each item in this safearray must be PIC N string.

I am using this code:

       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

Where w-item is PIC N(500). But it's not working, on the other side in C#, I receive in string variable complete gibberish. I think that "putString" accepts only PIC X.

So how to create safearray with Unicode strings?

Pavel Matras
  • 329
  • 1
  • 5
  • 13

1 Answers1

0

Does it really need to be a PIC N? If you want this in Unicode, try something like this:

   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

This STRING will convert the data from EBCDIC to UTF-8 which should be readable. The reason that w-unicode-data is twice as large as w-item is to account for the potential of double byte characters.

Because of the positional reference we make when invoking putString, we should only pass the actual converted data (and not padded with spaces at the end).

SaggingRufus
  • 1,814
  • 16
  • 32