...

View Full Version : PHP and dll



qazzaq
01-25-2010, 01:09 PM
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.

Fou-Lu
01-25-2010, 01:12 PM
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.

qazzaq
01-25-2010, 01:16 PM
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 ?

Fou-Lu
01-25-2010, 01:18 PM
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.

qazzaq
01-25-2010, 01:31 PM
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 ?

qazzaq
01-25-2010, 01:32 PM
Can you tell me about PHP_FUNCTION macro.

Fou-Lu
01-25-2010, 02:23 PM
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:


#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.


Also, I should mention that PHP is written in C. C++ does not work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum