C/C++函数指针取函数长度大小字
/*功能:取函数大小
参数1:函数指针
*/
ULONG GetFunctionLen(PVOID Buff)
{
int strL = 0;
PUCHAR p = (PUCHAR)Buff;
int a = 0;
for (int i = 0; i < 4096; i++)
{
if (p[i]==0xC2)
{
a = i;
while (1)
{
a+=3;
if (p[a] == 0xCC|| (p[a] ==0x0&& p[a+1] == 0x0))
{
return a;
}
a = 0;
break;
}
}
if (p[i] == 0xC3)
{
a = i;
while (1)
{
a++;
if ((p[a] == 0xCC&& (p[a+1]==0 && p[a + 2] == 0 && p[a + 3] == 0 && p[a + 4] == 0 && p[a + 5] == 0)))
{
return a;
}
if (p[a] == 0xFF || p[a] == 0xE9|| p[a] == 0xEB)
{
return a;
}
if ((p[a] == 0xCC && (p[a + 1] >= 0x48 )))
{
return a;
}
a = 0;
break;
}
}
}
return -1;
}
调用方法
#include <windows.h>
#include <iostream>
#include <stdio.h>
using namespace std;
int funca()
{
return 6;
}
int add(int a, int b)
{
return a + b;
}
int add1(int a, int b)
{
return a > b ? a : b;
}
int main()
{
cout << "add函数大小=:" << GetFunctionLen(add) << "\nadd1函数大小=:" << GetFunctionLen(add1) << endl;
cout << "\nGetFunctionLen函数大小=:" << GetFunctionLen(GetFunctionLen) <<"\nmain函数大小=:"<< GetFunctionLen(main) << "\nfunca函数大小=:" << GetFunctionLen(funca) <<endl;
getchar();
return 0;
}
运行效果