Announcement

Collapse
No announcement yet.

C to PBWin Conversion Problem

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

  • C to PBWin Conversion Problem

    I must admit my C skills are not quite as sharp as they used to
    be. I am creating a PowerBasic DLL that is to be called by a
    toolkit written in C. Of course, the development information is
    in C as well. Here is what I'm trying to convert:

    //This is the structure that will be used.
    typedef struct
    {
    char *module_name;
    char *func_name;
    char *c_func_name;
    }CAPIINFO;

    //This is an example of data being filled in the structure
    CAPIINFO c_api_info[] = {
    {"WSOCK32.DLL", "socket(INT, INT, INT)", "cSocket"},
    {NULL,NULL,NULL} };

    This is the skeleton of the function that I have to export,
    and that the 'C' library is trying to call. I think it is
    supposed to return a pointer to the structure.

    CAPIINFO *GetCAPIINFO()
    { return &c_api_info[0]; }

    I've tried creating a structure, and passing the pointer as the
    return value. That along with many other things. The toolkit
    keeps telling me that invalid data is being returned. I guess
    I would like to know exactly what PB code should be generated
    that would be equivalent to the above C. Any help would be
    greatly appreciated.


    ------------------

  • #2
    Grant, what do you have so far?, I have translated the code like this (Assuming it is a DLL)...

    Code:
    #Compile Dll
    #Dim All
    #Register All
     
    %USEMACROS = 1
    #Include "WIN32API.INC"
     
    Type CAPIINFO
         module_name As Asciiz Ptr
         func_name As Asciiz Ptr
         c_func_name As Asciiz Ptr
    End Type
     
    Global c_api_info() As CAPIINFO      ' Array of UDTs
    Global gsModule As String            ' Dynamic string members
    Global gsFunc As String              '
    Global gsCFunc As String             '
     
    Function LibMain(ByVal hInstance As Dword, ByVal lReason As Long, ByVal lReserved As Long) As Long
     
      If lReason = %DLL_PROCESS_ATTACH Then
     
         ' Only 1 element initially specified, second element is blank...
         ReDim c_api_info(0 To 1) As Global CAPIINFO
     
         gsModule = "WSOCK32.DLL"
         gsFunc = "socket(INT, INT, INT)"
         gsCFunc = "cSocket"
         c_api_info(0).module_name = StrPtr(gsModule)
         c_api_info(0).func_name = StrPtr(gsFunc)
         c_api_info(0).c_func_name = StrPtr(gsCFunc)
     
      End If
     
    End Function
     
    Function GetCAPIINFO Alias "GetCAPIINFO" Export As Dword
      Function = VarPtr(c_api_info(0))
    End Function
    ------------------
    email
    http://www.kgpsoftware.com
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #3
      ... or maybe something like this:

      Code:
      TYPE CAPIINFO
        module_name AS STRING PTR
        func_name   AS STRING PTR
        c_func_name AS STRING PTR
      END TYPE 
      
      GLOBAL c_api_info() AS CAPIINFO
      DECLARE FUNCTION getCAPIINFO() AS DWORD
      
      FUNCTION PBMAIN
         LOCAL pType AS CAPIINFO PTR
         pType = getCAPIINFO
         MSGBOX @pType[0][email protected]_name
      END FUNCTION   
      
      FUNCTION getCAPIINFO() AS DWORD
         LOCAL s1 AS STRING, s2 AS STRING, s3 AS STRING
         DIM c_api_info(1) AS CAPIINFO
         s1 = "WSOCK32.DLL"
         s2 = "socket(INT, INT, INT)"
         s3 = "cSocket"
         c_api_info(0).module_name = STRPTR(s1)
         c_api_info(0).func_name   = STRPTR(s2)
         c_api_info(0).c_func_name = STRPTR(s3)
         c_api_info(1).module_name = 0
         c_api_info(1).func_name   = 0
         c_api_info(1).c_func_name = 0
         FUNCTION = VARPTR(c_api_info())
      END FUNCTION

      ------------------

      Comment


      • #4
        I would stick with ASCIIZ for shoving strings to/from
        C/C++ programs in liu of the Basic "STRING".

        Grant,

        The function is returning an address to the first element of
        the C structure!!!!

        Cheers,
        Cecil

        ------------------




        [This message has been edited by Cecil L. Williams (edited August 18, 2004).]

        Comment


        • #5
          My code in the post above is wrong. The 3 strings used in the getCAPIINFO() function
          must be global. Also, as Cecil commented, the type declarations should be ASCIIZ PTRs.

          Code:
          TYPE CAPIINFO
            module_name AS ASCIIZ PTR
            func_name   AS ASCIIZ PTR
            c_func_name AS ASCIIZ PTR
          END TYPE 
          
          GLOBAL c_api_info() AS CAPIINFO
          GLOBAL module_name AS STRING, func_name AS STRING, c_func_name AS STRING
          DECLARE FUNCTION getCAPIINFO() AS DWORD
          
          FUNCTION PBMAIN
             LOCAL pType AS CAPIINFO PTR, ps AS STRING PTR, i AS LONG, s AS STRING
             pType = getCAPIINFO
             FOR i = 0 TO 1
                IF (@pType[i].module_name) THEN MSGBOX @pType[i][email protected]_name ELSE MSGBOX "Null pointer" 
             NEXT i
          END FUNCTION   
          
          FUNCTION getCAPIINFO() AS DWORD
             DIM c_api_info(1) AS CAPIINFO
             module_name = "WSOCK32.DLL"
             func_name   = "socket(INT, INT, INT)"
             c_func_name = "cSocket"
             c_api_info(0).module_name = STRPTR(module_name)
             c_api_info(0).func_name   = STRPTR(func_name)
             c_api_info(0).c_func_name = STRPTR(c_func_name)
             c_api_info(1).module_name = 0
             c_api_info(1).func_name   = 0
             c_api_info(1).c_func_name = 0
             FUNCTION = VARPTR(c_api_info(0))
          END FUNCTION
          ------------------

          Comment


          • #6
            Many thanks to all of you who responded. It was extremely
            helpful.

            The only modifications I had to make to the code snippets were
            the variables pointed to by the ASCIIZ PTR had to be of type
            ASCIIZ not string. The code works very well now.

            ------------------

            Comment

            Working...
            X