Summary : This article describes the use of Windows Messaging mechanisms to communicate with each other in different languages, connect to each other, and in two languages that are currently more prevalent in Microsoft Visual C + + 6.0 and Borland delphi 5.0 is an object that uses both languages to compile an application and to interact well with messages.
keyword : VC + +, Delphi, message
I, Introduction
Developing larger programs often involves splitting a project into several modules, which are completed by several development teams. The author has participated in several large-scale project research and development work, according to the needs of the project is often divided into a few parts, to 三、四家 scientific research units to complete, because the units are more dispersed, the coordination between each other can only be based on pre-established interface protocol to complete, That is, the interaction between the programs of each unit and the other units is done through only a few interfaces. And the development language used by all units is different, which adds to the difficulty of interacting with the different languages of the program. According to the actual work of the author to find some solutions to the above problems, some of the following are introduced.
Second, program design ideas
Although each unit takes a different development language, from Microsoft Visual C + + 6.0 to borland delphi 5.0 or even Microsoft Visual Basic 6.0, but one thing is the same: all work under the win 32 operating system. And the win 32 operating system has a very important mechanism-the message. We can use the DDE Spy tool that came with Microsoft Visual Studio 6.0 to intercept messages from applications running under the Windows operating system, regardless of the language in which they were written. So we can make it clear that just capturing the window handle to the target program can send a message to it, with two message parameters to pass the information over. The two functions for sending messages, PostMessage and SendMessage, are also well-defined:
BOOL PostMessage (HWND hwnd ,//target window handle UINT MSG,//message sent WPARAM WPARAM,//First message parameter LPARAM LPARAM//second message parameter); BOOL SendMessage (HWND hwnd,//target window handle UINT MSG,//message sent WPARAM WPARAM,//First message parameter LPARAM LPARAM//second Message parameters); |
As you can see from the
, the principle of using messages is that you can communicate with two programs that are programmed in different languages, the following two by Microsoft Visual C + + 6.0 and borland delphi 5.0 the simulation program compiled to give a brief introduction to the implementation process of the method.
Third, implementation of the simulation program compiled by borland delphi 5.0
Create a new project because the Microsoft Visual C + + 6.0 under the window handle to capture other applications to pass through the API function FindWindow (...) To implement, and this function is based on the window title of the target window to determine which window, so to ensure that the two languages programmed to communicate reliably, it is necessary to ensure that the respective window title does not change. So in this program you need to modify the window's properties to change the title of the form, set it as "Delphi message receiving, sending program."
Sending messages to Microsoft Visual C + + 6.0 under borland delphi 5.0 is simple, just call the Win32 API function FindWindow (...). Search in all current Windows based on the title of the opposing program form. When you get to the window handle of the other program, you can use PostMessage (...) Or SendMessage (...) function to send a message, it should be noted that although both functions are sent to the specified window message, but the former is a "mail" nature, as long as the message is sent out even if the finished, do not care whether the other party received the message after processing, the latter is waiting for the other party to finish the message, if the other party does not process the message So SendMessage (...) function is not returned. Although the difference is very small, but in the actual application can choose the appropriate message sending function, often will make the program more perfect. The key code to send the message is as follows:
...... {The nil parameter specifies that all windows are searched, and the handle of the application that is titled "VC message Receive, Sender" is captured} Hwnd:=findwindow (Nil,pchar (' VC message receiving, sending program '));
PostMessage (hwnd,2000,0,1); ...... |
To respond to messages sent from Microsoft Visual C + + 6.0, it is troublesome to first add a data structure to describe the message, which can be defined as follows:
Type Tmymessage=record a:cardinal; B:integer; C:integer; D:integer; End |
In Borland Delphi 5.0, the response message does not need to have a message map, just like adding a normal procedure, but its entry parameter must be the message structure object just added and then add a message number, such as procedure receive (Var Message:tmymessage); Message 2000; You can get the two message parameters that are sent with the message by judging the B and C two data member variables in the message structure of the entry parameters in the response function. and react accordingly according to the specific values.
Iv. implementation of the simulation program compiled by Microsoft Visual C + + 6.0
The design and implementation of message sending and response in Microsoft Visual C + + 6.0 is basically similar to the method mentioned earlier, as well as pinning your own title so that the other person gets his or her own window handle to send messages to itself, but the response message needs to be explicitly implemented by a message map.
Fixing your own program window caption is typically done in the initialization function oninitial () of the application class of the project as follows:
...... M_pmainwnd->showwindow (Sw_show); M_pmainwnd->setwindowtext ("VC message receiving, sending program"); M_pmainwnd->updatewindow (); ...... |
In particular, it is not possible to add a response function that sends messages from other programs in all classes, but only in the main framework class to receive messages from external programs, because the handle captured by the external program is only a handle to the program's main frame captured by the program title, so the message can only be sent to the main frame class. The addition of custom message and message maps is implemented as follows:
Modify the following fragment in the header file of the main frame class:
...... #define WM_MYMSG 2000 ...... {{afx_msg (CMainFrame) Note-the ClassWizard would add and remove member functions here. Do not EDIT these blocks of generated code! }}afx_msg void Onrecvmsg (WPARAM wparam,lparam LPARAM); Declare_message_map () ...... |
Then modify the following fragment in the implementation file of the main framework class:
...... Begin_message_map (CMainFrame, CFrameWnd) {{Afx_msg_map (CMainFrame) Note-the ClassWizard would add and remove mapping macros here. Do not EDIT these blocks of generated code! }}afx_msg_map On_message (WM_TODELPHI,ONRECVMSG) End_message_map () ...... void Cmainframe::onrecvmsg (WPARAM wparam,lparam LPARAM) |
Where the parameter from wparam is the first parameter that comes with the received message; The argument from lparam is the second parameter that comes with the received message. You can get more additional information from each other by using these two parameters.
As for sending messages to each other is very similar to the previous program, looking for the other side of the window can be used WIN32 API functions of the FindWindow (...), you can also use MFC's CWnd class FindWindow (...), the latter returned directly is the window pointer, the implementation of the main code is as follows:
...... Gets the window handle of the Delphi program titled "Delphi Message Receiving, sending program" CWnd *pwnd=cwnd::findwindow (NULL, "Delphi message receiving, sending program"); A message is sent to a window only when it is actually captured, or it causes an exception error. if (pWnd) Pwnd->postmessage (wm_mymsg,0,1); ...... |
Wm_mymsg is a custom message, 0,1 is an accompanying two message parameter. The specific meaning can be self-made through the interface protocol.
V. Interaction of the inspection procedure
First compiled in the respective programming environment, after running open the Debugging toolkit that is included with Microsoft Visual Studio 6.0 DDE Spy, when a program receives a message from another program, after performing a specific job. A DDE spy can be used to monitor that a custom message is actually emitted from a process, and then the message queue that passes through the win 32 system is received and responded to by another process.
Conclusion:
This program through a simple example of the programming in the heterogeneous language use of messages as a means of communication tools to do a simple introduction, in the actual application also to consider many specific details, the implementation is far more cumbersome than the program. But in this way to solve the mixed programming of small data volume of frequent data communication is undoubtedly very advantageous.
View the source of this article
Http://soft.zhiding.cn/software_zone/2007/1017/560580.shtml
Communication between VC and Delphi using message mechanism (send custom message)