• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            <2006年10月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            統(tǒng)計(jì)

            • 隨筆 - 44
            • 文章 - 0
            • 評(píng)論 - 86
            • 引用 - 0

            常用鏈接

            留言簿(6)

            隨筆分類(31)

            隨筆檔案(44)

            Mining

            最新隨筆

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            使用 python 寫 COM
            Python 支持Com調(diào)用(client com) 以及撰寫COM 組件(server com).
            1. com 調(diào)用示例(使用Windows Media Player 播放音樂)
            from win32com.client import Dispatch
            mp 
            = Dispatch("WMPlayer.OCX")
            tune 
            = mp.newMedia("C:/WINDOWS/system32/oobe/images/title.wma")
            mp.currentPlaylist.appendItem(tune)
            mp.controls.play()

            2. com server 的編寫
            主要可以參考 <<Python Programming on Win32 之 Chapter 12 Advanced Python and COM http://oreilly.com/catalog/pythonwin32/chapter/ch12.html >>
            示例(分割字符串)
            - 代碼
            class PythonUtilities:
                _public_methods_ 
            = [ 'SplitString' ]
                _reg_progid_ 
            = "PythonDemos.Utilities"
                
            # NEVER copy the following ID 
                # Use "print pythoncom.CreateGuid()" to make a new one.
                _reg_clsid_ = "{41E24E95-D45A-11D2-852C-204C4F4F5020}"
                
                
            def SplitString(self, val, item=None):
                    
            import string
                    
            if item != None: item = str(item)
                    
            return string.split(str(val), item)
             
            # Add code so that when this script is run by
            #
             Python.exe, it self-registers.
            if __name__=='__main__':
                
            print "Registering COM server"
                
            import win32com.server.register
                win32com.server.register.UseCommandLine(PythonUtilities)

            - 注冊(cè)/注銷Com

            Command-Line Option

            Description

             

            The default is to register the COM objects.

            --unregister

            Unregisters the objects. This removes all references to the objects from the Windows registry.

            --debug

            Registers the COM servers in debug mode. We discuss debugging COM servers later in this chapter.

            --quiet

            Register (or unregister) the object quietly (i.e., don't report success).


            - 使用COM
            可以在python 命令行下運(yùn)行
            >>> import win32com.client
            >>> s = win32com.client.Dispatch("PythonDemos.Utilities")
            >>> s.SplitString("a,b,c"",")
            ((u
            'a', u'a,b,c'),)
            >>>

            3. python server com 原理
            其實(shí)在注冊(cè)表中查找到python com 的實(shí)現(xiàn)內(nèi)幕
            Windows Registry Editor Version 5.00

            [HKEY_CLASSES_ROOT\CLSID\{41E24E95-D45A-11D2-852C-204C4F4F5020}]
            @
            ="PythonDemos.Utilities"

            [HKEY_CLASSES_ROOT\CLSID\{41E24E95-D45A-11D2-852C-204C4F4F5020}\Debugging]
            @
            ="0"

            [HKEY_CLASSES_ROOT\CLSID\{41E24E95-D45A-11D2-852C-204C4F4F5020}\Implemented Categories]

            [HKEY_CLASSES_ROOT\CLSID\{41E24E95-D45A-11D2-852C-204C4F4F5020}\Implemented Categories\{B3EF80D0-68E2-11D0-A689-00C04FD658FF}]

            [HKEY_CLASSES_ROOT\CLSID\{41E24E95-D45A-11D2-852C-204C4F4F5020}\InprocServer32]
            @
            ="pythoncom25.dll"
            "ThreadingModel"="both"

            [HKEY_CLASSES_ROOT\CLSID\{41E24E95-D45A-11D2-852C-204C4F4F5020}\LocalServer32]
            @
            ="D:\\usr\\Python\\pythonw.exe \"D:\\usr\\Python\\lib\\site-packages\\win32com\\server\\localserver.py\" {41E24E95-D45A-11D2-852C-204C4F4F5020}"

            [HKEY_CLASSES_ROOT\CLSID\{41E24E95-D45A-11D2-852C-204C4F4F5020}\ProgID]
            @
            ="PythonDemos.Utilities"

            [HKEY_CLASSES_ROOT\CLSID\{41E24E95-D45A-11D2-852C-204C4F4F5020}\PythonCOM]
            @
            ="PythonDemos.PythonUtilities"

            [HKEY_CLASSES_ROOT\CLSID\{41E24E95-D45A-11D2-852C-204C4F4F5020}\PythonCOMPath]
            @
            ="D:\\"

            inproc server 是通過pythoncom25.dll 實(shí)現(xiàn)
            local server 通過localserver.py 實(shí)現(xiàn)
            com 對(duì)應(yīng)的python 源文件信息在 PythonCOMPath & PythonCOM

            4. 使用問題
            用PHP 或者 c 調(diào)用com 的時(shí)候
            <?php
            $com = new COM("PythonDemos.Utilities");
            $rs = $com->SplitString("a b c");
            foreach($rs as $r)
                
            echo $r."\n";
            ?>
            會(huì)碰到下面的一些錯(cuò)誤.
            pythoncom error: PythonCOM Server - The 'win32com.server.policy' module could not be loaded.
            <type 'exceptions.ImportError'>: No module named server.policy pythoncom error: CPyFactory::CreateInstance failed to create instance. (80004005)


            可以通過2種方式解決:
            a. 設(shè)置環(huán)境 PYTHONHOME = D:\usr\Python
            另外在c ++ 使用python 的時(shí)候, 如果import module 出現(xiàn)錯(cuò)誤 'import site' failed; use -v for traceback 的話, 也可以通過設(shè)置這個(gè)變量解決.

            b. 為com 生產(chǎn)exe, dll 可執(zhí)行文件, setup.py 代碼如下 :
            from distutils.core import setup 
            import py2exe 

            import sys 
            import shutil 

            # Remove the build tree ALWAYS do that! 
            shutil.rmtree("build", ignore_errors=True) 

            # List of modules to exclude from the executable 
            excludes = ["pywin""pywin.debugger""pywin.debugger.dbgcon""pywin.dialogs""pywin.dialogs.list"

            # List of modules to include in the executable 
            includes = ["win32com.server"

            # ModuleFinder can't handle runtime changes to __path__, but win32com uses them 
            try
                
            # if this doesn't work, try import modulefinder 
                import py2exe.mf as modulefinder 
                
            import win32com 
                
                
            for p in win32com.__path__[1:]: 
                    modulefinder.AddPackagePath(
            "win32com", p) 
                
                
            for extra in ["win32com.shell""win32com.server"]: #,"win32com.mapi" 
                    __import__(extra) 
                    m 
            = sys.modules[extra] 
                    
            for p in m.__path__[1:]: 
                        modulefinder.AddPackagePath(extra, p) 

            except ImportError: 
                
            # no build path setup, no worries. 
                pass 

            # Set up py2exe with all the options 
            setup( 
                options 
            = {"py2exe": {"compressed"2
                                      
            "optimize"2
                                      
            #"bundle_files": 1, 
                                      "dist_dir""COMDist"
                                      
            "excludes": excludes, 
                                      
            "includes": includes}}, 
                
            # The lib directory contains everything except the executables and the python dll. 
                # Can include a subdirectory name. 
                zipfile = None, 
                com_server 
            = ['PythonDemos'], # 文件名!!
                ) 


            ref:
            http://oreilly.com/catalog/pythonwin32/chapter/ch12.html 
            http://blog.donews.com/limodou/archive/2005/09/02/537571.aspx 

            posted on 2008-08-14 17:02 泡泡牛 閱讀(6114) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Python

            国产午夜精品久久久久九九| 77777亚洲午夜久久多喷| 久久er国产精品免费观看2| 999久久久免费国产精品播放| 色99久久久久高潮综合影院| 久久国产精品成人影院| 国内精品久久久久久久影视麻豆| 老司机午夜网站国内精品久久久久久久久| 久久久久久综合网天天| segui久久国产精品| 亚洲午夜久久久久妓女影院| 国产精品一区二区久久精品无码 | 精品一久久香蕉国产线看播放| 亚洲美日韩Av中文字幕无码久久久妻妇| 久久亚洲AV成人出白浆无码国产 | 久久久久久狠狠丁香| 狠狠色丁香久久婷婷综合图片| 久久超碰97人人做人人爱| 精品乱码久久久久久夜夜嗨| 日韩久久久久久中文人妻| 理论片午午伦夜理片久久| 国产精品激情综合久久| 777米奇久久最新地址| 久久精品国产AV一区二区三区| 久久久久国色AV免费看图片| 青青草国产精品久久| 国产精品久久久久影院嫩草| 亚洲精品乱码久久久久久| 久久精品免费一区二区| 蜜桃麻豆WWW久久囤产精品| 一本久久免费视频| 亚洲一级Av无码毛片久久精品| 精品久久久久久无码免费| 久久香蕉国产线看观看99| 久久精品国产一区| 国产一级做a爰片久久毛片| 99久久成人国产精品免费| 久久国产亚洲精品麻豆| 99久久精品费精品国产| 国产午夜福利精品久久| 久久艹国产|