winapiassemblycompilationsymbolscoff

Make MS Visual C++ compiler treat Win32 API import calls as (unresolved) external symbols


Is it possible to make MS visual C++ compiler treat Win32 API import calls as (unresolved) external symbols?

In other words, I need to change dword ptr calls which reference to some IAT, e.g.:

FF 15 00 00 00 00  call        dword ptr [__imp__MessageBoxA@16]

to external symbol calls, e.g.:

E8 00 00 00 00     call        _MessageBoxA@16

This implies that after the compilation I don't need linking, because obviously it won't be possible. So as a product I want to get (MS) COFF .obj files which such unusual calls of Win32 API.


Solution

  • As it was pointed out in the comments under the question, the reason why Win32 API calls are compiled into call [__imp__xxx] is __declspec(dllimport).

    So to achieve what was asked in the question, all Win32 API functions must defined without __declspec(dllimport).

    In the Win32 header files (such as WinUser.h) you can see that all functions are defined with WINxxxAPI macro, which in turn are defined in apisetcconv.h. In the later file WINxxxAPI macros are defined with DECLSPEC_IMPORT, which in turn is defined as __declspec(dllimport).

    So, an easy way to achieve the requirement is to re-define DECLSPEC_IMPORT with the following empty preprocessor definition ( see /D flag ):

    /DDECLSPEC_IMPORT=
    

    Which is equivalent to

    #define DECLSPEC_IMPORT
    

    P.S. If there are alternative ways, I would still like to know them.