Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: PHP and dll

  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP and dll

    Hello All,

    I have a dll which is created using VC++. I need to call a function within this dll from php. I place demo.dll into apache2/bin and php/ext. Similarly, i updated php.ini with code (extension=demo2.dll). I write following code.

    <?php
    dl("demo2.dll");
    echo sum(10,20);
    ?>

    Following error is occured.

    Warning: dl() [function.dl]: Not supported in multithreaded Web servers - use extension=demo.dll in your php.ini in C:\wamp\www\test.php on line 3.

    How can i fix this problem.

    Please help me.

    Thanks in advance.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Remove the dl command. Its not available on windows (trust me, I've tried :P). If you've added it to you're php.ini, its not necessary to import it at runtime. It will also be much much faster when loaded with apache.
    Make sure you've restarted you're apache, then check if its been loaded with the phpinfo.

    BTW, you've ensured that you compiled it with the same version of PHP you're running right? I've had... issues with compiling on different source.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If i removed it then following error is occured.

    Call to undefined function sum() in C:\wamp\www\test.php on line 2.

    i.e. dll is not loaded. How can i load the dll ?

    I have tried it as per thread 1st. Is any step missing ?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Just put the dll in you're php/ext or wherever you're serving them from, alter the php.ini to add the extension load, and restart apache. If apache doesn't trigger any errors when loading the dll, the function will be available as long as you've exposed it to be a PHP function. If you've not wrapped in in the PHP_FUNCTION macro, it will not be usable in PHP.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have create dll file using Visual C++ with following code,
    #include "stdafx.h"

    BOOL APIENTRY DllMain( HANDLE hModule,
    DWORD ul_reason_for_call,
    LPVOID lpReserved
    )
    {
    return TRUE;
    }

    int _stdcall sum(int x , int y)
    {
    return x+y;
    }

    I have successfully used this demo.dll in Visual Basic 6.0 application.
    ------------------
    Then i place demo.dll in php/ext folder and update php.ini and restart the server.

    However, demo.dll is not loaded. Now, what can i do ?

  • #6
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you tell me about PHP_FUNCTION macro.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ah, this dll is not designed for PHP. You need to write it to be specific to the interpreter. Here is a site with a quick overview of extension writing for zend: http://devzone.zend.com/article/1021. This is a very good starting article.

    PHP_FUNCTION is what classifies a function as a PHP usable function. Here is an example from the actual article:
    Code:
    #ifdef HAVE_CONFIG_H
    #include "config.h"
    #endif
    
    #include "php.h"
    #include "php_hello.h"
    
    static function_entry hello_functions[] = {
        PHP_FE(hello_world, NULL)
        {NULL, NULL, NULL}
    };
    
    zend_module_entry hello_module_entry = {
    #if ZEND_MODULE_API_NO >= 20010901
        STANDARD_MODULE_HEADER,
    #endif
        PHP_HELLO_WORLD_EXTNAME,
        hello_functions,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
    #if ZEND_MODULE_API_NO >= 20010901
        PHP_HELLO_WORLD_VERSION,
    #endif
        STANDARD_MODULE_PROPERTIES
    };
    
    #ifdef COMPILE_DL_HELLO
    ZEND_GET_MODULE(hello)
    #endif
    
    PHP_FUNCTION(hello_world)
    {
        RETURN_STRING("Hello World", 1);
    }
    I recall it was a nightmare when I set up an environment for this.
    You might be able to just reuse you're current dll as a resource, and compile it in with the rest of you're extension.

    Edit:
    Also, I should mention that PHP is written in C. C++ does not work.
    Last edited by Fou-Lu; 01-25-2010 at 02:25 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •