Method of processing message in mixed programming of VB and VC

Source: Internet
Author: User

Now more and more people use VB and VC mixed programming: With VB quickly developed a beautiful interface and peripheral processing program, and then use VC to write the bottom of the various operations, such as memory operation, IO port operation, VC can also embed assembly language for more low-level operation.

The general practice is to compile the VC program into a DLL, in VB in the Declare statement declaration of functions in the DLL, for example:

Declare Function SendCommand Lib″c:\program Files\devstudio\wjfprojects\hr0506dllmnsr\debug\hr0506dllmnsr.dll″ ( CmdNum as Integer) as Boolean

After the declaration, the function SendCommand can be used as VB own function.

However, how to VC in a variety of messages (such as a variety of low-level status) in time to inform VB and VB can be processed in time? This is a flaw in VB: It is not easy to work with custom Windows messages. For example, in VC custom two messages: Mw_tapi_datacome,mw_tapi_busy, in C language, with PostMessage (hwnd,mw_tapi_connected,0,0), the statement can easily mw_tapi_ The connected message is passed to the form (the form handle is an HWND) and can be processed in the primary callback function. But to pass this message to VB, and let VB processing this message, not too easy. Although in VB5.0 version, you can define your own events (event), but more cumbersome. Here is an easy way to let VB handle custom messages skillfully:

1. First pass the handle of the TextBox control Text1 in the VB form to the DLL (note: The TextBox control in VB has an HWND attribute).

In VB: the declaration of functions in a DLL

Declare Function sendhwnd lib″c:\program files\devstudio\wjfprojects\hr0506dllmnsr\debug\ Hr0506dllmnsr.dll″ (ByVal HWnd as Long) as Boolean

The handle of Text1 passing

Sendhwnd (Text1.hwnd)

VC://Handle receive function

   declspec(dllexport) BOOL GethWnd(HWND hWnd1)
   { hWnd=hWnd1;
   if (hWnd==NULL)
   { return FALSE; }
   return TRUE;}

2. In VC, use API function SetWindowText to replace postmessage to send message directly to Text1 control in VB:

SetWindowText (Hwnd,″mw_tapi_datacome″);

3. To process a message in the Text1_change event:

  Private Sub Text1_Change()
   Select case Text1.text
   case W_TAPI_DATACOME:
   ′消息处理语句
   case MW_TAPI_BUSY:
   ′消息处理语句
   default:
   ′其他处理语句
   End Select
   End Sub

To? The method is used in the "TAPI Remote Monitoring System" written by the author and has a good effect.

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.