PageRenderTime 61ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Lib/site-packages/win32com/HTML/QuickStartServerCom.html

http://github.com/IronLanguages/main
HTML | 195 lines | 181 code | 14 blank | 0 comment | 0 complexity | 0fbd02cea181792b4d1022bac6e124b5 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. <HTML>
  2. <HEAD>
  3. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
  4. <META NAME="Generator" CONTENT="Microsoft Word 97">
  5. <TITLE>Quick Start to Server Side COM and Python</TITLE>
  6. <META NAME="Version" CONTENT="8.0.3410">
  7. <META NAME="Date" CONTENT="10/11/96">
  8. <META NAME="Template" CONTENT="D:\Program Files\Microsoft Office\Office\html.dot">
  9. </HEAD>
  10. <BODY TEXT="#000000" LINK="#0000ff" VLINK="#800080" BGCOLOR="#ffffff">
  11. <H1>Quick Start to Server side COM and Python</H1>
  12. <H2>Introduction</H2>
  13. <P>This documents how to quickly start implementing COM objects in Python. It is not a thorough discussion of the COM system, or of the concepts introduced by COM.</P>
  14. <P>For more details information on Python and COM, please see the <A HREF="http://www.python.org/windows/win32com/COMTutorial/index.htm">COM Tutorial given by Greg Stein and Mark Hammond at SPAM 6 (HTML format)</A> or download the same tutorial <A HREF="http://www.python.org/windows/win32com/COMTutorial.ppt">in PowerPoint format.</A></P>
  15. <P>For information on using external COM objects from Python, please see <A HREF="QuickStartClientCom.html">a Quick Start to Client side COM and Python</A>.</P>
  16. <P>In this document we discuss the <A HREF="#core">core functionality</A>, <A HREF="#Registering">registering the server</A>, <A HREF="#testing">testing the class</A>, <A HREF="#debugging">debugging the class</A>, <A HREF="#Exception">exception handling</A> and <A HREF="#Policies">server policies</A> (phew!)</P>
  17. <H2><A NAME="core">Implement the core functionality</A></H2>
  18. <H3><A NAME="Using">Implement a stand-alone Python class with your functionality</A></H3>
  19. <CODE><P>class HelloWorld:</P><DIR>
  20. <DIR>
  21. <P>def __init__(self):</P><DIR>
  22. <DIR>
  23. <P>self.softspace = 1</P>
  24. <P>self.noCalls = 0</P></DIR>
  25. </DIR>
  26. <P>def Hello(self, who):</P><DIR>
  27. <DIR>
  28. <P>self.noCalls = self.noCalls + 1</P>
  29. <P># insert "softspace" number of spaces</P>
  30. <P>return "Hello" + " " * self.softspace + who</P></DIR>
  31. </DIR>
  32. </DIR>
  33. </DIR>
  34. </CODE><P>This is obviously a very simple server. In particular, custom error handling would be needed for a production class server. In addition, there are some contrived properties just for demonstration purposes.</P>
  35. <H3>Make Unicode concessions</H3>
  36. <P>At this stage, Python and Unicode dont really work well together. All strings which come from COM will actually be Unicode objects rather than string objects.</P>
  37. <P>To make this code work in a COM environment, the last line of the "Hello" method must become:</P><DIR>
  38. <DIR>
  39. <DIR>
  40. <DIR>
  41. <CODE><P>return "Hello" + " " * self.softspace + str(who)</P></DIR>
  42. </DIR>
  43. </DIR>
  44. </DIR>
  45. </CODE><P>Note the conversion of the "who" to "str(who)". This forces the Unicode object into a native Python string object.</P>
  46. <P>For details on how to debug COM Servers to find this sort of error, please see <A HREF="#debugging">debugging the class</A></P>
  47. <H3>Annotate the class with win32com specific attributes</H3>
  48. <P>This is not a complete list of names, simply a list of properties used by this sample.</P>
  49. <TABLE CELLSPACING=0 BORDER=0 CELLPADDING=7 WIDTH=637>
  50. <TR><TD WIDTH="34%" VALIGN="TOP">
  51. <P><B>Property Name</B></TD>
  52. <TD WIDTH="66%" VALIGN="TOP">
  53. <B><P>Description</B></TD>
  54. </TR>
  55. <TR><TD WIDTH="34%" VALIGN="TOP">
  56. <P>_public_methods_</TD>
  57. <TD WIDTH="66%" VALIGN="TOP">
  58. <P>List of all method names exposed to remote COM clients</TD>
  59. </TR>
  60. <TR><TD WIDTH="34%" VALIGN="TOP">
  61. <P>_public_attrs_</TD>
  62. <TD WIDTH="66%" VALIGN="TOP">
  63. <P>List of all attribute names exposed to remote COM clients</TD>
  64. </TR>
  65. <TR><TD WIDTH="34%" VALIGN="TOP" HEIGHT=5>
  66. <P>_readonly_attrs_</TD>
  67. <TD WIDTH="66%" VALIGN="TOP" HEIGHT=5>
  68. <P>List of all attributes which can be accessed, but not set.</TD>
  69. </TR>
  70. </TABLE>
  71. <P>We change the class header to become:</P>
  72. <CODE><P>class HelloWorld:</P><DIR>
  73. <DIR>
  74. <P>_public_methods_ = ['Hello']</P>
  75. <P>_public_attrs_ = ['softspace', 'noCalls']</P>
  76. <P>_readonly_attrs_ = ['noCalls']</P>
  77. <P>def __init__(self):</P>
  78. <P>[Same from here]</P></DIR>
  79. </DIR>
  80. </CODE><H3><A NAME="Registering">Registering and assigning a CLSID for the object</A></H3>
  81. <P>COM requires that all objects use a unique CLSID and be registered under a "user friendly" name. This documents the process.</P>
  82. <H4>Generating the CLSID</H4>
  83. <P>Microsoft Visual C++ comes with various tools for generating CLSID's, which are quite suitable. Alternatively, the pythoncom module exports the function CreateGuid() to generate these identifiers.</P>
  84. <CODE><P>&gt;&gt;&gt; import pythoncom<BR>
  85. &gt;&gt;&gt; print pythoncom.CreateGuid()<BR>
  86. {7CC9F362-486D-11D1-BB48-0000E838A65F}</P>
  87. </CODE><P>Obviously the GUID that you get will be different than that displayed here.</P>
  88. <H4>Preparing for registration of the Class</H4>
  89. <P>The win32com package allows yet more annotations to be applied to a class, allowing registration to be effected with 2 lines in your source file. The registration annotations used by this sample are:</P>
  90. <TABLE CELLSPACING=0 BORDER=0 CELLPADDING=7 WIDTH=636>
  91. <TR><TD WIDTH="34%" VALIGN="TOP">
  92. <P><B>Property Name</B></TD>
  93. <TD WIDTH="66%" VALIGN="TOP">
  94. <B><P>Description</B></TD>
  95. </TR>
  96. <TR><TD WIDTH="34%" VALIGN="TOP">
  97. <P>_reg_clsid_</TD>
  98. <TD WIDTH="66%" VALIGN="TOP">
  99. <P>The CLSID of the COM object</TD>
  100. </TR>
  101. <TR><TD WIDTH="34%" VALIGN="TOP">
  102. <P>_reg_progid_</TD>
  103. <TD WIDTH="66%" VALIGN="TOP">
  104. <P>The "program ID", or Name, of the COM Server. This is the name the user usually uses to instantiate the object</TD>
  105. </TR>
  106. <TR><TD WIDTH="34%" VALIGN="TOP" HEIGHT=5>
  107. <P>_reg_desc_</TD>
  108. <TD WIDTH="66%" VALIGN="TOP" HEIGHT=5>
  109. <P>Optional: The description of the COM Server. Used primarily for COM browsers. If not specified, the _reg_progid_ is used as the description.</TD>
  110. </TR>
  111. <TR><TD WIDTH="34%" VALIGN="TOP" HEIGHT=5>
  112. <P>_reg_class_spec_</TD>
  113. <TD WIDTH="66%" VALIGN="TOP" HEIGHT=5>
  114. <P>Optional: A string which represents how Python can create the class instance. The string is of format<BR>
  115. [package.subpackage.]module.class</P>
  116. <P>The portion up to the class name must be valid for Python to "import", and the class portion must be a valid attribute in the specified class.</P>
  117. <P>This is optional from build 124 of Pythoncom., and has been removed from this sample.</TD>
  118. </TR>
  119. <TR><TD WIDTH="34%" VALIGN="TOP" HEIGHT=5>
  120. <P>_reg_remove_keys_</TD>
  121. <TD WIDTH="66%" VALIGN="TOP" HEIGHT=5>
  122. <P>Optional: A list of tuples of extra registry keys to be removed when uninstalling the server. Each tuple is of format ("key", root), where key is a string, and root is one of the win32con.HKEY_* constants (this item is optional, defaulting to HKEY_CLASSES_ROOT)</TD>
  123. </TR>
  124. </TABLE>
  125. <P>Note there are quite a few other keys available. Also note that these annotations are <I>not</I> required - they just make registration simple. Helper functions in the module <CODE>win32com.server.register</CODE> allow you to explicitly specify each of these attributes without attaching them to the class.</P>
  126. <P>The header of our class now becomes:</P>
  127. <CODE><P>class HelloWorld:</P><DIR>
  128. <DIR>
  129. <P>_reg_clsid_ = "{7CC9F362-486D-11D1-BB48-0000E838A65F}"</P>
  130. <P>_reg_desc_ = "Python Test COM Server"</P>
  131. <P>_reg_progid_ = "Python.TestServer"</P>
  132. <P>_public_methods_ = ['Hello']</P>
  133. <P>[same from here]</P></DIR>
  134. </DIR>
  135. </CODE><H4>Registering the Class</H4>
  136. <P>The idiom that most Python COM Servers use is that they register themselves when run as a script (ie, when executed from the command line.) Thus the standard "<CODE>if __name__=='__main___':</CODE>" technique works well.</P>
  137. <P>win32com.server.register contains a number of helper functions. The easiest to use is "<CODE>UseCommandLine</CODE>".</P>
  138. <P>Registration becomes as simple as:</P>
  139. <CODE><P>if __name__=='__main__':<BR>
  140. &#9;# ni only for 1.4!<BR>
  141. &#9;import ni, win32com.server.register <BR>
  142. &#9;win32com.server.register.UseCommandLine(HelloWorld)</P>
  143. </CODE><P>Running the script will register our test server.</P>
  144. <H2><A NAME="testing">Testing our Class</A></H2>
  145. <P>For the purposes of this demonstration, we will test the class using Visual Basic. This code should run under any version of Visual Basic, including VBA found in Microsoft Office. Any COM compliant package could be used alternatively. VB has been used just to prove there is no "smoke and mirrors. For information on how to test the server using Python, please see the <A HREF="QuickStartClientCom.html">Quick Start to Client side COM</A> documentation.</P>
  146. <P>This is not a tutorial in VB. The code is just presented! Run it, and it will work!</P>
  147. <H2><A NAME="debugging">Debugging the COM Server</A></H2>
  148. <P>When things go wrong in COM Servers, there is often nowhere useful for the Python traceback to go, even if such a traceback is generated.</P>
  149. <P>Rather than discuss how it works, I will just present the procedure to debug your server:</P>
  150. <B><P>To register a debug version of your class</B>, run the script (as above) but pass in a "<CODE>--debug</CODE>" parameter. Eg, for the server above, use the command line "<CODE>testcomserver.py --debug</CODE>".</P>
  151. <B><P>To see the debug output generated</B> (and any print statements you may choose to add!) you can simply select the "Remote Debug Trace Collector" from the Pythonwin Tools menu, or run the script "win32traceutil.py" from Windows Explorer or a Command Prompt.</P>
  152. <H2><A NAME="Exception">Exception Handling </A></H2>
  153. <P>Servers need to be able to provide exception information to their client. In some cases, it may be a simple return code (such as E_NOTIMPLEMENTED), but often it can contain much richer information, describing the error on detail, and even a help file and topic where more information can be found. </P>
  154. <P>We use Python class based exceptions to provide this information. The COM framework will examine the exception, and look for certain known attributes. These attributes will be copied across to the COM exception, and passed back to the client. </P>
  155. <P>The following attributes are supported, and correspond to the equivalent entry in the COM Exception structure:<BR>
  156. <CODE>scode, code, description, source, helpfile and helpcontext</P>
  157. </CODE><P>To make working with exceptions easier, there is a helper module "win32com.server.exception.py", which defines a single class. An example of its usage would be: </P>
  158. <CODE><P>raise COMException(desc="Must be a string",scode=winerror.E_INVALIDARG,helpfile="myhelp.hlp",...)</CODE> </P>
  159. <P>(Note the <CODE>COMException class supports (and translates) "desc" as a shortcut for "description", but the framework requires "description")</P>
  160. </CODE><H2><A NAME="Policies">Server Policies</A></H2>
  161. <P>This is information about how it all hangs together. The casual COM author need not know this. </P>
  162. <P>Whenever a Python Server needs to be created, the C++ framework first instantiates a "policy" object. This "policy" object is the gatekeeper for the COM Server - it is responsible for creating the underlying Python object that is the server (ie, your object), and also for translating the underlying COM requests for the object. </P>
  163. <P>This policy object handles all of the underlying COM functionality. For example, COM requires all methods and properties to have unique numeric ID's associated with them. The policy object manages the creation of these ID's for the underlying Python methods and attributes. Similarly, when the client whishes to call a method with ID 123, the policy object translates this back to the actual method, and makes the call. </P>
  164. <P>It should be noted that the operation of the "policy" object could be dictated by the Python object - the policy object has many defaults, but the actual Python class can always dictate its operation. </P>
  165. <H3>Default Policy attributes </H3>
  166. <P>The default policy object has a few special attributes that define who the object is exposed to COM. The example above shows the _public_methods attribute, but this section describes all such attributes in detail. </P>
  167. <H5>_public_methods_ </H5>
  168. <P>Required list of strings, containing the names of all methods to be exposed to COM. It is possible this will be enhanced in the future (eg, possibly '*' will be recognised to say all methods, or some other ideas) </P>
  169. <H5>_public_attrs_ </H5>
  170. <P>Optional list of strings containing all attribute names to be exposed, both for reading and writing. The attribute names must be valid instance variables. </P>
  171. <H5>_readonly_attrs_ </H5>
  172. <P>Optional list of strings defining the name of attributes exposed read-only. </P>
  173. <H5>_com_interfaces_ </H5>
  174. <P>Optional list of IIDs exposed by this object. If this attribute is missing, IID_IDispatch is assumed (ie, if not supplied, the COM object will be created as a normal Automation object.</P>
  175. <P>and actual instance attributes: </P>
  176. <P>_dynamic_ : optional method </P>
  177. <P>_value_ : optional attribute </P>
  178. <P>_query_interface_ : optional method </P>
  179. <P>_NewEnum : optional method </P>
  180. <P>_Evaluate : optional method </P></BODY>
  181. </HTML>