Getting The Size of a C++ Function

后端 未结 16 2171
醉酒成梦
醉酒成梦 2020-12-06 09:37

I was reading this question because I\'m trying to find the size of a function in a C++ program, It is hinted at that there may be a way that is platform specific. My target

16条回答
  •  广开言路
    2020-12-06 10:09

    This can work in very limited scenarios. I use it in part of a code injection utility I wrote. I don't remember where I found the information, but I have the following (C++ in VS2005):

    #pragma runtime_checks("", off)
    
    static DWORD WINAPI InjectionProc(LPVOID lpvParameter)
    {
        // do something
        return 0;
    }
    
    static DWORD WINAPI InjectionProcEnd()
    {
        return 0;
    }
    
    #pragma runtime_checks("", on)
    

    And then in some other function I have:

    size_t cbInjectionProc = (size_t)InjectionProcEnd - (size_t)InjectionProc;
    

    You have to turn off some optimizations and declare the functions as static to get this to work; I don't recall the specifics. I don't know if this is an exact byte count, but it is close enough. The size is only that of the immediate function; it doesn't include any other functions that may be called by that function. Aside from extreme edge cases like this, "the size of a function" is meaningless and useless.

提交回复
热议问题