1 #ifndef __cpBaseQtApplication__MainHelper__h__
2 #define __cpBaseQtApplication__MainHelper__h__
5 #include <QApplication>
7 // -------------------------------------------------------------------------
8 #define cpBaseQtApplication_Main( _window_class_ ) \
9 int main( int argc, char* argv[] ) \
11 QApplication a( argc, argv ); \
12 _window_class_ w( argc, argv ); \
14 return( a.exec( ) ); \
17 // -------------------------------------------------------------------------
18 #include <cpPlugins/Config.h>
20 #ifdef cpPlugins_OS_Windows
25 # include <shellapi.h>
27 // -------------------------------------------------------------------------
28 namespace cpBaseQtApplication
32 class Win32CommandLineConverter
35 std::unique_ptr< char*[ ] > argv_;
36 std::vector< std::unique_ptr< char[ ] > > storage_;
39 Win32CommandLineConverter( )
41 LPWSTR cmd_line = GetCommandLineW( );
43 LPWSTR* w_argv = CommandLineToArgvW( cmd_line, &argc );
44 argv_ = std::unique_ptr< char*[ ] >( new char*[ argc ] );
45 storage_.reserve( argc );
46 for( int i = 0; i < argc; ++i )
48 storage_.push_back( ConvertWArg( w_argv[ i ] ) );
49 argv_[ i ] = storage_.back( ).get( );
56 return( static_cast< int >(storage_.size( ) ) );
60 return( argv_.get( ) );
62 static std::unique_ptr< char[ ] > ConvertWArg( LPWSTR w_arg )
64 int size = WideCharToMultiByte(
65 CP_UTF8, 0, w_arg, -1, nullptr, 0, nullptr, nullptr
67 std::unique_ptr< char[ ] > ret( new char[ size ] );
69 CP_UTF8, 0, w_arg, -1, ret.get( ), size, nullptr, nullptr
77 // -------------------------------------------------------------------------
78 # define cpBaseQtApplication_MainComplement \
79 int CALLBACK WinMain( HINSTANCE i, HINSTANCE p, LPSTR c, int s ) \
81 cpBaseQtApplication::Win32CommandLineConverter cmd_line; \
82 return( main( cmd_line.argc( ), cmd_line.argv( ) ) ); \
84 #else // cpPlugins_OS_Windows
85 # define cpBaseQtApplication_MainComplement
86 #endif // cpPlugins_OS_Windows
88 #endif // __cpBaseQtApplication__MainHelper__h__