Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10791

Dynamically linking python into my vc project - help required

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <mrinalini@edss.co.in>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:p 40': 0.04; 'advance,': 0.07; 'char': 0.07; 'definitions': 0.07; 'statically': 0.07; 'python': 0.08; 'dynamically': 0.09; 'null)': 0.09; 'null);': 0.09; 'null,': 0.09; 'null;': 0.09; 'subject:python': 0.12; 'dword': 0.16; 'embed': 0.16; 'pointers': 0.16; 'pyobject': 0.16; 'pyobject*': 0.16; 'python.h': 0.16; 'subject:required': 0.16; 'help.': 0.19; 'subject:help': 0.22; 'trying': 0.23; 'code': 0.24; 'function': 0.26; 'load': 0.26; 'tried': 0.27; 'script': 0.29; 'compile': 0.29; 'fine.': 0.29; 'skip:( 20': 0.30; 'example': 0.30; 'fails.': 0.30; 'version': 0.30; 'thanks': 0.31; 'unable': 0.31; 'hi,': 0.31; 'copying': 0.31; 'skip:" 20': 0.33; 'done': 0.33; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'there': 0.34; 'subject:project': 0.35; 'installed': 0.35; 'skip:" 10': 0.36; 'idea': 0.36; 'facing': 0.37; 'some': 0.37; 'but': 0.37; 'could': 0.37; 'using': 0.37; 'else': 0.38; 'problems': 0.38; 'two': 0.38; 'run': 0.39; 'returned': 0.39; 'to:addr:python.org': 0.39; 'header': 0.40; 'target': 0.61; 'from:no real name:2**0': 0.62; 'link': 0.63; 'here': 0.66; 'complies': 0.91
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Date Wed, 03 Aug 2011 14:28:48 +0530
From mrinalini@edss.co.in
To <python-list@python.org>
Subject Dynamically linking python into my vc project - help required
X-Sender mrinalini@edss.co.in
User-Agent Roundcube Webmail/0.5.1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1824.1312361929.1164.python-list@python.org> (permalink)
Lines 100
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1312361929 news.xs4all.nl 23888 [2001:888:2000:d::a6]:52122
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10791

Show key headers only | View raw


 Hi,

 I am trying to embed python into my MFC application. I have done this 
 before by statically linking to the python lib. But I want to change 
 this now.
 The idea is to take the information from the registry for the installed 
 version of python on the target machine. Then load python using 
 loadlibrary call and use the functions from python using pointers to 
 functions returned by GetProcAddress .

 For example -

        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(...)
 	{

 	}

 And then I want to execute a python script through my MFC application  
 -

         HANDLE hFile = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, 
 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

 	DWORD dwSize = GetFileSize(hFile, NULL);
 	DWORD dwRead;

 	char *s = new char[dwSize +1];

 	ReadFile(hFile, s, dwSize, &dwRead, NULL);

 	s[dwSize] = '\0';

 	CString wholefile(s);

 	wholefile.Remove('\r');
 	wholefile+="\n";
 	CloseHandle(hFile);

 	pFnPy_CompileString *pFPy_CompileString = (pFnPy_CompileString 
 *)::GetProcAddress(hModPython, "Py_CompileString");

 	CString fl(file);

 	PyObject* pCodeObject = pFPy_CompileString(wholefile.GetBuffer(0), 
 fl.GetBuffer(0), Py_file_input);

 	if (pCodeObject != NULL)
 	{
 		pFnPyEval_EvalCode *pFPyEval_EvalCode = (pFnPyEval_EvalCode 
 *)::GetProcAddress(hModPython, "PyEval_EvalCode");

 		PyObject* pObject = pFPyEval_EvalCode((PyCodeObject*)pCodeObject, 
 m_Dictionary, m_Dictionary);
 	}


 I am facing two problems here , though I want to link to python 
 dynamically I am required to include 	python.h for my code to compile 
 the following declaration.

 PyObject* pCodeObject



 I tried copying some of the python definitions including  PyObject into 
 a header in my mfc app. Then it complies fine. but Py_CompileString call 
 fails. so finally I am unable to run script from my MFC application by 
 linking to python dynamically.

 How can this be done ? Please help. Is there a different approach to 
 linking to python dynamically. Please could you write to me ?

 Thanks in advance,
 Mrinalini

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Dynamically linking python into my vc project - help required mrinalini@edss.co.in - 2011-08-03 14:28 +0530

csiph-web