none
Passing a character array to a C++ subprogram from Cobol RRS feed

  • Question

  • How can I pass a large character array from a Cobol program (Fujitsu) to a C++ subprogram (Visual Studio 2019) and allow the C++ subprogram to change the data and return it to the calling Cobol program?

    I thought I could pass a pointer to the C++ program and update the data pointed to in the C++ .dll, but I haven't been able to get that to work.

    What is the best way?  I would appreciate some suggestions.

    Friday, November 1, 2019 11:41 PM

All replies

  • >How can I pass a large character array from a Cobol program (Fujitsu) to a C++ subprogram (Visual Studio 2019) and allow the C++ subprogram to change the data and return it to the calling Cobol program?

    You'll probably have to search (or ask Fujitsu) for information specific to the COBOL compiler you're using for how to do this.

    Dave

    Saturday, November 2, 2019 12:49 AM
  • Are you asking for help with the COBOL side or the C++ side? If you have a question that is entirely about C++ then just make that clear.

    It has been about thirty years since I have done much COBOL programming but back then COBOL did not do pointers. I do not know if you say you could pass a pointer to the C++ program because you know that COBOL (or at least Fujitsu COBOL) can pass pointers or if you were just hoping it could.

    If you could post a little relevant information from the Fujitsu documentation then perhaps someone could help but the vast majority of people here are reluctant to help with software from another vendor, especially COBOL. The fact that you provide so little information implies you have not studied the relevant documentation.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, November 2, 2019 4:42 PM
  • How can I pass a large character array from a Cobol program (Fujitsu) to a C++ subprogram (Visual Studio 2019) and allow the C++ subprogram to change the data and return it to the calling Cobol program?

    I thought I could pass a pointer to the C++ program and update the data pointed to in the C++ .dll, but I haven't been able to get that to work.

    What is the best way?  I would appreciate some suggestions.

    What does "haven't been able to get that to work" actually mean?  Be specific.

    How did you pass the pointer from the Cobol program?

    How did you specify what type the C++ function expected to receive? What value did the C++ function actually receive?  What data was at that location?

    How did you try to pass the modified data back to the Cobol program? What did the Cobol program receive?


    Sunday, November 3, 2019 12:36 AM
  • Hello,

    Thank you for posting here.

    Do you resolve this issue?  Could you please mark the helpful as answer. it will be beneficial to other community. If you resolve the issue by yourself, we also appreciate that you share your solution and mark it as the answer.

    Best Regards,

    Suarez Zhou


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 4, 2019 1:44 AM
  • All:

    Thanks much for all your replies.  I wouldn't think this should be so difficult, but something is eluding me.

    The Cobol side is fairly straight forward.  The Call statement supports pass by reference, and Cobol supports pointers.  I tried three versions of the call:

    1) Pass a 25 character array (pic x(25), to the C++ program function 1 as a char*.  Change that buffer with MemCpy, and return.  Fujitsu says this works, but the data area in the Cobol program remains unchanged.  If I specify a return of a character string in C++, and use the Returning phrase on the Cobol call, the return string is actually returned.  So why not the parameter passed by reference?  My problem 1.

    2) Create a pointer in Cobol and set the address to my 25 character array, and call the C++ program function 2 as char**.  The C++ function 2 accepts a pointer to a pointer.  Change that buffer with MemCpy and return.  The data area in the caller is updated as I would expect.  Not a problem 2.

    3) Same call as number 2, but I added a return of a string variable.  The Cobol call statement specifies Using by reference the pointer, and returning a Cobol character array variable (pic x(25)).  I get a C0000005 exception.  The Cobol syntax does not allow a returning by reference or by value, so I assume it is always by reference, but the manual is silent on this issue.

    I'm wondering if I am not linking the C++ program correctly.  I am using a new installation of Visual Studio 2019.  I created a .dll project, and while I don't see anything in the linker side of the project properties, the C05 exception tells me I'm trying to access memory I don't own.

    Also, I get a compiler warning saying the return variable will not be valid when the program exits.  Hmm, it's a library (.dll) that exports data.  Shouldn't the compiler know that and realize the .dll will be called?  This just adds to my confusion.

    I appreciate in the extreme all your replies.  It's been years since I've written any C, but I believe the C++ source is correct which points me to how Visual Studio is compiling and linking.  I've used DumpBin but I'm not sure what I'm looking at.  It's not like the S0C13s on a 360.  I could diagnose those dumps.  Maybe I need to find some documentation of the output of Dumpbin and start learning how these stack machines deal with memory.  Really, I just want my call statement to work.  OK, I'll stop ranting.

    Thanks again for all your help.

    Friday, November 8, 2019 4:57 PM
  • As best as I understand what you are saying, your #2 worked. So is the problem solved?


    Sam Hobbs
    SimpleSamples.Info

    Friday, November 8, 2019 6:48 PM
  • Well, No.

    I'm uncomfortable putting something into production when there are unknowns.  If I try to return an array with number 2 I get an exception and the entire run unit fails, and I don't know why.  Thank makes me nervous.

    The fact that number 1 and number 3 do not work when they should tells me I'm not doing something correctly.  I would like to figure out what that is.

    Thanks much

    Saturday, November 9, 2019 8:07 AM
  • Narrative descriptions of code only go so far in explaining things.

    We can provide better assistance if you post the C++ code in the dll that you are using for the scenarios you describe.

    After all, we can't spot errors if you don't show us the code.

    Saturday, November 9, 2019 9:44 AM