Hooking server DLL into PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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 wcCreateContextByChallenge(const char *szChalllege);
BOOL WINAPI wcDeleteContext();

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

    // establish context

     $challenge = getenv("WILDCATCONTEXT");
     if (!wcCreateContextByChallenge($challenge)) {
        echo "Error creating context";

     ... use other API functions ....


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

Re: Hooking server DLL into PHP

The PHP manual has a section on creating your own extensions:

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.

Quoted text here. Click to load it

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

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)


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.

Quoted text here. Click to load it

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.


Quoted text here. Click to load it

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.

Re: Hooking server DLL into PHP

Quoted text here. Click to load it

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.

Re: Hooking server DLL into PHP

You would do something like this:

  zval *obj, *elem;
  TFooBar foo;

  zend_parse_parameters(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.

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?


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

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

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.


Hector Santos, Santronics Software, Inc.

Quoted text here. Click to load it

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.

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


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.

Site Timeline