During the DLL development process, especially during the underlying hardware development, such as real-time receiving data, the process needs and the application interacts, and the best interaction of the DLL and the application is sent, and the message is defined in the DLL. And the way to define messages in your application is very similar. Let's talk about these two definitions:
First, in the application custom message method:
The general custom message has a certain range, although the custom message starts from the WM_USER, but because of the generals in our project, they should also take a part of the WM_USER message range, so we must leave a part of them. Here, we retain 100 messages, in general, this can meet our requirements.
(1) Define the value of the message. In the place where we want to happen (such as CMYVIEW.CPP's start part) or stdafx..h file, do the following definition:
#define WM_MSG (WM_USER 101)
The next work is actually very simple. We have said before, and there are 3 parts of the news to work. You must coordinate: message declaration, message mapping, message body. We will do it in hand.
(2) First join the message declaration in the AFX_MSG block: In CMYVIEW.H, find the following section, and join the message declaration:
protected:
// {{AFX_MSG (CMYVIEW)
......
AFX_MSG LRESULT ONMYMSG (WPARAM WPARAM, LPARAM LPARAM);
File: //}} AFX_MSG
(3) Add an on_message macro in the Message_Map block:
Begin_MESSAGE_MAP (CMYVIEW, CVIEW)
File: // {{AFX_MSG_MAP (CMYVIEW)
.....
ON_MESSAGE (WM_MSG, ONMYMSG)
File: //}} AFX_MSG_MAP
END_MESSAGE_MAP ()
(4) Add a message function:
LpeSult CMYView :: ONMYMSG (WPARAM WPARAM, LPARAM LPARAM)
{
AfxMessageBox ("The message has been received!");
Return 0;
}
The news is already defined, and then we can activate the message, you can send a message with the postMessage / SendMessage we mentioned earlier.
Such as :: PostMessage (HWND, WM_MSG, 0, 1);
Postmessage: No such message returns.
SendMessage: Need to wait for the message to return.
Second, pass the message from the DLL to EXE
Define messages in DLL and above, there are two different places:
1. Define the same message in two places in the DLL and applications.
2, after the application calls the DLL program, you need to pass the application's window handle to the DLL so that the message in the DLL is returned.
In the DLL project:
(1) Add a message definition in the stdafx.h header:
#define WM_MSG WM_USER 102
(2) Add the output function of the start message:
CMessagedLlapp theApp;
//send messages
Extern "C" _Declspec (dllexport) Void StartsendMessage (HWND HWND)
{
THEAPP.SENDMESSAGE (HWND);
}
Where hwnd is a window handle that receives the message.
(3) Add the implementation function of the start message: Add a function declaration in the header file:
Void SendMessage (HWND HWND);
Add functions to the CPP file implementation
// Start sending message
Void CMessagedllapp :: SendMessage (HWND HWND)
{
:: PostMessage (HWND, WM_MSG, 0, 1);
}
In the application:
(1) Add a message definition in the stdafx.h header:
#define WM_MSG WM_USER 102
(2) First, add a message declaration in the AFX_MSG block: In CTestMessageDLDLG.H, find the following section and join the message declaration:
. . . . . .
AFX_MSG LRESULT ONMYMSG (WPARAM WPARAM, LPARAM LPARAM);
Declare_message_map ()
(3) Add an on_message macro in the Message_Map block:
Begin_Message_Map (CtestMessagedLDLG, CDIALOG)
. . . . . .
ON_MESSAGE (WM_MSG, ONMYMSG)
//}} AFX_MSG_MAP
END_MESSAGE_MAP ()
(4) Add a message function:
Lresult CTestMessagedLDLG :: Onmymsg (WPARAM WPARAM, LPARAM LPARAM)
{
AfxMessageBox ("The message has been received!");
Return 0;
}
(5) Add a button on the dialog, in the button event, call the DLL file first, then send a test message response:
Void ctestMessagedldlg :: OnbnclicKedButton1 ()
{
// TODO: Add control notification processing code here
// Define functions
Typedef void (_cdecl * startsendment measure) (hwnd hwnd);
HModule HMESSAGE = NULL;
StartsendMessage StartsendMessage = NULL;
// Import DLL library file
HMESSAGE = LoadLibrary ("Messagedll.dll");
IF (HMESSAGE == NULL)
{
Freelibrary (HMESSAGE);
exit (0);
}
// Import the test message function in the DLL
StartsendMessage = (StartsendMessage) GetProcadDress (HMessage, "StartsendMessage);
IF (StartSendMessage == NULL)
{
Freelibrary (HMESSAGE);
Exit (1);
}
/ / Get the window handle of the dialog
HWND HWND = this-> getsafehwnd ();
// Send a test message function
(* StartsendMessage) (HWND);
}
Run the app, you can see the test results:
Program source code: Messagedll.rar reference: http://www.51pub.net/class/showproject.asp? Art_ID = 213