Search code examples
c++winapilinkerpragmacommon-controls

Can/Should C++ #pragma Preprocessors Reside In Functions


In short, I am wanting to make a function that embeds the visual styles manifest and links to ComCtl32.lib, version 6 if available. Here is basically what I am hoping to do:

void InitVivify() {
    // Embed visual style XML manifest
    #pragma comment(linker,                           \
        "\"/manifestdependency:type='Win32'"          \
        "   name='Microsoft.Windows.Common-Controls'" \
        "   version='6.0.0.0'"                        \
        "   processorArchitecture='*'"                \
        "   publicKeyToken='6595b64144ccf1df'"        \
        "   language='*'\""                           \
    )

    // Link common controls library
    #pragma comment(lib, "ComCtl32.lib")

    // Initialize controls used
    INITCOMMONCONTROLSEX InitCtrlEx;
    InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitCtrlEx.dwICC = ICC_LISTVIEW_CLASSES | ICC_TAB_CLASSES | ICC_USEREX_CLASSES;
    InitCommonControlsEx(&InitCtrlEx);
}

I realize this probably isn't good practice, but is it valid?


Solution

  • Pragma directives are pre-processor directives.
    They cause the implementation to behave in a implementation defined way.
    So it is valid but not encouraged.

    C++11 Standard:

    16.6 Pragma directive [cpp.pragma]

    1 A preprocessing directive of the form

    # pragma pp-tokensopt new-line
    

    causes the implementation to behave in an implementation-defined manner. The behavior might cause translation to fail or cause the translator or the resulting program to behave in a non-conforming manner. Any pragma that is not recognized by the implementation is ignored.