Pelles C forum

C language => Tips & tricks => Topic started by: TimoVJL on November 22, 2016, 02:04:11 PM

Title: ucrtbase test
Post by: TimoVJL on November 22, 2016, 02:04:11 PM
simple ucrtbase printf test.
many of us have now that dll in their system.

ucrtbase.dll don't have printf, fprintf, ...
Code: [Select]
#include <stdarg.h>
// https://msdn.microsoft.com/en-us/library/dn727675.aspx
// https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/
void exit(int status);

typedef struct FILE FILE;
#undef stdin
#undef stdout
#undef stderr
//__declspec(dllimport)
extern FILE* __cdecl __acrt_iob_func(unsigned);
#define stdin  (__acrt_iob_func(0))
#define stdout (__acrt_iob_func(1))
#define stderr (__acrt_iob_func(2))

#pragma comment (lib, "ucrtbase.lib")

int __cdecl __stdio_common_vfprintf(long long, FILE*, char const* , int, va_list);

int printf(const char * format, ...)
{
int ret;
va_list vl;
va_start(vl, format);
ret = __stdio_common_vfprintf(0, stdout, format, 0, vl);
va_end(vl);
return ret;
}

int fprintf(FILE * stream, const char * format, ...)
{
int ret;
va_list vl;
va_start(vl, format);
ret = __stdio_common_vfprintf(0, stream, format, 0, vl);
va_end(vl);
return ret;
}

int main(int argc, char **argv);

void __cdecl mainCRTStartup(void)
{
exit(main(0, 0));
}

int main(int argc, char **argv)
{
printf("Hello C\n");
fprintf(stdout, "StdOut\n");
fprintf(stderr, "StdErr\n");
return 0;
}
argc argv don't work yet :(

Code: [Select]
extern FILE* __cdecl __acrt_iob_func(unsigned);
FILE* stdin;
FILE* stdout;
FILE* stderr;
int main(int argc, char **argv);
void __cdecl mainCRTStartup(void)
{
stdin  = __acrt_iob_func(0);
stdout = __acrt_iob_func(1);
stderr = __acrt_iob_func(2);
exit(main(0, 0));
}

speed test with qsort:
msvcrt.dll    250ms
ucrtbase.dll 285ms

PS: last Firefox have those dll's, if someone don't have those, just copy from there.
These are needed in Windows 7
Code: [Select]
   2 048 test_ucrt.exe
 921 280 ucrtbase.dll
  18 624 api-ms-win-core-file-l1-2-0.dll
  18 624 api-ms-win-core-file-l2-1-0.dll
  21 184 api-ms-win-core-localization-l1-2-0.dll
  19 136 api-ms-win-core-processthreads-l1-1-1.dll
  19 136 api-ms-win-core-synch-l1-2-0.dll
  18 624 api-ms-win-core-timezone-l1-1-0.dll
WindowsXP:
Code: [Select]
ucrtbase.dll
api-ms-win-core-console-l1-1-0.dll
api-ms-win-core-datetime-l1-1-0.dll
api-ms-win-core-debug-l1-1-0.dll
api-ms-win-core-errorhandling-l1-1-0.dll
api-ms-win-core-file-l1-1-0.dll
api-ms-win-core-file-l1-2-0.dll
api-ms-win-core-file-l2-1-0.dll
api-ms-win-core-handle-l1-1-0.dll
api-ms-win-core-heap-l1-1-0.dll
api-ms-win-core-interlocked-l1-1-0.dll
api-ms-win-core-libraryloader-l1-1-0.dll
api-ms-win-core-localization-l1-2-0.dll
api-ms-win-core-memory-l1-1-0.dll
api-ms-win-core-namedpipe-l1-1-0.dll
api-ms-win-core-processenvironment-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-1.dll
api-ms-win-core-profile-l1-1-0.dll
api-ms-win-core-rtlsupport-l1-1-0.dll
api-ms-win-core-string-l1-1-0.dll
api-ms-win-core-synch-l1-1-0.dll
api-ms-win-core-synch-l1-2-0.dll
api-ms-win-core-sysinfo-l1-1-0.dll
api-ms-win-core-timezone-l1-1-0.dll
api-ms-win-core-util-l1-1-0.dll

EDIT: startup code for ucrtbase.dll
Code: [Select]
extern int*    __cdecl __p___argc (void);
extern char*** __cdecl __p___argv (void);
extern char*** __cdecl __p__environ (void);
//extern wchar_t*** __cdecl __p___wargv(void);
extern int _configure_narrow_argv(int);

int main(int argc, char **argv);

void __cdecl mainCRTStartup(void)
{
_configure_narrow_argv(1);
int __argc = *__p___argc();
char** __argv = *__p___argv();
//char**__env = *__p__environ();
exit(main(__argc, __argv));
}
Title: Re: ucrtbase test
Post by: Vortex on November 23, 2016, 07:47:30 AM
Hi TimoVJL,

Thanks for the info. Here is where you can find the necessary hotfix to install the DLL :

Update for Universal C Runtime in Windows :

https://support.microsoft.com/en-us/kb/2999226
Title: Re: ucrtbase test
Post by: TimoVJL on November 23, 2016, 08:12:34 AM
Another link: https://support.microsoft.com/en-us/kb/3118401

With Firefox came newer ones: 10.0.10586.212
Title: Re: ucrtbase test
Post by: Vortex on November 23, 2016, 11:56:59 AM
Hello again,

Thanks for the example and the link.

C function prototype :

Code: [Select]
int CDECL MSVCRT__stdio_common_vfprintf(unsigned __int64 options, MSVCRT_FILE *file, const char *format,
                                        MSVCRT__locale_t locale, __ms_va_list valist)

https://www.winehq.org/pipermail/wine-patches/2015-October/143260.html

 Here is a Poasm example :

Code: [Select]
; Source code assembled with Pelles Macro Assembler, Version 8.00.1

.386
.model flat, stdcall
option casemap:none

ExitProcess PROTO :DWORD

include     ucrtbase.inc

includelib  \PellesC\lib\Win\kernel32.lib
includelib  ucrtbase.lib

UCRT_STDOUT equ 1

.data

msg1     db 'Hello world!',0
msg2     db 'This is a test.',0
format1  db '%s %s',0

.data?

stdout   dd ?

.code

start:

    invoke  __acrt_iob_func,UCRT_STDOUT

    mov     stdout,eax  ; #define stdout (&__acrt_iob_func()[1])

    lea     ecx,[esp-8] ; start of the variable argument list

    invoke  __stdio_common_vfprintf,0,0,stdout,\
            ADDR format1,0,\
            ecx,ADDR msg1,ADDR msg2
           
    invoke  ExitProcess,0

END start