【发布时间】:2017-07-04 07:58:21
【问题描述】:
我正在尝试接收来自 QCollector 的消息,如QCollector Data Interface 开发人员指南中所述。该过程包括注册预定义的消息,找到 QCollector 服务器窗口,并通过注册的消息交换数据。
我的WndProc 回调接收到丢失的消息,但没有一个被识别为已注册消息之一。我在请求中传递了我的Form 的this.Handle,但我不确定这是否正确。
我做错了什么?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HistDataManager
{
public partial class Form1 : Form
{
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string sClass, string sWindow);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);
int nWinHandle = FindWindow("QCDataInterfaceWndClass", null);
uint wm_QCollectorClientDataRequest = RegisterWindowMessage("QCOLLECTOR_CLIENT_DATA_REQUEST");
uint wm_QCollectorClientPortfolioListRequest = RegisterWindowMessage("QCOLLECTOR_CLIENT_PORTFOLIO_LIST_REQUEST");
uint wm_QCollectorPortfolioListRequestComplete = RegisterWindowMessage("QCOLLECTOR_PORTFOLIO_LIST_REQUEST_COMPLETE ");
public void TestQC()
{
SendMessage(new IntPtr(nWinHandle), wm_QCollectorClientPortfolioListRequest, UIntPtr.Zero, this.Handle);
}
protected override void WndProc(ref Message m)
{
Console.WriteLine(m.HWnd + "," + m.Msg + "," + m.LParam + "," + m.WParam);
base.WndProc(ref m);
if (m.Msg == wm_QCollectorClientPortfolioListRequest || m.Msg == wm_QCollectorPortfolioListRequestComplete)
{
Console.WriteLine("Message from specified application found!");
}
}
}
}
编辑 1:
为了确保我具备使用 c# 的基础知识,我创建了这个应用程序的第二个版本并让他们互相交谈。这行得通,所以我知道我的句柄和消息结构是正确的。
但我从未收到 qCollector 的回复。有没有人有使用其他语言的经验?我怀疑 qCollector 是用 C++ 编写的。
【问题讨论】:
-
你说的TargetApp是什么意思?
-
尝试使用 PostMessage。
-
@JoshuaDrake TargetApp 是我在 FindWindow("QCDataInterfaceWndClass", null) 中找到的 wnd
-
您永远不会验证
nWinHandle是否拥有有效句柄。 -
@TnTinMn:
PtrToStringAuto将输入解释为指向以零结尾的字符数组的指针。不过,LParam不是这样。该值是一个atom,用于处理跨进程封送处理。可以通过GlobalGetAtomName调用找回。
标签: c# c++ winforms winapi sendmessage