每日一贴,今天的内容关键字为实现接口
每日一道理
美丽是平凡的,平凡得让你感觉不到她的存在;美丽是平淡的,平淡得只剩下温馨的回忆;美丽又是平静的,平静得只有你费尽心思才能激起她的涟漪。
美丽是平凡的,平凡得让你感觉不到她的存在;美丽是平淡的,平淡得只剩下温馨的回忆;美丽又是平静的,平静得只有你费尽心思才能激起她的涟漪。
// Service.h, DLL定义(供实现方、调用方使用)
#ifdef SERVICE_EXPORTS
#define SERVICE_API extern "C" __declspec(dllexport)
#else
#define SERVICE_API
//#define SERVICE_API extern "C" __declspec(dllimport)
#endif
interface IService
{
public:
IService() {}
virtual ~IService() {}
public:
virtual void Start() = 0;
virtual void Stop() = 0;
};
typedef IService *(*CreateInstance_t)();
typedef void (*DestroyInstance_t)(IService *pInst);
SERVICE_API IService *CreateInstance();
SERVICE_API void DestroyInstance(IService *pInst);
// Service.cpp, DLL实现
#include "Service.h"
class CService : public IService
{
public:
CService();
virtual ~CService();
public:
virtual void Start();
virtual void Stop();
private:
//.....
};
CService::CService()
{
}
CService::~CService()
{
}
void CService::Start()
{
}
void CService::Stop()
{
}
IService *CreateInstance()
{
return new CService();
}
void DestroyInstance(IService *pInst)
{
if (!pInst) return;
delete pInst;
}
SERVICE_API HRESULT WINAPI DllRegisterServer()
{
//CMD> regsvr32.exe Service.dll 时须要执行的安装代码
return S_OK;
}
SERVICE_API HRESULT WINAPI DllUnregisterServer()
{
//CMD> regsvr32.exe /u Service.dll 时须要执行的卸载代码
return S_OK;
}
// Demo.cpp, DLL调用
#include "Service.h"
int WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
HMODULE hDll = LoadLibraryA("Service.dll");
if (hDll)
{
CreateInstance_t pfnCreateInstance =
(CreateInstance_t)GetProcAddress(hDll, "CreateInstance");
DestroyInstance_t pfnDestroyInstance =
(DestroyInstance_t)GetProcAddress(hDll, "DestroyInstance");
if (pfnCreateInstance && pfnDestroyInstance)
{
IService *pInst = pfnCreateInstance();
if (pInst)
{
pInst->Start();
pInst->Stop();
pfnDestroyInstance(pInst);
}
}
FreeLibrary(hDll);
}
return 0;
}
文章结束给大家分享下程序员的一些笑话语录: 《诺基亚投资手机浏览器UCWEB,资金不详或控股》杯具了,好不容易养大的闺女嫁外国。(心疼是你养的吗?中国创业型公司创业初期哪个从国有银行贷到过钱?)
--------------------------------- 原创文章 By
实现和接口
---------------------------------