Question

I am trying to create php 5.2.17 extension for my c++ dll. Iam using Visual studio 2005 on windows xp with Sambar Server 7.0. I have two questions:

  1. I could not able to include c++ things (strings or STL maps) in my code.. its giving error like: "PHP has encountered an Access Violation at 00CF421B". How can I use strings and STL maps in my dll.

  2. I am trying to create a c++ dll which contains a function with 3 parameters of string type and return 2nd string of those 3.

Below is the code :

PHP_FUNCTION(add){
    char* a;
    char* b;
    char* c;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &a,&b,&c) == FAILURE) {
        RETURN_STRING("Bad parameters!", true);
    }

    //string ss=a;
    //ss=ss+b;
    //memset(a,0,30);
    //strcpy(a,ss.c_str());
    //php_printf("a : %s b : %s ",a,b);
    RETURN_STRING(b,1);
}

php file contains :

$a="abc";
$b="def";
$c="ghi";
$result = add($a,$b,$c);
print "Calling add($a,$b,$c) returned $result";

-> here 1st string is returning fine when two parameters were set for this function.

-> when tried to return 2nd string with two parameters for this function then i am getting an error : "PHP has encountered an Access Violation at 014AE07C"

-> when tried with 3 parameters like the above code then my sambar server is closing by itself.. no chance of seeing output.

Why this strange behaviour, where am I going wrong ?

Thanks in Advance

Anil

Was it helpful?

Solution

  1. PHP extensions are meant to be written in C, not C++. If you need to call PHP in a C++ application, use the embed SAPI, but the extensions themselves will be in C.

  2. Anyway, the call to zend_parse_parameters() should look like this:

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &a, &a_len,&b, &b_len,&c, &c_len) == FAILURE) where the _len variabiles are integers.

Use lxr.php.net for references.

OTHER TIPS

Your first problem is memset(a, 0, 30);. The string given is (probably) only 3 characters long, and you're setting 30 characters to the zero character, probably running over the end of the string's buffer.

Then strcpy(a,ss.c_str()); is likely to cause a similar buffer overrun.

Finally, you're returning whatever is in b (which might have been overwritten), rather than the results of your concatenation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top