宏替换中的#是把宏替换成字符串常量,比如:
例1:
#define MARK(name) printf(#name);
MARK(abc)被替换为 printf("abc");
例2:
#define MARK(name) printf(L#name);
MARK(abc)被替换为 printf(L"abc");
宏替换中的##是把自身前后的符号连接起来
例:
#define DECLAR_VAR(name) int prev_ ## name;
DECLARE_VAR(abc)被替换为 int prev_abc;
显然这可以用来自动生成变量/函数的名字
一个典型的例子:
// 根据函数名称,获得导出函数的指针
void FindAndSetProcAddress()
{
UNICODE_STRING funcName = {0};
#define SetProcAddress(function) /
RtlInitUnicodeString(&funcName, L"Zw" L#function); /
RealZw ## function = (Nt ## function ## _FuncPtr)MmGetSystemRoutineAddress(&funcName);
SetProcAddress(CreateSection)
// 上句会被替换为:
// RtlInitUnicodeString(&funcName, L"Zw" L"CreateSection");
// RealZwCreateSection = (NtCreateSection_FuncPtr)MmGetSystemRoutineAddress(&funcName);
// 以下相同
SetProcAddress(OpenKey)
SetProcAddress(CreateKey)
SetProcAddress(DeleteKey)
SetProcAddress(DeleteValueKey)
SetProcAddress(SetValueKey)
SetProcAddress(CreateFile)
SetProcAddress(DeleteFile)
SetProcAddress(OpenFile)
SetProcAddress(SetEaFile)
SetProcAddress(SetInformationFile)
SetProcAddress(WriteFile)
#undef SetProcAddress
}
VC中的#和##宏替换指令
最新推荐文章于 2023-04-27 09:55:58 发布