Skip to content

Instantly share code, notes, and snippets.

@zhester
Created June 15, 2014 14:32
Show Gist options
  • Save zhester/569ce2a0e666aa8db3b2 to your computer and use it in GitHub Desktop.
Save zhester/569ce2a0e666aa8db3b2 to your computer and use it in GitHub Desktop.
Example of a poor-man's WinMain-to-main entry point adapter.
/*=========================================================================*/
int WINAPI WinMain( //Windows program entry point
HINSTANCE hInstance, //handle to this application
HINSTANCE hPrevInstance,
//handle to previous application
LPTSTR lpCmdLine, //executed command
int nCmdShow //window display options
) { //program exit status
//local variables
int argc; //number of parsed arguments
char** argv; //list of parsed arguments
int i; //index/counter
int length; //lengths of strings
int result; //result of program execution
LPWSTR* strings; //list of parsed arguments
//retrieve parsed arguments from Windows-style invocation
strings = CommandLineToArgvW( GetCommandLineW(), &argc );
//allocate string pointers for argument list
argv = calloc( argc, sizeof( char* ) );
if( argv == NULL ) {
LocalFree( strings );
return 1;
}
//convert each argument from unicode to multibyte
for( i = 0; i < argc; ++i ) {
length = wcslen( strings[ i ] );
argv[ i ] = calloc( ( length + 1 ), 1 );
wcstombs( argv[ i ], strings[ i ], length );
}
//execute the main program
result = main( argc, ( const char** ) argv );
//release the allocated argument strings
for( i = 0; i < argc; ++i ) {
free( argv[ i ] );
}
//release allocated memory
free( argv );
LocalFree( strings );
//return the main program's exit status
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment