动态将python23.dll链接到C ++应用程序 [英] Dynamically linking python23.dll to c++ application

查看:96
本文介绍了动态将python23.dll链接到C ++应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个用c ++(MFC)编写的应用程序.我已包含python.h并指定了lib文件.这样,我就可以使用python c api中的函数并执行python脚本.

这需要我包括python.h并为链接器指定python23.lib.一切正常.

我现在正在尝试的是删除与python的静态链接,并使用LoadLibrary函数加载python dll.

我写了以下代码-

Hi
I have a application written in c++ (MFC). I have included python.h and specified the lib file. With this I am able to use functions from python c api and execute python scripts.

This requires me to include python.h and specify python23.lib to the linker. Everything works fine.

What I am trying now is , to remove the static linking to python and use LoadLibrary function to load the python dll.

I have written the following code -

typedef void (pFnPyRun_SimpleString)(char *);
typedef void (pFnPy_Initialize)();



在函数中,以下代码-



in a function the following code -

hModPython = AfxLoadLibrary("Python23.dll");

	

pFnPyRun_SimpleString *pFunction = NULL; 
pFnPy_Initialize *pPy_Initialize = NULL;

pFunction = (pFnPyRun_SimpleString *)::GetProcAddress(hModPython, "PyRun_SimpleString");
	
pPy_Initialize = (pFnPy_Initialize *)::GetProcAddress(hModPython, "Py_Initialize");

	
try
{
          pPy_Initialize();

	  if ( pFunction )
	  {
		(*pFunction)("import sys");		// call the code
	  }
	  else
	  {
	 AfxMessageBox("unable to access function from python23.dll.", MB_ICONSTOP|MB_OK);
		}
}
catch(...)
{
				
}



此代码也可以正常工作.我已经动态加载了python dll,并通过使用GetProcAddress获取函数指针从python c api调用了函数.

到目前为止很好.当我想在我的C ++代码中创建字典对象mdict以便将其传递给
时,就会出现问题



This code works fine too. I have loaded the python dll dynamically and called the functions from python c api by obtaining the function pointers using GetProcAddress.

So far good. The problem arises when i want to create a dictionary object mdict in my c++ code so that i can pass it to a

PyEval_EvalCode

PyObject* pCodeObject = 

Py_CompileString(wholefile.GetBuffer(0),path.GetBuffer(0),Py_file_input);
				
if(pCodeObject != NULL)
    PyObject* pObject = PyEval_EvalCode((PyCodeObject*)pCodeObject,mDict,mDict);



为了能够对此进行编译,我需要包含python.h并静态链接到python23.lib,因为找不到PyObject.静态链接是我想要避免的.因此使用了loadlibrary.

如何处理.

提前谢谢.
Sober



To be able to compile this i need to include python.h and link to python23.lib statically as PyObject cannot be found. Static linking was what i was trying to avoid. And therefore used loadlibrary.

How can this be handled.

Thanks in advance.
Sober

推荐答案

我不明白为什么遭受了如此之大的苦难?我会理解您是否需要在C ++代码中创建诸如可选或插件Python模块之类的东西,这些东西可以卸载.但是,您尝试做的事情看起来太单调乏味.

如果这是您的目标,我将提供替代方法:

创建一个C ++ DLL,它像以前一样静态链接您的Python DLL.将所有Python API包装在C ++类中,最好包装在一个C ++类中.在某个头文件中声明此类,并使用所链接的Python API在DLL中实现.创建一个用作包装类(类)的类工厂的函数.导出此功能.

在使用Python API的C ++代码中,使用LoadLibraryGetProcAddress,在声明包装器的位置包含相同的头文件,加载包装器DLL,加载包装器的类工厂函数,并在其中使用包装器的实例.您的代码.

—SA
I don''t understand why suffering so much? I would understand if you need to create something like optional or plug-in Python module in C++ code which can be unloadable. What you''re trying to do looks too tedious though.

If this is your goal, I would offer the alternative approach:

Create a C++ DLL which links your Python DLL statically as you did it before. Wrap all the Python API in C++ classes, preferably in one single C++ class. Declare this class in some header file and implement in the DLL using Python API you linked. Create a single function which serves as a class factory for your wrapper class (classes). Export this function.

In you C++ code using Python API, use LoadLibrary and GetProcAddress, include the same header file where you wrapper is declared, load your wrapper DLL, load your wrapper''s class factory function and use the instance of the wrapper in your code.

—SA


这可能很有趣:
http://www.boost.org/doc/libs/1_46_1/libs/python/doc / [^ ]

您可以使用/delayload:python23.dll链接器选项指定DLL来延迟加载.如果您不打算使用自己的帮助函数版本,则还必须将程序与Delayimp.lib链接.

最好的问候
Espen Harlinn
This might be of interest:
http://www.boost.org/doc/libs/1_46_1/libs/python/doc/[^]

You can specify DLLs to delay load with the /delayload:python23.dll linker option. If you do not plan to use your own version of a helper function, you must also link your program with Delayimp.lib.

Best regards
Espen Harlinn


这篇关于动态将python23.dll链接到C ++应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