Skip to content

Instantly share code, notes, and snippets.

@cloderic
Created February 9, 2012 13:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloderic/1780108 to your computer and use it in GitHub Desktop.
Save cloderic/1780108 to your computer and use it in GitHub Desktop.
WinMain to standard main
#ifdef WIN32
int WINAPI WinMain(HINSTANCE instance, HINSTANCE prev_instance, char* command_line, int show_command)
{
int argc;
char** argv;
char* arg;
int index;
int result;
// count the arguments
argc = 1;
arg = command_line;
while (arg[0] != 0)
{
while (arg[0] != 0 && arg[0] == ' ')
{
arg++;
}
if (arg[0] != 0)
{
argc++;
while (arg[0] != 0 && arg[0] != ' ')
{
arg++;
}
}
}
// tokenize the arguments
argv = (char**)malloc(argc * sizeof(char*));
arg = command_line;
index = 1;
while (arg[0] != 0)
{
while (arg[0] != 0 && arg[0] == ' ')
{
arg++;
}
if (arg[0] != 0)
{
argv[index] = arg;
index++;
while (arg[0] != 0 && arg[0] != ' ')
{
arg++;
}
if (arg[0] != 0)
{
arg[0] = 0;
arg++;
}
}
}
// put the program name into argv[0]
char filename[_MAX_PATH];
GetModuleFileName(NULL, filename, _MAX_PATH);
argv[0] = filename;
// call the user specified main function
result = main(argc, argv);
free(argv);
return result;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment