Jump to content

php_embed_init


Maxtor

Recommended Posts

Hi all,

I don't know if this is the right place to post my problem. If not seniors, please guide,

 

I have been trying offlate to call PHP code in C. I have been able to run few of the examples from the book "Extending and Embedding PHP - Sara Golemon". But I have been having problems in extending further.

 

My Problem is as follows:

Whenever I use php_embed_init() and php_embed_shutdown() in a dll I create, the dll doesn't get loaded by my webserver when I try to load it as an additional module. I think something else needs to be loaded before my dll gets loaded, not sure what?

 

Thank you for your time,

 

 

Link to comment
https://forums.phpfreaks.com/topic/148076-php_embed_init/
Share on other sites

Sorry, not mentioned this in the post earlier, I am using Apache/2.2.4 (Win32), PHP Version 5.2.3

 

I am creating a PHP module and make a call to "call_user_function()".

 

A small piece of I am doing is as follows:

// FirstPHPExt.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"


#ifdef ZTS
    void ***tsrm_ls;
#endif



long retval;



/* declaration of functions to be exported */
ZEND_FUNCTION(DoubleUp);


void testing()
{
int argc=2;
char *argv[] = {"test"};

	if (php_embed_init(1, argv PTSRMLS_CC) == FAILURE) {

		}

	//PHP_EMBED_START_BLOCK(argc, argv);
	zval fname,return_value;
	zval *params;
	MAKE_STD_ZVAL(params);
	//MAKE_STD_ZVAL(params[1]);
	ZVAL_STRING(&fname, "MyFunction", 1);
	ZVAL_STRING(params, "test", 1);

	if (call_user_function(EG(function_table), NULL,
				&fname, &return_value, 1, &params TSRMLS_CC) == FAILURE) {
		php_error_docref(NULL TSRMLS_CC, E_WARNING,
					"Unable to call MyFunction(), is it defined?");
		//        RETVAL_FALSE;
        goto cleanup;
	}

	cleanup:
		zval_dtor(&fname);
		zval_dtor(params);
		//zval_dtor(&params[1]);
	//PHP_EMBED_END_BLOCK();
	php_embed_shutdown(TSRMLS_C);
}






#ifdef __cplusplus
extern "C" {
#endif
void onMessage(HOBJ msg);	/* to handle message subscriptions */
#ifdef __cplusplus
};
#endif


/* compiled function list so Zend knows what's in this module */
zend_function_entry FirstPHPExtModule_functions[] = {
    ZEND_FE(DoubleUp, NULL)
    {NULL, NULL, NULL}
};

/* compiled module information */
zend_module_entry FirstPHPExtModule_module_entry = {
    STANDARD_MODULE_HEADER,
    "FirtsPHPExt Module",
    FirstPHPExtModule_functions,
    NULL, NULL, NULL, NULL, NULL,
    NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};




/* implement standard "stub" routine to introduce ourselves to Zend */
ZEND_GET_MODULE(FirstPHPExtModule)

/* DoubleUp function */
/* This method takes 1 parameter, a long value, returns the value multiplied by 2 */
ZEND_FUNCTION(DoubleUp){
    long paramValue = 0;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &paramValue) == FAILURE) {
        RETURN_STRING("Bad parameters!", true);
    }
testing();
    paramValue *= 2;
    RETURN_LONG(paramValue);
} 


#ifdef _MANAGED
#pragma managed(push, off)
#endif

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

#ifdef _MANAGED
#pragma managed(pop)
#endif


Link to comment
https://forums.phpfreaks.com/topic/148076-php_embed_init/#findComment-777877
Share on other sites

As I showed in the code I posted, I have functions "DoubleUp" and "testing".

 

I make a call to the function "testing" from within the function "DoubleUp", which calls the method php_embed_init().

Whenever I include this portion of the code and compile my dll,

void testing()
{
   int argc=2;
   char *argv[] = {"test"};

      if (php_embed_init(1, argv PTSRMLS_CC) == FAILURE) {
      
         }
      
      //PHP_EMBED_START_BLOCK(argc, argv);
      zval fname,return_value;
      zval *params;
      MAKE_STD_ZVAL(params);
      //MAKE_STD_ZVAL(params[1]);
      ZVAL_STRING(&fname, "MyFunction", 1);
      ZVAL_STRING(params, "test", 1);
      
      if (call_user_function(EG(function_table), NULL,
               &fname, &return_value, 1, &params TSRMLS_CC) == FAILURE) {
         php_error_docref(NULL TSRMLS_CC, E_WARNING,
                  "Unable to call MyFunction(), is it defined?");
         //        RETVAL_FALSE;
        goto cleanup;
      }

      cleanup:
         zval_dtor(&fname);
         zval_dtor(params);
         //zval_dtor(&params[1]);
      //PHP_EMBED_END_BLOCK();
      php_embed_shutdown(TSRMLS_C);
}

I get this error at the xampp startup and the module doesn't get loaded.

[11-Mar-2009 09:28:33] PHP Warning:  PHP Startup: Unable to load dynamic library 'C:\xampp\php\ext\m

y_php_ext.dll' - The specified module could not be found.

 

If I compiled the dll without the above function, the module is loaded perfectly fine.

 

You are trying to load it in PHP and not Apache right?

("the dll doesn't get loaded by my webserver")

I think I might be wrong at this saying that the dll is loaded by the webserver,

The way I load the dll is:

 

1. Compile the dll,

2. Place it in the ext direct of PHP

3. Edit the php.ini to load my dll "my_php_ext.dll"

4. Start the xampp server,

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/148076-php_embed_init/#findComment-781813
Share on other sites

Also when I try to use call_user_function()

I get a Access Violation Error at the point where I call call_user_function(),

 

void testing()
{	
zval funcname;
char *string_contents = "MessageHandler";
ZVAL_STRING(&funcname, "MessageHandler", 0);	
if(call_user_function(EG(function_table), NULL, &funcname,
							&returnval, 1, NULL TSRMLS_CC) != SUCCESS)
{
	zend_error(E_ERROR, "Function call failed");
}
}	

Link to comment
https://forums.phpfreaks.com/topic/148076-php_embed_init/#findComment-781889
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.