问题遇到的现象和发生背景
我自己用易语言 写了一个 雷神模拟器的 dll 文件, 然后需要在python 中调用, python 中的调用函数如下图
#!/usr/bin/env python3
# -*- coding: GBK -*-
' Ls Console module '
import sys, os
from ctypes import *
os.add_dll_directory(sys.path[0])
ls = CDLL('ls.dll')
def AddText(disk0, disk1):
# 添加dll的搜索路径为当前目录
os.add_dll_directory(sys.path[0])
# 创建dll对象
ls = CDLL('ls.dll')
# 易语言传参是文本型,这里需要编码为utf-8
disk0 = c_char_p(disk0.encode('GBK'))
disk1 = c_char_p(disk1.encode('GBK'))
# 返回值是文本型
info = bytes.decode(string_at(ls.AddText(disk0, disk1)))
return info
# 系统_以管理员身份运行
def AdminRun():
ls.AdminRun()
# 窗口自动排列 (窗口句柄可分成数组的字符串,分割符(|),横间距,纵间距,每行多少,对齐方式,新宽度,新高度)
def ArrayWindow():
ls.ArrayWindow(ArrWindow, RowDistance, ColDistance, HowMuchRow, DressMode, NewWidth, NewHight)
# 关闭模拟器
def CloseEmulator(Index):
ls.CloseEmulator(Index)
# 复制模拟器
def CopyEmulator(sNewName, Index):
ls.CopyEmulator(sNewName, Index)
# 新建模拟器
def CreateEmulator(sEmulatorName):
ls.CreateEmulator(sEmulatorName)
```python
不知道图片能不能上传成功, 写了一小段代码请过目,这个模块在其它python 程序中调用时,要是不注册调用 大漠插件可以运行,注册大漠,就会报错.
* 问题相关代码,请勿粘贴截图
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/667491059946152.png "#left")
下面是调用代码
from ctypes import WinDLL
from win32com.client import Dispatch
from ctypes import *
import Const
import sys, os
import lsConsole
G_sGlobalPath = os.getcwd()
print(G_sGlobalPath)
# 大漠免注册 dll 导入 赋值给 dll
os.add_dll_directory(sys.path[0])
# dll = WinDLL(G_sGlobalPath + '\DmReg.dll')
dll = CDLL('DmReg.dll')
dll.SetDllPathW(G_sGlobalPath + '\dm.dll', 0)
dm = Dispatch('dm.dmsoft')
dm_Ver = dm.Ver()
print(dm_Ver)
lsConsole.LaunchEmulator(1)
运行结果及报错内容
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/457123159946133.png "#left")
D:\Users\Administrator\AppData\Local\Programs\Python\Python39-32\python.exe F:/张瀚泽/Study/Python/dm/ScriptTest.py
中文函数可以吗
D:\Users\Administrator\AppData\Local\Programs\Python\Python39-32
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\win32com\client\dynamic.py", line 86, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221005, '无效的类字符串', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\张瀚泽\Study\Python\dm\ScriptTest.py", line 15, in <module>
dm = Dispatch('dm.dmsoft')
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\win32com\client\__init__.py", line 117, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\win32com\client\dynamic.py", line 106, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\win32com\client\dynamic.py", line 88, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(
pywintypes.com_error: (-2147221005, '无效的类字符串', None, None)
我的解答思路和尝试过的方法
如果把这个导入模块这种代码放到大漠注册调用之后,不会报错,就可以运行
图片地址
http://m.qpic.cn/psc?/V1493Few2zrddP/Sc7wZG8Q0BUeMz.O80ebfCc3gbg36LOgSgVEGBdtA6mFcknFn9zHvjathOTTGQMRk1RJEmE9EBYr.OJ0ywuSHZPtHhwabXrJD6ss1wJL8mg!/b&bo=*QQPBAAAAAADF8A!&rf=viewer_4?%ra=link
```python
from ctypes import WinDLL
from win32com.client import Dispatch
from ctypes import *
import Const
import sys, os
G_sGlobalPath = os.getcwd()
print(G_sGlobalPath)
# 大漠免注册 dll 导入 赋值给 dll
os.add_dll_directory(sys.path[0])
# dll = WinDLL(G_sGlobalPath + '\DmReg.dll')
dll = CDLL('DmReg.dll')
dll.SetDllPathW(G_sGlobalPath + '\dm.dll', 0)
dm = Dispatch('dm.dmsoft')
dm_Ver = dm.Ver()
print(dm_Ver)
import lsConsole
lsConsole.LaunchEmulator(1)
还有就是把python 调用的模块代码前两句全部放到函数中,也可以运行 ,但是我觉得应该有一次调用,全部函数共用的方法,但是我不会,请大家支援.
就像是这样
#!/usr/bin/env python3
# -*- coding: GBK -*-
' Ls Console module '
import sys, os
from ctypes import *
def TracePrint():
# 添加dll的搜索路径为当前目录
os.add_dll_directory(sys.path[0])
# 创建dll对象
ls = CDLL('ls.dll')
# 易语言传参是文本型,这里需要编码为GBK
info = bytes.decode(string_at(ls.TracePrint()), encoding="GBK")
return info
def AddText(disk0, disk1):
# 添加dll的搜索路径为当前目录
os.add_dll_directory(sys.path[0])
# 创建dll对象
ls = CDLL('ls.dll')
# 易语言传参是文本型,这里需要编码为utf-8
disk0 = c_char_p(disk0.encode('GBK'))
disk1 = c_char_p(disk1.encode('GBK'))
# 返回值是文本型
info = bytes.decode(string_at(ls.AddText(disk0, disk1)))
return info
# 系统_以管理员身份运行
def AdminRun():
os.add_dll_directory(sys.path[0])
ls = CDLL('ls.dll')
ls.AdminRun()
# 窗口自动排列 (窗口句柄可分成数组的字符串,分割符(|),横间距,纵间距,每行多少,对齐方式,新宽度,新高度)
def ArrayWindow(ArrWindow, RowDistance, ColDistance, HowMuchRow, DressMode, NewWidth, NewHight):
os.add_dll_directory(sys.path[0])
ls = CDLL('ls.dll')
ls.ArrayWindow(ArrWindow, RowDistance, ColDistance, HowMuchRow, DressMode, NewWidth, NewHight)
# 关闭模拟器
def CloseEmulator(Index):
os.add_dll_directory(sys.path[0])
ls = CDLL('ls.dll')
ls.CloseEmulator(Index)
# 复制模拟器
def CopyEmulator(sNewName, Index):
os.add_dll_directory(sys.path[0])
ls = CDLL('ls.dll')
ls.CopyEmulator(sNewName, Index)
# 新建模拟器
def CreateEmulator(sEmulatorName):
os.add_dll_directory(sys.path[0])
ls = CDLL('ls.dll')
ls.CreateEmulator(sEmulatorName)
# 删除模拟器
def DeleteEmulator(Index):
os.add_dll_directory(sys.path[0])
ls = CDLL('ls.dll')
ls.DeleteEmulator(Index)
我想要达到的结果
就是一次导入模块就可以后面全部函数调用, 不用每个函数都调用, 或是我第一种写法,怎么样放到前面也不会报错,因为后面要用到多线程,其它线程调用大漠时避不开这个问题,所以后调用这个方法行不通.