DLL (MFC) transmits data to C # program through window message

Source: Internet
Author: User

This is a problem I encountered when developing ioserver with C #.ProgramThe DLL originally developed with MFC needs to be used. The biggest problem is that some information during the DLL running process, and data is sent to the specified hwnd (Form handle) through the window message. The Data Pointer is placed in the wparam parameter, and the lparam parameter is the data type information.

C # provide M. the getlparam () function is a pity that my data is stored in the wparam parameter (this cannot be modified yet; otherwise, the original program cannot run and the modification cost is too high), so this function cannot be used.

After multiple tests, the data is obtained successfully.

Related in MFC DLLCode:

Tchar cmessage [255];
Systemtime stime;
Getlocaltime (& stime );
Swprintf (cmessage, _ T ("% 04d-% 02d-% 02d % 02d: % 02d: % 02d [%-16 s # % 03d]-% s "), stime. wyear, stime. wmonth, stime. wday, stime. whour, stime. wminute, stime. wsecond, strsource, lngno, strmessage );
If (g_iomrun.lngmsgflag = 0)
{
Sendmessage (g_iomrun.hwnd, wm_user+ 7722, (long) cmessage, lngtype );//
}

In C #, the message processing function of the form needs to be reloaded. The related code is as follows:

[Dllimport ("Kernel32", entrypoint = "copymemory")]
Public static extern void copymemory (stringbuilder destination, intptr source, int length );
/// <Summary>
/// Reload the Window Function
/// </Summary>
/// <Param name = "M"> </param>
Protected override void wndproc (ref message m)
{
//---------------------
If (M. MSG = 0x0400 + 7722)
{
Stringbuilder strdata = new stringbuilder (255 );
Copymemory (strdata, M. wparam, 255 );
Showinfo (INT) M. lparam, strdata. tostring ());
}
//---------------------
Base. wndproc (ref m );
}

Note: the Declaration of the copymemory function must be modified by yourself (the original declaration is as follows). You must remove the "Ref" keyword; otherwise, the result will be incorrect.

[Dllimport ("Kernel32", entrypoint = "copymemory")]
Public static extern void copymemory (
Ref int destination,
Ref int source,
Int Length
);

 

 

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.