Communication between two processes on Windows platform

Keywords: Windows

Functional requirements: now there are two processes, one is process A, the other is process B. through the message mechanism of Windows, process A can send messages to B, and process B can also send messages to A.

Platform: first, we use the Win32 program to realize the corresponding functions, and use VS2017+C + + and Windows API tools and functions to wake up the development.

Solution: through the requirement (that is, we only need two processes to receive the response message, and the specific application scenario is that two processes are processes with the same property, that is, they both survive and serve for another process. For example, now process a and B are all for opening main program C. If the user opens it through process a, then after the main program is opened, process a and B can both Closed, in order to achieve synchronization, process a needs to send a closing message to process B, and process B can execute the destruction process after receiving the closing message). A simple hidden window is used to receive the messages sent by the relevant processes, and the subsequent processing is carried out by itself;

Program implementation:

This program provides its template, which can not be run directly. You need to modify it accordingly!

Process A:

#include"processA"

#include<windows>
#include"processA.h"


#define WM_CLOSEPROCESSA							(WM_USER + 0x2007)	
#define WM_CLOSEPROCESSB						    (WM_USER + 0x2008)


// Hide window handle
HINSTANCE g_hInstance = NULL;
// Bubble system tray icon menu window class name
LPCTSTR g_szKBubbleTrayIconMenuWindowClass = _T("QWidget");
// Bubble system tray icon menu window title
LPCTSTR g_szKBubbleTrayIconMenuWindowTitle = _T("1BEEF4DE-B3B3-49CA-A03A-0F2EB34920F1_mini");
// Window class name string
LPCTSTR g_szToastIconWindowClassName = _T("mini_window");
//Window title string
LPCTSTR g_szToastIconWindowTitle = _T("23A5B06E-20BB-4E7E-A0AC-6982ED6A6041_tosat");


/*
@brief Call the main program and send information
*/

  BOOL sendSomeMessage()
  {

  /*
  lanch process C (or other process)
  method: ShellExecute(NULL ,L"open" , ExepathName, CommandPath , NULL , SW_NORMAL);
  then send colse message
  */

  
  HWND hwnd = FindWindow(g_szKBubbleTrayIconMenuWindowClass , g_szKBubbleTrayIconMenuWindowTitle);
  if (hwnd == NULL)
  return false;
  ::SendMessage(hwnd , WM_CLOSEPROCESSB , 0 , 0);
  return true;
  }




/*
 @brief Processing notification window messages
 @param uMsg Contains shutdown message notification sent by mini
*/
LRESULT CALLBACK HiddenWindowProcess(__in HWND hwnd , __in UINT uMsg , __in WPARAM wParam , __in LPARAM lParam)
{
	switch (uMsg) 
	{
		case WM_CLOSEPROCESSA:
			closeHandle(g_hInstance);
                //Deal with closure
			break;
		default:
			return DefWindowProc(hwnd , uMsg , wParam , lParam);
	}
	return true;
}

/*
*brief Create a hidden window for receiving closing messages
*return TRUE Create successful FALSE create failed
*/
BOOL CreateHiddenWindow()
{
	WNDCLASS wcMinisiteWindowClass = { 0 };
	wcMinisiteWindowClass.style = CS_HREDRAW | CS_VREDRAW;
	wcMinisiteWindowClass.lpfnWndProc = HiddenWindowProcess;
	wcMinisiteWindowClass.cbClsExtra = 0;
	wcMinisiteWindowClass.cbWndExtra = 0;
	wcMinisiteWindowClass.hInstance = g_hInstance;
	wcMinisiteWindowClass.hIcon = NULL;
	wcMinisiteWindowClass.hCursor = ::LoadCursor(NULL , IDC_ARROW);
	wcMinisiteWindowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcMinisiteWindowClass.lpszMenuName = NULL;
	wcMinisiteWindowClass.lpszClassName = g_szToastIconWindowClassName;
	if (::RegisterClass(&wcMinisiteWindowClass) == 0)
		return FALSE;
	::CreateWindow(g_szToastIconWindowClassName , g_szToastIconWindowTitle , WS_OVERLAPPED , 0 , 0 , 1 , 1 , NULL , NULL , g_hInstance , NULL);
	MSG msg;
	while (::GetMessage(&msg , NULL,0,0)) 
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
	return TRUE;
}

