This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

| WinMain Command Line Parser Submitted by

It's an implementation of the WinMain function which parses the command line into the format that the main function supplies. This is useful for making cross-platform code or if you need the argc, argv format for a library like glut.

Download Associated File: winmain.cpp (1,531 bytes)

    // COTD Entry submitted by Max McGuire [amcguire@andrew.cmu.edu]

    #include <windows.h>

    extern int main(int argc, char* argv[]);

    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;

    }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.