dynamic link library -- Run-Time dynamic linking

//dll project 

 

.h

View Code
#ifdef __cplusplus

extern"C"
{

#endif

#ifdef DLL_DECL
#define DLL_DECLSPEC __declspec(dllexport)

#else
#define DLL_DECLSPEC __declspec(dllimport)
#endif



 DLL_DECLSPEC int   Add(int, int); 

#ifdef __cplusplus
}
#endif

.cpp & .def

View Code
#include <Windows.h>
#include <stdio.h>

#define  DLL_DECL

#include "DllTest1.h"



 BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
 {
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
            printf("attached\n");
            break;

    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
            break;
    }
            return TRUE;
 }


 int  Add(int a, int b)
 {
     return a + b;
 }
LIBRARY "DllTest1"

EXPORTS
 Add @1

//app project

main.cpp

 

View Code
#include <Windows.h>

#include <stdio.h>
//#include "..\\dllmain\DllTest1.h"
//#pragma comment(lib, "..//release//DllTest1.lib")

//extern "C" __declspec(dllimport) int WINAPI Add(int, int);

typedef  int  ( *pAdd)(int, int);


int main()
{

    HMODULE hDll = LoadLibrary("..\\release\\dlltest1.dll");
    
    pAdd add = (pAdd)GetProcAddress(hDll,"Add");
    
    
    printf("4 + 6 = %d\n", add(4, 6));
    

    return  0;
}

 

posted on 2013-02-26 01:17  All IN  阅读(174)  评论(0)    收藏  举报

导航