📄 videomanfactory.cpp
字号:
//#include "StdAfx.h"
#include <iostream>
#include "VideoManFactory.h"
using namespace std;
VideoManFactory::VideoManFactory(void)
{
}
VideoManFactory::~VideoManFactory(void)
{
for ( loadedModulesIter m = loadedModules.begin(); m != loadedModules.end(); ++m )
FreeLibrary( (*m).second);
}
void VideoManFactory::LoadModules( const std::string &path )
{
#ifdef WIN32
//char path2[MAX_PATH];
WIN32_FIND_DATA fd;
DWORD dwAttr = FILE_ATTRIBUTE_ARCHIVE;
//sprintf( path2, "%s\\*", path.c_str() );
const std::string modulesPath = path + "\\*.dll";
HANDLE hFind = FindFirstFile( modulesPath.c_str(), &fd);
if(hFind == INVALID_HANDLE_VALUE)
{
FindClose( hFind);
return;
}
do
{
if ( fd.dwFileAttributes & dwAttr )
{
std::string fileName = fd.cFileName;
if ( fileName.rfind(".dll") != std::string::npos )
{
HINSTANCE hinstLib = LoadLibrary( fd.cFileName );
//Check the version
typedef std::string (*checkVersionImportFunction)();
checkVersionImportFunction checkVersion = (checkVersionImportFunction)GetProcAddress(hinstLib, "checkVersion");
if ( checkVersion == NULL )
continue;
#ifdef _DEBUG
if ( checkVersion() != "DEBUG" )
{
FreeLibrary(hinstLib);
continue;
}
#endif
#ifdef NDEBUG
if ( checkVersion() != "RELEASE" )
{
FreeLibrary(hinstLib);
continue;
}
#endif
//Get the module indentifiers
typedef void (*getIdentifiersImportFunction)( std::vector< std::string >& );
getIdentifiersImportFunction getIdentifiers = (getIdentifiersImportFunction)GetProcAddress(hinstLib, "getIdentifiers");
if ( getIdentifiers != NULL )
{
std::vector< std::string > identifierList;
getIdentifiers( identifierList );
for ( size_t i = 0; i < identifierList.size(); ++i )
{
identifiersMap[identifierList[i]] = fileName;
}
moduleList.push_back( fileName );
}
FreeLibrary(hinstLib);
}
}
}while( FindNextFile( hFind, &fd));
FindClose( hFind);
#endif
}
VideoInput *VideoManFactory::createUserInput( VideoManInputFormat *format )
{
if ( format == NULL )
{
cerr << "createUserInput: Must specify the format" << endl;
return NULL;
}
UserInput *userInput = new UserInput();
if ( !userInput->init( *format ) )
{
delete userInput;
return NULL;
}
return userInput;
}
VideoInput *VideoManFactory::createVideoInput( const inputIdentification &device, VideoManInputFormat *format )
{
VideoInput *input;
if ( device.identifier == "USER_INPUT" )
{
input = createUserInput( format );
}
else
{
//Check if the identifier is known
if ( identifiersMap.find( device.identifier ) == identifiersMap.end() )
{
cerr <<"ERROR: unknown identifier " << device.identifier << endl;
return NULL;
}
std::string moduleName = identifiersMap[device.identifier];
//Check if the module was already loaded
HINSTANCE hinstLib = getModuleHandle( moduleName );
if ( hinstLib == NULL )
hinstLib = LoadLibrary( moduleName.c_str() );
if ( hinstLib == NULL )
{
cerr << "ERROR: unable to load DLL " << moduleName << endl ;
return NULL;
}
typedef VideoInput* (*importFunction)( const inputIdentification &device, VideoManInputFormat *format );
importFunction initFunc = (importFunction)GetProcAddress(hinstLib, "initVideoInput");
if ( initFunc == NULL )
{
FreeLibrary(hinstLib);
cerr << "ERROR: unable to find initVideoInput DLL function in " << moduleName << endl ;
return NULL;
}
input = initFunc( device, format );
if ( input == NULL )
{
delete input;
return NULL;
}
loadedModules[moduleName] = hinstLib;
}
return input;
}
VideoManFactory &VideoManFactory::getInstance()
{
static VideoManFactory instance;
return instance;
}
HINSTANCE VideoManFactory::getModuleHandle( std::string moduleName )
{
if ( loadedModules.find( moduleName ) != loadedModules.end() )
{
return loadedModules[moduleName];
}
else
{
return NULL;
}
}
void VideoManFactory::getAvailableDevices( std::vector<inputIdentification> &deviceList )
{
for ( size_t m = 0; m< moduleList.size(); ++m )
{
getAvailableDevicesFromModule( moduleList[m], deviceList );
}
}
void VideoManFactory::getAvailableDevices( const std::string &identifier, std::vector<inputIdentification> &deviceList )
{
//Check if the identifier is known
if ( identifiersMap.find( identifier )== identifiersMap.end() )
return;
getAvailableDevicesFromModule( identifiersMap[identifier], deviceList );
}
void VideoManFactory::getAvailableDevicesFromModule( const std::string &moduleName, std::vector<inputIdentification> &deviceList )
{
//Check if the module was already loaded
bool freeModule = false;
HINSTANCE hinstLib = getModuleHandle( moduleName );
if ( hinstLib == NULL )
{
hinstLib = LoadLibrary( moduleName.c_str() );
freeModule = true;
}
if ( hinstLib == NULL )
{
cerr << "ERROR: unable to load DLL" << endl;
return;
}
typedef void (*importFunction)( std::vector<inputIdentification> &deviceList );
importFunction getAvailableDevices = (importFunction)GetProcAddress(hinstLib, "getAvailableDevices");
if ( getAvailableDevices == NULL )
{
cerr << "ERROR: unable to find getAvailableDevices DLL function in " << moduleName << endl;
if ( freeModule )
FreeLibrary(hinstLib);
return;
}
//Get the module devices
std::vector<inputIdentification> newDevices;
getAvailableDevices( newDevices );
//Insert the devices into the list
for ( size_t d = 0; d < newDevices.size(); ++d )
{
deviceList.push_back( newDevices[d] );
}
if ( freeModule )
FreeLibrary(hinstLib);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -