VC++ DLL创建与使用_v2

来源:互联网 发布:unity3d gui text 编辑:程序博客网 时间:2024/06/09 14:31

1 DLL类型:有三种
         Win32DLL: 不使用MFC库,可被任何程序使用。
         MFC Regular DLL 使用MFC库,与MFC库静态或动态连接,可被任何程序使用。
         MFC Extension DLL ,与MFC动态联合编译,只能被MFC程序使用。
   三种DLL的异同:

2 DLL的创建与调用
    2.1 Win32DLL的创建
          两种方法:A通过.h文件定义要输出的资源 B通过.def文件定义要输出的资源
     2.1.1用.h文件定义要输出的资源:
        例子:
        win32dll_h.h
              extern "C" __declspec(dllexport) int MaxNum(int a, int b);
         win32dll_h.cpp
          DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
         {
               switch(ul_reason_for_call)
               {
                case DLL_PROCESS_ATTACH:
                case DLL_THREAD_ATTACH:
                case DLL_THREAD_DETACH:
                case DLL_PROCESS_DETACH:
                break;
                }
                return TRUE;
         } 

         int MaxNum(int a, int b)
        {
              if(a>b) return a;
              else return b;
        }
    2.1.2用.def文件定义要输出的资源
       w32dll_def.def
             ; ; is the notes symbol in a def file
             LIBRARY W32DLL_DEF

             DESCRIPTION  "W32DLL_DEF Windows Dynamic Link Library"

             ; Function name must equal  the name in .cpp file and sensitive to 大小写
             EXPORTS
                    MinNum
       W32dll_def.cpp
                     int MaxNum(int a, int b)
                     {
                       if(a<b) return a;
                      else return b;
                     }
    2.1.3 隐式调用W32DLL中的函数
           相同的部分:将.lib文件和.dll文件copy到要调用的程序的目录下或系统目录(/windows等)下
           不同的部分:是调用前的声明。
            .h创建的dll: extern "C" __declspec(dllimport) int MaxNum(int a, int b);
            .def创建的dll:                 __declspec(dllimport) int MaxNum(int a, int b);

                                      不要前面的extern "C"
   2.2 MFC Regular DLL的创建

原创粉丝点击