Discussion on plugin programs (on) [Original] DLL

xiaoxiao2021-03-06  39

Discussion on plugin programs (on) [Original] DLL

Preface: This article describes the idea of ​​plug-in design through a simple engineering. Complex design and modular decomposition capabilities are an important sign of a programmer level, welcome everyone to discuss.

1. Our purpose is to find the dynamic link library of the PTZ * .dll format in the PLUG-INS directory. Each library supports multiple protocols, with this plugin to create an instance of a protocol by looking for an instance of a protocol to operate the PTT.

2. Structural definition:

Comparam

Communication parameters for serial port: (defined in the include / comManager / comparam.h file)

Class Comparamext_API Comparam

{

PUBLIC:

Comparam (int nbaudrate, // baud rate

INT NBYTESIZE = 8, // Data Bit

INT nParity = 0, // Check bit 0 - Noparity

INT NSTOPBITS = 0); // Stop bit 0 - onestopbit (refer to definition in WinBase.h)

}

3. In order to achieve the purpose of using the DLL, first package a few simple classes:

CDLLOAD class: SRC / PTZ_TEST / Controls / DLLLOAD.H (this class is only in src / ptz_test / controls / ptzdll.h)

// dlload.h # prgma overce class cdlload {public:

CDLLOAD ()

{} Virtual ~ cdllload (void) {close ();} Void Open (LPCTSTR LPSZNAME) // Load DLL {if (m_hmodule) {close ();} m_hmodule = :: loadLibrary (lpszname);} void close () { :: FreeLibrary (m_hModule); // unload DLL m_hModule = NULL;} operator HMODULE () {return m_hModule;} PROC GetProcAddress (LPCSTR lpProcName) {return :: GetProcAddress (m_hModule, lpProcName); //} protected function entry address acquired : HModule m_hmodule;}; cptzdll class: src / ptz_test / controls / dllload.h (main function: According to the support of the support of the DLL, then the required function is called by the function of the object, due to the actual function Using the member function of the class of the DLL, there must also be an instance of cptzdll when the object exists and it is open.

) // PTZDll.h # pragma once #include "DllLoad.h" #include "PTZInterface.h" class CPTZDll {typedef CPTZInterface * (__stdcall * CREATE_PTZ_INSTANCE) (const char * pszProtocolsName); public: CPTZDll () // initialize member : m_pDllLoad (0), m_pfnCreatePTZInstance (0), m_ppProtocolsName (0) {} ~ CPTZDll () {Close (); delete m_pDllLoad; m_pDllLoad = 0;} BOOL OpenPlugIn (LPCTSTR lpszName) // open the plug, if it is valid plugin returns TRUE, otherwise FALSE {if (m_pDllLoad) Close (); else {m_pDllLoad = new CDllLoad (); m_pDllLoad-> Open (lpszName);} m_pfnCreatePTZInstance = (CREATE_PTZ_INSTANCE) m_pDllLoad-> GetProcAddress ( "CreatePTZInstance"); m_ppProtocolsName = ( const char **) m_pDllLoad-> GetProcAddress ( "ProtocolNames"); return (m_ppProtocolsName && m_ppProtocolsName [0]);} void Close () {m_pfnCreatePTZInstance = 0; m_ppProtocolsName = 0; if (m_pDllL oad) m_pDllLoad-> Close ();} const char ** GetSupportProtocols () {return m_ppProtocolsName;} CPTZInterface * CreatePTZInstance (LPCTSTR lpszProtolName) // produce an object based on a protocol within the plug {if (m_pfnCreatePTZInstance) {return m_pfnCreatePTZInstance (lpszProtolName);} return 0;} private: CDllLoad * m_pDllLoad; CREATE_PTZ_INSTANCE m_pfnCreatePTZInstance; const char ** m_ppProtocolsName;}; CPlugInInfo categories: src / PTZ_Test / controls / PTZProtocolFind.h and src / PTZ_Test / controls / ptzprotocolfind.cpp

