A reliable DLL Interface implementation solution

Source: Internet
Author: User

[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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.