Hooking server DLL into PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hector

    Hooking server DLL into PHP

    We have a RPC based client/server hosting system (Wildcat!). Various RPC
    clients include FTP, TELNET, DIALUP as well as WEB server. Each use a
    RPC SDK/API to communicate with the central RPC server to establish context
    and user sessions.

    A few years back (before PHP 4.0) a former employee very familiar with PHP
    began to add PHP server side support and wrote a "PHP Module?" to support
    PHP 3.0 to expose the SDK API to PHP scripts.

    It was never finished (not all the functions were exposed) and although it
    was unofficially release to some PHP developers, for some reason or another
    it no longer worked with PHP 4.0. We have been asked to update the DLL for
    PHP 4.0/5.0.

    Unfortunately, the C/C++ source code for the 3.0 version of the DLL was lost
    or misplaced.

    I believe I got all the information from the PHP support sites, but it
    wasn't too obvious to me how to re-engineer the work.

    I would like to get the basic skinny on writing a support DLL module to
    expose our library of server functions.

    Here are some example C/C++ functions and most primitive requirements for a
    server-side applet to establish thread context:

    BOOL WINAPI wcConnect(const char *szServerName=" ");
    BOOL WINAPI wcCreateContext ();
    BOOL WINAPI wcCreateContext ByChallenge(con st char *szChalllege);
    BOOL WINAPI wcDeleteContext ();

    I'm not a PHP script programming expert, but I see a script like so:

    <?PHP
    // establish context

    $challenge = getenv("WILDCAT CONTEXT");
    if (!wcCreateConte xtByChallenge($ challenge)) {
    echo "Error creating context";
    end??
    }

    ... use other API functions ....


    wcDeleteContext ();
    ?>

    I appreciate any guidance you may provide to assist in getting this PHP
    4.0/5.0 wildcat DLL module written.

    Thanks in advance

    -- Hector

  • Chung Leong

    #2
    Re: Hooking server DLL into PHP

    The PHP manual has a section on creating your own extensions:
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Comment

    • hector

      #3
      Re: Hooking server DLL into PHP

      I was hoping to snap my fingers to get this done. It would be easier of
      the provided Window projects file actually compiled the first time.

      I guess its time to roll up the sleeves to figure it out. :-)

      Thanks. I appreciate the time you took to respond.


      <chernyshevsky@ hotmail.com> wrote in message
      news:1115917387 .342697.95380@o 13g2000cwo.goog legroups.com...[color=blue]
      > The PHP manual has a section on creating your own extensions:
      > http://www.php.net/manual/en/zend.php
      >[/color]

      Comment

      • Chung Leong

        #4
        Re: Hooking server DLL into PHP

        The is a PHP extension cakked win32 that lets you call functions in DLL
        directly from PHP code. If's not terribly reliable however. PHP also
        can also chandle COM objects. Creating an automation interface is not
        trivial though; and COM trhouhg PHP like rather flakey. You will like
        spend less time writing the wrapper via the standard extension
        mechanism.

        Comment

        • hector

          #5
          Re: Hooking server DLL into PHP

          Thanks for the info.

          I was able to finally get ZEND interface/wrapper to work. It was really
          much easier than the spread out documentation makes it out to be. It can be
          clearer. Of course, once someone knows a system, its all obvious. :-)
          Mostly stuff people already encountered and mostly resolved with a google
          search. Maybe if I can get the time I can contribute a neat summary layout
          for C/C++ Windows developers.

          My next step is handle API functions with structures. Once I have atleast
          one function worked out as far as what needs to be done to interface a
          structure with PHP, I can then write a write a C/C++ converter to a PHP
          class like we have for other native languages:

          CPP2VB (visual basic
          CPP2PAS (delphi pascal)
          CPP2VFP (visual foxpro)
          CPP2JAVA (java)
          CPP2PHP (php)

          etc.

          I also want to study the "session" stuff to see if we can better interface
          with it. Wildcat! is an intranet. Exclusive for login systems only. So its
          API automatically establishes sessions context for its inherit server side
          WCX p-code engine which is based on a BASIC like compiled language. So when
          a WCX script runs it inherits globals a User stricture, Msg structure, File
          structures, ConnectionInfo structure, etc. I want to see if I can interface
          these globals with PHP as well. It looks like I can with a ZEND GLOBAL
          macro, if that want it means.

          Anyway, thanks.

          --
          Hector Santos, Santronics Software, Inc.
          ClearingHouse, File Database, Upload, Download



          <chernyshevsky@ hotmail.com> wrote in message
          news:1115965767 .838262.228310@ f14g2000cwb.goo glegroups.com.. .[color=blue]
          > The is a PHP extension cakked win32 that lets you call functions in DLL
          > directly from PHP code. If's not terribly reliable however. PHP also
          > can also chandle COM objects. Creating an automation interface is not
          > trivial though; and COM trhouhg PHP like rather flakey. You will like
          > spend less time writing the wrapper via the standard extension
          > mechanism.
          >[/color]

          Comment

          • hector

            #6
            Re: Hooking server DLL into PHP

            Maybe you can tell me this, because it isn't quite clear in the docs and I
            have yet to find an example.

            If I have a API function with a pointer to a structure:

            BOOL WINAPI foobar(Tfoobar &foo);

            How does a PHP SCRIPT create the TFooBar structure? Is there a "Type"
            keyword in PHP?

            I am looking at the PHP source EXT folder with all the various modules and I
            do see some that are using structures, but I don't see how it is used via a
            PHP script.


            Thanks



            <chernyshevsky@ hotmail.com> wrote in message
            news:1115965767 .838262.228310@ f14g2000cwb.goo glegroups.com.. .[color=blue]
            > The is a PHP extension cakked win32 that lets you call functions in DLL
            > directly from PHP code. If's not terribly reliable however. PHP also
            > can also chandle COM objects. Creating an automation interface is not
            > trivial though; and COM trhouhg PHP like rather flakey. You will like
            > spend less time writing the wrapper via the standard extension
            > mechanism.
            >[/color]

            Comment

            • Chung Leong

              #7
              Re: Hooking server DLL into PHP

              PHP classes/structs are hash tables. The wrapper has to rebuild the
              structure by searching the table and filling in the elements.
              Unfortunately the manual doesn't say much on this topic. The function
              you need is zend_hash_find( ) if I remember correctly.

              Comment

              • hector

                #8
                Re: Hooking server DLL into PHP


                <chernyshevsky@ hotmail.com> wrote in message
                news:1115988562 .473810.105880@ f14g2000cwb.goo glegroups.com.. .[color=blue]
                > PHP classes/structs are hash tables. The wrapper has to rebuild the
                > structure by searching the table and filling in the elements.
                > Unfortunately the manual doesn't say much on this topic. The function
                > you need is zend_hash_find( ) if I remember correctly.[/color]

                Ok, I kinda of saw this. Wasn't sure if this was the only method. Thanks
                for clarifying it.

                think I can really fly with this if I can see an example PHP script, lets
                say for a simple typedef example:

                typedef struct _TFooBar {
                BOOL fBoolean;
                int iNumber;
                char szString[256];
                } TFooBar;

                What is the PHP script for this? Whats the interface like?

                Do I have to expose each field as a Function "Get/Put" like method?

                --
                Hector Santos, Santronics Software, Inc.
                ClearingHouse, File Database, Upload, Download


                Comment

                • Chung Leong

                  #9
                  Re: Hooking server DLL into PHP

                  You would do something like this:

                  PHP_FUNCTION(so me_func)
                  {
                  zval *obj, *elem;
                  TFooBar foo;

                  zend_parse_para meters(ZEND_NUM _ARGS() TSRMLS_CC, "o", &obj);

                  zend_hash_find( foo, "fBoolean", &elem);
                  foo.fBoolean = ZVAL_BOOL(elem) ;

                  zend_hash_find( foo, "iNumber", &elem);
                  foo.iNumber = ZVAL_BOOL(elem) ;

                  }

                  Basically, you need to crack the structure yourself. They might have
                  some funky macros that does it for you though. Google around and maybe
                  look through some of the header files.

                  Comment

                  • hector

                    #10
                    Re: Hooking server DLL into PHP

                    The DLL module side I think I got a relatively good handle (because there
                    are plenty of EXT module examples).

                    It is the PHP script side that I can't see right now. Like would be the PHP
                    script to use this function some_func() with a structure?

                    Something like this?

                    <?PHP

                    class TFooBar {
                    var $boo_field;
                    var $num_field;
                    var $string_field:
                    };

                    $foobar = new(TFoobar);
                    if (!some_func($fo obar)) {
                    echo "error with function";
                    exit;
                    }

                    echo "Success!";

                    ?>

                    I did a search (PHP Script passing structure and other various keywords) and
                    couldn't see anything specifically in this area.

                    What do you mean by "crack?" I am not familar with that term in this regard.

                    Thanks

                    --
                    Hector Santos, Santronics Software, Inc.
                    ClearingHouse, File Database, Upload, Download



                    <chernyshevsky@ hotmail.com> wrote in message
                    news:1115994490 .759716.192660@ g47g2000cwa.goo glegroups.com.. .[color=blue]
                    > You would do something like this:
                    >
                    > PHP_FUNCTION(so me_func)
                    > {
                    > zval *obj, *elem;
                    > TFooBar foo;
                    >
                    > zend_parse_para meters(ZEND_NUM _ARGS() TSRMLS_CC, "o", &obj);
                    >
                    > zend_hash_find( foo, "fBoolean", &elem);
                    > foo.fBoolean = ZVAL_BOOL(elem) ;
                    >
                    > zend_hash_find( foo, "iNumber", &elem);
                    > foo.iNumber = ZVAL_BOOL(elem) ;
                    >
                    > }
                    >
                    > Basically, you need to crack the structure yourself. They might have
                    > some funky macros that does it for you though. Google around and maybe
                    > look through some of the header files.
                    >[/color]

                    Comment

                    • Chung Leong

                      #11
                      Re: Hooking server DLL into PHP

                      There is no equivalence of the C struct in PHP. Objects are just hash
                      tables. You can create them without actually defining the structure.
                      Example:

                      $obj->cow = 'cow';
                      $obj->bobo = 5;

                      some_func($obj) ;

                      The object would just spring into being, as do the object variables. If
                      this seems too spooky, you can always explicitly define the structure
                      and create it using the new operator.

                      class TFooBar {
                      var $cow;
                      var $bobo;
                      };

                      $foobar = new TFooBar;

                      The fact that the variables are declared doesn't mean you can't add
                      something else to the object:

                      $foobar->deep_throat = 'Richard Nixon';

                      Internally, PHP stores the object the same way as an associative array:

                      $obj['cow'] = 'cow';
                      $obj['bobo'] = 5;

                      It's just the zval type that differentiates the two.

                      Comment

                      Working...