求助:Python Web的CGI下调用C++ Dll中再调用相对路径文件的问题。
求助:Python Web的CGI下调用C++ Dll中再调用相对路径文件的问题。
各位朋友,现在我遇到了这样一个问题,不知道大家遇到过没有,遇到过的请指点一下。
我编写了一个
---------------------------------TestSimplePythonDll.cpp---------------------------------
#include <iostream>
#include <fstream>
#include <string>
#include "TestPythonDll.h"
using namespace std;
int main2()
{
ifstream file1("../data/1.txt");
ofstream file2("../data/2.txt");
string strTemp = "";
int type = 1;
while(getline( file1, strTemp, '\n') )
{
cout << strTemp << endl;
file2 << strTemp << endl;
type = 0;
}
file1.close();
file2.close();
cout << "Good" << endl;
return type;
}
---------------------------------------------------------------------------------------
---------------------------------TestSimplePythonDll.h---------------------------------
#include "Python.h"
#pragma comment(lib,"python24.lib")
#ifndef TestSimplePythonDll_H
#define TestSimplePythonDll_H
int main2();
static PyObject *TestSimplePythonDll_main2(PyObject *self, PyObject *args)
{
return ( PyObject * )Py_BuildValue( "i", main2() );
}
static PyMethodDef TestSimplePythonDllMethods[] = {
{"main2", TestSimplePythonDll_main2, METH_VARARGS},
{NULL, NULL}
};
extern "C" __declspec(dllexport) void initTestSimplePythonDll()
{
(void) Py_InitModule("TestSimplePythonDll", TestSimplePythonDllMethods);
};
#endif
---------------------------------------------------------------------------------------
这样在VC7下生成一个动态链接库TestSimplePythonDll.dll。
之后我编写了一个这样的py
---------------------------------TestSimplePythonDll.py---------------------------------
import cgi
import os
import sys
def printHeader( title ):
print """Content-type: text/html
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE html PUBLIC
"-//W#C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head><title>%s</title></head>
<body>""" % title
printHeader( "Test Dlls" )
os.chdir("C:\\Inetpub\\wwwroot\\")
import TestSimplePythonDll
result = TestSimplePythonDll.main2()
print "Good:%s" % result
print "</body></html>"
---------------------------------------------------------------------------------------
然后我在IIS的默认文件夹下放置了这个py文件和动态链接库文件,以及需要的data/1.txt文件。
然后我在IE中打开http://127.0.0.1/TestSimplePythonDll.py
这个时候出现的总是
Good:1
从而我知道在main2函数中没有能够执行type = 0;这句。
这样就出现了问题。我觉得可能是python下调用C++的DLL再调用相对路径下的文件就不能正确的定位。
不知道各位大虾怎么看待这个问题。
问题有点罗嗦,还请多多见谅,谢谢!