During the DLL development process, especially during underlying hardware development, such as real-time receiving data, process needs and application interaction, while the best interaction of DLL and applications is issued, and the means of defining messages in DLL The way to define messages in the application is very similar. Let's talk about these two definitions:
First, in the application custom message method:
General custom message has a certain range, although the custom message starts from WM_USER, but because of our projects, there are many other controls, they also take up part of the WM_USER message range, so we must be for them. Leave a part of the scope, 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. There must be coordinated: message statement, message mapping, message body. We will do it in hand.
(2) First add a message 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 Message Function:
lpeSult CMYView :: ONMYMSG (WPARAM WPARAM, LPARAM LPARAM)
{
AfxMessageBox ("The message has been received!");
Return 0;
}
The message has been defined here, 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 parts in DLL and applications.
2. After the application calls the DLL program, it is necessary to pass the application's window handle to the DLL so that the message in the DLL returns.
In the DLL project:
(1) Add a message definition in the stdafx.h header file:
#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 the 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 file:
#define WM_MSG WM_USER 102
(2) First, add a message 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 box, in the button event, call the DLL file first, then send a test message response:
Void ctestMessagedldlg :: OnbnclicKedButton1 ()
{
// Todo: Add control notification handler code here
// definition function
TypedEf void (_CDECL * StartsendMessage) (HWND HWND);
Bamboo
hmodule hmessage = null;
StartsendMessage StartsendMessage = NULL;
// Import DLL library file
himentage = loadingLibrary ("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 test message function
(* StartsendMessage) (HWND);
}
Run the app, you can see the test results:
Program source code: Messagedll.rar (http://www.51pub.net/class/classlibrary/20046984317374.rar)