diff --git a/CWeChatRobot/WeChatRobotCOM.idl b/CWeChatRobot/WeChatRobotCOM.idl index 9e87daa2c88ab711165c19239c7d58b2200d2125..53fffdfda6b2f4b70d5d4393cb3a95ee876980bb 100644 --- a/CWeChatRobot/WeChatRobotCOM.idl +++ b/CWeChatRobot/WeChatRobotCOM.idl @@ -69,7 +69,7 @@ interface IWeChatRobot : IDispatch interface IRobotEvent : IDispatch { [id(1), helpstring("用于微信主动推送消息")] HRESULT CPostMessage([in] DWORD pid, [in] int msgtype, [in] VARIANT* msg, [out, retval] int* __result); - [id(2), helpstring("用于微信主动推送消息")] HRESULT CRegisterWxPidWithCookie([in] DWORD pid, [in] DWORD cookie, [out, retval] int* __result); + [id(2), helpstring("用于客户端通过微信PID注册消息接收服务")] HRESULT CRegisterWxPidWithCookie([in] DWORD pid, [in] DWORD cookie, [out, retval] int* __result); }; [ uuid(721abb35-141a-4aa2-94f2-762e2833fa6c), diff --git a/CWeChatRobot/pch.cpp b/CWeChatRobot/pch.cpp index afc314a57b1dae3c7bf901fa1c60335de7faba36..e6bd2b86d27ab680a9b6ef2cd77d143cca2337f9 100644 --- a/CWeChatRobot/pch.cpp +++ b/CWeChatRobot/pch.cpp @@ -2,6 +2,14 @@ #include "pch.h" +/* + * 保证RobotService可以重复调用及停止,类似于可重入锁,用于支持多个客户端同时调用 + * StopRobotService的次数必须不小于StartRobotService才可以停止服务,当服务已经停止后 + * 可以重复调用StopRobotService,但只需一次StartRobotService即可将计数置0并开启服务 + */ +map ServiceCount; + + // 当使用预编译的头时,需要使用此源文件,编译才能成功。 BOOL isFileExists_stat(string& name) { @@ -54,18 +62,37 @@ DWORD GetWeChatPid() { return wxPid; } -DWORD StartRobotService(DWORD pid) { - wstring wworkPath = GetComWorkPath(); - wchar_t* workPath = (wchar_t*)wworkPath.c_str(); - bool status = Inject(pid, workPath); - return status; +DWORD StartRobotService(DWORD pid) +{ + if (ServiceCount[pid] < 0) + { + ServiceCount[pid] = 0; + } + if (ServiceCount[pid] == 0) + { + wstring wworkPath = GetComWorkPath(); + wchar_t* workPath = (wchar_t*)wworkPath.c_str(); + bool status = Inject(pid, workPath); + if (!status) + { + ++ServiceCount[pid]; + } + return status; + } + ++ServiceCount[pid]; + return 0; } -DWORD StopRobotService(DWORD pid) { +DWORD StopRobotService(DWORD pid) +{ DWORD cpid = GetCurrentProcessId(); if (pid == 0) return cpid; - RemoveDll(pid); + --ServiceCount[pid]; + if (ServiceCount[pid] == 0) + { + RemoveDll(pid); + } return 0; } diff --git a/CWeChatRobot/pch.h b/CWeChatRobot/pch.h index 7472bbd3302199c61c7e4e957cf9fc27069aa8aa..47d48137d9543cc693559a5be3cf7fa6ca1fded3 100644 --- a/CWeChatRobot/pch.h +++ b/CWeChatRobot/pch.h @@ -15,13 +15,14 @@ #include "stdlib.h" #include #include -#include +#include #include #include #include #include #include #include +#include #include #pragma comment(lib, "comsuppw.lib") diff --git a/Release/CWeChatRobot.exe b/Release/CWeChatRobot.exe index 2da3559ee3663849474f1906a9a5063af690e9ed..298a46c13e64a610907a057e0a4b251ac9e98024 100644 Binary files a/Release/CWeChatRobot.exe and b/Release/CWeChatRobot.exe differ