Announcement

Collapse
No announcement yet.

C conversion

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

  • C conversion

    was able to convert most of the functions in the fastcgi app server
    header (fcgiapp.h) from c to pb with no problem, but the last one
    (with two compiler-specific options) i'm not sure about:

    'dllapi int fcgx_fprintf(fcgx_stream *stream, const char *format, ...)
    'dllapi int fcgx_vfprintf(fcgx_stream *stream, const char *format, va_list arg)

    the optional variable arguments at the end can be of any type.

    i found this example by dave navarro from a while back, but don't
    know whether it's currently valid:
    http://www.powerbasic.com/support/pb...ad.php?t=22795

    using his example, the above would look something like:

    declare function fcgx_fprintf lib "fcgi.dll" alias "fcgx_fprintf (stream as fcgx_stream, format as asciiz [, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any]) as long

    anyone know the best/most valid way to convert either of these
    prototypes?

    also, this should be obvious, but i'm not 100% sure how to translate it:

    typedef char **fcgx_paramarray;

    perhaps a macro?


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

  • #2
    not sure what to do with printf, other than ignore it. not even sure how it could work unless it CDECL ... i assume it is.
    since pb has great string handling, i'd do it like this i guess ...

    declare function CDECL FCGX_FPrintF lib "whateveritis.dll" alias "FCGX_FPrintF" (byval pStream as long, _
    zCharFormat as asciiz) as long

    pass zCharFormat as byval 0. never pass any params. just build your string in pb and pass it in (in the stream parameter? not sure what that does).

    per the param **, it would be a pointer to the pointer.

    so if you have
    dim pString as asciiz ptr

    '- fictional function to return a pointer to a string.
    pString = makeAString("This is a test")
    stdout @pString

    your param array would equal varptr(pString)

    best regards,
    don



    ------------------
    Don Dickinson
    www.greatwebdivide.com
    Don Dickinson
    www.greatwebdivide.com

    Comment


    • #3
      hi, don.

      guess i wasn't clear enough in my second question. as an old c
      programmer, i am well aware of double indirection, and have translated
      others, such as "char **envp" within a typedef in "fcgi.inc" here:
      http://www.powerbasic.com/support/pb...ad.php?t=24568

      however, that was within a typedef struct, where the dotted notation
      for the individual elements translates perfectly. in this case, the
      typedef defines fcgx_paramarray as a "char **". so, i want to be able
      to do the equivalent, merely for purposes of readability, in my
      pb translation. i'm assuming a macro can easily do this, but to be
      honest, i've never used the macro facility in pb...

      maybe this is what i want?

      macro fcgx_paramarray = dword ptr

      thanks

      btw, anyone interested in kick-a** fast, persistent cgis in pb that run
      independently of their web server should have a look at the translation
      and fastcgi.

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


      [this message has been edited by chuck hicks (edited april 25, 2005).]

      Comment


      • #4
        Well, played with MACROS for a while, and they were indeed the solution.
        I like - very much like C macros.

        Here's what I came up with (wouldn't take "..." as a valid macro name,
        so I used "etc" instead):

        In "fcgi.inc"
        Code:
        MACRO FCGX_ParamArray = DWORD PTR
        MACRO opt_ = OPTIONAL x AS ANY
        MACRO etc = opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_,opt_
        
        'DLLAPI int FCGX_FPrintF(FCGX_Stream *stream, const char *format, ...)
        DECLARE FUNCTION FCGX_FPrintF LIB "fcgi.dll" ALIAS "FCGX_FPrintF" (pStream AS FCGX_Stream PTR, format AS ASCIIZ, etc ) AS LONG
        In the test app server:
        Code:
        SUB PrintEnv(pOut AS FCGX_Stream PTR, szlabel AS ASCIIZ, envp AS FCGX_ParamArray)
            LOCAL count AS LONG
            LOCAL p AS ASCIIZ PTR
        
            FCGX_FPrintF(pOut, "%s:<br><pre>", szlabel)
            count = 0
            DO
                p = @envp[count]
                IF p = %NULL THEN EXIT DO
                FCGX_FPrintF(pOut, "%s<br>", @p)
                INCR count
            LOOP
            FCGX_FPrintF(pOut, "</pre><p>")
        
        END SUB
        Note that "envp" is passed as "FCGX_ParamArray" for readability.

        And, I get to use a variant of one of my favorite C functions, printf().
        Unfortunately, widely used shortcuts like "\n" and "\t" aren't escaped,
        so get passed through as is. Oh well, can't have everything.

        Will have to use PB MACROS more often.


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

        Comment


        • #5
          and, i get to use a variant of one of my favorite c functions, printf().
          unfortunately, widely used shortcuts like "\n" and "\t" aren't escaped,
          so get passed through as is. oh well, can't have everything.
          but maybe you <u>can</u> have a lot....

          you might try the winapi "wsprintf" function in user32.dll. that uses "c formatting strings"

          and, the "c" runtime library (msvcrt40.dll) present on all windows boxes exports a lot of functions, too.. you might look for the formatting functions you want there. (on my system, it exports 1165 functions, including printf).

          you can use this program to create a little text file report of all the exported functions in any dll or exe:
          show exports and imports for pb/win 6x, 7x (original by torsten reinow)

          just because the pb version of win32api.inc does not include the functions in the c runtime dll doesn't mean you can't use em...

          mcm


          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Michael,

            The version of printf() I'm using (FCGX_FPrintF()) is part of the
            FastCGI library that I'm writing PB headers for. It is "special"
            in that it writes to FCGX_Streams, which are tied to either a socket
            or named pipe. The FastCGI library has been around and widely
            used since 1996, so I'm not about to rewrite anything in it.

            Also, the character sequences (\n, \t, \r, etc.) appear to be converted
            by the C compiler at compile time, not by the printf() function. A
            look inside a compiled C .exe confirms this - \n isn't there, but
            a line break is.

            Thanks, anyway.

            Don,

            You're absolutely right that this needs to be declared CDECL. I
            had memory leaking all over the floor before adding that little keyword.
            Not noticable when hitting it a few times from a browser, but stress
            testing with Microsoft's WAST quickly caused GPFs all over the place,
            with "stack overflow" as the cause. Adding CDECL to all the functions
            cured this quickly. A quick read of the help file explains why -
            stack cleanup wasn't being done without CDECL.

            Thanks.



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

            Comment


            • #7
              Don,

              Discovered your service wrapper code, which allowed me to quickly
              rewrite the example FastCGI server as a Windows service.

              Nice!

              Thanks.


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

              Comment


              • #8
                Chuck,
                Abyss came out with FastCgi support.
                Can you point me to any example code?

                Thank you,
                Mike

                ------------------
                CMD shortcut to open any location:
                %windir%\system32\cmd.exe /k " cd\ & x: & cd x:\xxxx
                Change to run as administrator
                How long is an idea? Write it down.

                Comment

                Working...
                X