来自Windows API的消息

Man*_*oon 9 c# c++ winapi sendmessage winforms

我正在尝试从QCollector接收消息,如QCollector数据接口开发人员指南中所述.该过程包括注册预定义消息,查找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!");
            }

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

编辑1:

为了确保我在c#中有基本工作,我创建了这个应用程序的第二个版本并让他们互相交谈.这有效,所以我知道我的句柄和消息结构是正确的.

但我从来没有得到qCollector的回复.有没有人有使用任何其他语言的经验?我怀疑qCollector是用c ++编写的.

Man*_*dar -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 ");
}
Run Code Online (Sandbox Code Playgroud)