/ / =========================================================================================================================================================================================== ========= //// copyright (c) 2000-2004 iWise Technologies, co. Ltd./// ALL RIGHTS Reserved.///product: IW988 // File: ptzprotocolfind.h // created : Tiangye has seamless // data: 2004.12.22 pm // description: // valueadded main program for q988.//undact: // Waterpub@mail.9cbs.net/ / / ======== ================================================== # Pragma ONCE

Class CPluginInfo // Plugin Information Class {public: String SzFileName; // File Name Vector Protocols; // Protocol list String SzfullPath; // Full Path Public: CPluginInfo (Void) {} ​​~ cpluginInfo (void) {} }; class cptzprotocolfind {public: cptzprotocolfind (void); ~ cptzprotocolfind (void); public: void enumplugins (vector & pluginlist); // enumerate the PTZ file} in the PLUG-INS directory;

/ / =========================================================================================================================================================================================== ========= //// Copyright (C) 2000-2004 IWISE TECHNOLOGIES, CO. Ltd.///////// Created : Tiangye has seamless // data: 2004.12.22 pm // description: // valueadded main program for q988.//undact: // Waterpub@mail.9cbs.net/ / / ======== ================================================== # INCLUDE "stdafx.h"

#include "ptzprotocolfind.h"

#include "ptzdll.h"

CPTZPROTOCOLFIND :: CPTZPROTOCOLFIND (VOID)

{

}

CPTZPROTOCOLFIND :: ~ CPTZPROTOCOLFIND (VOID)

{

} // Enumerate all valid plugins in the "plug-inscription" directory

Void cptzprotocolfind :: Enumplugins (Vector & pluginlist): VOID CPTZPROTOCOLFIND :: ENUMPLUGINFO> & PLUGINLIST)

{

Char szfilepath [MAX_PATH];

:: GetModuleFileName (Null, SzFilePath, Max_Path);

Char * pfind = strrchr (szfilepath, '//');

* pfind = '/ 0';

STRCAT (SZFILEPATH, "//Plug-ins/ptz*.dll"); // Find match string

CFILEFIND FIND;

Bool bfind;

bfind = find.findfile (SZFilePath);

While (bfind)

{

CPTZDLL DLL;

BFIND = Find.FindNextFile ();

IF (Find.isarchived ()) // file instead of folder

{

// Output this file:

Xtrace ("Path:% S / N", Find.getFilePath ());

IF (DLL.OpenPlugin (Find.GetFilePath ()))) // If it is a cloud block plugin, you enumerate its protocol

{

CPluginInfo Info;

Const char ** ppprotocols = dll.getsupportprotocols ();

IF (! PPPROTOCOLS)

{

CONTINUE;

}

While (0! = * ppprotocols)

{

String _string = * ppprotocols;

Info.Protocols.push_back (_string);

PPPROTOCOLS ;

}

Info.szFullPath = Find.GetFilePath ();

Info.szFileName = Find.getFileName ();

PluginList.push_back (info);

}

}

DLL.Close ();

}

Find.close (); // below is debugging code

Xtrace ("Slugs:% D / N", PluginList.size ());

Vector :: item it = pluginlist.begin ();

For (; it! = pluginlist.end (); it )

{

Xtrace ("file name:% s ----" file path:% s / n ", (* it) .szFilename.c_str (), (* it) .szFullPath.c_str ());

}

} 4. Add two members variables in the form of dialog project: cptzdll m_ptzdll; // DLL load object cptzinterface * m_ptzinterface; // is being used by plugin protocol object users Select a plugin: BOOL BRETURN = m_ptzdll.OpenPlugin (m_strprotocolpath) ); Then select an agreement according to the plugin: m_ptzinterface = m_ptzdll.createptzinstance (m_strprotocolname); // Parameter is a string representing the protocol. At this time we can use this M_PtzInterface object to perform various operations. Description: The class "cptzinterface" of m_ptzinterface is an abstraction class that has a wide variety of protocols. It is defined as follows:

//Ptzinterface.h

