Windows 통신 메커니즘 과 DLL 메커니즘

DLL
<1>event.h
#include<windows.h>
#include<math.h>
#ifndef LIB_H
#define LIB_H
extern "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[] = "";
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                     +
+         *   :Check                                              + 
+         *  :                                               +
+         *  :  a[]                                          +
+         *   :      TRUE     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;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                     +
+         *   :setCalculate                                       + 
+         *  :   CalculateButton                              +
+                      ,  ,                             +
+         *  :   hwnd                                         +
+                   hwnd1                                      +
+					hwnd2                                      +
+					hwnd3                                      +  
+					hwnd4                                        +
+         *   :                                                   +
+                                                                     +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void setCalculate(HWND 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("   ,        "),TEXT("  "),MB_OK);
		SetWindowText(hwnd1,"");
		SetWindowText(hwnd2,"");
		SetWindowText(hwnd3,"");
		SetWindowText(hwnd4,"");
	}
	
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                     +
+         *   :setClearAll                                        + 
+         *  :   ClearButton                                  +
+                                                           +
+         *  :   hwnd1                                      +
+					hwnd2                                      +
+					hwnd3                                      +  
+					hwnd4                                        +
+         *   :                                                   +
+                                                                     +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void setClearAll(HWND  hwnd1,HWND  hwnd2,HWND  hwnd3,HWND  hwnd4)
{
	SetWindowText(hwnd1,"");
	SetWindowText(hwnd2,"");
	SetWindowText(hwnd3,"");
	SetWindowText(hwnd4,"");
}

 
window
<1>window.h
//windows      
#include<windows.h>
extern char szTitle[];                                           //      
extern char szWindowClass[];                                     //    
extern  char szMenuName[] ; 
extern  int ID,ID_1;

//      
ATOM    InitApplication(HINSTANCE);                              //       
BOOL     InitInstance(HINSTANCE ,  int);                          //       
LRESULT CALLBACK WndProc( HWND,  UINT,  WPARAM,  LPARAM);       //      
int   APIENTRY WinMain(  HINSTANCE ,  HINSTANCE ,  LPSTR ,  int);  //   
BOOL  CreateControl(HINSTANCE,HWND, LPSTR);                       //                                                 

 
<2>window.cpp
#include "window.h"
#include<math.h>
//    
char szTitle[] = "Windows     DLL  ";                           //      
char szWindowClass[] = "w32";                                                  //    
char szMenuName[] = "NULL";                                                  //   


typedef void (*lpsetCalculateFun)(HWND ,HWND ,HWND ,HWND ,HWND);
typedef void (*lpsetClearAllFun)(HWND ,HWND ,HWND ,HWND);


HWND hwnd1,hwnd2,hwnd3,hwnd4,calculateButton,clearButton;
HWND hwnd;    

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                               +
+         *   :WinMain                                                      + 
+         *  :            ,    ,                 +
+         *  :   hInstance                                               +
+                       hPrevInstance                                      + 
+                       lpCmdLine                                            + 
+                       nCmdShow                                       +  
+         *   :msg.wParam                                                   +
+                                                                               +
++++++++++++++++++++++++++++++++++++++++++++++++*+++++++++++++++++++++++++++++++*/


int APIENTRY WinMain(HINSTANCE     hInstance,    HINSTANCE  hPrevInstance ,    LPSTR    lpCmdLine ,    int   nCmdShow )
{
	MSG msg;                                                                         //        
	InitApplication( hInstance);                                                  //    

	if(   !InitInstance(  hInstance,  nCmdShow  )   )                   //   
	{
		return FALSE;
	}


	while (  GetMessage(&msg, (HWND) NULL, 0, 0)  )             //    
	{
		TranslateMessage(&msg);                                              //                                                  
		DispatchMessage(&msg);                                              //           
	}
	return  msg.wParam;                                                          //        PostQuitMesage 
}

/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                          +
+         *   :InitApplication                                         + 
+         *  :                                                   +
+         *  :   hInstance                                          +
+         *   :    ,         ,    ,  0           +
+                                                                          +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

