【问题标题】:Message back from windows API从 Windows API 返回的消息
【发布时间】:2017-07-04 07:58:21
【问题描述】:

我正在尝试接收来自 QCollector 的消息,如QCollector Data Interface 开发人员指南中所述。该过程包括注册预定义的消息,找到 QCollector 服务器窗口,并通过注册的消息交换数据。

我的WndProc 回调接收到丢失的消息,但没有一个被识别为已注册消息之一。我在请求中传递了我的Formthis.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


【解决方案1】:

我不知道在 .Net 中是否可以,但我想建议您在构造函数或 init() 函数中调用所有函数。

建议设计

int nWinHandle=0;
uint wm_QCollectorClientDataRequest=0;
uint wm_QCollectorClientPortfolioListRequest=0;
uint wm_QCollectorPortfolioListRequestComplete=0;

void init()
{
    nWinHandle = FindWindow("QCDataInterfaceWndClass", null);
    wm_QCollectorClientDataRequest = RegisterWindowMessage("QCOLLECTOR_CLIENT_DATA_REQUEST");
    wm_QCollectorClientPortfolioListRequest = RegisterWindowMessage("QCOLLECTOR_CLIENT_PORTFOLIO_LIST_REQUEST");
    wm_QCollectorPortfolioListRequestComplete = RegisterWindowMessage("QCOLLECTOR_PORTFOLIO_LIST_REQUEST_COMPLETE ");
}

【讨论】:

  • 问题不是:“如何改进我的设计?”问题是:“如何跨进程检索消息中返回的值?”这个提议的答案没有解决这个问题。甚至远程也不行。它也没有添加任何有用的东西。
猜你喜欢
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 2020-09-29
  • 1970-01-01
相关资源
最近更新 更多