[Tutor] doze embedding info

Alan Trautman ATrautman@perryjudds.com
Tue, 30 Jul 2002 11:17:29 -0500


All,

In response to the question about embedding python in Windows I found this
article in my mailbox yesterday and thought it might help answer the
question. It is from builder.com and is on of their columns converted to
plain text. I can't find a URL listed on their website for it so it is
below. If somebody wants the HTML for please email me.

Alan



Extending Python the Windows way 

My favorite feature of Python is how easy it is to extend. If you want to
write C Python extensions, there's a C++ library that makes creating Python
extensions nearly as easy as writing a C++ class. You can also write
extensions in Delphi. You can even use C functions directly from a DLL and
use COM components written in a variety of languages. In this article, I'll
focus on the last two methods. 
Note: I'm assuming that you have Python installed. If you don't, go to
ActiveState's Web site
<http://clickthru.online.com/Click?q=8b-SBGBQY0VljNDiSYEbODU_baFUL9R> and
download the 2.1.1, 2.1.3 or 2.2.1. release. These distributions come
complete with COM support. 
Using C DLLs 
First, you'll want to get CallDLL. You can download the source and compile
it, or if you have ActiveState's Python distribution installed, you can use
PPM to get it. Then do the same for DynWin. 
I've included a simple DLL in this downloadable Zip file
<ftp://ftp.download.com/pub/builder/techmails/SoftDev0725.zip> that contains
sample code. The DLL exports a function called ShowMsg that shows a pop-up
MessageBox. The function has the following signature: 
extern "C" {
__declspec( dllexport ) void MsgBox(const char *msg);
} 
Something to notice about the declaration is the extern "C". The function
names can't be mangled, so you either have to use straight C or declare the
functions extern "C". 
Here's the Python code to use this function: 
from dynwin.windll import *
mod = module('TestDLL')
buf = cstring("test")
mod.MsgBox(buf) 
The key to the Python code is the cstring line. This takes the Python string
and converts it into a pointer to a character buffer. The disadvantage in
extending Python in this manner is that you lose Python's excellent
error-handling capabilities such as exceptions. The advantage of using this
method to extend Python is that other programming languages such as Delphi
can use the extensions; also, it's somewhat cross-platform. 
Using ActiveX controls 
COM is well supported in Python. You can use ActiveX controls and can create
ActiveX controls in Python. 
For this example, we'll make use of Microsoft's XML control. First, open the
PythonWin development environment that ships with ActiveState's Python
distribution. Then from the tools menu, select the Makepy utility. This will
bring up a dialog that lists all of the registered ActiveX controls on the
system. Scroll down until you find the Microsoft XML control. Choose v3.0 if
it's available. What this does is set up all the constants and the Python
wrapper classes, which will make life much easier. (You can skip this step,
but I don't recommend it.) The following code demonstrates using MSXML in
Python: 
from win32com.client import Dispatch
import pythoncom

parser = Dispatch("Microsoft.XMLDOM")
parser.load('license.xml')
children = parser.getElementsByTagName('string')

for child in children:
print child.xml 
This code is very close to what you would do when using MSXML in Visual
Basic. One thing to note is that Python is a case-sensitive language where
Visual Basic is case-insensitive. This can cause some confusion if you don't
use the Makepy utility before using MSXML. 
When you use Makepy, it forces Python to use early binding, which also makes
all of the MSXML methods and properties case-sensitive. If you don't use
Makepy, then Python uses late binding and the MSXML methods and properties
are case-insensitive. 
Are you wondering why I'm advising using Makepy? When you combine a
case-sensitive language with a case-insensitive library, it's just asking
for trouble. It makes spotting errors in your code very difficult. 
If you're targeting Windows only, then extending Python using COM is an
excellent way to go. Unfortunately, it completely ties you to the Windows
platform. The nice thing about using COM is that you get very good language
interoperability. 
So what's my second favorite thing about Python? It runs on even more
platforms than Java! 


Mike Owens, who has been involved in the software industry for over eight
years, is a software engineer with Allscripts Healthcare Solution.