程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

C#调用微信接口方法(通过wxDriver.dll)

发布于2021-05-30 20:23     阅读(1323)     评论(0)     点赞(1)     收藏(5)


C#调用微信接口方法(通过wxDriver.dll)

先看 c# api.cs文件,里面定义了相关接口:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Threading.Tasks;
  8. using System.Runtime.InteropServices;
  9. /*
  10. * 获取正试版接口 QQ 150311852
  11. * 电话微信 17756258757 联系时请说明来意
  12. * 接口详细说明:https://www.showdoc.com.cn/WeChatHelper?page_id=6401634409030983
  13. *
  14. */
  15. namespace Api
  16. {
  17. /// <summary>
  18. /// 定义回调委托
  19. /// </summary>
  20. /// <param name="intPtr">消息指针</param>
  21. /// <param name="dwSize">消息长度</param>
  22. public delegate void OnReceiveCallBack(IntPtr intPtr, int dwSize);
  23. public abstract class WxApi
  24. {
  25. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  26. /*
  27. * @Name 初始化
  28. * @Parame callback 回调函数
  29. * @return 返回结果是否成功
  30. */
  31. public static extern bool InitWxDriver(OnReceiveCallBack callback);
  32. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  33. /*
  34. * @Name 打开或多开微信
  35. @return 返回多开端口
  36. */
  37. public static extern int OpenWechat();
  38. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  39. /*
  40. * @Name 获取登录二维码文件名
  41. * @Parame port 端口
  42. * @return 返回生成的二维码文件名路径
  43. */
  44. public static extern IntPtr GetQrCodeFileName(int port);
  45. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  46. public static extern int GetWechatLoginWindowCount();
  47. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  48. /*
  49. * @Name 发送基本信息
  50. * @Parame port 端口
  51. * @Parame wxid 微信ID
  52. * @Parame content 微信ID
  53. * @Parame type 消息类型:
  54. * 1-文本类型
  55. * 2-文件类型
  56. * 3-图片类型
  57. * 4-GIF动画类型
  58. * @return 返回操作结果指针 JSON格式
  59. */
  60. public static extern IntPtr SendBaseMessage(int port, byte[] wxid, byte[] content, int type);
  61. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  62. /*
  63. * @Name 发送链接信息
  64. * @Parame port 端口
  65. * @Parame wxid 微信ID
  66. * @Parame url 链接地址
  67. * @Parame title 标题
  68. * @Parame des 说明
  69. * @Parame thumbUrl 缩略图
  70. * @return 返回操作结果指针 JSON格式
  71. */
  72. public static extern IntPtr SendLinkMessage(int port, byte[] wxid, byte[] url, byte[] title, byte[] des, byte[] thumbUrl);
  73. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  74. /*
  75. * @Name 发送原始XML信息 可以是小程序 链接 视频号 等
  76. * @Parame port 端口
  77. * @Parame wxid 微信ID
  78. * @Parame content xml消息内容
  79. * @return 返回操作结果指针 JSON格式
  80. */
  81. public static extern IntPtr SendXmlMessage(int port, byte[] wxid, byte[] content);
  82. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  83. /*
  84. * @Name 发送引用消息
  85. * @Parame port 端口
  86. * @Parame wxid 微信ID
  87. * @Parame content 消息内容
  88. * @Parame param 微信ID
  89. * @Parame type
  90. * 1 文本 , 3 图片XML ,47 动态表情XML , 49 文件 链接 小程序XML
  91. * @return 返回操作结果指针 JSON格式
  92. */
  93. public static extern IntPtr SendReferMessage(int port, byte[] wxid, byte[] content, byte[] param, int type);
  94. //*****************************************************************************************************************
  95. //***********************************************好友相关操作*******************************************************
  96. //*****************************************************************************************************************
  97. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  98. /*
  99. * @Name 通过好友申请
  100. * @Parame port 端口
  101. * @Parame encryptusername XML中encryptusername值
  102. * @Parame ticket XML中ticket值
  103. * @Parame scene 添加类型 XML中scene值
  104. * 0 通过搜索微信号添加
  105. * 3 通过搜索微信号添加
  106. * 6 通过搜索微信号添加
  107. * 14 通过群聊添加
  108. * 15 通过搜索手机号添加
  109. * 17 通过名片分享添加
  110. * 30 通过扫一扫添加
  111. * @return 返回操作结果指针 JSON格式
  112. */
  113. public static extern IntPtr VerifyFriend(int port, byte[] encryptusername, byte[] ticket, int scene);
  114. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  115. /*
  116. * @Name 添加好友
  117. * @Parame port 端口
  118. * @Parame wxid 微信ID
  119. * @Parame content 附加内容
  120. * @Parame type 添加类型
  121. * 0 通过搜索微信号添加
  122. * 3 通过搜索微信号添加
  123. * 6 通过搜索微信号添加
  124. * 14 通过群聊添加
  125. * 15 通过搜索手机号添加
  126. * 17 通过名片分享添加
  127. * 30 通过扫一扫添加
  128. * @return 返回操作结果指针 JSON格式
  129. */
  130. public static extern IntPtr AddFriend(int port, byte[] wxid, byte[] content, int type);
  131. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  132. /*
  133. * @Name 添加群成员为好友
  134. * @Parame port 端口
  135. * @Parame wxid 群微信ID
  136. * @Parame memberId 群成员微信ID
  137. * @Parame content 附加内容
  138. * 添加类型 14 通过群聊添加
  139. * @return 返回操作结果指针 JSON格式
  140. */
  141. public static extern IntPtr AddGroupFriend(int port, byte[] wxid, byte[] memberId, byte[] content);
  142. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  143. /*
  144. * @Name 删除好友
  145. * @Parame port 端口
  146. * @Parame wxid 微信ID
  147. * @return 返回操作结果指针 JSON格式
  148. */
  149. public static extern IntPtr DelFriend(int port, byte[] wxid);
  150. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  151. /*
  152. * @Name 拉黑或取消拉黑好友
  153. * @Parame port 端口
  154. * @Parame wxid 微信ID
  155. * @Parame flag 1为拉黑 0为取消拉黑
  156. * @return 返回操作结果指针 JSON格式
  157. */
  158. public static extern IntPtr BlackFriend(int port, byte[] wxid, int flag);
  159. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  160. /*
  161. * @Name 关注公众号
  162. * @Parame port 端口
  163. * @Parame wxid 公众号ID
  164. * @return 返回操作结果指针 JSON格式
  165. */
  166. public static extern IntPtr FollowOfficialAccount(int port, byte[] wxid);
  167. //*****************************************************************************************************************
  168. //***********************************************群相关操作*******************************************************
  169. //*****************************************************************************************************************
  170. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  171. /*
  172. * @Name 创建群聊
  173. * @Parame port 端口
  174. * @Parame wxids 微信ID,最少2个,用小写逗号(,)隔开
  175. * @return 返回操作结果指针 JSON格式
  176. */
  177. public static extern IntPtr CreateChatRoom(int port, byte[] wxids);
  178. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  179. /*
  180. * @Name 发送群公告
  181. * @Parame port 端口
  182. * @Parame wxid 微信群ID
  183. * @Parame content 公告内容
  184. * @return 返回操作结果指针 JSON格式
  185. */
  186. public static extern IntPtr SetGroupAnnouncement(int port, byte[] wxid, byte[] content);
  187. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  188. /*
  189. * @Name 修改群名称
  190. * @Parame port 端口
  191. * @Parame wxid 微信群ID
  192. * @Parame content 群名称
  193. * @return 返回操作结果指针 JSON格式
  194. */
  195. public static extern IntPtr SetGroupName(int port, byte[] wxid, byte[] content);
  196. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  197. /*
  198. * @Name 修改自己在群里的名称
  199. * @Parame port 端口
  200. * @Parame wxid 微信群ID
  201. * @Parame content 群名称
  202. * @return 返回操作结果指针 JSON格式
  203. */
  204. public static extern IntPtr SetSelfGroupNick(int port, byte[] wxid, byte[] content);
  205. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  206. /*
  207. * @Name 修改群名称
  208. * @Parame port 端口
  209. * @Parame wxid 微信群ID
  210. * @Parame memberId 群成员微信Id
  211. * @return 返回操作结果指针 JSON格式
  212. */
  213. public static extern IntPtr DelGroupMember(int port, byte[] wxid, byte[] memberId);
  214. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  215. /*
  216. * @Name 添加群成员
  217. * @Parame port 端口
  218. * @Parame wxid 微信群ID
  219. * @Parame memberId 群成员微信Id
  220. * @return 返回操作结果指针 JSON格式
  221. */
  222. public static extern IntPtr AddGroupMember(int port, byte[] wxid, byte[] memberId);
  223. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  224. /*
  225. * @Name 邀请群成员
  226. * @Parame port 端口
  227. * @Parame wxid 微信群ID
  228. * @Parame memberId 群成员微信Id
  229. * @return 返回操作结果指针 JSON格式
  230. */
  231. public static extern IntPtr InviteGroupMember(int port, byte[] wxid, byte[] memberId);
  232. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  233. /*
  234. * @Name 用微信内置浏览器打开网页
  235. * @Parame port 端口
  236. * @Parame url 网址
  237. * @return 返回操作结果指针 JSON格式
  238. */
  239. public static extern IntPtr OpenUrl(int port, byte[] url);
  240. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  241. /// <summary>
  242. /// 获取JSon中数字成员值
  243. /// </summary>
  244. public static extern int JsonGetIntValue(string jsonDoc, string member);
  245. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  246. /// <summary>
  247. /// 获取JSon中字符成员值
  248. /// </summary>
  249. public static extern IntPtr JsonGetStringValue(string jsonDoc, string member);
  250. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  251. /*
  252. * @Name 发送Json格式数据
  253. * @Parame port 端口
  254. * @Parame json json数据
  255. * @return 返回操作结果 JSON格式
  256. */
  257. public static extern IntPtr SendJsonData(int port, byte[] json);
  258. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  259. /*
  260. * @Name 通过二维码进群
  261. * @Parame port 端口
  262. * @Parame filename 二维码图片文件
  263. * @return 返回操作结果 JSON格式
  264. */
  265. public static extern IntPtr EnterGroupFromQrcode(int port, byte[] filename);
  266. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  267. /*
  268. * @Name 获取图片二维码
  269. * @Parame filename 二维码图片文件
  270. * @return 返回操作结果 JSON格式
  271. */
  272. public static extern IntPtr GetImageQrcode(byte[] filename);
  273. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  274. /*
  275. * @Name 通过链接获取a8key
  276. * @Parame port 端口
  277. * @Parame url 链接
  278. * @return 返回操作结果 JSON格式
  279. */
  280. public static extern IntPtr GetA8keyFromUrl(int port, byte[] url);
  281. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  282. /*
  283. * @Name 通过二维码图片获取a8key
  284. * @Parame port 端口
  285. * @Parame filename 二维码图片文件
  286. * @return 返回操作结果 JSON格式
  287. */
  288. public static extern IntPtr GetA8keyFromImage(int port, char filename);
  289. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  290. /*
  291. * @Name 获取已注入登陆的微信
  292. */
  293. public static extern void GetLoginWechat();
  294. [DllImport("wxDriver.dll", CallingConvention = CallingConvention.Cdecl)]
  295. /*
  296. * @Name 退出微信
  297. * @Parame port 端口
  298. */
  299. public static extern void LoginOut(int port);
  300. }
  301. }