int WINAPI wWinMain(_In_ HINSTANCE hInstance , _In_opt_ HINSTANCE , _In_ LPWSTR cmdLineArgs , _In_ int)
{
	
	CreateHiddenWindow();
/*
do other something...........
*/

	sendSomeMessage()
	return 0;
}



Process B:

#include"processB"

#include<windows>
#include"processB.h"

#define WM_CLOSEPROCESSA							(WM_USER + 0x2007)	
#define WM_CLOSEPROCESSB						    (WM_USER + 0x2008)		


// Hide window handle
HINSTANCE g_hInstance = NULL;
// Bubble system tray icon menu window class name
LPCTSTR g_szKBubbleTrayIconMenuWindowClass = _T("QWidget");
// Bubble system tray icon menu window title
LPCTSTR g_szKBubbleTrayIconMenuWindowTitle = _T("1BEEF4DE-B3B3-49CA-A03A-0F2EB34920F1_mini");
// Window class name string
LPCTSTR g_szToastIconWindowClassName = _T("mini_window");
//Window title string
LPCTSTR g_szToastIconWindowTitle = _T("23A5B06E-20BB-4E7E-A0AC-6982ED6A6041_tosat");


/*
@brief Call the main program and send information
*/

  BOOL sendSomeMessage()
  {

  /*
  lanch process C (or other process)
  method: ShellExecute(NULL ,L"open" , ExepathName, CommandPath , NULL , SW_NORMAL);
  then send colse message
  */

  
  HWND hwnd = FindWindow(g_szToastIconWindowClassName , g_szToastIconWindowTitle);
  if (hwnd == NULL)
  return false;
  ::SendMessage(hwnd , WM_CLOSEPROCESSA , 0 , 0);//Send message
  return true;
  }




/*
 @brief Processing notification window messages
 @param uMsg Contains shutdown message notification sent by mini
*/
LRESULT CALLBACK HiddenWindowProcess(__in HWND hwnd , __in UINT uMsg , __in WPARAM wParam , __in LPARAM lParam)
{
	switch (uMsg) //Accept information
	{
		case WM_CLOSEPROCESSB:
			closeHandle(g_hInstance);
                //Deal with closure
			break;
		default:
			return DefWindowProc(hwnd , uMsg , wParam , lParam);
	}
	return true;
}

/*
*brief Create a hidden window for receiving closing messages
*return TRUE Create successful FALSE create failed
*/
BOOL CreateHiddenWindow()
{
	WNDCLASS wcMinisiteWindowClass = { 0 };
	wcMinisiteWindowClass.style = CS_HREDRAW | CS_VREDRAW;
	wcMinisiteWindowClass.lpfnWndProc = HiddenWindowProcess;
	wcMinisiteWindowClass.cbClsExtra = 0;
	wcMinisiteWindowClass.cbWndExtra = 0;
	wcMinisiteWindowClass.hInstance = g_hInstance;
	wcMinisiteWindowClass.hIcon = NULL;
	wcMinisiteWindowClass.hCursor = ::LoadCursor(NULL , IDC_ARROW);
	wcMinisiteWindowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcMinisiteWindowClass.lpszMenuName = NULL;
	wcMinisiteWindowClass.lpszClassName = g_szKBubbleTrayIconMenuWindowClass;
	if (::RegisterClass(&wcMinisiteWindowClass) == 0)
		return FALSE;
	::CreateWindow(g_szKBubbleTrayIconMenuWindowClass , g_szKBubbleTrayIconMenuWindowTitle , WS_OVERLAPPED , 0 , 0 , 1 , 1 , NULL , NULL , g_hInstance , NULL);
	MSG msg;
	while (::GetMessage(&msg , NULL,0,0)) 
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
	return TRUE;
}

int WINAPI wWinMain(_In_ HINSTANCE hInstance , _In_opt_ HINSTANCE , _In_ LPWSTR cmdLineArgs , _In_ int)
{
	
	CreateHiddenWindow();

/*

do other something.........
 
*/
	sendSomeMessage()

	return 0;
}


 

Posted by multe-media on Wed, 01 Jan 2020 02:54:36 -0800