ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Call RPGLE from a C program

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Call RPGLE from a C program

    Hey. I have a service program in RPGLE that has a procedure that returns a value (10 characters). Can someone explain how to declare this procedure in a C program to get the return value?

  • #2
    Normally, C will only pass character strings by reference (i.e. a pointer is passed.) However, you can work around this with a struct or typedef. Is that what you're asking? Can you provide a copy of the RPG prototype so we can see the specifics you are referring to?

    Comment


    • #3
      Scott, you probably misunderstood me. I need to call a procedure in C code, which is in a service program written in RPG. Here is a synthetic example of a procedure in a service program:
      Code:
           D SomeFoo         pr            10    EXTPROC('SomeFoo')
           D   param1                      10A   const
      
      
           PSomeFoo          B                   export
           DSomeFoo          pi            10
           D   param1                      10A   const
            *
           C                   clear                   ret              10
           C                   movel     'Text from'   ret
           C     param1        dsply
           C                   return    ret
           C
           pSomeFoo          e
      And this is a C program in which a procedure from a service program is called and in which I need to get what the procedure returns:
      Code:
      char* SomeFoo(char*);
      
      int main()
      {
        char* ptr;
        ptr = SomeFoo("asdfg     "); // Here ptr doesn't get any meaning
      }
      In this example, in C, the service program works, since when the SomeFoo procedure is called, the line passed in the procedure parameter appears in the job log. However, I cannot get the information that the procedure returns to me.

      Comment


      • #4
        Scott, you probably misunderstood me. I need to call a procedure in C code, which is in a service program written in RPG. Here is a synthetic example of a procedure in a service program:
        Code:
             D SomeFoo         pr            10    EXTPROC('SomeFoo')
             D   param1                      10A   const
        
        
             PSomeFoo          B                   export
             DSomeFoo          pi            10
             D   param1                      10A   const
              *
             C                   clear                   ret              10
             C                   movel     'Text from'   ret
             C     param1        dsply
             C                   return    ret
             C
             pSomeFoo          e
        And this is a C program in which a procedure from a service program is called and in which I need to get what the procedure returns:
        Code:
        char* SomeFoo(char*);
        
        int main()
        {
          char* ptr;
          ptr = SomeFoo("asdfg     "); // Here ptr doesn't get any meaning
        }
        In this example, in C, the service program works, since when the SomeFoo procedure is called, the line passed in the procedure parameter appears in the job log. However, I cannot get the information that the procedure returns to me.

        Comment


        • #5
          So... I've already explained to you why that code sample won't work!

          While C expects strings as a pointer, RPG does not. So you can't assign a pointer to the return value because the RPG is NOT returning a pointer. Change your C prototype to use a struct or typedef.

          Comment


          • #6
            Here's an example:

            Code:
            #include <stdio.h>
            
            typedef struct _RPG_Char10_t {
              char data[10];
            } RPG_Char10_t;
            
            RPG_Char10_t SomeFoo(char *param1);
            
            int main()
            {
              RPG_Char10_t ret;
              ret = SomeFoo("asdfg     ");
              printf("%10.10s\n", ret.data);
            }

            Comment


            • #7
              Scott, thank you very much! It works now!

              Comment

              Working...
              X