Windows communication mechanism and DLL Mechanism

Source: Internet
Author: User

DLL

<1> event. h

#include<windows.h>#include<math.h>#ifndef LIB_H#define LIB_Hextern "C" void  _declspec(dllexport)setCalculate(HWND  hwnd,HWND  hwnd1,HWND  hwnd2,HWND  hwnd3,HWND  hwnd4);extern "C" void  _declspec(dllexport)setClearAll(HWND  hwnd1,HWND  hwnd2,HWND  hwnd3,HWND  hwnd4);#endif

 

<2> event. cpp

# Include "event. H "char money [] =" "; char rate [] =" "; char year [] =" "; char total [] = ""; /* ++ ++ * main function: check ++ * function: Input judgment function ++ * parameter: A [] input string ++ * return value: returns true if it is all numeric values. Otherwise, returns false +++ ++ ++ * /bool check (char a []) {int I; int Len = strlen (a); for (I = 0; I <Len; I ++) {if (a [I]> '9' | A [I] <'0') {return false ;}} return true ;} /* ++ ++ * main function: setcalculate ++ * function: Add a response for calculatebutton + input principal, interest rate, and total annual output amount + + * parameter: hwnd parent window handle + hwnd1 principal input box handle + hwnd2 interest rate input box handle + hwnd3 years input box handle + hwnd4 output box handle + * return value: ++ ++ */void setcalculate (hwnd, hwnd hwnd1, hwnd hwnd2, hwnd hwnd3, hwnd hwnd4) {getwindowtext (hwnd1, money, 20); getwindowtext (hwnd2, rate, 20); getwindowtext (hwnd3, year, 20 ); double totalmoney; If (check (money) & check (rate) & check (year) {totalmoney = atof (money) * POW (1/atof (Rate) + 1, atoi (year); gcvt (totalmoney, 10, total); setwindowtext (hwnd4, total);} else {MessageBox (hwnd, text ("sorry, the format you entered is not "), text (" prompt "), mb_ OK); setwindowtext (hwnd1," "); setwindowtext (hwnd2," "); setwindowtext (hwnd3, ""); setwindowtext (hwnd4 ,"");}} /* ++ ++ * main function: setclearall ++ * function: Add a response for clearbutton + + clear all input box output box + + * parameter: hwnd1 principal input box handle + hwnd2 interest rate input box handle + hwnd3 years input box handle + hwnd4 output box handle + * return value: ++ ++ */void setclearall (hwnd hwnd1, hwnd hwnd2, hwnd hwnd3, hwnd hwnd4) {setwindowtext (hwnd1, ""); setwindowtext (hwnd2, ""); setwindowtext (hwnd3, ""); setwindowtext (hwnd4, "");}

 

Window

<1> window. h

// Windows program header file # include <windows. h> extern char sztitle []; // Title Bar string extern char szwindowclass []; // window class name extern char szmenuname []; extern int ID, id_1; // function prototype declaration atom initapplication (hinstance); // used to register the window class bool initinstance (hinstance, INT); // used to create the main window lresult callback wndproc (hwnd, uint, wparam, lparam); // message processing function int apientry winmain (hinstance, hinstance, lpstr, INT); // main function bool createcontrol (hinstance, hwnd, lpstr); // create a text box

 

<2> window. cpp

