Silly little C++ wrapper class

Did you ever have the same problem: if you want to compiler a Win32 program with UNICODE/_UNICODE defined, for some reason the argv vector does not contain a single proper argument?! This happens in particular in DDK/WDK projects. I was sick of it, so here’s a little wrapper class that does almost nothing if UNICODE/_UNICODE are not defined and otherwise simply fills new values into the argc and argv parameters to the main/wmain function. The use is as simple as can be, make sure shell32.lib is linked in and include ShellAPI.h, then simply instantiate the class within (w)main like this:

CTcharArgs dummy(argc, argv);

Easy, huh? Here’s the class:

class CTcharArgs
{
public:
    CTcharArgs(int &argc, _TCHAR** &argv)
#if defined(UNICODE) || defined(_UNICODE)
        : m_argv(::CommandLineToArgvW(::GetCommandLine(), &m_argc))
#endif // UNICODE
    {
#if defined(UNICODE) || defined(_UNICODE)
        argc = m_argc;
        argv = m_argv;
#endif // UNICODE
    }

#if defined(UNICODE) || defined(_UNICODE)
    ~CTcharArgs()
    {
        if(m_argv)
        {
            ::LocalFree(reinterpret_cast(m_argv));
        }
    }
private:
    LPWSTR* m_argv;
    int m_argc;
#endif // UNICODE
};

This class is released into the PUBLIC DOMAIN. Disclaimer: This software is provided ‘as-is’, without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software.

// Oliver

PS: Should even be compatible with cross-platform code (in which case _TCHAR will probably be an alias for char) 😉

This entry was posted in EN, Programming, Software. Bookmark the permalink.

One Response to Silly little C++ wrapper class

  1. Oliver says:

    BTW: The proper way is to set

    UNICODE=1

    in the SOURCES file 😉

Leave a Reply

Your email address will not be published. Required fields are marked *