我尝试在VS2010中使用pythoncapi for 32位项目。在
我已经安装了64位和32位Python。区分两者 版本,我已将32位dll重命名为“python26_32.dll”。我用vsdumpbin和lib工具创建了相应的import.lib文件“python26_32.lib”(请参见。https://adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll/)。在
我已经调整了'pyconfig.h'并取消了#pragma comment(lib...)
语句的注释。在
对于32位,这些项目编译得很好。当调用PyDict_Check
时,运行exe会导致访问冲突。其他方法调用在正常工作之前(例如Py_Initialize
,PyRun_SimpleString
,PyDict_New
…)
下面是我的一个小例子:
#include "stdafx.h"
#include "python.h" //include in stdafx.h has the same result
#include <iostream>
using std::cout; using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Tryin to initialize Python." << endl;
Py_SetPythonHome("D:\\Python26_32");
Py_Initialize();
char *codeString =
"import platform\n"
"import sys\n"
"print platform.architecture()[0]\n"
"print sys.path\n"
"sys.path.insert(0,'D:\\Python26_32')\n"
"print sys.path\n";
PyRun_SimpleString(codeString);
PyObject *pDict;
pDict = PyDict_New();
PyDict_SetItemString(pDict, "Key1", PyString_FromString("Value1"));
int i = PyDict_Check(pDict);
cout << "working" << endl;
return 0;
}
我注意到,“PyDict_Check”不在dll的导出中。它在python头文件中定义。在
(但我没有在“帮助”窗口中看到)。在
64位版本运行良好(在更改了相关的VC++目录和Py_SetPythonHome
语句之后)。在
最让我困惑的是,c-api的那些部分是有效的,而其他部分则不行。在
刚试过PyDict_CheckExact
,这是有效的。在
非常感谢你的帮助。在
在尝试了其他版本的Python(2.7.9)并获得了相同的结果之后,我发现构建“发布版本”总是可行的。 在
pyconfig.h
中禁用Py_DEBUG
之后,调试构建也能满足我的需要。在虽然我无法解释这种行为,但这为我解决了问题。因此,我认为问题已解决。在
相关问题 更多 >
编程相关推荐