Essentially what I have is this structure:
PHP Application -> PHP Extension (in C++) -> DLL Library (C++)
The DLL library calls functions inside itself, for example
int Library::FunctionA(){
return Library::FunctionB + 4;
}
The problem is, when I try and call Library::Function A from my PHP extension, php crashes. I guess this is because php cannot call functions inside the DLL library (it cannot call Library::FunctionB from within the library) and therefore crashes?
The source code for the DLL Library is:
using namespace std;
namespace PMDInfo
{
class PMDParser
{
public:
static __declspec(dllexport) string GetVariants(string FileName);
static __declspec(dllexport) streamoff GetOffset(string FileName,streamoff offset);
};
}
namespace PMDInfo {
streamoff PMDParser::GetOffset(string FileName,streamoff offset){
ifstream file2(FileName.c_str(), ios::binary);
file2.seekg(offset);
return (streamoff)file2.get();
}
string PMDParser::GetVariants(string FileName){
char *buffer1[40];
ifstream ReadPMD(FileName.c_str(), ios::binary);
streamoff begin = GetOffset(FileName,pmd_variant_name_table); offsets
streamoff end = PMDInfo::PMDParser::GetOffset(FileName,pmd_group_table);
return itoa(begin-end,buffer1,10);
}
}
And the source code for the php extension:
using namespace std;
namespace PMDInfo
{
class PMDParser
{
public:
static __declspec(dllimport) string GetVariants(string FileName);
static __declspec(dllimport) streamoff GetOffset(string FileName,streamoff offset);
};
}
--- PHP EXTENSION initiation code in here, all works fine ---
ZEND_FUNCTION(GetVariants)
{
int title_len;
char *title = "";
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &title, &title_len) == FAILURE) {
RETURN_NULL();
}
RETURN_STRING(PMDInfo::PMDParser::GetVariants(title).c_str(),1);
}
The point at which the application crashes is in the dll library when calling GetOffset(FileName,pmd_variant_name_table);. Am I referencing the functions wrong or something, or is the DLL unable to call functions inside itself?
If this still isn’t very clear please say what isnt!
Any ideas?
Have you tried using Swig? It’s a preprocessor like tool to create everything you need to call C++ (or C) code from other languages, e.g. PHP, Perl, etc. You just have to provide a header with the symbols you’d like to make accessible and it will create the wrapper code to compile the library as well as code for the interfaces in your target language. More details and example code can be found here.