// iWise DVR PTZ Interface Define Header File.

#pragma overce

#include "ptzdef.h"

#include "comparam.h"

Class Cptzinterface

{

PUBLIC:

Virtual Void SetPTZSettings (int Ncomport, Int Naddrid, Const Comparam & PARAM) = 0;

Virtual void Move (movement_dir ndirection) = 0;

Virtual void Action (int NActionID, int param) = 0;

Virtual Int setspeed (int nspeedtrgid, int nspeed) = 0;

Virtual Void DeviceSwitch (int NDeviceID, Bool Bswitch) = 0;

Virtual int setPreset (int NINDEX) = 0;

Virtual Int CallPreset (INT NINDEX) = 0;

Virtual void reset () = 0;

Virtual Int selfcheck () = 0;

Virtual Int getCaps (int ncapid, void * lparam) = 0;

Virtual int extendop (Int nexopid, void * lparam) = 0;

Virtual void destroy () = 0;

}

Reference documentation:

Yuntai Control Protocol Plugin Development Document

This document describes the program structure, design principle, plug-in interface and other important information about the IWISE-DVR PTZ control protocol plug-in, and iWiseptzTest is a plugin's test program (plugin can also be put directly into the iWise-DVR environment). The "DVR" that appears below this document refers to the development of DVR-based cloud-based control protocol plug-in using a serial port-based cloud control device.

Plugin works

PTZ controls There are a variety of different control protocols, and DVR provides a unified plug-in interface to support different cloud control protocols to control the control operation of PTZ through the protocol plugin. The DVR starts to scan all the file names in the PLUG-INS directory with "PTZ" dynamic connection library (DLL), and determine if these DLLs are valid PTZ control protocol plugins. DVR defines the basic operation of a set of cloud-based control. The plugin must provide specific implementation of these operations. When the user wants to control the PTZ, the DRV finally calls the plugin method to control the PTZ, the plugin needs to generate corresponding according to the protocol feature Command data and send these instructions to the serial port device. At the same time, DVR provides a unified method to send data to the serial port.

Yuntai Control Protocol Plugin Interface

DVR Definition Class Cptzinterface As the PTZ Control Protocol interface, this class is the core of the interface, and the plugin must be derived with CPTzInterface to derive a class and implement all methods. The CPTzinterface class is defined as follows:

Class Cptzinterface

{

PUBLIC:

Virtual Void SetPTZSettings (int Ncomport, int Naddrid,

Const Comparam & PARAM) = 0;

Virtual void Move (movement_dir ndirection) = 0;

Virtual void Action (int NActionID, int param) = 0;

Virtual Int setspeed (int nspeedtrgid, int nspeed) = 0;

Virtual Void DeviceSwitch (int NDeviceID, Bool Bswitch) = 0;

Virtual int setPreset (int NINDEX) = 0;

Virtual Int CallPreset (INT NINDEX) = 0;

Virtual void reset () = 0;

Virtual Int selfcheck () = 0;

Virtual Int getCaps (int ncapid, void * lparam) = 0;

Virtual int extendop (Int nexopid, void * lparam) = 0;

Virtual void destroy () = 0;

}

SetPTZSettings function Sets the serial number, address code, and communication parameters of the cloud station device.

Ncomport - the serial number where the cloud station is located

NadDrid - device address code

PARAM - Communication parameters, reference to the Comparam structure (see "IWISE-DVR Environment Serial Operating Interface Instructions").

The movement of the MOVE function controls the movement of the cloud platform in the directions.

NDIRECTION-Motion Direction, Defined by Movement_Dir

ENUM MOVEMENT_DIR

{

MD_STOP, // Stopping

MD_LEFT, / /

MD_Right, //

MD_UP, //

MD_DOWN, / /

MD_LEFT_UP, //

MD_LEFT_DOWN, //

MD_Right_UP, //

MD_Right_Down, // Right

}

The Action function controls the action of the PTZ camera.

NACTIONID-action definition

Action_ZOOM Zoom, PARAM = 0 is zoomed in zooming in PARAM = 1

