提交 98fcc768 编写于 作者: A amchii

微信消息添加根据唯一id去重功能

上级 f494d14a
......@@ -8,17 +8,20 @@
// CRobotEvent
STDMETHODIMP CRobotEvent::CPostMessage(DWORD pid,int msgtype,VARIANT* msg, int* __result)
STDMETHODIMP CRobotEvent::CPostMessage(DWORD pid, int msgtype, unsigned long long msgid, VARIANT *msg, int *__result)
{
// TODO: 在此处添加实现代码
// 将收到的消息广播给所有用户
switch (msgtype) {
case WX_MESSAGE: {
Fire_OnGetMessageEvent(pid, msg);
switch (msgtype)
{
case WX_MESSAGE:
{
Fire_OnGetMessageEvent(pid, msgid, msg);
break;
}
case WX_LOG_MESSAGE:{
case WX_LOG_MESSAGE:
{
break;
}
default:
......@@ -32,7 +35,7 @@ STDMETHODIMP CRobotEvent::CPostMessage(DWORD pid,int msgtype,VARIANT* msg, int*
* \param pid 微信PID
* \param cookie Cookie
*/
STDMETHODIMP CRobotEvent::CRegisterWxPidWithCookie(DWORD pid, DWORD cookie, int* __result)
STDMETHODIMP CRobotEvent::CRegisterWxPidWithCookie(DWORD pid, DWORD cookie, int *__result)
{
WxPidToEventCookie[pid].insert(cookie);
*__result = 0;
......
// RobotEvent.h: CRobotEvent 的声明
#pragma once
#include "resource.h" // 主符号
#include "resource.h" // 主符号
#include "WeChatRobotCOM_i.h"
#include "_IRobotEventEvents_CP.h"
using namespace ATL;
// CRobotEvent
class ATL_NO_VTABLE CRobotEvent :
public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CRobotEvent, &CLSID_RobotEvent>,
public IConnectionPointContainerImpl<CRobotEvent>,
public CProxy_IRobotEventEvents<CRobotEvent>,
public IDispatchImpl<IRobotEvent, &IID_IRobotEvent, &LIBID_WeChatRobotCOMLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
class ATL_NO_VTABLE CRobotEvent : public CComObjectRootEx<CComMultiThreadModel>,
public CComCoClass<CRobotEvent, &CLSID_RobotEvent>,
public IConnectionPointContainerImpl<CRobotEvent>,
public CProxy_IRobotEventEvents<CRobotEvent>,
public IDispatchImpl<IRobotEvent, &IID_IRobotEvent, &LIBID_WeChatRobotCOMLib, /*wMajor =*/1, /*wMinor =*/0>
{
public:
CRobotEvent()
{
}
DECLARE_REGISTRY_RESOURCEID(107)
DECLARE_CLASSFACTORY_SINGLETON(CRobotEvent)
CRobotEvent()
{
}
BEGIN_COM_MAP(CRobotEvent)
COM_INTERFACE_ENTRY(IRobotEvent)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IConnectionPointContainer)
END_COM_MAP()
DECLARE_REGISTRY_RESOURCEID(107)
DECLARE_CLASSFACTORY_SINGLETON(CRobotEvent)
BEGIN_CONNECTION_POINT_MAP(CRobotEvent)
CONNECTION_POINT_ENTRY(__uuidof(_IRobotEventEvents))
END_CONNECTION_POINT_MAP()
BEGIN_COM_MAP(CRobotEvent)
COM_INTERFACE_ENTRY(IRobotEvent)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IConnectionPointContainer)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(CRobotEvent)
CONNECTION_POINT_ENTRY(__uuidof(_IRobotEventEvents))
END_CONNECTION_POINT_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
void FinalRelease()
{
}
public:
STDMETHOD(CPostMessage)(DWORD pid, int msgtype, VARIANT* msg, int* __result);
STDMETHOD(CRegisterWxPidWithCookie)(DWORD pid, DWORD cookie, int* __result);
STDMETHOD(CPostMessage)
(DWORD pid, int msgtype, unsigned long long msgid, VARIANT *msg, int *__result);
STDMETHOD(CRegisterWxPidWithCookie)
(DWORD pid, DWORD cookie, int *__result);
};
OBJECT_ENTRY_AUTO(__uuidof(RobotEvent), CRobotEvent)
......@@ -68,7 +68,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(1), helpstring("用于微信主动推送消息")] HRESULT CPostMessage([in] DWORD pid, [in] int msgtype,[in] unsigned long long msgid, [in] VARIANT* msg, [out, retval] int* __result);
[id(2), helpstring("用于客户端通过微信PID注册消息接收服务")] HRESULT CRegisterWxPidWithCookie([in] DWORD pid, [in] DWORD cookie, [out, retval] int* __result);
};
[
......
......@@ -245,6 +245,7 @@
<ClInclude Include="targetver.h" />
<ClInclude Include="SearchContact.h" />
<ClInclude Include="templatefunc.h" />
<ClInclude Include="utils.h" />
<ClInclude Include="VerifyFriendApply.h" />
<ClInclude Include="WeChatRobot.h" />
<ClInclude Include="WeChatRobotCOM_i.h" />
......
......@@ -238,6 +238,9 @@
<ClInclude Include="templatefunc.h">
<Filter>template</Filter>
</ClInclude>
<ClInclude Include="utils.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="WeChatRobotCOM.cpp">
......
......@@ -5,4 +5,7 @@
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>D:\VS2019C++\MyWeChatRobot\Debug\</LocalDebuggerWorkingDirectory>
</PropertyGroup>
<PropertyGroup>
<ShowAllFiles>false</ShowAllFiles>
</PropertyGroup>
</Project>
\ No newline at end of file
此差异已折叠。
此差异已折叠。
#pragma once
#include <map>
#include <set>
#include "utils.h"
/**
* 微信PID与客户端事件ConnectionPoint的cookie的映射,客户端使用自己关心的微信PID和cookie进行注册,
......@@ -8,33 +9,37 @@
*/
map<DWORD, set<DWORD>> WxPidToEventCookie;
template<class T>
class CProxy_IRobotEventEvents :
public ATL::IConnectionPointImpl<T, &__uuidof(_IRobotEventEvents)>
template <class T>
class CProxy_IRobotEventEvents : public ATL::IConnectionPointImpl<T, &__uuidof(_IRobotEventEvents)>
{
public:
HRESULT Fire_OnGetMessageEvent(DWORD pid, VARIANT* msg)
HRESULT Fire_OnGetMessageEvent(DWORD pid, unsigned long long msgid, VARIANT *msg)
{
HRESULT hr = S_OK;
T* pThis = static_cast<T*>(this);
if (WxPidToEventCookie.count(pid)==0)
static ExpireSet es(2000);
if (!es.CheckIfDuplicatedAndAdd(msgid))
{
return hr;
}
T *pThis = static_cast<T *>(this);
if (WxPidToEventCookie.count(pid) == 0)
{
return hr;
}
const set<DWORD> cookies = WxPidToEventCookie[pid];
for (DWORD cookie:cookies)
for (DWORD cookie : cookies)
{
pThis->Lock();
ATL::CComPtr<IUnknown> punkConnection=this->m_vec.GetUnknown(cookie);
ATL::CComPtr<IUnknown> punkConnection = this->m_vec.GetUnknown(cookie);
pThis->Unlock();
if (punkConnection)
{
IDispatch* pConnection = static_cast<IDispatch*>(punkConnection.p);
IDispatch *pConnection = static_cast<IDispatch *>(punkConnection.p);
if (pConnection)
{
ATL::CComVariant varResult;
DISPPARAMS params = { msg, nullptr, 1, 0 };
DISPPARAMS params = {msg, nullptr, 1, 0};
hr = pConnection->Invoke(1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &params, &varResult, nullptr, nullptr);
}
else
......
......@@ -200,7 +200,7 @@ static void dealMessage(DWORD messageAddr)
VARIANT vsaValue;
vsaValue.vt = VT_ARRAY | VT_VARIANT;
V_ARRAY(&vsaValue) = psaValue;
PostComMessage(message->pid, WX_MESSAGE, &vsaValue);
PostComMessage(message->pid, WX_MESSAGE, message->msgid, &vsaValue);
#endif
HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendSocketMessage, message, NULL, 0);
if (hThread)
......@@ -210,10 +210,10 @@ static void dealMessage(DWORD messageAddr)
}
/*
* 消息处理函数,根据消息缓冲区组装结构并存入容器
* messageAddr:保存消息的缓冲区地址
* return:void
*/
* 消息处理函数,根据消息缓冲区组装结构并存入容器
* messageAddr:保存消息的缓冲区地址
* return:void
*/
VOID ReceiveMessage(DWORD messagesAddr)
{
// 此处用于区别是发送的还是接收的消息
......@@ -225,8 +225,8 @@ VOID ReceiveMessage(DWORD messagesAddr)
}
/*
* HOOK的具体实现,接收到消息后调用处理函数
*/
* HOOK的具体实现,接收到消息后调用处理函数
*/
_declspec(naked) void dealReceiveMessage()
{
__asm {
......@@ -244,8 +244,8 @@ _declspec(naked) void dealReceiveMessage()
}
/*
* HOOK的具体实现,发送消息后调用处理函数
*/
* HOOK的具体实现,发送消息后调用处理函数
*/
_declspec(naked) void dealSendMessage()
{
__asm {
......@@ -262,9 +262,9 @@ _declspec(naked) void dealSendMessage()
}
/*
* 开始接收消息HOOK
* return:void
*/
* 开始接收消息HOOK
* return:void
*/
VOID HookReceiveMessage(int port)
{
SRVPORT = port;
......@@ -283,9 +283,9 @@ VOID HookReceiveMessage(int port)
}
/*
* 停止接收消息HOOK
* return:void
*/
* 停止接收消息HOOK
* return:void
*/
VOID UnHookReceiveMessage()
{
SRVPORT = 0;
......
......@@ -10,21 +10,20 @@
// CComModule m_commodule;
class CSink :
public CComObjectRoot,
public _IRobotEventEvents
class CSink : public CComObjectRoot,
public _IRobotEventEvents
{
BEGIN_COM_MAP(CSink)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(_IRobotEventEvents)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(_IRobotEventEvents)
END_COM_MAP()
public:
virtual ~CSink() {}
STDMETHODIMP GetTypeInfoCount(UINT* pctinfo) { return E_NOTIMPL; }
STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo) { return E_NOTIMPL; }
STDMETHODIMP GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId) { return E_NOTIMPL; }
STDMETHODIMP Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
STDMETHODIMP GetTypeInfoCount(UINT *pctinfo) { return E_NOTIMPL; }
STDMETHODIMP GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo) { return E_NOTIMPL; }
STDMETHODIMP GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) { return E_NOTIMPL; }
STDMETHODIMP Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
#ifdef _DEBUG
printf("sink, id: %d, parm: %f", dispIdMember, pDispParams->rgvarg[0].fltVal);
......@@ -33,16 +32,18 @@ public:
}
};
BOOL PostComMessage(DWORD pid, int msgtype, VARIANT* msg) {
BOOL PostComMessage(DWORD pid, int msgtype, unsigned long long msgid, VARIANT *msg)
{
HRESULT hr = S_OK;
hr = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
return false;
CComPtr<IRobotEvent> spRobotEvent;
hr = ::CoCreateInstance(CLSID_RobotEvent, NULL, CLSCTX_ALL, IID_IRobotEvent, (LPVOID*)&spRobotEvent);
if (SUCCEEDED(hr)) {
hr = ::CoCreateInstance(CLSID_RobotEvent, NULL, CLSCTX_ALL, IID_IRobotEvent, (LPVOID *)&spRobotEvent);
if (SUCCEEDED(hr))
{
/*
// 这段逻辑用于回调到微信
CComObject<CSink>* sinkptr = nullptr;
......@@ -51,12 +52,13 @@ BOOL PostComMessage(DWORD pid, int msgtype, VARIANT* msg) {
AtlAdvise(spRobotEvent, sinkptr, __uuidof(_IRobotEventEvents), &cookies);
*/
int __result = 0;
spRobotEvent->CPostMessage(pid, msgtype, msg, &__result);
spRobotEvent->CPostMessage(pid, msgtype, msgid, msg, &__result);
}
else {
else
{
return false;
}
CoUninitialize();
return true;
}
#endif // !USE_COM
\ No newline at end of file
#endif // !USE_COM
#pragma once
#include<windows.h>
#include <windows.h>
#define USE_COM
#include<comutil.h>
#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")
#define WX_MESSAGE 1
#define WX_LOG_MESSAGE 2
BOOL PostComMessage(DWORD pid, int msgtype,VARIANT* msg);
\ No newline at end of file
BOOL PostComMessage(DWORD pid, int msgtype, unsigned long long msgid, VARIANT *msg);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册