#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
typedef int(_cdecl* FunctionAdd)(int,int);
/*
Template designed by RuPeng.com. Please visit http://www.rupeng.com for more information
如鹏网(http://www.rupeng.com)大学生计算机学习社区,提供大量免费视频学习教程,提供个性化一对一学习指导
*/
BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
        HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
        HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
    }
    return FALSE;
}
BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
    return TRUE;
}
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
    switch(id)
    {
        case IDC_OK:
        {
        HMODULE hModule;
        FunctionAdd add;
        hModule = LoadLibrary("Newdll.dll");
        //调试时hModule为0x10000000
        //LoadLibrary返回的就是dll的句柄
        //参数可以是相对路径,也可以是绝对路径
        if(NULL==hModule )
            {
            MessageBox(hwnd,TEXT("加载dll失败"),TEXT("错误"),MB_OK);
          
            }
            add =(FunctionAdd)GetProcAddress(hModule,"add");
            if(NULL==add)
            {
            MessageBox(hwnd,TEXT("加载dll失败"),TEXT("错误"),MB_OK);
          
            }
            int r=add(1,1);
            TCHAR msg[256];
            wsprintf(msg,"1+1=%i",r);
            MessageBox(hwnd,msg,TEXT("错误"),MB_OK);
            FreeLibrary(hModule);
                      
          
        }
        break;
        default:
        break;
    }
}
void Main_OnClose(HWND hwnd)
{
    EndDialog(hwnd, 0);
}