Action_ZOOM_STOP stop scaling

Action_focus Focus, PARAM = 0 is nearly nearby to focus 1

Action_focus_stop Stop Focus

Action_AUTO_FOCUS Auto Focus

Action_AUTO_FOCUS_STOP Stop Auto Focus

Action_AUTO_SCAN automatic scan

Action_AUTO_SCAN_STOP stops automatic scanning

SetSpeed ​​Sets the speed of the cloud block or camera action.

Nspeedtrgid- want to set the speed of the speed

Speed_trg_pan horizontal direction

Speed_trg_tilt vertical direction

Action_AUTO_SCAN automatic scan

Action_AUTO_SCAN_STOP stops automatic scanning

NSPEED - speed value (0x00-0x40)

If successful, the function returns the set speed, otherwise it returns 0.

The DEVICESWITCH function provides a function of turning off a device.

NDEviceID-device ID definition

Device_camera camera

Device_light light

Device_Rainbrush Water Brush

Device_iris lens aperture

BSwitchon-true is open device, False is a shutdown device

Setpreset function Sets the preset point.

NINDEX-preset point index number, starting from 1, using getCaps to acquire the total number of preset points that the device can support.

Returns 0 is success, otherwise failed

The CallPreset function calls the preset point and moves the PTZ camera to the specified preset point position.

NINDEX-Preset Point Search, which is valid for index numbers that have been successfully set using SetPreset.

Returns 0 is success, otherwise failed

The RESET function restores the PTZ camera device to the initial state.

The Selfcheck function self-tests the PTZ camera device.

Returns 0 is success, otherwise failed

The getCaps function can obtain the capabilities that the device can provide.

Ncapid- The ID definition of the ability to achieve, please refer to the following value

GC_BASAL achieves basic capabilities, lparam is a pointer to the PTZBasalCaps structure.

LPARAM-points to a pointer to a specific capacity structure.

Returns 0 is success, otherwise failed

The Extendop function provides extension features to the CPTZinterFace interface.

NEXOPID- Extended Function ID Definition.

LPARAM - has different significances according to different Nexopids.

This function is currently not used

The DESTROY function is responsible for destroying its own object.

The destruction method must be consistent with the creation of CreatePtzinstance.

Device Basic Ability Structure Definition:

Struct ptzbasalcaps

{

INT nsize; // ptzbasalcaps structure

INT npresetcount; / / Support for the number of preset points

INT nminspeed; // Yuntai mobile speed minimum

INT nmaxSpeed; // Yuntai mobile speed maximum

Bool bautoscansupported; / / Do you support automatic scanning

}

Plug-in DLL interface agreement

After the specific implementation method of cptzinterface is implemented, the plug-in DLL must provide the method of constructing and destroying the CPTZinterface object, so the following must be exported: • cptzinterface * __declspec (dllexport) Createptzinstance

Const char * pszprotocolsname;

PSZProtocolsname - A protocol name, which is obtained by the GetSupportProtocols function.

This function constructs a protocol interface object and returns its pointer, which is destroyed by the CPTzinterface :: Destroy () function. For security, the plugin can also destroy all objects that the user does not destroy when uninstall.

• const char ** __DECLSPEC (DLLEXPORT) GetSupportProtocols ();

NPROTOCOLS - Supported Agreement

This function returns the list of protocols supported by the plugin

GetSupportPROTOCOLS code example:

Char * szprotocol [] = {

"Pelco-D",

"Pelco-P",

0 // must end with 0

}

Const char ** __DECLSPEC (DLLEXPORT) GetSupportProtocols ()

{

Return (const char **) szprotocol;

}

The above code indicates that the plugin supports "Pelco-D" and "Pelco-P" agreement.

Class Cptzinterface declares in ptzinterface.h.

For the sending of the serial port instruction, please refer to "IWISE-DVR Environment Serial Operating Interface Instructions".

转载请注明原文地址:https://www.9cbs.com/read-76731.html

New Post(0)