-
Notifications
You must be signed in to change notification settings - Fork 251
python regfree
qiannian edited this page Jun 13, 2026
·
1 revision
通过 tools.dll 的 setupW 加载 op_x86.dll 或 op_x64.dll,不需要使用 regsvr32 注册插件。
示例假设脚本旁边有如下目录结构:
demo.py
op/
x86/
tools.dll
op_x86.dll
x64/
tools.dll
op_x64.dll
tools.dll 由项目的 tools/ 工程生成。Python、tools.dll、op_x86.dll/op_x64.dll 的位数必须一致。
import ctypes
from pathlib import Path
from win32com.client import Dispatch
def setup_op_regfree(plugin_root):
# 根据当前 Python 进程位数选择 x86 或 x64
is_x64 = ctypes.sizeof(ctypes.c_void_p) == 8
arch = "x64" if is_x64 else "x86"
op_name = "op_x64.dll" if is_x64 else "op_x86.dll"
runtime_dir = Path(plugin_root) / arch
tool_dll = runtime_dir / "tools.dll"
op_dll = runtime_dir / op_name
# 检查免注册工具和 op 插件是否存在
if not tool_dll.exists():
raise FileNotFoundError(f"找不到免注册工具: {tool_dll}")
if not op_dll.exists():
raise FileNotFoundError(f"找不到 op 插件: {op_dll}")
# setupW 是 tools.dll 导出的 cdecl 函数,中文路径推荐使用 setupW
tool = ctypes.CDLL(str(tool_dll))
tool.setupW.argtypes = [ctypes.c_wchar_p]
tool.setupW.restype = ctypes.c_int
# setupW 必须在 Dispatch("op.opsoft") 之前调用
ret = tool.setupW(str(op_dll))
if ret != 1:
raise RuntimeError(f"免注册加载失败: {op_dll}")
# 返回 tool,保留 DLL 句柄在当前进程中有效
return tool
# 当前脚本旁边的 op 目录,也可以改成自己的插件释放目录
base_dir = Path(__file__).resolve().parent
tool_handle = setup_op_regfree(base_dir / "op")
# setupW 成功后,就可以按普通 COM 方式创建对象
op = Dispatch("op.opsoft")
# 输出版本号,验证免注册调用成功
print("op version:", op.Ver())-
setupW只在当前进程内生效,不会写入系统注册表。 -
setupW需要传入op_x86.dll或op_x64.dll的完整路径或可解析路径。 - 如果是 64 位 Python,请使用
x64/tools.dll和x64/op_x64.dll。 - 如果是 32 位 Python,请使用
x86/tools.dll和x86/op_x86.dll。