Create Activex With Python

http://blog.csdn.net/xugangjava/article/details/8450346

Python 开发 ActiveX (comtypes+py2exe)
分类: C++ Python 2012-12-29 15:09 323人阅读 评论(0) 收藏 举报
转载注明出处
http://blog.csdn.net/xugangjava/article/details/8450346
工程目录如下:

1.首先编写idl文件
[cpp] view plaincopy
import "oaidl.idl";
import "ocidl.idl";
[
uuid(80DDC35E-320E-4f9c-979F-522DDCD34FD3),
dual,
oleautomation
]

interface IXGUtilitiesInterface : IDispatch {
HRESULT Hello([in] INT a, [in] INT b, [out, retval] INT *presult);
}
[
uuid(78FBDE61-B979-47cf-B3EC-15F7814E58B1)
]

library XGUtilitiesLib
{
importlib("stdole2.tlb");
[uuid(ED2A47D0-D7CC-4edd-8C17-61361EB1B851)]
coclass XGUtilitiesObject {
[default] interface IXGUtilitiesInterface;
};
};

2.编译idl文件
打开vs 命令行工具cmd,midl XGUtilities.idl /tlb XGUtilities.tlb 在同目录下面生成tlb文件
我的插件是 XGUtilities.tlb
3.编写com实现
[python] view plaincopy

  1. -*- coding: gbk -*-

import comtypes
import comtypes.server.localserver
from comtypes.client import GetModule
#每次改动时需要执行一次,打包时无需执行
#GetModule("XGUtilities.tlb")
from comtypes.gen.XGUtilitiesLib import XGUtilitiesObject
class XGUtilitiesObjectImpl(XGUtilitiesObject):
_reg_threading_ = "Both"
_reg_progid_ = "XGUtilitiesLib.XGUtilitiesObject.1"
_reg_novers_progid_ = "XGUtilitiesLib.XGUtilitiesObject"
_reg_desc_ = "许刚工具集"
_reg_clsctx_ = comtypes.CLSCTX_INPROC_SERVER | comtypes.CLSCTX_LOCAL_SERVER
_regcls_ = comtypes.server.localserver.REGCLS_MULTIPLEUSE

def Hello(self,a, b):
return a + b

if name == 'main':
from comtypes.server.register import UseCommandLine
UseCommandLine(XGUtilitiesObjectImpl)

4.编写py2exe打包
[python] view plaincopy

  1. -*- coding: gbk -*-

from distutils.core import setup
import py2exe
import sys

manifest = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<noInheritable></noInheritable>
<assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
<file name="msvcr90.dll" hashalg="SHA1" hash="9785b1c493deb5b2134dc4aef3719cee207001bc"><asmv2:hash xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><dsig:Transforms><dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity"></dsig:Transform></dsig:Transforms><dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></dsig:DigestMethod><dsig:DigestValue>VF5ECUAHPV7EnUf+/UIXMPizPvs=</dsig:DigestValue></asmv2:hash></file> <file name="msvcp90.dll" hashalg="SHA1" hash="0f6bbf7fe4fb3fca2cb5b542eca1a1cad051f01c"><asmv2:hash xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><dsig:Transforms><dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity"></dsig:Transform></dsig:Transforms><dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></dsig:DigestMethod><dsig:DigestValue>3Wg+StVMq2uhx7POnAkl2w4dDmY=</dsig:DigestValue></asmv2:hash></file> <file name="msvcm90.dll" hashalg="SHA1" hash="7f3290ab2b7444c2b4a9b1fedfdb16466d7a21bb"><asmv2:hash xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><dsig:Transforms><dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity"></dsig:Transform></dsig:Transforms><dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></dsig:DigestMethod><dsig:DigestValue>/YfRn7UQENzdMeoMHxTgdRMiObA=</dsig:DigestValue></asmv2:hash></file>
</assembly>
"""

class Target:
def init(self, **kw):
self.dict.update(kw)
self.version = "0.0.0"
self.company_name = "http://blog.csdn.net/xugangjava"
self.copyright = "@2012,power by 许刚"
self.name = "http://blog.csdn.net/xugangjava"

my_com_server_target = Target(
description = "许刚工具集",
modules = ["XGUtilities"],
other_resources = [
("TYPELIB", 1,open(r"XGUtilities.tlb", "rb").read()),
(24,1,manifest),
],
create_exe = False
)

setup(
name="许刚工具集",
options={"py2exe": {
"compressed": 2,
"dll_excludes": ["mswsock.dll", "powrprof.dll"],
"includes":["comtypes.gen._78FBDE61_B979_47CF_B3EC_15F7814E58B1_0_0_0"]
}},
zipfile=None,
version="0.0.0",
description="许刚工具集",
author="许刚",
author_email="http://blog.csdn.net/xugangjava",
ctypes_com_server=[my_com_server_target]
)

运行打包 python setup.py py2exe
reg.bat 内容
regsvr32 XGUtilities.dll
运行注册
接下来来测试这个插件
Test.html内容
[html] view plaincopy
<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<SCRIPT LANGUAGE="JavaScript">
window.onload = function () {
var obj = new ActiveXObject("XG.Utilities");

alert(obj.Hello(1,3));
}
</SCRIPT>
</head>
<body>
</body>
</html>
打开html 测试成功

为了验证兼容性 ,插件是否依赖python环境,
在没有安装python的环境中测试是否能注册插件,在2003虚拟机(没有安装python环境)下注册插件后

这里html修改为
[html] view plaincopy
<!DOCTYPE html PUBLIC "-W3CDTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<SCRIPT LANGUAGE="JavaScript">
function Hello() {

var obj = document.getElementById("obj")

alert(obj.Hello(1,3));
}
</SCRIPT>
</head>
<body>
<object id="obj" width="100" height="100" classid="clsid:ED2A47D0-D7CC-4edd-8C17-61361EB1B851" ></object>
<input type="button" value="Hello" onclick="Hello()"/>
</body>
</html>

希望大家通过本文的介绍,可以用python快速开发出自己的ActiveX 插件