我試圖接收來自QCollector的消息,如QCollector Data Interface開發人員指南中所述。該過程包括註冊預定義的消息,查找QCollector服務器窗口以及通過註冊消息交換數據。從窗口回來的消息API
我的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找到的wnd(「QCDataInterfaceWndClass」,null) – ManInMoon