用python开发window dll,并用在web中调用dll

闲来没事,自己做了个办公用的OA插件(单位用的OA是桌面版的安装程序),做这个的起因是公司有个网站,上面有个OA的连接图标,某领导突发奇想:如果点击这个图标直接把自己的OA调出来就好了。所以我这个东西应用而生,  ,语言组织能力有限,看不明白的地方多多保函。
标签: PythonWin  py2exe  Inno Setup

代码片段(7)

[文件] winUtils.py ~ 4KB    下载(7)

001 # -*- coding: cp936 -*-
002 import win32api
003 import win32con
004 import win32gui
005 import os
006  
007 debugmode = False#True
008  
009 def getSystemHome():
010     #pth = win32api.GetSystemDirectory()
011     pth = win32api.GetWindowsDirectory()
012     if len(pth)>0:
013         return pth[0]
014     return None
015  
016 def _windowEnumerationHandler(hwnd, resultList):
017     resultList.append((hwnd,win32gui.GetWindowText(hwnd),win32gui.GetClassName(hwnd)))
018 #==================================================
019 #2949560 OfficeIM 网络办公 Tfm_main <type 'str'>
020 #983308 OfficeIM 网络办公 TApplication <type 'str'>
021 #66226 OfficeIM 网络办公 Tfm_login <type 'str'>
022 #==================================================
023  
024 def listOfficeIM():
025     theID = None;
026     topWindows = []
027     win32gui.EnumWindows(_windowEnumerationHandler, topWindows)
028     for hwnd, windowText, windowClass in topWindows:
029         if debugmode:
030             print hwnd,windowText,windowClass,type(windowClass)
031         if windowClass !='CabinetWClass':
032             title = str(windowText)
033             if "OfficeIM" in title and "网络" in title and "办公" in title:
034                 if debugmode:
035                    print hwnd,windowText,windowClass,type(windowClass)
036                 if windowClass == 'Tfm_login':
037                    theID = hwnd
038                    break
039     return theID
040  
041  
042 #============================================
043 # win7
044 #C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OfficeIM网络智能办公客户端
045 #C:\Program Files\OfficeIM网络智能办公客户端\OfficeIM.exe
046 # xp
047 #C:\Documents and Settings\All Users\「开始」菜单\程序\OfficeIM 网络办公客户端\OfficeIM网络智能办公客户端.lnk
048 #C:\Program Files\OfficeIM网络智能办公客户端\OfficeIM.exe
049 #=============================================
050 def runWhereOfficeIM():
051     _findfile = None
052     _sysroot = getSystemHome()
053     xpdir = _sysroot+":/Documents and Settings/All Users/「开始」菜单/程序"
054     if os.path.isdir(xpdir):
055         files = os.listdir(xpdir)
056         for in files:
057             if "OfficeIM" in and "网络" in and "办公" in f:
058                 _dir = os.path.join(xpdir,f)
059                 if os.path.isdir(_dir):
060                     _files = os.listdir(_dir)
061                     for _f in _files:
062                         _truefile = os.path.join(_dir,_f)
063                         if debugmode:
064                             print _truefile
065                         if "OfficeIM" in _f and "网络" in _f and "办公" in _f and".lnk" in _f:
066                             _findfile = _truefile
067                             break
068     if _findfile is not None:
069         win32api.ShellExecute(0,None,_findfile,None,None,1)
070         return 1
071  
072     win7dir = _sysroot+":\ProgramData\Microsoft\Windows\Start Menu\Programs"
073     if os.path.isdir(win7dir):
074         files = os.listdir(win7dir)
075         for in files:
076             if "OfficeIM" in and "网络" in and "办公" in f:
077                 _dir = os.path.join(win7dir,f)
078                 if os.path.isdir(_dir):
079                     _files = os.listdir(_dir)
080                     for _f in _files:
081                         _truefile = os.path.join(_dir,_f)
082                         if debugmode:
083                             print _truefile
084                         if "OfficeIM" in _f and "网络" in _f and "办公" in _f and".lnk" in _f:
085                             _findfile = _truefile
086                             break
087     if _findfile is not None:
088         win32api.ShellExecute(0,None,_findfile,None,None,1)
089         return 1
090  
091     return 0
092  
093 def runOfficeIM():
094     _ret = 2
095     theID = listOfficeIM()
096     if theID is not None:
097         try:
098             #win32gui.SetForegroundWindow(theID)
099             #win32gui.SetActiveWindow(theID)
100             win32gui.ShowWindow(theID,1)
101             _ret = 2
102         except Exception,e:
103             if debugmode:
104                 print e
105             _ret = 3
106     else:
107         try:
108             _ret = runWhereOfficeIM()
109         except Exception,e:
110             if debugmode:
111                 print e
112             _ret = 4
113     return _ret
114  
115 if __name__ == '__main__':
116     #getSystemHome()
117     #listOfficeIM()
118     runOfficeIM()

