473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

embedding Python in COM server loaded with win32com

Hi,

I have a problem which is quite circular, and hopefully either someone
has encountered something similar or has a reason why this will not
work.

We have a COM library providing mathematics to various systems, most
functions are hard-coded but we want to embed a scripting language to
allow arbitrary functions to be used in the numeric engines within the
library, and am using Python for this.

This seems to work fine called from standalone apps, and from VB,
however, Python scripts, which access the scripts via win32com.client
fail in the embedding code in C++ whenever I attempt to call
PyImport_AddModule.

As a concrete example, consider the following minimal interface,
(created using an ATL project in VC7), which has a single property,
the user supplied script, and a single function 'findRoot', which in
this case is nothing more than an indicator that the embedding worked,

-------------------------------------------------------------
STDMETHODIMP CMinEmbed::get_script(BSTR* pVal)
{
USES_CONVERSION;
*pVal = SysAllocString(A2OLE(__script.c_str()));
return S_OK;
}
STDMETHODIMP CMinEmbed::put_script(BSTR newVal)
{
USES_CONVERSION;
__script = std::string( OLE2A( newVal));
return S_OK;
}
STDMETHODIMP CMinEmbed::findRoot(DOUBLE* root)
{
std::string progress;
PyObject * main, * globals, * res, * func;

try {

progress = "calling PyInitialize";
if(!Py_IsInitialized()) Py_Initialize();

progress = "get __main__ module";
main = PyImport_AddModule("__main__");

progress = "get __main__module dictionary";
globals = PyModule_GetDict(main);

progress = "Run the script.";
res = PyRun_String(__script.c_str(), Py_file_input, globals,
globals);

progress = "Get the function from main dictionary.";
func = PyDict_GetItemString(globals, "func");

progress = "test function, and return indicator";
if(NULL != func && PyCallable_Check(func)) {
*root = 1.0;
} else {
*root = -1.0;
}

progress = "clean up";
Py_XDECREF(res);
Py_Finalize();
return S_OK;

} catch(...) {
// SetFailString just sets the ISupportErrorInfo interface
SetFailString(IID_IMinEmbed, progress.c_str());
return E_FAIL;
}
}
-------------------------------------------------------------
When I build my server with the above method and run it at the Python
interpretor I get,
from win32com.client import Dispatch
s = Dispatch('minServer.MinEmbed')
s.script = 'def func(x) : return x*x'
s.findRoot()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<COMObject minServer.MinEmbed>", line 2, in findRoot
File "i:\1111\Python24\lib\site-packages\win32com\client\dynamic.py",
line 251, in _ApplyTypes_
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType,
argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None,
'Failure to get main module', None, 0, -2147467259), None)

However, works fine from VB and standalone apps.

Is this approach even doable?
Thanks in advance
Dave Foster

Jun 5 '06 #1
0 1157

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mikko Ohtamaa | last post by:
Hi, I am quite new to Python, PythonCom and COM generally. As a former Java programmer, I have found Python's flexible ability to access native Win32, especially COM, very comfortable. However,...
0
by: Dave | last post by:
I have been trying an example from the Python Programming on Win32 book on the lastest versions of python (2.3.3) and win32all (build 163). I create the COM object and try to call it from VB but i...
3
by: Paul Keating | last post by:
I have a very simple COM server written in Python that is trying to return a two-dimensional array 268 x 20. The values it contains are some small integers, some short (<29 character) Unicode...
8
by: Thomas Bartkus | last post by:
Name: lib64python2.4-devel Summary: The libraries and header files needed for Python development Description: The Python programming language's interpreter can be extended with dynamically...
8
by: Joakim Persson | last post by:
Hello all. I am involved in a project where we have a desire to improve our software testing tools, and I'm in charge of looking for solutions regarding the logging of our software (originating...
1
by: freesteel | last post by:
I have posted about this problem before. SInce then I found a much better article to help with embedding python in a multithreaded application: http://www.linuxjournal.com/article/3641 I...
9
by: Larry Bates | last post by:
Does anyone know if there is a way to make a Python COM object act like a proper iterator in VB/Delphi? Example: Python COM object class foo: _public_methods_=
3
by: vml | last post by:
Hello, I am really new in python scipy win32com and scipy I tried to setup a COM server to interact with vb 6 the pythom COM server is : from win32com.server import exception, register import...
1
by: Girish | last post by:
Hi, I want to embed a txt document into an excel using python. Here is my code, but i get an error message =================================================== Traceback (most recent call...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.