再看程序里面调用方法:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Runtime.InteropServices;
  11. using Api;
  12. /*
  13. * 获取正试版接口 QQ 150311852
  14. * 电话微信 17756258757 联系时请说明来意
  15. * 接口详细说明:https://www.showdoc.com.cn/WeChatHelper?page_id=6401634409030983
  16. *
  17. */
  18. namespace TestDemo
  19. {
  20. public partial class MainForm : Form
  21. {
  22. //定义委托回调消息
  23. private static OnReceiveCallBack mOnReceiveCallBack;
  24. public static MainForm mMainForm = null;
  25. int port = 0;
  26. public MainForm()
  27. {
  28. InitializeComponent();
  29. }
  30. private void button1_Click(object sender, EventArgs e)
  31. {
  32. //打开或多开微信
  33. Api.WxApi.OpenWechat();
  34. }
  35. /// <summary>
  36. ///回调函数
  37. /// </summary>
  38. /// param name="intPtr">消息指针</param>
  39. /// <param name="dwSize">消息长度</param>
  40. static void WxReceiveCallBack(IntPtr intPtr, int dwSize)
  41. {
  42. byte[] data = new byte[dwSize];
  43. Marshal.Copy(intPtr, data, 0, (int)dwSize);
  44. string sData = System.Text.Encoding.UTF8.GetString(data);
  45. int api = Api.WxApi.JsonGetIntValue(sData, "api");
  46. //4 为登录推送过来的消息
  47. mMainForm.port = Api.WxApi.JsonGetIntValue(sData, "port");
  48. //显示回调的消息
  49. mMainForm.Invoke(new Action<String>(mMainForm.text_out.AppendText), sData + "\r\n");
  50. }
  51. private void MainForm_Load(object sender, EventArgs e)
  52. {
  53. mMainForm = this;
  54. //赋值回调
  55. mOnReceiveCallBack = WxReceiveCallBack;
  56. //初始化回调
  57. Api.WxApi.InitWxDriver(mOnReceiveCallBack);
  58. //启动前获取所有已登陆的微信
  59. Api.WxApi.GetLoginWechat();
  60. }
  61. private void btn_send_Click(object sender, EventArgs e)
  62. {
  63. if (port > 0)
  64. {
  65. //发送文本消息
  66. byte[] utf8 = Encoding.UTF8.GetBytes(txt_msg.Text);
  67. byte[] wxid = Encoding.UTF8.GetBytes("filehelper");
  68. IntPtr intptr = Api.WxApi.SendBaseMessage(port, wxid, utf8, 1);
  69. //显示返回消息
  70. string data= Marshal.PtrToStringAnsi(intptr);
  71. string json = System.Text.Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(data));
  72. text_out.AppendText(json + "\r\n");
  73. }
  74. }
  75. }
  76. }

下载地址:https://tshelper.lanzoui.com/iyrq8pi8asj

 



所属网站分类: 技术文章 > 博客

作者:jjjjjjjj

链接:http://www.phpheidong.com/blog/article/86844/4f6c5389ed61773fc700/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

1 0
收藏该文
已收藏

评论内容:(最多支持255个字符)