ljc545w / ComWeChatRobot

PC微信机器人,实现获取通讯录,发送文本、图片、文件等消息,封装COM接口供Python、C#调用
1.61k stars 438 forks source link

子线程中怎么调用COM组件,求解答 #152

Closed thelosttimes closed 1 year ago

thelosttimes commented 1 year ago

我确定我已经查看了 (标注[ ][x])


我要申请 (标注[ ][x])

ljc545w commented 1 year ago

先调用CoInitialize

thelosttimes commented 1 year ago

没调用报这个错

import threading

import wxRobot from wxRobot import WeChatRobot

def send_message(): result = robot.SendText("filehelper", "test") print("send success: {}".format(result))

if name == "main": pid_list = wxRobot.get_wechat_pid_list() if len(pid_list) == 0: robot = wxRobot.start_wechat() pid_list.append(robot.pid) else: robot = WeChatRobot(pid_list[0]) try: robot.StartService() robot.ChangeWeChatVer("3.7.6") robot.StartReceiveMessage(0) threading.Thread(target=send_message).start() wxRobot.register_msg_event(pid_list[0]) except Exception as e: print('ex: ', e) robot.StopService()

Exception in thread Thread-1 (send_message): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\desktop\wechat\main.py", line 8, in send_message result = robot.SendText("filehelper", "test") File "C:\Users\Administrator\Desktop\desktop\wechat\wxRobot.py", line 329, in SendText return self.robot.CSendText(self.pid, receiver, msg) _ctypes.COMError: (-2147221008, '尚未调用 CoInitialize。', (None, None, None, 0, None))

thelosttimes commented 1 year ago

调用之后报这个错

import threading

import comtypes

import wxRobot from wxRobot import WeChatRobot

def send_message(): comtypes.CoInitialize() result = robot.SendText("filehelper", "test") print("send success: {}".format(result)) comtypes.CoUninitialize()

if name == "main": pid_list = wxRobot.get_wechat_pid_list() if len(pid_list) == 0: robot = wxRobot.start_wechat() pid_list.append(robot.pid) else: robot = WeChatRobot(pid_list[0]) try: robot.StartService() robot.ChangeWeChatVer("3.7.6") robot.StartReceiveMessage(0) threading.Thread(target=send_message).start() wxRobot.register_msg_event(pid_list[0]) except Exception as e: print('ex: ', e) robot.StopService()

======== RESTART: C:\Users\Administrator\Desktop\desktop\wechat\main.py ======== Exception in thread Thread-1 (send_message): Traceback (most recent call last): File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\Users\Administrator\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run self._target(*self._args, **self._kwargs) File "C:\Users\Administrator\Desktop\desktop\wechat\main.py", line 11, in send_message result = robot.SendText("filehelper", "test") File "C:\Users\Administrator\Desktop\desktop\wechat\wxRobot.py", line 329, in SendText return self.robot.CSendText(self.pid, receiver, msg) _ctypes.COMError: (-2147417842, '应用程序调用一个已为另一线程整理的接口。', (None, None, None, 0, None))

thelosttimes commented 1 year ago

实在是不知道怎么写了

ljc545w commented 1 year ago

报错信息很明显了。main下面写的逻辑挪到线程函数里面去

thelosttimes commented 1 year ago

我想多线程共享robot,如果这样的话,创建线程就没意义了呀

thelosttimes commented 1 year ago

解决了,感谢大佬的解答

tmsdy commented 1 year ago

想了解下你最后用了啥方式,多线程共享robot?

Xuzan9396 commented 1 year ago

@thelosttimes 代码贴下,怎么处理的,我是想在发消息的处理消息的时候异步