C#调用Demo
<p><code>1.回调函数是在线程中执行的,如果要操作UI, 请自行处理在线程中操作UI</code></p>
<pre><code>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WxHookDemo
{
public partial class FormMain : Form
{
public delegate void AcceptCallbackFunc(uint dwClientId);
public delegate void RecvCallbackFunc(uint dwClientId, IntPtr intPtr, uint dwSize);
public delegate void CloseCallbackFunc(uint dwClientId);
private static AcceptCallbackFunc m_AcceptCallbackFunc;
private static RecvCallbackFunc m_RecvCallbackFunc;
private static CloseCallbackFunc m_CloseCallbackFunc;
// 引入DLL导出函数
[DllImport(&quot;WxLoader.dll&quot;)]
public static extern bool InitWeChatSocket(AcceptCallbackFunc acceptCallback,
RecvCallbackFunc recvCallback, CloseCallbackFunc closeCallback);
[DllImport(&quot;WxLoader.dll&quot;)]
public static extern uint InjectWeChat(String strDllPath);
[DllImport(&quot;WxLoader.dll&quot;)]
public static extern bool SendWeChatData(uint dwClienId, String strJsonData);
[DllImport(&quot;WxLoader.dll&quot;)]
public static extern bool DestroyWeChat();
// 建一个主窗口的实例,让静态方法可以访问
public static FormMain Instance = null;
// 存一个clientId,用于发送消息 (多客户端自己来维护clientId)
public static uint WxClientId = 0;
// 注入成功回调
static void WxAcceptCallback(uint dwClientId)
{
FormMain.WxClientId = dwClientId;
FormMain.Instance.textBoxLog.BeginInvoke(new Action(() =&gt; { FormMain.Instance.textBoxLog.AppendText(&quot;Accept\n&quot;); }));
}
// 接收消息回调
static void WxRecvCallback(uint dwClient, IntPtr intPtr, uint dwSize)
{
String recvData = Marshal.PtrToStringAnsi(intPtr);
FormMain.Instance.Invoke(new Action&lt;String&gt;(FormMain.Instance.textBoxLog.AppendText), recvData &quot;\n&quot;);
}
// 关闭回调
static void WxCloseCallback(uint dwClient)
{
FormMain.Instance.Invoke(new Action&lt;String&gt;(FormMain.Instance.textBoxLog.AppendText), &quot;Close\n&quot;);
}
public FormMain()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
private void buttonOpen_Click(object sender, EventArgs e)
{
// 注入并多开
String dllPath = System.IO.Directory.GetCurrentDirectory() &quot;\\WeChatHelper.dll&quot;;
InjectWeChat(&quot;WeChatHelper.dll&quot;);
}
private void buttonSend_Click(object sender, EventArgs e)
{
// 构造json消息,可使用相关Json库
String strMsg = &quot;{\&quot;type\&quot;: 11036, \&quot;data\&quot;:{\&quot;to_wxid\&quot;:\&quot;&quot;
textBoxWxid.Text &quot;\&quot;, \&quot;content\&quot;:\&quot;&quot; textBoxMsg.Text &quot;\&quot;}}&quot;;
SendWeChatData(WxClientId, strMsg);
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
// 释放Socket并卸载注入的dll
DestroyWeChat();
}
private void FormMain_Load(object sender, EventArgs e)
{
Instance = this;
m_AcceptCallbackFunc = WxAcceptCallback;
m_RecvCallbackFunc = WxRecvCallback;
m_CloseCallbackFunc = WxCloseCallback;
// 初始化Callback
InitWeChatSocket(m_AcceptCallbackFunc, m_RecvCallbackFunc, m_CloseCallbackFunc);
}
private void button1_Click(object sender, EventArgs e)
{
// 构造json消息,可使用相关Json库
String strMsg = &quot;{\&quot;type\&quot;: 11030}&quot;;
SendWeChatData(WxClientId, strMsg);
}
}
}
</code></pre>