Skip to main content

Problem:

How can I pass a null terminates string to .Net  so that the null is retained in the string being passed?

The calling program is Unmanaged COBOL and it will be using COM Interop to call the .Net assembly.

Resolution:

You need to use a chararray to pass this data across as you can specify the size of the  chararray when you create it.

Some example code that does this is:-

      *> This is required for OLE applications

      $set ooctrl( P)

       program-id. testnet.

       special-names.

           call-convention 66 is winapi.  *> Non Litlinked apis

       class-control.

       *> OLE automation classes

           olesup is class "olesup"

           dotnet is class "$OLE$TestCOBOLNull.CTester"

           oleVariant is class "olevar"

           CharacterArray is class "chararry"

           .

       working-storage section.

       copy mfole.

       01  string1             pic x(8)   value "12345678".

       01  string2             pic x(8)   value spaces.

       01  string3             pic x(200) value spaces.

       01  obj-string3         object reference.

       01  inta                pic s9(9) comp-5 value 1.

       01  intb                pic s9(9) comp-5 value 2.

       01  ws-dnobj            object reference.

       01  ws-missingref       object reference.

       01  mynull              object reference.

       01  ws-ptr              pointer.

       01 retval               pic s9(9) comp-5.

       01 ws-size              PIC S9(9) COMP-5.

       linkage section.

       01  lnk-var             VARIANT.

       procedure division.

       start-it section.

           invoke dotnet "new" returning ws-dnobj

           invoke ws-dnobj "TestMethod"

                   using by reference string1

                         by reference string2

                         by value     inta

                         by value     intb

                   returning retval

           invoke oleVariant "new" returning mynull

           invoke mynull "getVariantAddress" returning ws-ptr

           set address of lnk-var to ws-ptr

           move DISP-E-PARAMNOTFOUND

                     to VARIANT-VT-SCODE of lnk-var

           invoke ws-dnobj "TestMethod"

                   using by reference string1

                         by reference mynull

                         by value     inta

                         by value     intb

                   returning retval

           invoke ws-dnobj "TestMethod"

                   using by reference string1

                         by reference mynull

                         by value     inta

                         by value     intb

                   returning retval

      ***** Setup a chararray to pass data

           move z"1234" to string3

           perform create-null-terminated-bstr

           invoke ws-dnobj "TestMethod"

                   using by reference string1

                         by reference obj-string3

                         by value     inta

                         by value     intb

                   returning retval

      ***** Setup a chararray to pass data

           move z"1234567890123456789012345678901234567890" to string3

           perform create-null-terminated-bstr

           invoke ws-dnobj "TestMethod"

                   using by reference string1

                         by reference obj-string3

                         by value     inta

                         by value     intb

                   returning retval

           display "Press return to TERMINATE".

           accept retval

           stop run

           .

       create-null-terminated-bstr section.

           move length of string3 to ws-size

           perform varying ws-size from length of string3 by -1

           until ((string3(ws-size:1) = x"00") or (ws-size = 1))

               continue

           end-perform

           invoke CharacterArray "withlengthvalue" using ws-size

                                                       string3

               returning obj-string3

           .

Old KB# 3937