Implement a generic infrastructure to facilitate run-time linking of API functions
Consider the (trivial) program, (contrived in conjunction with development related to feature request #41567):
- #define _MINGW_LEGACY_SUPPORT
- #define NTDDI_VERSION NTDDI_WIN10_RS5
- #include <stdio.h>
- #include <winbase.h>
- #include <winerror.h>
- #include <wincon.h>
- int conapi_test( void )
- { HPCON hPC; COORD size = { 80, 25 };
- if( CreatePseudoConsole( size, 0, 0, 0, &hPC ) == ERROR_OLD_WIN_VERSION )
- fprintf( stderr,
- "Pseudo-consoles not supported; Win10-RS5 (or later) is required.\n"
- );
- ResizePseudoConsole( hPC, size );
- ClosePseudoConsole( hPC );
- return 0;
- }
- int main()
- { printf( "Main program started\n" );
- return conapi_test();
- }
$ mingw32-gcc -Wall -O3 conapi-test.c pc.c: In function 'conapi_test': pc.c:10:3: error: unknown type name 'HPCON'; did you mean 'HICON'? 10 | { HPCON hPC; COORD size = { 80, 25 }; | ^~~~~ | HICON pc.c:11:7: warning: implicit declaration of function 'CreatePseudoConsole' [-Wimplicit-function-declaration] 11 | if( CreatePseudoConsole( size, 0, 0, 0, &hPC ) == ERROR_OLD_WIN_VERSION ) | ^~~~~~~~~~~~~~~~~~~ pc.c:15:3: warning: implicit declaration of function 'ResizePseudoConsole' [-Wimplicit-function-declaration] 15 | ResizePseudoConsole( hPC, size ); | ^~~~~~~~~~~~~~~~~~~ pc.c:16:3: warning: implicit declaration of function 'ClosePseudoConsole' [-Wimplicit-function-declaration] 16 | ClosePseudoConsole( hPC ); | ^~~~~~~~~~~~~~~~~~
$ mingw32-gcc -Wall -O3 -I ../w32api/include/ conapi-test.c -L ./w32api/ $ cp a.exe ~/VirtualBox/share/
Curiously, if I run it under Wine, (configured to emulate Win7 behaviour), it runs successfully:
$ ./a.exe Main program startedwhich should not have happened; I guess this is a bug in Wine-6.7, insofar as it fails to reject API calls which should not be supported in its configured Windows version emulation.
I've attached a proposed implementation.
With this in place, and with the <wincon.h> update, as proposed for feature request #41567, augmented by the further addition of:
my example program, from my previous comment, not only compiles and links successfully, but when I run it on the WinXP virtual machine, I now see the expected output:
- diff --git a/w32api/include/wincon.h b/w32api/include/wincon.h
- --- a/w32api/include/wincon.h
- +++ b/w32api/include/wincon.h
- @@ -423,10 +423,49 @@ WINAPI HRESULT CreatePseudoConsole (COOR
- WINAPI HRESULT ResizePseudoConsole (HPCON, COORD);
- WINAPI void ClosePseudoConsole (HPCON);
- #define PSEUDOCONSOLE_INHERIT_CURSOR (DWORD)(1)
- +#ifdef _MINGW_LEGACY_SUPPORT
- +/* For MinGW legacy platform support, provide inline redirector stubs,
- + * to facilate graceful fallback action, in the event that any program,
- + * which has been linked with the pseudo-console API, is run on an older
- + * version of Windows.
- + */
- +#include "legacy.h"
- +
- +__CRT_ALIAS WINAPI HRESULT CreatePseudoConsole
- +( COORD size, HANDLE input, HANDLE output, DWORD flags, HPCON *con )
- +{
- + typedef WINAPI HRESULT (*api)( COORD, HANDLE, HANDLE, DWORD, HPCON * );
- +
- + static void *call = API_UNCHECKED;
- + return ((call = __kernel32_entry_point( call, __FUNCTION__ )) != NULL)
- + ? ((api)(call))( size, input, output, flags, con )
- + : __legacy_support( ERROR_OLD_WIN_VERSION );
- +}
- +
- +__CRT_ALIAS WINAPI HRESULT ResizePseudoConsole (HPCON con, COORD size)
- +{
- + typedef WINAPI HRESULT (*api)( HPCON, COORD );
- +
- + static void *call = API_UNCHECKED;
- + return ((call = __kernel32_entry_point( call, __FUNCTION__ )) != NULL)
- + ? ((api)(call))( con, size ) : __legacy_support( ERROR_OLD_WIN_VERSION );
- +}
- +
- +__CRT_ALIAS WINAPI void ClosePseudoConsole (HPCON con)
- +{
- + typedef WINAPI void (*api)( HPCON );
- +
- + static void *call = API_UNCHECKED;
- + if( (call = __kernel32_entry_point( call, __FUNCTION__ )) == NULL )
- + (void)__legacy_support( ERROR_OLD_WIN_VERSION );
- + else ((api)(call))( con );
- +}
- +#endif /* _MINGW_LEGACY_SUPPORT */
- +
- #endif /* Win10 Redstone 5 and later */
- #endif /* Vista and later */
- #endif /* WinXP and later */
- #endif /* Win2K and later */
$ /e/a.exe Main program started Pseudo-consoles not supported; Win10-RS5 (or later) is required.
Well it appeared to be working, so I committed 7b37e9a. I then moved on to ticket #42383, which unfortunately revealed a careless logic error ... corrected by commit e5e5f06.
As support for new (Windows version-dependent) APIs is added to MinGW, there is increased potential for creating an application which will not load, much less run, on legacy Windows versions; the typical manifestation of such a load failure is that the process crashes with an "entry point not found" exception, for the unsupported DLL function, before the main() function even gets control, so there's no opportunity to handle the exception.
The only way to mitigate such exceptions, such that the application retains control, is to always link potentially unsupported API functions dynamically, at run-time, by retrieval of an entry-point reference, using GetProcAddress(), and to always invoke the function via the reference pointer, (and never call it directly, by name). The mechanism for dynamically linking such functions is mostly the same, in each case, often resulting in significant repetitive coding; it would be a great convenience, if MinGW were to implement a generic API infrastructure, to encapsulate the common features of the required run-time linking interface.