Created
February 9, 2012 13:45
-
-
Save cloderic/1780108 to your computer and use it in GitHub Desktop.
WinMain to standard main
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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