Getting ordinal from function name programmatically

后端 未结 2 1327
無奈伤痛
無奈伤痛 2020-12-28 22:33

What\'s the easiest way in C++ to get an ordinal of an exported dll function, given its name? (Looking for a way which doesn\'t invlove parsing the IATs myself...)

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 23:28

    An ugly way would be to run a system call with a dumpbin command and parse the output. But that has about the same elegance as a bull in the proverbial china shop.

    dumpbin /exports c:\windows\system32\user32.dll | grep FunctionOfInterest

    Otherwise, you could write a simple loop calling GetProcAddress with ordinals (passed in the low two bytes of the name parameter). When the function pointer matches the pointer returned when passing the actual name, then you are done.

    Here is the basic idea without error checking:

      HANDLE hMod;
      HANDLE byname, byord;
      int ord;
    
      hMod = LoadLibrary( "user32.dll" );
      byname = GetProcAddress( hMod, "GetWindow" );
      byord = 0;
      ord = 1;
      while ( 1 ) {
         byord = GetProcAddress( hMod, (LPCSTR)ord );
         if ( byord == byname ) {
            printf( "ord = %d\n", ord );
            break;
            }
         ord++;
         }
    

提交回复
热议问题