[Tutor] creating a non-COM DLL

Bob Gailer ramrom@earthling.net
Sat Nov 16 06:41:02 2002


--=====================_2203899==.ALT
Content-Type: text/plain; charset="us-ascii"; format=flowed


> > >I want to create a simple non-COM dll that I will call from a third 
> party program.
> >
> > How timely. I have the same need. I've wandered around in various
> > documentation, but have yet to fine a sample python program for this 
> purpose.
> >
> > It's my intention to call the dll from MS Visual FoxPro. Why, you ask? I
> > have an intention of converting a large VFP application to Python, with
> > some GUI yet to be determined. I want to convert chunks of VFP code that
> > run behind the scenes to python; eventually VFP will just manage the GUI;
> > then I can redo the GUI.

I've done more research; found help in ActiveState's ActivePython 
distribution documentation and its win32com/servers sample code. I realize 
that this creates a COM object rather than a non-COM dll, but that's OK for 
my purposes. Article

Quick Start to Server Side COM and Python provides a simple program (which 
I've modified), with a good discussion of the various components

:
----------------------------------------------------------------------------------------------
class HelloWorld:
   _reg_clsid_ = '{BEA1AA48-1D0A-4D19-8E98-03C54F195B59}'
   _reg_desc_ = "Python Test COM Server"
   _reg_progid_ = "Python.TestServer"
   _public_methods_ = ['Hello']
   _public_attrs_ = ['softspace', 'noCalls']
   _readonly_attrs_ = ['noCalls']

   def __init__(self):
     self.softspace = 1
     self.noCalls = 0

   def Hello(self, arg1):
     return arg1 * 5

if __name__=='__main__':
   import win32com.server.register
   win32com.server.register.UseCommandLine(HelloWorld)
----------------------------------------------------------------------------------------
To obtain a unique clsid:
 >>> import pythoncom
 >>> print pythoncom.CreateGuid() # different each time
{7CC9F362-486D-11D1-BB48-0000E838A65F}
----------------------------------------------------------------------------------------
In Visual FoxPro:
     o = createobject("Python.TestServer")
     with o
       ?.hello(5) && displays 25
       ?.softspace && displays 1
       .softspace = 2 && updates attribute
     endwith
I discovered that VFP seems to cache the COM object; editing and 
re-registering the Python program has no effect until one closes and 
re-opens VFP. WIFNI Python had a "with" statement. I know its been 
discussed a lot, but I still like it.

In VB:
    set o = createobject("Python.TestServer")

Other languages??

Bob Gailer
170 Forsythe Rd
Nederland CO 80466
303-442-2625 
--=====================_2203899==.ALT
Content-Type: text/html; charset="us-ascii"

<html>
<body>
<blockquote type=cite class=cite cite>&gt; &gt;I want to create a simple
non-COM dll that I will call from a third party program.<br>
&gt;<br>
&gt; How timely. I have the same need. I've wandered around in
various<br>
&gt; documentation, but have yet to fine a sample python program for this
purpose.<br>
&gt;<br>
&gt; It's my intention to call the dll from MS Visual FoxPro. Why, you
ask? I<br>
&gt; have an intention of converting a large VFP application to Python,
with<br>
&gt; some GUI yet to be determined. I want to convert chunks of VFP code
that<br>
&gt; run behind the scenes to python; eventually VFP will just manage the
GUI;<br>
&gt; then I can redo the GUI.</blockquote><br>
I've done more research; found help in ActiveState's ActivePython
distribution documentation and its win32com/servers sample code. I
realize that this creates a COM object rather than a non-COM dll, but
that's OK for my purposes. Article <h1><b>Quick Start to Server Side COM
and Python </b>provides a simple program (which I've modified), with a
good discussion of the various components</h1>:<br>
----------------------------------------------------------------------------------------------<br>
class HelloWorld:<br>
&nbsp; _reg_clsid_ = '{BEA1AA48-1D0A-4D19-8E98-03C54F195B59}'<br>
&nbsp; _reg_desc_ = &quot;Python Test COM Server&quot;<br>
&nbsp; _reg_progid_ = &quot;Python.TestServer&quot;<br>
&nbsp; _public_methods_ = ['Hello']<br>
&nbsp; _public_attrs_ = ['softspace', 'noCalls']<br>
&nbsp; _readonly_attrs_ = ['noCalls']<br>
&nbsp; <br>
&nbsp; def __init__(self):<br>
&nbsp;&nbsp;&nbsp; self.softspace = 1<br>
&nbsp;&nbsp;&nbsp; self.noCalls = 0<br><br>
&nbsp; def Hello(self, arg1):<br>
&nbsp;&nbsp;&nbsp; return arg1 * 5<br><br>
if __name__=='__main__':<br>
&nbsp; import win32com.server.register <br>
&nbsp; win32com.server.register.UseCommandLine(HelloWorld)<br>
----------------------------------------------------------------------------------------<br>
To obtain a unique clsid:<br>
&gt;&gt;&gt; import pythoncom<br>
&gt;&gt;&gt; print pythoncom.CreateGuid() # different each time<br>
{7CC9F362-486D-11D1-BB48-0000E838A65F}<br>
----------------------------------------------------------------------------------------<br>
In Visual FoxPro:<br>
&nbsp;&nbsp;&nbsp; o = createobject(&quot;Python.TestServer&quot;)<br>
&nbsp;&nbsp;&nbsp; with o<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?.hello(5) &amp;&amp; displays 25<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?.softspace &amp;&amp; displays 1<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .softspace = 2 &amp;&amp; updates
attribute<br>
&nbsp;&nbsp;&nbsp; endwith<br>
I discovered that VFP seems to cache the COM object; editing and
re-registering the Python program has no effect until one closes and
re-opens VFP. WIFNI Python had a &quot;with&quot; statement. I know its
been discussed a lot, but I still like it.<br><br>
In VB:<br>
&nbsp;&nbsp; set o = 
createobject(&quot;Python.TestServer&quot;)<br><br>
Other languages??<br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
170 Forsythe Rd<br>
Nederland CO 80466<br>
303-442-2625</body>
</html>

--=====================_2203899==.ALT--