Novice question: Implementing COM Objects with Python

David Bolen db3l at fitlinxx.com
Tue Jan 28 18:07:28 EST 2003


rolfedh at hotmail.com (Rolfe) writes:

> I'm trying the code (see further down) from Mark Hammonds Python
> Programming on Win32, page 68, and get the following error:
> "Traceback (most recent call last):
>   File "C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
> line 301, in RunScript
>     exec codeObject in __main__.__dict__
>   File "C:\Python22\examples\SimpleCOMServer.py", line 4, in ?
>     class PythonUtilities:
>   File "C:\Python22\examples\SimpleCOMServer.py", line 8, in
> PythonUtilities
>     print pythoncom.CreateGuid()
> NameError: name 'pythoncom' is not defined
> >>> 
> "
> 
> What am I doing wrong? 

You need to have an "import pythoncom" somewhere in your code.  In the
example in the book the suggested use of pythoncom.CreateGuid() to
generate a new GUID is in comments and thus isn't executed.

However, from your code:

> Code:
> "
> ##SimpleCOMServer.py - a sample com server
> ##
> ##we expose a single method in a python com object
> class PythonUtilities:
>     _public_methods_ = [ 'SplitString' ]
>     _reg_progid_ = "PythonDemos.Utilities"
>     #NEVER copy the following ID
>     print pythoncom.CreateGuid()
>     #_reg_clsid_ = print.pythoncom.CreateGuid()
(...)

it sounds like you didn't quite understand the comment.  The function
CreateGuid() in the pythoncom module can be used to return a new GUID
in the form appropriate for use as the class id for an object (which
is what you assign to _reg_clsid_.  But you only need to call it once,
since each execution will assign a new unique value.

This is covered on page 71 in the discussion of Globally Unique
Identifiers.

So the suggested comment is to print out the result from the function
(presumably in the interactive interpreter) and then copy that value
into your code.

For example, working from an interactive interpreter:

    Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pythoncom
    >>> print pythoncom.CreateGuid()
    <iid:{E37EB70A-7F46-4A0F-A3EA-1765E461A42F}>
    >>> print pythoncom.CreateGuid()
    <iid:{69F8A5C6-BC59-4BF6-990B-766CC44BFF23}>
    >>> 

Notice how the second call is a completely different value.  Just use
this each time you need a new object identifier (but then always use
that same value for that object), and cut 'n paste the portion within
the braces into your object source in lieu of the number given in the
example.

Of course, if you're just entering in the example to experiment with
it's perfectly ok to stick with the value in the book, since that
value was unique when it was generated for the book, and thus uniquely
represents the actual object from the book (not that the risk of
collision is high for such an example anyway, but GUIDs are cheap to
regenerate) :-)

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/




More information about the Python-list mailing list