ATOM InitApplication(HINSTANCE     hInstance)
{
	WNDCLASS wc;                                                                                //        
	//        
	wc.style                      =              CS_HREDRAW | CS_VREDRAW;                       //             
	wc.lpfnWndProc                =             WndProc;                                        //        
	wc.cbClsExtra                 =              0;                                             //          
	wc.cbWndExtra                 =              0;                                             //        
	wc.hInstance                  =              hInstance;                                     //    
	wc.hIcon                      =              LoadIcon(NULL,  IDI_APPLICATION);              //   
	wc.hCursor                    =              LoadCursor(NULL, IDC_ARROW);                   //     
	wc.hbrBackground              =              (HBRUSH)GetStockObject(WHITE_BRUSH);           //     
	wc.lpszClassName              =               szWindowClass;                                //     
	wc.lpszMenuName               =               szMenuName;                                   //   

	return  RegisterClass(&wc);                                                                 //  
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                      +
+         *   :InitInstance                                        + 
+         *  :                                               +
+         *  :   hInstance                                      +
+                       nCmdShow                              +
+         *   :    ,       ,    ,  0           +
+                                                                      +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


BOOL InitInstance(HINSTANCE     hInstance,      int   nCmdShow)
{                                                       //      
	hwnd   =  CreateWindow(
			 szWindowClass,                                              //     ,           
		     szTitle,                                                    //    ,             
		     WS_OVERLAPPEDWINDOW |WS_VSCROLL|WS_HSCROLL,                 //                 
		     CW_USEDEFAULT,                                              // x    
		     CW_USEDEFAULT,                                              // y    
		    400,                                                         //  
		     300,                                                        //  
		    NULL,                                                        //     
		    NULL,                                                        //     
		    hInstance,                                                   //    
		    NULL                                                         //        
		);
	if(!hwnd)                                                            //         
	{
		return FALSE;
	}
    CreateControl( hInstance,  hwnd, NULL);
	ShowWindow(hwnd,  nCmdShow);                                          //         
	UpdateWindow(hwnd);                                                    //  WM_PAINT  
	return  TRUE;
}


/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                  +
+         *   :CreateControl                                   + 
+         *  :                                             +
+         *  :   hwndp                                     +
+                   szTitle                                    +
+         *   :                                                +
+                                                                  +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
BOOL  CreateControl(HINSTANCE     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","  ",  WS_CHILD |WS_VISIBLE|WS_BORDER,                       
			 180, 40,60, 20,                                                                                                                                                      
		     hwndp, NULL,hInstance, NULL);

	clearButton   =  CreateWindow( "BUTTON","  ",  WS_CHILD |WS_VISIBLE|WS_BORDER,                       
			 180, 70,60, 20,                                                                                                                                                      
		     hwndp, NULL,hInstance, NULL);

   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);

	ShowWindow(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;
}



/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                     +
+         *   :WndProc                                            + 
+         *  :                                               +
+         *  :  hwnd                                           +
+                  message                                          +
+                  wParam           MSG                        +
+                  lParam           MSG                        +
+         *   :0                                                  +
+                                                                     +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


LRESULT  CALLBACK WndProc(HWND  hwnd , UINT   message, WPARAM   wParam, LPARAM    lParam)
{
	PAINTSTRUCT    ps;
	HDC  hdc;
	

	switch(message)
	{
	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);
		TextOut(hdc, 150 , 10, "     ", strlen("     "));
		TextOut(hdc, 10 , 40, "  :", strlen("  :"));
		TextOut(hdc, 10 , 70, "  :", strlen("  :"));
		TextOut(hdc, 10 , 100, " :", strlen(" :"));
		EndPaint(hwnd,&ps);
		break;
	case WM_COMMAND:
		HINSTANCE hDll;
	    lpsetCalculateFun setCalculateFun;
		lpsetClearAllFun setClearAllFun;
	    hDll = LoadLibrary("..\\DLL\\Debug\\DLL.dll");                                 //  DLL    
	    if(hDll!=NULL)
		{
			setCalculateFun =  (lpsetCalculateFun)GetProcAddress(hDll,"setCalculate");//         
			setClearAllFun =  (lpsetClearAllFun)GetProcAddress(hDll,"setClearAll");   //         
			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     "),TEXT("  "),MB_OK);
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	return 0;
}


좋은 웹페이지 즐겨찾기