Search code examples
visual-c++concatenationc-preprocessorstringification

Weird define in C++ preprocessor


I've come across this

#define DsHook(a,b,c) if (!c##_) {  INT_PTR* p=b+*(INT_PTR**)a;  VirtualProtect(&c##_,4,PAGE_EXECUTE_READWRITE,&no); *(INT_PTR*)&c##_=*p;  VirtualProtect(p,4,PAGE_EXECUTE_READWRITE,&no);  *p=(INT_PTR)c; }

and everything is clear except the "c##_" word, what does that mean?


Solution

  • It means to "glue" together, so c and _ get "glued together" to form c_. This glueing happens after argument replacement in the macro. See my example:

    #define glue(a,b) a##_##b
    
    const char *hello_world = "Hello, World!";
    
    int main(int arg, char *argv[]) {
        printf("%s\n", glue(hello,world)); // prints Hello, World!
        return 0;
    }