How to send a message to a C # class in an MFC DLL

Source: Internet
Author: User

How to send a message to a C # class in an MFC DLL

First, Introduction

Because Windows message is the universal Data Flow format for the Windows platform, message is a good choice when transferring data across languages, and this document describes how to send messages to the C # window class in an MFC DLL.

Second, the realization process

1. Create an MFC-based DLL project, which is used to generate DLL library functions;

2. Add a new class to the project, Cmessager;

3. Add the following code to the header file:

1 #define Zs_api extern "C" _declspec (dllexport) void23 ZS_API _sendmessagetocs (HWND hwnd, UINT MSG, WPARAM Wpara M, LPARAM LPARAM);

4. Add the following code to the CPP file:

1 ZS_API _sendmessagetocs (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM LPARAM) 2 {3    SendMessage (hWnd, MSG, WParam, LParam); 4 }

5. Create a new C # windowsapplication test Engineering Testmfcdllmessage;

6. Add Msgtest normal class, the code in the class is as follows:

1  Public class msgtest 2 {3      Public Const int 0x400 ; 4      Public Const int 0x100 ; 5 }

7. Add the Messager class with the following code in the class:

1  Public classMessager2 {3[DllImport ("MfcDllSendMessage.dll", EntryPoint ="_sendmessagetocs", CharSet =CharSet.Auto)]4 5      Public Static extern void_sendmessagetocs (IntPtr hWnd, UInt32 Msg, UInt32 WParam, UInt32 lParam);6}

8. Add a "Call MFC DLL Send Message" button to Form1, and double-click to add the following code to the event response:

1 Private void button1_click (object  sender, EventArgs e)2{3     Messager._sendmessagetocs (this); 4 }

9. Override the default window procedure in the Form1 class with the following code:

1 protected Override voidDefwndproc (refMessage m)2 {3     Switch(m.msg)4     {5      Casemsgtest.msg_test:6         stringStrinfo =string. Format ("Hwnd:{0}\r\nmsg:{1}\r\nwparam:{2}\r\nlparam:{3}", M.hwnd.tostring (), m.msg.tostring (), m.wparam.tostring (), m.lparam.tostring ());7 MessageBox.Show (strinfo);8          Break;9 Ten     default: One         Base. Defwndproc (refm); A          Break; -     } -}

Build the entire project

Third, the realization effect

Click "Invoke MFC DLL to send Message" with the following effect:

Notice of reprint:

Please respect my intellectual property rights and labor results, this article can be freely reproduced, please specify the source:

Blog URL: http://www.cnblogs.com/zs8861/

Contact e-mail:[email protected]

Welcome to the Exchange!

How to send a message to a C # class in an MFC DLL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.