Visual C++ 2005 下创建新 Win32/ Win32 DLL project. 这样不用手写 *.def文件和手工编译链接。
然后将附带cpp文件做成 dll.
Python can only use this "*.dll" by changing it to "*.pyd".
Example:
1. Change the built "pyUtil.dll" to file name "pyUtil.pyd".
2. In python command line:
import sys
sys.path.append(".\\") # where pyUtil.pyd locates
# Or, put pyUtil.pyd file under python dll directory, "C:\Python25\DLLs".
# Note: "C:\Python25\libs" store static library like "pyUtil.lib".
import pyUtil
pyUtil.Recognise("ddddd")
C program can use python extension like static library.
That is: C program with pyUtil.py under the same directory,
which needs support of "C:\python25\libs\python25.lib".
C program can use this "*.dll" normally.
The usage is same as above.
Note that put pyUtil.dll under directory "c:\windows\system32\".
Source code for dll:
//////////////////////////////////////////////////////////////////
// pyUtil.cpp :
#ifdef _MANAGED
#pragma managed(push, off)
#endif
#include
#include "string.h"
#include
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
/*
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
*/
return TRUE;
}
/////////// my code
std::string Recognise_Img(const std::string url)
{ return "hello, " +url;
}
static PyObject* Recognise(PyObject *self, PyObject *args)
{
const char *url;
std::string sts;
if (!PyArg_ParseTuple(args, "s", &url)) // parse python objects from input
return NULL;
sts = Recognise_Img(url); // add your code here.
return Py_BuildValue("s", sts.c_str() ); // return python objects if used in python
}
static PyMethodDef AllMyMethods[] = { // declare of function list
{"Recognise", Recognise, METH_VARARGS}, // function name
{NULL, NULL} // need this to end decl
};
extern "C" PyMODINIT_FUNC initpyUtil() // initMyPythonModuleName
{
PyObject *m, *d;
m = Py_InitModule("pyUtil", AllMyMethods); // Module Name = file name
d = PyModule_GetDict(m);
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
没有评论:
发表评论