# Include "window. H "# include <math. h> // global variable char sztitle [] = "Windows communication mechanism and DLL mechanism"; // Title string char szwindowclass [] = "W32 "; // window class name char szmenuname [] = "null"; // menu name typedef void (* lpsetcalculatefun) (hwnd, hwnd ); typedef void (* lpsetclearallfun) (hwnd, hwnd); hwnd hwnd1, hwnd2, hwnd3, hwnd4, calculatebutton, clearbutton; hwnd; /* ++ ++ ++ * Main function: winmain ++ * function: the initialization and registration window class creates a window and enters the message loop and terminates the application. The ++ * parameter is as follows: the current hinstance instance ++ the previous instance of hprevinstance ++ the lpcmdline command line ++ ncmdshow selects the display window or icon ++ * return value: MSG. wparam ++ ++ +++ */INT apientry winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {MSG; // declare the message structure object initap Plication (hinstance); // registration window if (! Initinstance (hinstance, ncmdshow) // initialize {return false;} while (getmessage (& MSG, (hwnd) null, 0, 0 )) // message loop {translatemessage (& MSG); // retrieves the message dispatchmessage (& MSG); // indirectly transmits the message to the function pointer} return MSG. wparam; // The End message is sent back to postquitmesage in a loop}/* ++ ++ ++ * main function: initapplication ++ * function: used to register the window class ++ * parameter: hinstance current instance ++ * return value: if the operation succeeds, a unique integer is returned. If the operation fails, 0 ++ is returned. ++ ++ */Atom initapplication (hinstance) {wndclass WC; // declare the window structure object // initialize the object's Domain value WC. style = cs_hredraw | cs_vredraw; // if the size of the redrawing window is changed, the WC. lpfnwndproc = wndproc; // window message processing function WC. cbclsextra = 0; // No additional window class memory WC. cbwndextra = 0; // No additional window memory WC. hinstance = hinstance; // The WC of the current instance. hicon = loadicon (null, idi_application); // icon WC. hcursor = loadcur Sor (null, idc_arrow); // pointer WC. hbrbackground = (hbrush) getstockobject (white_brush); // background painter WC. lpszclassname = szwindowclass; // window class name WC. lpszmenuname = szmenuname; // menu name return registerclass (& WC ); // register}/* ++ ++ * main function: initinstance ++ * function: used to create the main window + * parameter: hinstance current instance + + ncmdshow select display window or icon + + * return value: If successful, a non-zero value is returned, if the request fails, 0 ++ is returned. ++ ++ */Bool initinstance (hinstance, int ncmdshow) {// declare window handle hwnd = createwindow (szwindowclass, // window class name, use the registered main window class sztitle, // window name, ws_overlappedwindow | ws_vscroll | ws_hscroll, which is displayed on the title bar of the window. // The display mode supports horizontal and vertical scrolling of cw_usedefault, // X starting coordinate of cw_usedefault, // y starting coordinate of 400, // 300 in width, // null in height, // null in the non-parent window, // hinstance in the menu handle, // null in the instance handle // No window creation count Data); If (! Hwnd) // determine whether the creation is successful {return false;} createcontrol (hinstance, hwnd, null); showwindow (hwnd, ncmdshow ); // The Window updatewindow (hwnd) is displayed after the creation is successful; // return true when the wm_paint message is sent ;} /* ++ ++ * main function: createcontrol ++ * function: Create a text box + * parameter: hwndp parent window handle + + sztitle window title + + * return value: ++ ++ */bool createcontrol (H Instance hinstance, hwnd hwndp, lpstr sztitle) {hwnd1 = createwindow ("edit", null, ws_child | ws_visible | ws_border, 50, 40,100, 20, hwndp, null, hinstance, null); calculatebutton = createwindow ("button", "calculation", ws_child | ws_visible | ws_border, 180, 20, hwndp, null, hinstance, null ); clearbutton = createwindow ("button", "clear", ws_child | ws_visible | ws_border, 180, 60, 20, hwndp, null, hinstance, NUL L); hwnd2 = createwindow ("edit", null, ws_child | ws_visible | ws_border, 50, 70,100, 20, hwndp, null, hinstance, null ); hwnd3 = createwindow ("edit", null, ws_child | ws_visible | ws_border, 50,100,100, 20, hwndp, null, hinstance, null); hwnd4 = createwindow ("edit", null, ws_child | ws_visible | ws_vscroll | ws_border | es_left | es_multiline | es_autovscroll, 50,130,300,100, hwndp, null, hinstance, null); showwind Ow (hwnd1, sw_show); showwindow (calculatebutton, sw_show); showwindow (clearbutton, sw_show); showwindow (hwnd2, sw_show); showwindow (hwnd3, sw_show); showwindow (hwnd4, sw_show); Return true ;} /* ++ ++ * main function: wndproc ++ * function: Message Processing Function ++ * parameter: hwnd instance handle + message + + domain in wparam MSG message structure + + domain in lparam MSG message structure + * return value: 0 ++ ++ ++ */Lresult callback wndproc (hwnd, uint message, wparam, lparam) {paintstruct pS; HDC; Switch (Message) {Case wm_paint: HDC = beginpaint (hwnd, & PS); textout (HDC, 150, 10, "interest calculator", strlen ("interest calculator"); textout (HDC, 10, 40, "principal:", strlen ("principal:"); textout (HDC, 10, 70, "Interest Rate:", strlen ("Interest Rate:"); textout (HDC, 10,100, "year:", strlen ("year:"); endpa INT (hwnd, & PS); break; Case wm_command: hinstance hdll; lpsetcalculatefun setcalculatefun; lpsetclearallfun setclearallfun; hdll = loadlibrary (".. \ dll \ debug \ DLL. DLL "); // obtain the DLL module handle if (hdll! = NULL) {setcalculatefun = (Counter) getprocaddress (hdll, "setcalculate"); // get the export address of the function setclearallfun = (lpsetclearallfun) getprocaddress (hdll, "setclearall "); // obtain the function export address if (setcalculatefun! = NULL) {If (loword (lparam) = loword (calculatebutton) {setcalculatefun (hwnd, hwnd1, hwnd2, hwnd3, hwnd4) ;}} if (setclearallfun! = NULL) {If (loword (lparam) = loword (clearbutton) {setclearallfun (hwnd1, hwnd2, hwnd3, hwnd4) ;}} freelibrary (hdll );} else {MessageBox (hwnd, text ("hdll creation failed"), text ("prompt"), mb_ OK) ;}break; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}

 

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.