Dynamically linking python into my vc project - help required

2011-08-03 Thread mrinalini

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


--
http://mail.python.org/mailman/listinfo/python-list


Help required accessing dictionary

2011-08-31 Thread mrinalini

Hi

I need to access the dictionary of the script that I am running through
my vc++ application by embedding python.
I am linking to python dynamically. I want to obtain the dictionary of
the script and access the variables declared in the script.
However, with the PyObject * that I get from the dictionary, I am not
able to find the type of the object. The reason being that
GetProcAddress to PyInt_Check returns a NULL. The same thing with
PyFloat_Check and so on. I think this is because they are macros and 
not

exported functions.

What can be done to be able to perform these checks without statically
linking to the pyhon lib ?

Thanks,
Abhaya
--
http://mail.python.org/mailman/listinfo/python-list


Executing .pyc using python c api

2011-11-29 Thread Mrinalini Kulkarni

Hi

I need to run .pyc files using python c api. if i do PyImport_Import it 
executes the script. However, i need to pass a set of variables and 
their values which will be accessed from within the script. How can this 
be done.


thanks,

--
http://mail.python.org/mailman/listinfo/python-list


help - obtaining the type of the object using tp_name

2011-12-25 Thread Mrinalini Kulkarni

Hello,

I have embedded python into a vc++ app. I need to obtain type of the 
variable defined in the python script, in my c++ code.


PyObject *pMyObject; -> assume points to a variable defined in the 
python script


Now I want to do something like this

const char * typeName;

typeName = pMyObject->ob_type->tp_name

Is this possible ? I tried doing this but it crashes.

What is the alternative way for getting the name of the type from 
python. Is there a function available for this purpose such that I can 
obtain the address of that function using GetProcAddress and then use it 
for determining the type. (I am required to link to python dynamically)


thanks,
MK
--
http://mail.python.org/mailman/listinfo/python-list