2017-02-15 37 views
9

我試圖接收來自QCollector的消息,如QCollector Data Interface開發人員指南中所述。該過程包括註冊預定義的消息,查找QCollector服務器窗口以及通過註冊消息交換數據。從窗口回來的消息API

我的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++編寫的。

+0

你說的_TargetApp_是什麼意思? –

+0

請嘗試使用PostMessage。 –

+0

@JoshuaDrake TargetApp是我用FindWindow找到的wnd(「QCDataInterfaceWndClass」,null) – ManInMoon

回答

-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 "); 
} 
+0

問題不在於:「我如何改進設計?「問題是:」我如何檢索跨進程的消息中返回的值?「這個建議的答案沒有解決這個問題,甚至不是遠程的,也沒有增加任何有用的東西。 – IInspectable