本质上,我所拥有的是这个结构:
应用程序-> PHP扩展(在C++中) -> DLL库(C++)
DLL库调用自身内部的函数,例如
int Library::FunctionA(){
return Library::FunctionB + 4;
}问题是,当我尝试从PHP扩展调用Library::Function A时,php崩溃了。我猜这是因为php不能调用DLL库中的函数(它不能从库中调用Library::FunctionB ),因此崩溃了?
DLL库的源代码为:
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);
}
}和php扩展的源代码:使用名称空间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);}
调用GetOffset(FileName,pmd_variant_name_table);时,应用程序崩溃的点在dll库中。我是否引用了错误的函数或什么,或者DLL无法调用其内部的函数?
如果这还不是很清楚,请说出不清楚的地方!有什么想法吗?
发布于 2012-02-05 18:59:55
你试过使用Swig吗?它是一个类似预处理器的工具,可以创建从其他语言调用C++ (或C)代码所需的一切,例如PHP、Perl等。您只需提供一个头文件,其中包含您想要访问的符号,它将创建包装器代码来编译库以及目标语言中的接口代码。更多细节和示例代码可以在here中找到。
https://stackoverflow.com/questions/8994421
复制相似问题