0

Possible Duplicate:
What is the use of ## preprocessor in C

From Fun with Google TTS ... Anyone can shed a light on how this code is working? And in particular - what kind of notation is "c##_"? -

#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;

It is unfolded in code as -

//  redirect  7th member func of IAsyncReader (SyncReadAlligned) to grab mp3 data from output pin of source filter
DsHook(reader,6,SyncReadAlligned); 

I guess, it is not very safe way of playing with memory, but it is commented by the author as "unimportant":

Unimportant code like hook is folded in snippet form but feel free to unfold and format the code in the way you like.

P.S. The "Fun with Google" itself if blocked already by Google (if anyone is interested to test).

Update: I think, that the short analysis would be - To "redirect 7th member func of IAsyncReader (SyncReadAlligned) to grab mp3 data from output pin of source filter", we change the memory protection on certain region of committed pages from "whatever it was" to PAGE_EXECUTE_READWRITE (allowing full access).

And concerning safety MSDN says -

It is best to avoid using VirtualProtect to change page protections on memory blocks allocated by GlobalAlloc, HeapAlloc, or LocalAlloc, because multiple memory blocks can exist on a single page. ... When protecting a region that will be executable, the calling program bears responsibility for ensuring cache coherency...

Community
  • 1
  • 1
SChepurin
  • 1,814
  • 25
  • 17

2 Answers2

5

It's a preprocessor directive meaning "concatenation". c##_ will result in a token containing the substitued value of the argument c and the literal character _. Example:

#define foo(c) c##_

when called using foo(bar), this will expand to bar_.

2

This notation ## has a special meaning in macro defintions namely concatenation. Here c##_ concatenate c with underscore to form a new token c_. Note here c is an argument. So by passing hereIAm as argument to the macro, after the expansion c##_ will become hereIAm_.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176