[文件] implug.py ~ 458B    下载(6)

01 # -*- coding: cp936 -*-
02 from winUtils import *
03  
04 class implug:
05     _public_methods_ = ["detectOfficeIM"]
06     #_public_attrs_ = ["filename","host","user","password"]
07     _reg_progid_ = "EOS.IMPLUG"
08     _reg_clsid_ = "{A47E8D38-6202-4AA5-A9A0-259F3386E669}"
09  
10     def detectOfficeIM(self):
11         = runOfficeIM()
12         return r
13  
14 if __name__=='__main__':
15     import win32com.server.register
16     win32com.server.register.UseCommandLine(implug)

[文件] setup.py ~ 728B    下载(8)

01 from distutils.core import setup
02 import py2exe
03 import sys
04  
05  
06 excludes = ["pywin""pywin.debugger""pywin.debugger.dbgcon",
07             "pywin.dialogs""pywin.dialogs.list"#, "win32com.client"]
08  
09 options = {
10     "bundle_files"1,
11     "ascii"1# to make a smaller executable, don't include the encodings
12     "compressed"1# compress the library archive
13     "excludes": excludes, # COM stuff we don't want
14     "includes""encodings,encodings.*"
15     }
16  
17 setup(
18     options = {"py2exe": options},
19     zipfile = "implug.zip"# append zip-archive to the executable.
20     com_server = ["implug"],
21     data_files=["install.bat","uninstall.bat"],
22     #console=["KLRFOA.py"]
23     )
24  
25 #python setup.py py2exe

[代码] 一些说明

1 winUtils.py implug.py 这个2个文件是主文件
2 setup.py 文件是把python文件编译成exe,dll的工具文件
3 用 setup.py 文件生成dll后需要在文件夹里放置2个dll的注册和销毁文件
4 1. install.bat  文件内容如下:作用大家都明白.
5    regsvr32 implug.dll
6 2. uninstall.bat  文件内容如下:作用大家都明白.
7    regsvr32 /u implug.dll

[图片] implug.gif

用python开发window dll,并用在web中调用dll_第1张图片

[代码] Inno setup

01 #用Inno setup 生成exe安装包
02  
03 [Setup]
04 AppName=OA插件
05 AppVerName=OA插件1.0
06 DefaultDirName={pf}\implug
07 DefaultGroupName=OA插件
08 OutputBaseFilename=OA插件
09 Compression=lzma
10 SolidCompression=yes
11 ;AlwaysRestart=yes
12 ;UninstallRestartComputer=yes
13  
14 [Languages]
15 ;Name: "mytrans"; MessagesFile: "compiler:mytrans.isl"
16  
17 ;[Tasks]
18 ;Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
19 ;Name: "quicklaunchicon"; Description: "{cm:CreateQuickLaunchIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
20  
21 [Files]
22

你可能感兴趣的:(python)