#include <assert.h> // assert
#include <memory> // std::unique_ptr
#include <sstream> // std::wostringstream
#include <stddef.h> // ptrdiff_t
#include <stdexcept> // std::runtime_error, std::exception
#include <stdlib.h> // EXIT_FAILURE, EXIT_SUCCESS
#include <string> // std::wstring
#include <vector> // std::vector
using namespace std;
#include <winplus/wrapper/windows_h.h> // A [windows.h] wrapper -- u can use your own!
#include <shellapi.h> // CommandLineToArgvW
typedef ptrdiff_t Size;
typedef Size Index;
bool throwX( string const& s ) { throw std::runtime_error( s ); }
bool hopefully( bool const condition ) { return condition; }
namespace simple {
class CommandLineArgs
{
private:
struct DeleterUsingLocalFree
{
void operator() ( wchar_t const* const* p ) const
{
::LocalFree( const_cast< wchar_t** >( p ) );
}
};
static wchar_t const* const* parsed( wchar_t const* const commandLine, int& n )
{
return ::CommandLineToArgvW( commandLine, &n );
}
vector< wstring > arguments_;
public:
Size n() const { return arguments_.size(); }
wstring const& operator[]( Index i ) const { return arguments_[i]; }
wstring const* begin() const { return &arguments_[0]; }
wstring const* end() const { return begin() + n(); }
explicit CommandLineArgs( wstring const& commandLine = GetCommandLine() )
{
typedef unique_ptr< wchar_t const* const [], DeleterUsingLocalFree > ArrayPtr;
typedef ArrayPtr::pointer RawArrayPtr;
int n;
ArrayPtr a( parsed( commandLine.c_str(), n ) );
hopefully( a != 0 )
|| throwX( "CommandLineArgs::<init>: CommandLineToArgvW failed" );
arguments_.resize( n );
for( int i = 0; i < n; ++i ) { arguments_[i] = a[i]; }
}
};
} // namespace simple
int main()
{
wstring const commandLine = GetCommandLine();
simple::CommandLineArgs const args( commandLine );
wostringstream text;
text << L"Command line:" << endl;
text << L"[" << commandLine << L"]" << endl;
text << endl;
text << L"As individual arguments:" << endl;
for( Index i = 0; i < args.n(); ++i )
{
text << L"#" << i << L":\t[" << args[i] << "]" << endl;
}
DWORD const infoBoxOptions = MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND;
MessageBox( 0, text.str().c_str(), L"Command line info:", infoBoxOptions );
}