[Cpp]
// Service. h, DLL definition (used by implementers and callers)
//////////////////////////////////////// ////////////////////////////////////////
# 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 implementation
//////////////////////////////////////// ////////////////////////////////////////
# 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> the installation code to be executed when regsvr32.exe Service. dll
Return S_ OK;
}
SERVICE_API hresult winapi DllUnregisterServer ()
{
// CMD> uninstall code to be executed when regsvr32.exe/u Service. dll
Return S_ OK;
}
// Demo. cpp, DLL call
//////////////////////////////////////// ////////////////////////////////////////
# 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;
}