News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Creating import libraries for the Universal C Runtime

Started by Vortex, December 06, 2023, 07:26:42 AM

Previous topic - Next topic

Vortex

Hello,

Here is how to create the Universal C Runtime import library :

a) Retrieving the list of the exported functions from the original import library :

\PellesC\bin\podump /EXPORTS "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x64\ucrt.lib" > 64Exports.txt
b) Creating the module definiton files :

busybox64.exe sh extract.txt api-ms-win-crt-conio-l1-1-0
busybox64.exe sh extract.txt api-ms-win-crt-convert-l1-1-0
.
.
busybox64.exe sh extract.txt api-ms-win-crt-utility-l1-1-0

extract.txt :

echo LIBRARY $1 > $1.def
echo EXPORTS >> $1.def
grep $1 64Exports.txt | cut -d' ' -f2- >> $1.def
sed -i '/^$/d' $1.def
unix2dos $1.def

The script above reads the second column in the text file 64Exports.txt, removes the last blank line and converts the module defition file to CR+LF.

64Exports :

api-ms-win-crt-conio-l1-1-0.dll __conio_common_vcprintf
api-ms-win-crt-conio-l1-1-0.dll __conio_common_vcprintf_p
.
.
api-ms-win-crt-conio-l1-1-0.dll _cgets
api-ms-win-crt-conio-l1-1-0.dll _cgets_s
.
.

c) The final step is running the MS library manager to create the individual import libraries and merging them into ucrt.lib :

set p="C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64"

%p%\lib.exe /MACHINE:x64 /OUT:api-ms-win-crt-conio-l1-1-0.lib /DEF:api-ms-win-crt-conio-l1-1-0.def

%p%\lib.exe /MACHINE:x64 /OUT:api-ms-win-crt-convert-l1-1-0.lib /DEF:api-ms-win-crt-convert-l1-1-0.def
.
.
%p%\lib.exe /MACHINE:x64 /OUT:api-ms-win-crt-utility-l1-1-0.lib /DEF:api-ms-win-crt-utility-l1-1-0.def

%p%\lib.exe /MACHINE:x64 /OUT:ucrt.lib api-ms-win-crt-conio-l1-1-0.lib api-ms-win-crt-convert-l1-1-0.lib api-ms-win-crt-environment-l1-1-0.lib api-ms-win-crt-filesystem-l1-1-0.lib api-ms-win-crt-heap-l1-1-0.lib api-ms-win-crt-locale-l1-1-0.lib api-ms-win-crt-math-l1-1-0.lib api-ms-win-crt-multibyte-l1-1-0.lib api-ms-win-crt-process-l1-1-0.lib api-ms-win-crt-runtime-l1-1-0.lib api-ms-win-crt-stdio-l1-1-0.lib api-ms-win-crt-string-l1-1-0.lib api-ms-win-crt-time-l1-1-0.lib api-ms-win-crt-utility-l1-1-0.lib

The import libraries with a small example demonstrating how to use them with Pelles C :

https://masm32.com/masmcode/vortex/files/ucrt-implib.zip

QuoteThe Windows 10 Universal CRT is a Windows operating system component that enables CRT functionality on the Windows operating system. This update allows Windows desktop applications that depend on the Windows 10 Universal CRT release to run on earlier Windows operating systems.

https://support.microsoft.com/en-us/topic/update-for-universal-c-runtime-in-windows-c0514201-7fe6-95a3-b0a5-287930f3560c

Vortex

Poasm example using the UCRT :

.386
.model flat,stdcall
option casemap:none

_stdout equ 1

ExitProcess             PROTO :DWORD
printf                  PROTO C :DWORD,:VARARG
__acrt_iob_func         PROTO C :DWORD

; int __cdecl __stdio_common_vfprintf(unsigned __int64 _Options,FILE* _Stream,
;                                     char const* _Format,int _Locale,va_list);

__stdio_common_vfprintf PROTO C :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
_strupr                 PROTO C :DWORD

includelib  \PellesC\lib\Win\kernel32.lib
includelib  ucrt.lib

.data

frmt    db '%s %s %s %u.',0
a       db 'This',0
b       db 'is',0
c       db 'printf test',0

.code

start:

    invoke  _strupr,ADDR c

    invoke  printf,ADDR frmt,ADDR a,ADDR b,eax,1

    invoke  ExitProcess,0


printf PROC C _format:DWORD,args:VARARG

;   #define _stdout (__acrt_iob_func(1))

    invoke  __acrt_iob_func,_stdout   

    lea     ecx,args
    invoke  __stdio_common_vfprintf,0,0,\ ; unsigned __int64 _Options
            eax,_format,0,ecx
    ret

printf ENDP

END start

Vortex

Poasm 64-bit example :

_stdout EQU 1

ExitProcess             PROTO :QWORD
printf                  PROTO :QWORD,:VARARG
__acrt_iob_func         PROTO :QWORD

; int __cdecl __stdio_common_vfprintf(unsigned __int64 _Options,FILE* _Stream,
;                                     char const* _Format,int _Locale,va_list);

__stdio_common_vfprintf PROTO :QWORD,:QWORD,:QWORD,:QWORD,:QWORD
_strupr                 PROTO :QWORD


includelib  \PellesC\lib\Win64\kernel32.lib
includelib  ucrt.lib

.data

frmt    db '%s %s %s %u.',0
s1      db 'This',0
s2      db 'is',0
s3      db 'printf test',0

.code

start PROC PARMAREA=5*QWORD

    invoke  _strupr,ADDR s3

    invoke  printf,ADDR frmt,ADDR s1,ADDR s2,\
            ADDR s3,1

    invoke  ExitProcess,0

start ENDP

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

printf PROC _format:QWORD,args:VARARG

    mov     QWORD PTR [rsp+8],rcx
    mov     QWORD PTR [rsp+16],rdx
    mov     QWORD PTR [rsp+24],r8
    mov     QWORD PTR [rsp+32],r9

    sub     rsp,8+40+8

;   #define _stdout (__acrt_iob_func(1))

    invoke  __acrt_iob_func,_stdout   

    xor     rcx,rcx
    mov     rdx,rax
    mov     r8,QWORD PTR [rsp+64]
    xor     r9,r9
    lea     rax,QWORD PTR [rsp+72]
    mov     QWORD PTR [rsp+32],rax
    call    __stdio_common_vfprintf

    add     rsp,8+40+8
    retn

printf ENDP

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

END start