天天看点

_declspec(dllexport)和_declspec(dllimport)

含义:

__declspec(dllexport)导出到dll

__declspec(dllimport)从dll导入

解决的问题:

 考虑下面的需求,使用一个方法,一个是提供者,一个是使用者,二者之间的接口是头文件。头文件中声明了方法,在提供者那里方法应该被声明为__declspec(dllexport),在使用者那里,方法应该被声明为__declspec(dllimport)。

举例:

#ifndef _INC_MYSOCKETCLIENT_H_
#define _INC_MYSOCKETCLIENT_H_
#define Import_SSS
#ifdef Import_SSS
#define API _declspec(dllexport)
#else 
#define API _declspec(dllimport)
#endif
 
#ifdef _cplusplus//extern"C" 来告诉编译器:这是一个用C写成的库文件,请用C的方式来链接它们。
extern "C" {
#endif // _cplusplus
    API//导出函数,让外界调用。
    int socketClient_Init(void **handle);
    API
    int socketClient_Send(void *handle, unsigned char *buf, int buflen);
    API
    int socketClient_Recv(void *handle, unsigned char *buf, int *buflen);
    API
    int socketClient_Destory(void *handle);
#ifdef _cplusplus
}
#endif // _cplusplus
#endif //_INC_MYSOCKETCLIENT_H_
       

存在Import_SSS的宏定义是提供者,不包含时,作为使用者。在Window时,采用LoadLibrary的方式加载dll时,二者都可以。