#ifndef _MODULE_PATH_
#define _MODULE_PATH_

#pragma warning(disable:4290)

#include <vector>

inline std::string GetModuleFileName() throw(std::exception)
{
	std::vector<char> path(MAX_PATH+1, '\0');
	while( path.size() < 10 * MAX_PATH )
	{
		DWORD length = ::GetModuleFileName( NULL, &path[0], path.size() );
		if( 0 == length )
		{ // failed
			DWORD dwLastError = ::GetLastError();
		}
		else if( path.size() > length )
		{ // succeeded
			return std::string( path.begin(), path.begin() + length );
		}
		else if( path.size() == length )
		{ // buffer too small
			// enlarge buffer and retry
			path.resize( 2 * path.size() );
			continue;
		}
		else
		{ // unexpected return value from GetModuleFileName()
		}
	}

	throw std::exception("ModuleFileName is too long");
}

inline std::string GetModuleFileDirectory() throw(std::exception)
{
	std::string path = GetModuleFileName();
	std::string::size_type pos = path.rfind( '\\' );

	if( std::string::npos == pos )
	{

	}

	return path.substr( 0, pos + 1 );
}

#endif