473,732 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_AddMod ule.

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::find Root(DOUBLE* root)
{
std::string progress;
PyObject * main, * globals, * res, * func;

try {

progress = "calling PyInitialize";
if(!Py_IsInitia lized()) Py_Initialize() ;

progress = "get __main__ module";
main = PyImport_AddMod ule("__main__") ;

progress = "get __main__module dictionary";
globals = PyModule_GetDic t(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_GetItemS tring(globals, "func");

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

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

} catch(...) {
// SetFailString just sets the ISupportErrorIn fo interface
SetFailString(I ID_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('minSe rver.MinEmbed')
s.script = 'def func(x) : return x*x'
s.findRoot()

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<COMObject minServer.MinEm bed>", line 2, in findRoot
File "i:\1111\Python 24\lib\site-packages\win32c om\client\dynam ic.py",
line 251, in _ApplyTypes_
result = self._oleobj_.I nvokeTypes(*(di spid, 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 1173

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

Similar topics

3
6415
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, there is quite little on-line documentation and examples available about PythonCOM. The only good sources I have found were the sample chapter from Mark Hammond's book and some decades old PowerPoint show. I have a following problem:
0
1843
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 can't seem to create the child. debugging = 1 if debugging: from win32com.server.dispatcher import DefaultDebugDispatcher useDispatcher = DefaultDebugDispatcher else: useDispatcher = None class Parent:
3
1968
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 strings, and None. To isolate the problem I have taken out the code that builds the matrix from real data, and just substituted a literal tuple of tuples, like this: class AtlasSecurity:
8
1825
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 loaded extensions and can be embedded in other programs. This package contains the header files and libraries needed to do these types of tasks. ------------------------------------------------------
8
4600
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 from embedded devices). Currently, we are using a heavyweight, proprietary log tool developed by another part of the company. This tool contains all "standard" logging functionality, but we also need to insert "debug" log points in the software of...
1
3080
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 found this article very good and it clarified for me what needs doing. Now I have an example application that almost works, but it is far from reliable. If I run this several times I get crashes telling me that the heap is modified after deallocation....
9
2305
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
3140
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 pythoncom, win32pdhutil, winerror import math import numpy import sys
1
4811
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 last): File "C:\Documents and Settings\kusumap\Desktop\Girish.py", line 7, in ? worksheet.OLEObjects.Add(Filename="C:\Documents and Settings
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9235
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.