1. COM 组件

有关于Windows COM组件的描述与讨论参考知乎的这个帖子:https://www.zhihu.com/question/49433640 ,大概的总结就是一种已经过时的技术,COM是OO技术的巅峰之作,COM之后的OO都是COM的子集而已

COM的意图在于标准化OO组件的界面,使得

  1. 跨平台
  2. 跨语言
  3. 跨机器
  4. 跨进程

的分布式对象服务可以标准化,而且服务方可以被动态替换。COM的本质是一个分布式OO-RPC规范,如果光是RPC规范,那么COM倒也不稀奇。COM最酷的一点是,允许把本地的RPC转化成In-Process Procedure Call(进程内过程调用)。这点极大地优化了性能,而且使得COM成为了有史以来最强的RPC方案

COM的最核心的思想,说白了就是要做个跨语言的 “class” “object” “function”

2. COM组件注册

既然是RPC远程调用那,那就就有分Server和Clinet而服务端需要注册到Windows系统下才客户端才正常调用,这里有一个使用C#实现的COM组件并让Python 调用的demo

首先生成一个dll COM组件,然后注册到windows 系统内部。

> cd 到 xx.dll文件目录

> gacutil /i xxx.dll 添加dll至全局缓存

> regasmComToPython.dll 注册dll至系统

参考脚本,下面是一个注册ocx COM组件并运行客户端程序的bat文件:

@Echo Off
CLS
@REM
@REM -- Change to the current working directory.
@REM
cd /d %~dp0
CLS
Echo.
Echo.
Echo.
Echo This test application must be run from media that has
Echo read/write capabilities. The test utility will be
Echo copied to hard drive.
Echo.
Echo.
Echo.
Echo.
Echo Please wait while loading the test application .....
Echo.
REM
REM -- Copy the OCX files to the Windows\System32 folder.
REM
If "%PROCESSOR_ARCHITECTURE%"=="x86" (
Copy /V /Y "COMDLG32.OCX" "%WinDir%\System32"
Copy /V /Y "MSCOMCTL.ocx" "%WinDir%\System32"
Copy /V /Y "TABCTL32.ocx" "%WinDir%\System32"
) ELSE (
Copy /V /Y "COMDLG32.OCX" "%WinDir%\SysWOW64"
Copy /V /Y "MSCOMCTL.ocx" "%WinDir%\SysWOW64"
Copy /V /Y "TABCTL32.ocx" "%WinDir%\SysWOW64"
)
REM
REM -- Regisgter the OCX file.
REM
If "%PROCESSOR_ARCHITECTURE%"=="x86" (
REM regsvr32 /S "%WinDir%\System32\COMDLG32.OCX"
regsvr32 /S "%WinDir%\System32\MSCOMCTL.ocx"
regsvr32 /S "%WinDir%\System32\TABCTL32.ocx"
) ELSE (
REM regsvr32 /S "%WinDir%\SysWOW64\COMDLG32.OCX"
regsvr32 /S "%WinDir%\SysWOW64\MSCOMCTL.ocx"
regsvr32 /S "%WinDir%\SysWOW64\TABCTL32.ocx"
)
REM **
REM
REM The following will copy the test utility to hard drive.
REM
REM **
REM
MKDir "C:\Receipt_OPOS"
Copy /V /Y "grid.bmp" "C:\Receipt_OPOS"
Copy /V /Y "hp.bmp" "C:\Receipt_OPOS"
Copy /V /Y "test.bmp" "C:\Receipt_OPOS"
Copy /V /Y "OPOS_Test_Application.exe" "C:\Receipt_OPOS"
Copy /V /Y "HP Receipt Printer OPOS Test Utility.pdf" "C:\Receipt_OPOS"
:TEST_APP
REM **
REM
REM The following will launch the test applete.
REM
REM **
REM
C:
CD "C:\Receipt_OPOS"
start /wait "" "OPOS_Test_Application.exe"
Exit

\3. python 调用 COM 组件:

首先需要安装Pywin32包, 这是python 对win32 api的封装,使得python 能轻松调用到win32接口。

下载地址:https://github.com/mhammond/pywin32/releases, 注意需要版本与python对应否则无法安装。安装先查看文档:

python添加com组件 python编写com组件_RPC

也自带了很好的demo

python添加com组件 python编写com组件_Test_02

python 首先需要实例化并加载COM组件, 然后就是按COM组件提供方的API文档调调用就行了, 组件查找的方法有3种,一种是查看注册表:

方法1:Win键+ R 输入regedit 调出注册表信息,找到对应的key复制下来。

python添加com组件 python编写com组件_python添加com组件_03

就拿OPOS.CashDrawer来看存在多个版本而当前版本是1.14,所以使用的时候可以写OPOS.CashDrawer.1.14或者OPOS.CashDrawer都可以。

方法2:是使用python 安装目录\Lib\site-packages\win32com\client\makepy.py生成文件,可以直接调用。

python添加com组件 python编写com组件_python_04

生成对应的python 文件可以直接调用

方法3: 使用VC自带的OLE View 工具查看

# !/usr/bin/env python
# -*- coding: utf-8 -*-
# 
# @project : CPOS-DF
# @file    : com_demo.py.py
# @time    : 2019/12/30 19:14
# @author  : GuangLin
# @version : 0.01
# @desc    : 


from win32com import client

c = client.Dispatch("OPOS.CashDrawer.1.14")  # 连接组件名称
ret = c.open("HP_CashDrawerPort-1")  # 打开设备
print(ret)
if not ret:
    c.DeviceEnabled = True  # 时能设备
    print(c.DeviceEnabled)
    if c.DeviceEnabled:
        print(c.OpenDrawer())  # 大开钱箱

c.close()