STL & Boost
Dll Loader ( LoadLibrary, GetProcAddress, FreeLibrary ) PDF Print E-mail

An dieser Stelle möchte ich eine neue Klasse in meiner BonnyCXX Klassensammlung vorstellen: Dll.
Diese Klasse vereinfacht das arbeiten mit Dll's enorm. Laden, Funktionen suchen und Entladen sind mit einer Zeile Code möglich.

Hier die aktuelle Header Datei.


/*
Bonny C++ Library - libbonny

Axel Sauerhoefer
Jockgrimerstr. 13
76764 Rheinzabern

axel[at]willcodeqtforfood.de
http://www.willcodeqtforfood.de

This file is part of libbonny.

Libbonny is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Libbonny is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Libbonny.If not, see <http://www.gnu.org/licenses/>.


3:6:2009 18:44
*/

#ifndef _BONNYCXX_SYSTEM_DLL_H
#define _BONNYCXX_SYSTEM_DLL_H 1

#include <boost/tuple/tuple.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <boost/function.hpp>

namespace BonnyCxx{
namespace System {

/**
* @brief DllVersion
*
* Typedef a simple tuple to store the dll version information.
*
* 1: The major version of the DLL. If the DLL's version is 4.0.950, this value will be 4.
* 2: The minor version of the DLL. If the DLL's version is 4.0.950, this value will be 0.
* 3: The build number of the DLL. If the DLL's version is 4.0.950, this value will be 950.
*/
typedef boost::tuple< std::wstring, // The major version of the DLL
std::wstring, // The minor version of the DLL
std::wstring // The build number of the DLL
> DllVersion;

/**
* @brief forward and typedefs
*/
class Dll;
typedef boost::shared_ptr<Dll> DllPtr;

/**
* @brief Dll
*
* Simple dll class wrapper, simple working with dll's ( e.g. load, execute methods ... )
*
* @code
*
* using namespace BonnyCxx::System;
*
* //Create an interface pointer
* DllPtr pDllInstance = Dll::Create( L"shdocvw.dll" );
*
* //Load the library
* if( pDllInstance->Load() == false )
* {
* return false;
* }
*
* //Get the dll version if available
* DllVersion version = pDllInstance->GetVersion();
*
* std::wcout << L"Dll Major version: " << version.get<0>() << std::endl;
* std::wcout << L"Dll Minor version: " << version.get<1>() << std::endl;
* std::wcout << L"Dll Build version: " << version.get<2>() << std::endl;
*
* // Or resolve directly the method by using GetFunction function like this
* DLLGETVERSIONPROC pFunc = pDllInstance->Resolve<DLLGETVERSIONPROC>( L"DllGetVersion" );
*
* if( pFunc )
* {
* DLLVERSIONINFO dvi = {};
* dvi.cbSize = sizeof(dvi);
*
* const HRESULT hr = (*pFunc)(&dvi);
*
* if( SUCCEEDED( hr ) )
* {
* std::wcout << L"Dll Major version: " << dvi.dwMajorVersion << std::endl;
* std::wcout << L"Dll Minor version: " << dvi.dwMinorVersion << std::endl;
* std::wcout << L"Dll Build version: " << dvi.dwBuildNumber << std::endl;
* }
* }
* //Unlaod the dll
* if( pDllInstance->UnLoad() == false )
* {
* return false;
* }
*
* @endcode
*/
class Dll : public boost::noncopyable
{
public:

/**
* D'Tor
*/
~Dll(){};

/**
* Try to load the library
*
* @return bool true on success otherwise false
*/
bool Load();

/**
* Unload the library
*
* @return bool on success otherwise false
*/
bool UnLoad();

/**
* Check if the given dll is loaded or not.
*
* @return bool true if the dll was successfully loaded, otherwise false
*/
const bool IsLoaded() const;

/**
* Get version information about the dll
*
* @return DllVersion
*/
const DllVersion GetVersion() const;

/**
* Resolve a dll function/method by given signature,
* Must use ugly void*, virtual template functions not possible ;)
*/
template< typename Func >
Func Resolve( const std::wstring& signature ) const
{
std::string ascii( signature.length(), ' ');
std::copy( signature.begin(), signature.end(), ascii.begin() );
return reinterpret_cast<Func>( GetProcAddress( m_handle, ascii.c_str() ) );
}

/**
* Inplace factory, create an instance of DllPtr @see DllPtr
*
* @param std::wstring dll name
* @return DllPtr object
*/
static DllPtr Create( const std::wstring& dll );

private:

/**
* C'tor
*
* Construct our Dll object, and store the given dll name into
* a member variable m_dllName.
*/
explicit Dll( const std::wstring& dll )
: m_dllName( dll ),
m_handle( NULL )
{

}

/**
* hold the dll name, e.g. path
*/
std::wstring m_dllName;

/**
* Dll handle
*/
HMODULE m_handle;

}; //end class IDll

} //end namespace System
} //end namespace BonnyCxx


