c 程序間的通訊實現之一簡單字串收發

2021-09-07 06:35:46 字數 3960 閱讀 7557

使用windows api實現兩個程序間(含窗體)的通訊在windows下的兩個程序之間通訊通常有多種實現方式,在.net中,有如命名管道、訊息佇列、共享記憶體等實現方式,這篇文章要講的是使用windows的api來實現簡單的程序間通訊,這兩個程序既可以都是基於c#開發,也可以都是基於c++開發,也可以是乙個c#開發而另乙個為c++開發,在c++開發方面,不需要額外呼叫windows的api,而是可以直接使用相關方法即可。所以,這裡重點要講的就是在c#中是如何做的,而至於在c++中是如何做的將給出例子,並不做詳述。

對於接收訊息,只需要重寫defwndproc函式即可,對於傳送訊息,筆者編寫了乙個類msghandler來實現。要順利實現訊息的接收與傳送,使用了windows的api:findwindow、sendmessage等。在c#環境中,通過dllimport來引入相應的api,**示例如下:

// findwindow method, using windows api [dllimport("user32.dll", entrypoint = "findwindow")] private static extern int findwindow(string lpclassname, string lpwindowname); // iswindow method, using windows api [dllimport("user32.dll", entrypoint = "iswindow")] private static extern bool iswindow(int hwnd); // sendmessage method, using windows api [dllimport("user32.dll", entrypoint = "sendmessage")] private static extern int sendmessage( int hwnd, // handle to destination window int msg, // message int wparam, // first message parameter ref copydatastruct lparam // second message parameter ); // sendmessage method, using windows api [dllimport("user32.dll", entrypoint = "sendmessage")] private static extern int sendmessage( int hwnd, // handle to destination window int msg, // message int wparam, // first message parameter string lparam // second message parameter );

筆者查閱了相關網路資源,發現很少有提及使用自定義訊息來傳送和接收訊息的,幾乎都是使用了系統訊息wm_copydata來實現。在本例中,筆者除了使用系統訊息wm_copydata來收發訊息外,還將使用自定義訊息來實現收發訊息。不過,值得注意的是,筆者在測試過程中發現,使用自定義的訊息來收發結構體時發生了一些異常,該異常提示說記憶體不能讀,對於該問題,還有待進一步解決,當然,若是哪位前輩或朋友有遇到過該問題並已順利解決的話,不妨告知,筆者將洗耳恭聽。

訊息傳送類msghandler的**示例如下:

using system; using system.text; using system.windows.forms; using system.diagnostics; using system.runtime.interopservices; namespace communicationtest /// /// send message to target window /// /// the window name which we want to found /// the message to be sent, string /// success or not public static bool sendmessagetotargetwindow(string wndname, string msg) : ", wndname, msg)); int ihwnd = findwindow(null, wndname); if (ihwnd == 0) ] was not found!", wndname); messagebox.show(strerror, "error", messageboxbuttons.ok, messageboxicon.error); debug.writeline(strerror); return false; } else ] failed!", wndname); messagebox.show(strerror, "error", messageboxbuttons.ok, messageboxicon.error); debug.writeline(strerror); return false; } return true; } } /// /// send message to target window /// /// the window name which we want to found /// first parameter, integer /// second parameter, string /// success or not public static bool sendmessagetotargetwindow(string wndname, int wparam, string lparam) : wparam:, lparam:", wndname, wparam, lparam)); int ihwnd = findwindow(null, wndname); if (ihwnd == 0) ] was not found!", wndname); messagebox.show(strerror, "error", messageboxbuttons.ok, messageboxicon.error); debug.writeline(strerror); return false; } else ] failed!", wndname); messagebox.show(strerror, "error", messageboxbuttons.ok, messageboxicon.error); debug.writeline(strerror); return false; } return true; } } } }

訊息接收重寫了defwndproc方法,其**示例如下:

/// /// override the defwndproc function, in order to receive the message through it. /// /// message protected override void defwndproc(ref system.windows.forms.message m) }

訊息的接收與傳送最終通過乙個winform展現出來,其**實現如下:

通過上述的c#部分的**,已經可以實現兩個c#窗體間的通訊,其介面截圖如下圖所示:

那麼,在c++中又是如何實現的呢?這個其實也是很簡單的。要實現訊息的收發,同理也要重寫windowproc以便於接收訊息,而通過呼叫方法亦可實現訊息的傳送。

對於訊息接收,如果是系統訊息,可以通過oncopydata(cwnd* pwnd, copydatastruct* pcopydatastruct)事件完成;如果是自定義訊息,可以通過重寫windowproc完成。**示例如下:

bool ctestdlg::oncopydata(cwnd* pwnd, copydatastruct* pcopydatastruct) lresult ctestdlg::windowproc(uint message, wparam wparam, lparam lparam) return cdialog::windowproc(message, wparam, lparam); }

對於訊息傳送,只需要呼叫形如sendmessage(m_hwndmsg, wm_data_transfer, wparam, lparam)方法即可實現,lparam引數可以是pcopydatastruct等。

通過上面的介紹,相信已經可以輕鬆實現兩個程序間(含窗體)的通訊的,使用這樣的方法,既簡單又能夠滿足大部分的應用需求,不失為一種簡便的方法。

c 程序間的通訊實現之一簡單字串收發

使用windows api實現兩個程序間 含窗體 的通訊在windows下的兩個程序之間通訊通常有多種實現方式,在.net中,有如命名管道 訊息佇列 共享記憶體等實現方式,這篇文章要講的是使用windows的api來實現簡單的程序間通訊,這兩個程序既可以都是基於c 開發,也可以都是基於c 開發,也可...

程序間的通訊實現(IPC)的11種方法

程序間的通訊實現 ipc 的11種方法 程序通常被定義為乙個正在執行的程式的例項,它由兩個部分組成 乙個是作業系統用來管理程序的核心物件。核心物件也是系統用來存放關於程序的統計資訊的地方 另乙個是位址空間,它包含所有的可執行模組或dll模組的 和資料。它還包含動態分配的空間。如執行緒堆疊和堆分配空間...

RPC程序間通訊的一種實現

客戶端專案中不可避免的要用到程序間的通訊,方式也多種多樣。單就開發而言,rpc這種模式最方便來做程序間通訊的手段。因為類似於本地函式的呼叫。實現這個機制需要滿足以下內容 介面定義。乙個元件是否方便使用,主要是看介面的設計是否簡潔合理。滿足本地函式呼叫的特點 函式未執行完不返回,只需要函式簽名和函式引...