#endif //_BONNYCXX_SYSTEM_DLL_H

 Der gesamte Quellcode ist in folgendem Subversion Repository verfügbar: svn://willcodejoomlaforfood.de/bonny/trunk/bonny

 

Last Updated on Friday, 05 June 2009 06:47
 
std::ostream &operator PDF Print E-mail
User Rating: / 2
PoorBest 

Hier eine kleine C++ Template Klasse, mit welcher man sehr einfach den std::ostream &operator << bedienen kann. Sinn und Zweck
dieser Klasse ist es, eine einfache Handhabung des genannten operators zu ermöglichen. Z.b. beim Logging oder Debuging.

Die Klasse ist ein Teil meines neusten Projektes Bonny. Bonny ist meine persönliche C++ Bibliothek, in welcher ich hin und wieder
Klassen packe, die sich als besonder nütlich in Projekten gemacht haben.

Bonny findet man natürlich auch bei Ohloh: https://www.ohloh.net/projects/Bonny

Die Klasse IOStreamout:

/*
Bonny C++ Library - libbonny

Axel Sauerhoefer
Beethovenring 20
76761 Ruelzheim

axel[at]willcodeqtforfood.de
http://www.willcodeqtforfood.de

This file is part of libbonny.

Libbonny is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Libbonny is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with libbonny. If not, see <http://www.gnu.org/licenses/>.


18:10:2008 22:54
*/

#ifndef _BONNYCXX_OSTREAMOUT_H_
#define _BONNYCXX_OSTREAMOUT_H_ 1

#include <iostream>
#include <boost/noncopyable.hpp>

namespace BonnyCxx{

/*
* @brief IOStreamOut
*
* Template class for wrapping object to stream it out to an std::ostream.
* The wrapped class must have a std::string ToString method implemented.
*
* @code
*
* class Foo
* {
* ...
* public:
* void ToString()
* {
return "FooBar";
}
* }; //end class Foo
*
* ...
* Foo obj;
* std::cout << IOStreamOut<Foo>(obj); // the ToString method is called
*
* @endcode
*/

/*
* Forwarder
*/
template<typename T>
class IOStreamOut;

template<typename T>
std::ostream& operator<<(std::ostream& os, const IOStreamOut<T>& obj)
{
std::string buffer = obj.Get().ToString();
os.write( buffer.c_str(), static_cast<std::streamsize>(buffer.size()) );
return os;
}

template<typename T>
class IOStreamOut
{
public:

/*
* C'tor
*/
IOStreamOut( const T& obj )
: m_obj( obj )
{
}

/*
* D'tor
*/
virtual ~IOStreamOut(){};

/*
* ostream operator
*/
friend std::ostream &operator<< <>(std::ostream &, const IOStreamOut<T>& obj);

/*
* Get the wrapped object
*/
T Get() const
{
return m_obj;
}

private:

/*
* C'tor
*
* Default constructor should never be called.
*/
IOStreamOut(){};

/*
* The wrapped object
*/
T m_obj;

};//end class IOStreamOut

}//end namepsace BonnyCxx

#endif //_BONNYCXX_OSTREAMOUT_H_

 

Hier ein Beispiel:

class Foo
{

public:

Foo(){};
~ Foo(){};

std::string ToString() const
{
return "This is Foo";
}

};


Foo obj;
IOStreamOut<Foo> wrapper(obj);
std::cout<<wrapper;

Die einzigste Bedingung die erfüllt sein muss ist, das die Klasse die "gewrapped" werden soll eine ToString Mehtode implementiert, welche dann von IOStreamout aufgerufen wird.

Weiterführende Links: 

Last Updated on Thursday, 23 October 2008 21:23
 
1_36_0 unter Windows in 5 Minuten PDF Print E-mail

Hier ein kleines Howto boost 1_36_0 kompilieren ( unter Windows ). Voraussetzung natürlich Visual Sutdio Installation.

1. Quellen besorgen

Speicher und entpacken nach c:/boost_1_36_0

2. Konfigurieren und bauen

Visual Studio Prompt öffnen und folgende Befehle eingeben:

cd c:/boost_1_36_0
bjam --build-dir=build --toolset=msvc --build-type=complete stage
 
boost::tokenizer mit wchar_t PDF Print E-mail
User Rating: / 5
PoorBest 

Hier ein paar kleine Zeilen Code wie man den boost::tokenizer mit Wide Characters benutzt. Leider habe ich in der Boost Dokumentation kein Beispiel gefunden ;(

using namespace std;
using namespace boost;

std::wstring myText = L"This-is-an-simple-test";

typedef tokenizer< char_separator<wchar_t>, std::wstring::const_iterator, std::wstring> wtokenizer;
char_separator<wchar_t> seperator(L"-");

wtokenizer tokens(inputText,seperator);
           
for( wtokenizer::iterator it = tokens.begin();  it != tokens.end(); ++it)
{
    wcout << *it << std::endl;
}

Last Updated on Thursday, 07 August 2008 13:29